Skip to content

Commit 11d434c

Browse files
committed
delete mapping session and mapping sessions results and test
1 parent f4e7953 commit 11d434c

File tree

5 files changed

+78
-63
lines changed

5 files changed

+78
-63
lines changed

mapswipe_workers/mapswipe_workers/firebase_to_postgres/delete_project.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,18 @@ def delete_project(project_ids: list) -> bool:
9090
ref.delete()
9191

9292
pg_db = auth.postgresDB()
93-
sql_query = "DELETE FROM results WHERE project_id = %(project_id)s;"
93+
sql_query = """
94+
DELETE FROM mapping_sessions_results msr
95+
USING mapping_sessions ms
96+
WHERE ms.mapping_session_id = msr.mapping_session_id
97+
AND ms.project_id = %(project_id)s;
98+
"""
99+
pg_db.query(sql_query, {"project_id": project_id})
100+
sql_query = """
101+
DELETE
102+
FROM mapping_sessions
103+
WHERE project_id = %(project_id)s ;
104+
"""
94105
pg_db.query(sql_query, {"project_id": project_id})
95106
sql_query = "DELETE FROM tasks WHERE project_id = %(project_id)s;"
96107
pg_db.query(sql_query, {"project_id": project_id})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2020-02-27 13:13:11.41795 test 0106000020E6100000010000000103000000010000000500000005000020E87C4140DF0CEE9456CC2FC0030000406E8B4140DF0CEE9456CC2FC0030000406E8B4140743F7755D7A32FC005000020E87C4140743F7755D7A32FC005000020E87C4140DF0CEE9456CC2FC0 f buildings test - Malawi (1)\ntest 0 test test_build_area 1 15120 0 inactive 3 {"groupMaxSize": 0, "projectTopic": null, "projectRegion": null, "projectNumber": null, "requestingOrganisation": null, "groupSize": 120, "zoomLevel": 18, "tileServer": {"name": "bing", "url": "https://ecn.t0.tiles.virtualearth.net/tiles/a{quad_key}.jpeg?g=7505&mkt=en-US&token={key}", "apiKeyRequired": null, "apiKey": "", "wmtsLayerName": null, "captions": null, "date": null, "credits": "\\u00a9 2019 Microsoft Corporation, Earthstar Geographics SIO"}} ARC
1+
2020-02-27 13:13:11.41795 test 0106000020E6100000010000000103000000010000000500000005000020E87C4140DF0CEE9456CC2FC0030000406E8B4140DF0CEE9456CC2FC0030000406E8B4140743F7755D7A32FC005000020E87C4140743F7755D7A32FC005000020E87C4140DF0CEE9456CC2FC0 f buildings test - Malawi (1)\ntest 0 test test_build_area 1 15120 0 inactive 3 {"groupMaxSize": 0, "projectTopic": null, "projectRegion": null, "projectNumber": null, "requestingOrganisation": null, "groupSize": 120, "zoomLevel": 18, "tileServer": {"name": "bing", "url": "https://ecn.t0.tiles.virtualearth.net/tiles/a{quad_key}.jpeg?g=7505&mkt=en-US&token={key}", "apiKeyRequired": null, "apiKey": "", "wmtsLayerName": null, "captions": null, "date": null, "credits": "\\u00a9 2019 Microsoft Corporation, Earthstar Geographics SIO"}} ARC

mapswipe_workers/tests/integration/fixtures/tile_map_service_grid/results/build_area.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@
262262
"18-156623-142784" : 0,
263263
"18-156623-142785" : 0
264264
},
265-
"startTime" : "2020-02-03T15:36:29.286Z",
265+
"startTime" : "2020-02-03T14:36:29.286Z",
266266
"timestamp" : "2020-02-03T15:39:39.332Z"
267267
}
268268
}

mapswipe_workers/tests/integration/set_up.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def set_postgres_test_data(
3939
file_path = os.path.join(
4040
test_dir, "fixtures", project_type, data_type, fixture_name
4141
)
42-
4342
pg_db = auth.postgresDB()
4443
with open(file_path) as test_file:
4544
pg_db.copy_from(test_file, data_type)

mapswipe_workers/tests/integration/test_delete_project.py

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33

44
import set_up
55
import tear_down
6+
from base import BaseTestCase
67

78
from mapswipe_workers import auth
89
from mapswipe_workers.config import FIREBASE_DB
910
from mapswipe_workers.definitions import CustomError
1011
from mapswipe_workers.firebase_to_postgres import delete_project
1112

1213

13-
class TestDeleteProject(unittest.TestCase):
14+
class TestDeleteProject(BaseTestCase):
1415
def setUp(self):
16+
super().setUp()
17+
project_type = "tile_map_service_grid"
18+
fixture_name = "build_area"
1519
self.project_id = set_up.create_test_project(
16-
"tile_map_service_grid", "build_area", results=True
20+
project_type, fixture_name, results=True
1721
)
1822

1923
def tearDown(self):
@@ -25,12 +29,7 @@ def tearDown(self):
2529
FIREBASE_DB == "ci-mapswipe",
2630
"Test is unreliable when running in Github Actions",
2731
)
28-
def test_deletion(self):
29-
"""Test if tasks, groups, project and results are deleted."""
30-
delete_project.delete_project([self.project_id])
31-
32-
time.sleep(1) # Wait for Firebase Functions to complete
33-
32+
def verify_firebase_empty(self):
3433
fb_db = auth.firebaseDB()
3534
ref = fb_db.reference(f"v2/results/{self.project_id}")
3635
self.assertIsNone(ref.get())
@@ -43,29 +42,7 @@ def test_deletion(self):
4342
ref = fb_db.reference(f"v2/projects/{self.project_id}")
4443
self.assertIsNone(ref.get())
4544

46-
pg_db = auth.postgresDB()
47-
sql_query = f"SELECT * FROM tasks WHERE project_id = '{self.project_id}'"
48-
result = pg_db.retr_query(sql_query)
49-
50-
self.assertEqual(result, [])
51-
sql_query = f"SELECT * FROM groups WHERE project_id = '{self.project_id}'"
52-
result = pg_db.retr_query(sql_query)
53-
self.assertEqual(result, [])
54-
sql_query = f"SELECT * FROM projects WHERE project_id = '{self.project_id}'"
55-
result = pg_db.retr_query(sql_query)
56-
self.assertEqual(result, [])
57-
sql_query = "SELECT * FROM results WHERE project_id = '{}'".format(
58-
self.project_id
59-
)
60-
result = pg_db.retr_query(sql_query)
61-
self.assertEqual(result, [])
62-
63-
def test_project_id_not_exists(self):
64-
"""Test for project id which does not exists."""
65-
delete_project.delete_project(["tuna"])
66-
67-
time.sleep(5) # Wait for Firebase Functions to complete
68-
45+
def verify_firebase_not_empty(self):
6946
fb_db = auth.firebaseDB()
7047
ref = fb_db.reference("v2/results")
7148
self.assertIsNotNone(ref.get(shallow=True))
@@ -78,51 +55,79 @@ def test_project_id_not_exists(self):
7855
ref = fb_db.reference("v2/projects")
7956
self.assertIsNotNone(ref.get(shallow=True))
8057

58+
def verify_postgres_empty(self):
8159
pg_db = auth.postgresDB()
8260
sql_query = f"SELECT * FROM tasks WHERE project_id = '{self.project_id}'"
8361
result = pg_db.retr_query(sql_query)
84-
self.assertNotEqual(result, [])
62+
self.assertListEqual(result, [])
8563
sql_query = f"SELECT * FROM groups WHERE project_id = '{self.project_id}'"
8664
result = pg_db.retr_query(sql_query)
87-
self.assertNotEqual(result, [])
65+
self.assertListEqual(result, [])
8866
sql_query = f"SELECT * FROM projects WHERE project_id = '{self.project_id}'"
8967
result = pg_db.retr_query(sql_query)
90-
self.assertNotEqual(result, [])
91-
sql_query = f"SELECT * FROM results WHERE project_id = '{self.project_id}'"
68+
self.assertListEqual(result, [])
69+
sql_query = (
70+
f"SELECT * FROM mapping_sessions WHERE project_id = '{self.project_id}'"
71+
)
9272
result = pg_db.retr_query(sql_query)
93-
self.assertNotEqual(result, [])
94-
95-
def test_project_id_equals_none(self):
96-
"""Test for project id which does not exists."""
97-
delete_project.delete_project([None])
98-
99-
time.sleep(5) # Wait for Firebase Functions to complete
100-
101-
fb_db = auth.firebaseDB()
102-
ref = fb_db.reference("v2/results")
103-
self.assertIsNotNone(ref.get(shallow=True))
104-
ref = fb_db.reference("v2/tasks")
105-
self.assertIsNotNone(ref.get(shallow=True))
106-
ref = fb_db.reference("v2/groups")
107-
self.assertIsNotNone(ref.get(shallow=True))
108-
ref = fb_db.reference("v2/groupsUsers")
109-
self.assertIsNotNone(ref.get(shallow=True))
110-
ref = fb_db.reference("v2/projects")
111-
self.assertIsNotNone(ref.get(shallow=True))
73+
self.assertListEqual(result, [])
74+
sql_query = f"""
75+
SELECT msr.*
76+
FROM mapping_sessions_results msr
77+
JOIN mapping_sessions ms USING (mapping_session_id)
78+
WHERE ms.project_id = '{self.project_id}'
79+
"""
80+
result = pg_db.retr_query(sql_query)
81+
self.assertListEqual(result, [])
11282

83+
def verify_postgres_not_empty(self):
11384
pg_db = auth.postgresDB()
11485
sql_query = f"SELECT * FROM tasks WHERE project_id = '{self.project_id}'"
11586
result = pg_db.retr_query(sql_query)
116-
self.assertNotEqual(result, [])
87+
self.assertGreater(len(result), 0)
11788
sql_query = f"SELECT * FROM groups WHERE project_id = '{self.project_id}'"
11889
result = pg_db.retr_query(sql_query)
119-
self.assertNotEqual(result, [])
90+
self.assertGreater(len(result), 0)
12091
sql_query = f"SELECT * FROM projects WHERE project_id = '{self.project_id}'"
12192
result = pg_db.retr_query(sql_query)
122-
self.assertNotEqual(result, [])
123-
sql_query = f"SELECT * FROM results WHERE project_id = '{self.project_id}'"
93+
self.assertGreater(len(result), 0)
94+
sql_query = (
95+
f"SELECT * FROM mapping_sessions WHERE project_id = '{self.project_id}'"
96+
)
97+
result = pg_db.retr_query(sql_query)
98+
self.assertGreater(len(result), 0)
99+
sql_query = f"""
100+
SELECT msr.*
101+
FROM mapping_sessions_results msr
102+
JOIN mapping_sessions ms USING (mapping_session_id)
103+
WHERE ms.project_id = '{self.project_id}'
104+
"""
124105
result = pg_db.retr_query(sql_query)
125-
self.assertNotEqual(result, [])
106+
self.assertGreater(len(result), 0)
107+
108+
def test_deletion(self):
109+
"""Test if tasks, groups, project and results are deleted."""
110+
delete_project.delete_project([self.project_id])
111+
time.sleep(1) # Wait for Firebase Functions to complete
112+
113+
self.verify_firebase_empty()
114+
self.verify_postgres_empty()
115+
116+
def test_project_id_not_exists(self):
117+
"""Test for project id which does not exists."""
118+
delete_project.delete_project(["tuna"])
119+
time.sleep(5) # Wait for Firebase Functions to complete
120+
121+
self.verify_firebase_not_empty()
122+
self.verify_postgres_not_empty()
123+
124+
def test_project_id_equals_none(self):
125+
"""Test for project id which does not exists."""
126+
delete_project.delete_project([None])
127+
time.sleep(5) # Wait for Firebase Functions to complete
128+
129+
self.verify_firebase_not_empty()
130+
self.verify_postgres_not_empty()
126131

127132
def test_project_id_invalid(self):
128133
"""Test for project id which does not exists."""

0 commit comments

Comments
 (0)