This repository was archived by the owner on Apr 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathset_up.py
More file actions
185 lines (159 loc) · 5.56 KB
/
set_up.py
File metadata and controls
185 lines (159 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
Helper functions for test set up.
Directory structure of fixtures: fixtures/project_type/data_type/fixture_name
- project_type is either tile_map_service_grid or arbitrary_geometry.
- data_type is one of following: projectDrafts, projects, groups, tasks, users, results
- fixture_name is the name of the fixture file without extension. E.g. build_area
"""
import json
import os
import time
from typing import List, Union
from mapswipe_workers import auth
def set_firebase_test_data(
project_type: str,
data_type: str,
fixture_name: str,
identifier: str,
tutorial_id: str = None,
):
test_dir = os.path.dirname(__file__)
fixture_name = fixture_name + ".json"
file_path = os.path.join(
test_dir, "fixtures", project_type, data_type, fixture_name
)
upload_file_to_firebase(file_path, data_type, identifier, tutorial_id=tutorial_id)
def upload_file_to_firebase(
file_path: str, data_type: str, identifier: str, tutorial_id: str = None
):
with open(file_path) as test_file:
test_data = json.load(test_file)
if tutorial_id:
test_data["tutorialId"] = tutorial_id
fb_db = auth.firebaseDB()
ref = fb_db.reference(f"/v2/{data_type}/{identifier}")
ref.set(test_data)
def set_postgres_test_data(
project_type: str,
data_type: str,
fixture_name: str,
columns: Union[None, List[str]] = None,
) -> None:
test_dir = os.path.dirname(__file__)
fixture_name = fixture_name + ".csv"
file_path = os.path.join(
test_dir, "fixtures", project_type, data_type, fixture_name
)
pg_db = auth.postgresDB()
with open(file_path) as test_file:
pg_db.copy_from(test_file, data_type, columns=columns)
def create_test_project(
project_type: str,
fixture_name: str,
results: bool = False,
create_user_group_session_data: bool = False,
mapping_sessions_results: str = "mapping_sessions_results",
) -> str:
"""Create a test data in Firebase and Posgres."""
project_id = "test_{0}".format(fixture_name)
for data_type, columns in [
("projects", None),
(
"groups",
[
"project_id",
"group_id",
"number_of_tasks",
"finished_count",
"required_count",
"progress",
"project_type_specifics",
],
),
("tasks", None),
]:
set_firebase_test_data(project_type, data_type, fixture_name, project_id)
set_postgres_test_data(project_type, data_type, fixture_name, columns=columns)
if results:
set_firebase_test_data(project_type, "users", "user", project_id)
set_postgres_test_data(project_type, "users", "user")
set_firebase_test_data(project_type, "user_groups", "user_group", "")
set_firebase_test_data(project_type, "results", fixture_name, project_id)
set_postgres_test_data(
project_type,
"mapping_sessions",
fixture_name,
columns=[
"project_id",
"group_id",
"user_id",
"mapping_session_id",
"start_time",
"end_time",
"items_count",
],
)
set_postgres_test_data(project_type, mapping_sessions_results, fixture_name)
if create_user_group_session_data:
set_postgres_test_data(
project_type,
"user_groups",
fixture_name,
columns=[
"user_group_id",
"name",
"description",
"is_archived",
"created_at",
],
)
set_postgres_test_data(
project_type, "mapping_sessions_user_groups", fixture_name
)
time.sleep(5) # Wait for Firebase Functions to complete
return project_id
def create_test_results(project_type: str, fixture_name: str) -> str:
"""Create test results only in Firebase."""
project_id = f"test_{project_type}"
set_firebase_test_data(project_type, "results", fixture_name, project_id)
time.sleep(5) # Wait for Firebase Functions to complete
return project_id
def create_test_user(project_type: str, user_id: str = None) -> str:
"""Create test user only in Firebase"""
if not user_id:
user_id = f"test_{project_type}"
set_firebase_test_data(project_type, "users", "user", user_id)
return user_id
def create_test_project_draft(
project_type: str,
fixture_name: str = "user",
identifier: str = "",
tutorial_id: str = None,
) -> str:
"""
Create test project drafts in Firebase and return project ids.
Project drafts in Firebase are created by project manager using the dashboard.
"""
if tutorial_id:
set_firebase_test_data(
project_type,
"projectDrafts",
fixture_name,
identifier,
tutorial_id=tutorial_id,
)
return identifier
if not identifier:
identifier = f"test_{fixture_name}"
set_firebase_test_data(project_type, "projectDrafts", fixture_name, identifier)
return identifier
def create_test_tutorial_draft(
project_type: str, fixture_name: str = "user", identifier: str = ""
) -> str:
"""."""
if not identifier:
identifier = f"test_{fixture_name}"
set_firebase_test_data(project_type, "tutorialDrafts", fixture_name, identifier)
return f"tutorial_{identifier}"
if __name__ == "__main__":
create_test_project_draft("build_area")