Skip to content

Commit 1c1a6ba

Browse files
committed
feat: add integration test for tutorials
1 parent 994f398 commit 1c1a6ba

File tree

2 files changed

+112
-14
lines changed

2 files changed

+112
-14
lines changed

mapswipe_workers/tests/integration/set_up.py

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,28 @@
1616

1717

1818
def set_firebase_test_data(
19-
project_type: str, data_type: str, fixture_name: str, identifier: str
19+
project_type: str,
20+
data_type: str,
21+
fixture_name: str,
22+
identifier: str,
23+
tutorial_id: str = None,
2024
):
2125
test_dir = os.path.dirname(__file__)
2226
fixture_name = fixture_name + ".json"
2327
file_path = os.path.join(
2428
test_dir, "fixtures", project_type, data_type, fixture_name
2529
)
26-
upload_file_to_firebase(file_path, data_type, identifier)
30+
upload_file_to_firebase(file_path, data_type, identifier, tutorial_id=tutorial_id)
2731

2832

29-
def upload_file_to_firebase(file_path: str, data_type: str, identifier: str):
33+
def upload_file_to_firebase(
34+
file_path: str, data_type: str, identifier: str, tutorial_id: str = None
35+
):
3036
with open(file_path) as test_file:
3137
test_data = json.load(test_file)
3238

39+
if tutorial_id:
40+
test_data["tutorialId"] = tutorial_id
3341
fb_db = auth.firebaseDB()
3442
ref = fb_db.reference(f"/v2/{data_type}/{identifier}")
3543
ref.set(test_data)
@@ -85,15 +93,20 @@ def create_test_project(
8593
set_postgres_test_data(project_type, "users", "user")
8694
set_firebase_test_data(project_type, "user_groups", "user_group", "")
8795
set_firebase_test_data(project_type, "results", fixture_name, project_id)
88-
set_postgres_test_data(project_type, "mapping_sessions", fixture_name, columns=[
89-
"project_id",
90-
"group_id",
91-
"user_id",
92-
"mapping_session_id",
93-
"start_time",
94-
"end_time",
95-
"items_count",
96-
])
96+
set_postgres_test_data(
97+
project_type,
98+
"mapping_sessions",
99+
fixture_name,
100+
columns=[
101+
"project_id",
102+
"group_id",
103+
"user_id",
104+
"mapping_session_id",
105+
"start_time",
106+
"end_time",
107+
"items_count",
108+
],
109+
)
97110
set_postgres_test_data(project_type, mapping_sessions_results, fixture_name)
98111
if create_user_group_session_data:
99112
set_postgres_test_data(
@@ -108,7 +121,9 @@ def create_test_project(
108121
"created_at",
109122
],
110123
)
111-
set_postgres_test_data(project_type, "mapping_sessions_user_groups", fixture_name)
124+
set_postgres_test_data(
125+
project_type, "mapping_sessions_user_groups", fixture_name
126+
)
112127

113128
time.sleep(5) # Wait for Firebase Functions to complete
114129
return project_id
@@ -131,12 +146,24 @@ def create_test_user(project_type: str, user_id: str = None) -> str:
131146

132147

133148
def create_test_project_draft(
134-
project_type: str, fixture_name: str = "user", identifier: str = ""
149+
project_type: str,
150+
fixture_name: str = "user",
151+
identifier: str = "",
152+
tutorial_id: str = None,
135153
) -> str:
136154
"""
137155
Create test project drafts in Firebase and return project ids.
138156
Project drafts in Firebase are created by project manager using the dashboard.
139157
"""
158+
if tutorial_id:
159+
set_firebase_test_data(
160+
project_type,
161+
"projectDrafts",
162+
fixture_name,
163+
identifier,
164+
tutorial_id=tutorial_id,
165+
)
166+
return identifier
140167
if not identifier:
141168
identifier = f"test_{fixture_name}"
142169
set_firebase_test_data(project_type, "projectDrafts", fixture_name, identifier)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
3+
from click.testing import CliRunner
4+
5+
from mapswipe_workers import auth, mapswipe_workers
6+
from mapswipe_workers.utils.create_directories import create_directories
7+
from tests.integration import set_up, tear_down
8+
9+
10+
class TestCreateTileClassificationProject(unittest.TestCase):
11+
def setUp(self):
12+
self.tutorial_id = set_up.create_test_tutorial_draft("footprint", "footprint")
13+
14+
self.project_id = set_up.create_test_project_draft(
15+
"tile_classification",
16+
"tile_classification",
17+
"test_tile_classification_tutorial",
18+
tutorial_id=self.tutorial_id,
19+
)
20+
create_directories()
21+
22+
def tearDown(self):
23+
tear_down.delete_test_data(self.project_id)
24+
25+
def test_create_tile_classification_project(self):
26+
runner = CliRunner()
27+
runner.invoke(mapswipe_workers.run_create_projects, catch_exceptions=False)
28+
29+
pg_db = auth.postgresDB()
30+
query = "SELECT project_id FROM projects WHERE project_id = %s"
31+
result = pg_db.retr_query(query, [self.project_id])[0][0]
32+
self.assertEqual(result, self.project_id)
33+
34+
query = """
35+
SELECT project_id
36+
FROM projects
37+
WHERE project_id = %s
38+
and project_type_specifics::jsonb ? 'customOptions'
39+
"""
40+
result = pg_db.retr_query(query, [self.project_id])[0][0]
41+
self.assertEqual(result, self.project_id)
42+
43+
query = "SELECT count(*) FROM groups WHERE project_id = %s"
44+
result = pg_db.retr_query(query, [self.project_id])[0][0]
45+
self.assertEqual(result, 20)
46+
47+
query = "SELECT count(*) FROM tasks WHERE project_id = %s"
48+
result = pg_db.retr_query(query, [self.project_id])[0][0]
49+
self.assertEqual(result, 5040)
50+
51+
fb_db = auth.firebaseDB()
52+
ref = fb_db.reference(f"/v2/projects/{self.project_id}")
53+
result = ref.get(shallow=True)
54+
self.assertIsNotNone(result)
55+
56+
ref = fb_db.reference(f"/v2/groups/{self.project_id}")
57+
result = ref.get(shallow=True)
58+
self.assertEqual(len(result), 20)
59+
60+
# Tile classification projects do not have tasks in Firebase
61+
ref = fb_db.reference(f"/v2/tasks/{self.project_id}")
62+
result = ref.get(shallow=True)
63+
self.assertIsNone(result)
64+
65+
ref = fb_db.reference(f"/v2/projects/{self.project_id}/tutorialId")
66+
result = ref.get(shallow=True)
67+
self.assertEqual(self.tutorial_id, result)
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()

0 commit comments

Comments
 (0)