Skip to content

Commit c9d5c49

Browse files
committed
add readme to tests and explain how test data can be added to fixtures
1 parent 87e4808 commit c9d5c49

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

mapswipe_workers/tests/readme.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# How to add test data
2+
Test data can be extracted from the existing MapSwipe database. This is often useful when you want to fix a bug which you observed in the production environment but are not yet exactly sure why it actually occurred in the first place.
3+
4+
For a given project id (e.g. `-NFNr55R_LYJvxP7wmte`) you can follow these steps:
5+
6+
## Export Data from Production or Development Database
7+
To set up a project in the test environment it is very likely that you first need to set up all the data in postgres (and maybe also in Firebase).
8+
9+
You can extract the data into csv files like this. Make sure to adjust the where statement to your needs. If you know which part of the data is affected or causing the bug, then it's good to be very specific and filter only for these groups etc.
10+
11+
```
12+
\copy (
13+
select created, created_by, geom, image, is_featured, look_for
14+
,"name" , progress , project_details , project_id , project_type
15+
,required_results , result_count , status , verification_number
16+
,project_type_specifics
17+
,'test_org' as organization_name
18+
from projects
19+
where project_id = '-NFNr55R_LYJvxP7wmte'
20+
) to 'test_project.csv' delimiter E'\t' NULL '\N' csv
21+
```
22+
23+
```
24+
\copy (
25+
select *
26+
from groups
27+
where
28+
project_id = '-NFNr55R_LYJvxP7wmte'
29+
and group_id in ('g101', 'g102', 'g103', 'g104', 'g105', 'g106', 'g107', 'g108', 'g109' )
30+
) to 'test_groups.csv' delimiter E'\t' NULL '\N' cs
31+
```
32+
33+
```
34+
\copy (
35+
select project_id, group_id, task_id, geom, project_type_specifics::json as project_type_specifics from tasks
36+
where
37+
project_id = '-NFNr55R_LYJvxP7wmte'
38+
and group_id in ('g101', 'g102', 'g103', 'g104', 'g105', 'g106', 'g107', 'g108', 'g109' )
39+
) to 'test_tasks.csv' delimiter E'\t' csv
40+
```
41+
42+
These 3 tables contain a column in json format. When exporting this into a csv file there is an issue with the quotes, which prevents us from importing the csv file correctly into the test database. Hence you need to adjust the quotes manually in the csv file.
43+
44+
* open the file with `vi filename.csv`
45+
* type `:` and the first command: `%s/\(.*\t\)"{\(.*\)}"/\1{\2}/g` (capturing groups: \(something\) will save "something" into \1)
46+
* hit enter, then do the same with the second command: `%s/""/"/g` (it's a search/replace expression, like sed, :%s/thing_to_search/repalcement/g)
47+
* save with `:w` for (write) and quit with `:q`
48+
49+
```
50+
\copy (
51+
select * from mapping_sessions ms
52+
where project_id = '-NFNr55R_LYJvxP7wmte'
53+
and group_id in ('g101', 'g102', 'g103', 'g104', 'g105', 'g106', 'g107', 'g108', 'g109' )
54+
) to 'test_mapping_sessions.csv' delimiter E'\t' csv
55+
```
56+
57+
```
58+
\copy (
59+
select msr.* from mapping_sessions_results msr
60+
join mapping_sessions ms using (mapping_session_id)
61+
where
62+
ms.project_id = '-NFNr55R_LYJvxP7wmte'
63+
and group_id in ('g101', 'g102', 'g103', 'g104', 'g105', 'g106', 'g107', 'g108', 'g109' )
64+
) to 'test_mapping_sessions_results.csv' delimiter E'\t' csv
65+
```
66+
67+
```
68+
\copy (
69+
select user_id ,username ,created ,updated_at from users u
70+
join mapping_sessions ms using (user_id)
71+
where
72+
ms.project_id = '-NFNr55R_LYJvxP7wmte'
73+
and group_id in ('g101', 'g102', 'g103', 'g104', 'g105', 'g106', 'g107', 'g108', 'g109' )
74+
group by user_id
75+
) to 'test_users.csv' delimiter E'\t' NULL '\N' csv
76+
```
77+
78+
## Copy files to fixtures
79+
When testing some mapswipe_workers functionality which relies on the postgres DB we use fixtures which are defined in `mapswipe_workers/tests/integration/fixtures`.
80+
81+
Add your files to the right folder concerning the type of your project.
82+
* e.g. add your project file here: `mapswipe_workers/tests/integration/fixtures/tile_map_service_grid/projects/my_project_name.csv`
83+
84+
You can copy the files using scp like this. Make sure to adjust the json quoting issue as described above.
85+
86+
```
87+
scp build_area_sandoa.csv mapswipe-workers-hetzner:/opt/mapswipe/python-mapswipe-workers/test_projects.csv ./tile_map_service_grid/projects
88+
scp build_area_sandoa.csv mapswipe-workers-hetzner:/opt/mapswipe/python-mapswipe-workers/test_groups.csv ./tile_map_service_grid/groups
89+
scp build_area_sandoa.csv mapswipe-workers-hetzner:/opt/mapswipe/python-mapswipe-workers/test_tasks.csv ./tile_map_service_grid/tasks
90+
scp build_area_sandoa.csv mapswipe-workers-hetzner:/opt/mapswipe/python-mapswipe-workers/test_mapping_sessions.csv ./tile_map_service_grid/mapping_sessions
91+
scp build_area_sandoa.csv mapswipe-workers-hetzner:/opt/mapswipe/python-mapswipe-workers/test_mapping_sessions_results.csv ./tile_map_service_grid/mapping_sessions_results
92+
scp build_area_sandoa.csv mapswipe-workers-hetzner:/opt/mapswipe/python-mapswipe-workers/test_users.csv ./tile_map_service_grid/users
93+
```
94+
95+
## Add your test
96+
When adding a new test make sure to set up your postgres DB using your newly created csv files by setting the correct fixture name. In this case all the csv files should be named `build_area_sandoa.csv`.
97+
98+
So for example this file should exist: `mapswipe_workers/tests/integration/fixtures/tile_map_service_grid/projects/build_area_sandoa.csv`
99+
100+
```python
101+
def setUp(self):
102+
super().setUp()
103+
project_type = "tile_map_service_grid"
104+
fixture_name = "build_area_sandoa"
105+
self.project_id = "-NFNr55R_LYJvxP7wmte"
106+
107+
for data_type in [
108+
"projects",
109+
"groups",
110+
"tasks",
111+
"users",
112+
"mapping_sessions",
113+
"mapping_sessions_results",
114+
]:
115+
set_up.set_postgres_test_data(project_type, data_type, fixture_name)
116+
```
117+

0 commit comments

Comments
 (0)