-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_export_import.py
301 lines (260 loc) · 11.9 KB
/
test_export_import.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import pandas as pd
from flask import url_for
from openatlas import app
from openatlas.api.resources.api_entity import ApiEntity
from openatlas.models.export import current_date_for_filename
from tests.base import ExportImportTestCase
class ExportImportTest(ExportImportTestCase):
def test_export(self) -> None:
c = self.client
assert b'Export SQL' in c.get(url_for('export_sql')).data
date_ = current_date_for_filename()
rv = c.get(
url_for('export_execute', format_='sql'),
follow_redirects=True)
assert b'Data was exported' in rv.data
rv = c.get(url_for('download_sql', filename=f'{date_}_export.sql.7z'))
assert b'7z' in rv.data
date_ = current_date_for_filename()
rv = c.get(
url_for('export_execute', format_='dump'),
follow_redirects=True)
assert b'Data was exported' in rv.data
rv = c.get(url_for('download_sql', filename=f'{date_}_export.dump.7z'))
assert b'7z' in rv.data
assert b'Warning' in c.get(url_for('sql_index')).data
assert b'execute' in c.get(url_for('sql_execute')).data
rv = c.post(
url_for('sql_execute'),
data={'statement': 'SELECT * FROM web.user;'})
assert b'Alice' in rv.data
rv = c.post(url_for('sql_execute'), data={'statement': 'e'})
assert b'syntax error' in rv.data
rv = c.get(url_for('import_project_insert'))
assert b'name *' in rv.data
rv = c.post(url_for('import_project_insert'), data={'name': 'X-Files'})
p_id = rv.location.split('/')[-1]
rv = c.get(url_for('import_project_update', id_=p_id))
assert b'name *' in rv.data
rv = c.post(
url_for('import_project_update', id_=p_id),
data={'name': 'X-Files', 'description': 'whoa!'},
follow_redirects=True)
assert b'whoa!' in rv.data
rv = c.post(url_for('import_project_insert'), data={'name': 'X-Files'})
assert b'The name is already in use' in rv.data
rv = c.get(url_for('import_index'))
assert b'X-Files' in rv.data
rv = c.get(url_for('import_data', class_='person', project_id=p_id))
assert b'file *' in rv.data
with open(self.test_path / 'bibliography.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='bibliography', project_id=p_id),
data={'file': file, 'duplicate': True},
follow_redirects=True)
assert b'OpenAtlas 2024' in rv.data
with open(self.static_path / 'example.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file, 'duplicate': True},
follow_redirects=True)
assert b'Vienna' in rv.data
with open(self.static_path / 'example.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file, 'duplicate': True},
follow_redirects=True)
assert b'IDs already in database' in rv.data
with open(self.static_path / 'favicon.ico', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file},
follow_redirects=True)
assert b'File type not allowed' in rv.data
with open(self.test_path / 'invalid_1.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='source', project_id=p_id),
data={'file': file},
follow_redirects=True)
assert b'missing name column' in rv.data
with open(self.test_path / 'invalid_2.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file},
follow_redirects=True)
assert b'invalid columns: not_existing_column' in rv.data
assert b'invalid administrative units' in rv.data
assert b'invalid type ids' in rv.data
assert b'invalid value type ids' in rv.data
assert b'invalid value type values' in rv.data
assert b'invalid reference system' in rv.data
assert b'invalid reference id' in rv.data
assert b'empty names' in rv.data
assert b'double IDs in import' in rv.data
with open(self.test_path / 'invalid_3.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file},
follow_redirects=True)
assert b'invalid coordinates' in rv.data
assert b'invalid dates' in rv.data
assert b'invalid value types' in rv.data
assert b'invalid reference system value' in rv.data
assert b'invalid match type' in rv.data
assert b'invalid openatlas class' in rv.data
assert b'invalid parent class' in rv.data
assert b'empty ids' in rv.data
data_frame = pd.read_csv(
self.test_path / 'invalid_3.csv',
keep_default_na=False)
data_frame.at[0, 'openatlas_class'] = 'place'
data_frame.to_csv(
self.test_path / 'invalid_3_modified.csv',
index=False)
with open(self.test_path / 'invalid_3_modified.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file},
follow_redirects=True)
assert b'invalid parent class' in rv.data
data_frame.at[4, 'parent_id'] = 'strati_1'
data_frame.at[4, 'openatlas_class'] = 'human remains'
data_frame.to_csv(
self.test_path / 'invalid_3_modified.csv',
index=False)
with open(self.test_path / 'invalid_3_modified.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file},
follow_redirects=True)
assert b'invalid parent id' in rv.data
(self.test_path / 'invalid_3_modified.csv').unlink()
def test_export2(self) -> None:
c = self.client
rv = c.post(url_for('import_project_insert'), data={'name': 'X-Files'})
p_id = rv.location.split('/')[-1]
with app.test_request_context():
app.preprocess_request()
for entity in ApiEntity.get_by_cidoc_classes(['all']):
match entity.name:
case 'Boundary Mark':
boundary_mark = entity
case 'Infrastructure':
infrastructure = entity
case 'Austria':
austria = entity
case 'Height':
height = entity
case 'Carantania':
carantania = entity
case 'Place':
place_type = entity
case 'https://lotr.fandom.com/':
reference = entity
case 'Shire':
place = entity
data_frame = pd.read_csv(
self.test_path / 'invalid_3.csv',
keep_default_na=False)
data_frame.at[4, 'openatlas_parent_id'] = place.id
data_frame.to_csv(
self.test_path / 'invalid_3_modified.csv',
index=False)
with open(self.test_path / 'invalid_3_modified.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file},
follow_redirects=True)
assert b'multiple parent IDs' in rv.data
data_frame.at[3, 'openatlas_parent_id'] = 99999
data_frame.to_csv(
self.test_path / 'invalid_3_modified.csv',
index=False)
with open(self.test_path / 'invalid_3_modified.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file},
follow_redirects=True)
assert b'invalid parent id' in rv.data
data_frame.at[3, 'openatlas_parent_id'] = place_type.id
data_frame.to_csv(
self.test_path / 'invalid_3_modified.csv',
index=False)
with open(self.test_path / 'invalid_3_modified.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file},
follow_redirects=True)
assert b'invalid parent class' in rv.data
(self.test_path / 'invalid_3_modified.csv').unlink()
data_frame = pd.read_csv(
self.static_path / 'example.csv',
keep_default_na=False)
data_frame.at[0, 'id'] = 'new_place_1'
data_frame.at[1, 'id'] = 'new_place_2'
data_frame.at[2, 'id'] = 'new_place_3'
data_frame.at[0, 'administrative_unit_id'] = austria.id
data_frame.at[0, 'historical_place_id'] = carantania.id
type_ids = [
boundary_mark.id,
infrastructure.id,
austria.id,
place_type.id]
data_frame.at[0, 'type_ids'] = ' '.join(map(str, type_ids))
data_frame.at[0, 'value_types'] = f'{height.id};42'
data_frame.at[0, 'reference_ids'] = f'{reference.id};IV'
data_frame.at[0, 'wkt'] = "POLYGON((16.1203 BLA, 16.606275))"
data_frame.to_csv(self.test_path / 'example.csv', index=False)
with open(self.test_path / 'example.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file, 'duplicate': True},
follow_redirects=True)
assert b'single type duplicates' in rv.data
assert b'invalid origin reference id' in rv.data
assert b'Vienna' in rv.data
with open(self.test_path / 'example.csv', 'rb') as file:
rv = c.post(
url_for('import_data', class_='source', project_id=p_id),
data={'file': file, 'duplicate': True},
follow_redirects=True)
assert b'invalid reference system class' in rv.data
(self.test_path / 'example.csv').unlink()
data_frame = pd.read_csv(
self.static_path / 'example_place_hierarchy.csv',
keep_default_na=False)
data_frame.at[7, 'openatlas_parent_id'] = place.id
data_frame.to_csv(
self.test_path / 'example_place_hierarchy.csv',
index=False)
with open(
self.test_path / 'example_place_hierarchy.csv',
'rb') as file:
rv = c.post(
url_for('import_data', class_='place', project_id=p_id),
data={'file': file, 'duplicate': True},
follow_redirects=True)
assert b'Bone' in rv.data
(self.test_path / 'example_place_hierarchy.csv').unlink()
rv = c.get(url_for('import_project_view', id_=p_id))
assert b'London' in rv.data
rv = c.get(
url_for('import_project_delete', id_=p_id),
follow_redirects=True)
assert b'Project deleted' in rv.data
date_ = current_date_for_filename()
rv = c.get(
url_for('delete_export', filename=f'{date_}_export.sql.7z'),
follow_redirects=True)
if os.name == 'posix':
assert b'File deleted' in rv.data
rv = c.get(
url_for('delete_export', filename=f'{date_}_export.dump.7z'),
follow_redirects=True)
if os.name == 'posix':
assert b'File deleted' in rv.data
rv = c.get(
url_for('delete_export', filename='non_existing'),
follow_redirects=True)
assert b'An error occurred when trying to delete the f' in rv.data