You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.7 KiB

2 years ago
2 years ago
  1. from .dbtest import (
  2. DbTest,
  3. dbconnect
  4. )
  5. import os
  6. from psycopg2.extras import (
  7. RealDictCursor,
  8. RealDictRow
  9. )
  10. PATH_TO_SQL_DIR = os.path.abspath(
  11. os.path.join(
  12. os.path.dirname(__file__),
  13. "..",
  14. "sql"
  15. )
  16. )
  17. class TestExample(DbTest):
  18. @dbconnect
  19. def test_select_organizations(self, conn):
  20. self.load_fixtures(
  21. conn,
  22. os.path.join(PATH_TO_SQL_DIR, "organizations.sql")
  23. )
  24. sql = """
  25. SELECT * FROM organizations;
  26. """
  27. with conn.cursor(cursor_factory=RealDictCursor) as cur:
  28. cur.execute(sql)
  29. organizations = cur.fetchall()
  30. assert len(organizations) == 7
  31. @dbconnect
  32. def test_count_the_number_of_subordinates(self, conn):
  33. self.load_fixtures(
  34. conn,
  35. os.path.join(PATH_TO_SQL_DIR, "organizations.sql")
  36. )
  37. sql = """
  38. SELECT
  39. COUNT(customers.*) AS "subordinates_count",
  40. organizations.id AS "id"
  41. FROM organizations
  42. LEFT OUTER JOIN enterprise_sales_enterprise_customers customers ON customers.sales_organization_id = organizations.id
  43. GROUP BY organizations.id
  44. ORDER BY organizations.id ASC
  45. ;
  46. """
  47. with conn.cursor(cursor_factory=RealDictCursor) as cur:
  48. cur.execute(sql)
  49. actual = cur.fetchall()
  50. print(actual)
  51. assert len(actual) == 7
  52. assert actual == [
  53. RealDictRow(**{
  54. "subordinates_count": 0,
  55. "id": 1,
  56. })
  57. , RealDictRow(**{
  58. "subordinates_count": 4,
  59. "id": 2,
  60. })
  61. , RealDictRow(**{
  62. "subordinates_count": 0,
  63. "id": 3,
  64. })
  65. , RealDictRow(**{
  66. "subordinates_count": 0,
  67. "id": 4,
  68. })
  69. , RealDictRow(**{
  70. "subordinates_count": 0,
  71. "id": 5,
  72. })
  73. , RealDictRow(**{
  74. "subordinates_count": 1,
  75. "id": 6,
  76. })
  77. , RealDictRow(**{
  78. "subordinates_count": 0,
  79. "id": 7,
  80. })
  81. ]
  82. @dbconnect
  83. def test_calculate_center_of_each_segment(self, conn):
  84. self.load_fixtures(
  85. conn,
  86. os.path.join(PATH_TO_SQL_DIR, "japan_segments.sql")
  87. )
  88. # PostGIS is very useful!
  89. sql = """
  90. SELECT
  91. id,
  92. ST_X(ST_Centroid(bounds)) AS "longitude",
  93. ST_Y(ST_Centroid(bounds)) AS "latitude"
  94. FROM japan_segments
  95. ;
  96. """
  97. with conn.cursor(cursor_factory=RealDictCursor) as cur:
  98. cur.execute(sql)
  99. actual = cur.fetchall()
  100. print(actual)
  101. assert len(actual) == 10
  102. assert actual == [
  103. RealDictRow(**{
  104. "id": "KAGOSHIMA_1",
  105. "longitude": 130.642228315775,
  106. "latitude": 30.7045454545455,
  107. })
  108. , RealDictRow(**{
  109. "id": "KAGOSHIMA_2",
  110. "longitude": 130.694183864916,
  111. "latitude": 30.7045454545455,
  112. })
  113. , RealDictRow(**{
  114. "id": "KAGOSHIMA_3",
  115. "longitude": 130.746139414057,
  116. "latitude": 30.7045454545455,
  117. })
  118. , RealDictRow(**{
  119. "id": "KAGOSHIMA_4",
  120. "longitude": 129.707028431231,
  121. "latitude": 30.75,
  122. })
  123. , RealDictRow(**{
  124. "id": "KAGOSHIMA_5",
  125. "longitude": 129.758983980373,
  126. "latitude": 30.75,
  127. })
  128. , RealDictRow(**{
  129. "id": "KAGOSHIMA_6",
  130. "longitude": 129.810939529514,
  131. "latitude": 30.75,
  132. })
  133. , RealDictRow(**{
  134. "id": "KAGOSHIMA_7",
  135. "longitude": 129.862895078655,
  136. "latitude": 30.75,
  137. })
  138. , RealDictRow(**{
  139. "id": "KAGOSHIMA_8",
  140. "longitude": 129.914850627797,
  141. "latitude": 30.75,
  142. })
  143. , RealDictRow(**{
  144. "id": "KAGOSHIMA_9",
  145. "longitude": 129.966806176937,
  146. "latitude": 30.75,
  147. })
  148. , RealDictRow(**{
  149. "id": "KAGOSHIMA_10",
  150. "longitude": 130.018761726079,
  151. "latitude": 30.75,
  152. })
  153. ]
  154. @dbconnect
  155. def test_segments_using_geojson_boundary(self, conn):
  156. self.load_fixtures(
  157. conn,
  158. os.path.join(PATH_TO_SQL_DIR, "japan_segments.sql")
  159. )
  160. # Taking this task very literally and using the GeoJSON
  161. # directly with PostGIS `ST_GeomFromGeoJSON`
  162. sql = """
  163. SELECT id FROM japan_segments
  164. WHERE ST_Within(
  165. bounds,
  166. ST_SetSRID(ST_GeomFromGeoJSON('{
  167. "type": "Polygon",
  168. "coordinates": [
  169. [
  170. [
  171. 130.27313232421875,
  172. 30.519681272749402
  173. ],
  174. [
  175. 131.02020263671875,
  176. 30.519681272749402
  177. ],
  178. [
  179. 131.02020263671875,
  180. 30.80909017893796
  181. ],
  182. [
  183. 130.27313232421875,
  184. 30.80909017893796
  185. ],
  186. [
  187. 130.27313232421875,
  188. 30.519681272749402
  189. ]
  190. ]
  191. ]
  192. }'), 4326)
  193. );
  194. """
  195. with conn.cursor(cursor_factory=RealDictCursor) as cur:
  196. cur.execute(sql)
  197. actual = cur.fetchall()
  198. print(actual)
  199. assert len(actual) == 3
  200. assert actual == [
  201. RealDictRow(**{
  202. "id": "KAGOSHIMA_1",
  203. })
  204. , RealDictRow(**{
  205. "id": "KAGOSHIMA_2",
  206. })
  207. , RealDictRow(**{
  208. "id": "KAGOSHIMA_3",
  209. })
  210. ]