Coverage for tests/test_contacts.py: 100%
199 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-03 20:32 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-03 20:32 +0000
1# SPDX-FileCopyrightText: 2021 Forschungszentrum Jülich GmbH
2# SPDX-License-Identifier: MIT
4import pytest
5import json
6from toardb.contacts.models import Person, Organisation, Contact
7# Required imports 'create_test_database'
8from toardb.test_base import (
9 client,
10 get_test_db,
11 create_test_database,
12 url,
13 get_test_engine,
14 test_db_session as db,
15)
17class TestApps:
18 def setup(self):
19 self.application_url = "/contacts/"
21 """Set up all the data before each test
22 If you want the setup only once (per test module),
23 the scope argument is not working in the expected way, as discussed here:
24 https://stackoverflow.com/questions/45817153/py-test-fixture-use-function-fixture-in-scope-fixture
25 """
26 @pytest.fixture(autouse=True)
27 def setup_db_data(self, db):
28 # id_seq will not be reset automatically between tests!
29 _db_conn = get_test_engine()
30 fake_conn = _db_conn.raw_connection()
31 fake_cur = fake_conn.cursor()
32 fake_cur.execute("ALTER SEQUENCE persons_id_seq RESTART WITH 1")
33 fake_conn.commit()
34 fake_cur.execute("ALTER SEQUENCE organisations_id_seq RESTART WITH 1")
35 fake_conn.commit()
36 fake_cur.execute("ALTER SEQUENCE contacts_id_seq RESTART WITH 1")
37 fake_conn.commit()
38 infilename = "tests/fixtures/contacts/persons.json"
39 with open(infilename, encoding="utf-8") as f:
40 metajson=json.load(f)
41 for entry in metajson:
42 new_person = Person(**entry)
43 db.add(new_person)
44 db.commit()
45 db.refresh(new_person)
46 infilename = "tests/fixtures/contacts/organisations.json"
47 with open(infilename, encoding="utf-8") as f:
48 metajson=json.load(f)
49 for entry in metajson:
50 new_organisation = Organisation(**entry)
51 db.add(new_organisation)
52 db.commit()
53 db.refresh(new_organisation)
54 infilename = "tests/fixtures/contacts/contacts.json"
55 with open(infilename, encoding="utf-8") as f:
56 metajson=json.load(f)
57 for entry in metajson:
58 new_contact = Contact(**entry)
59 db.add(new_contact)
60 db.commit()
61 db.refresh(new_contact)
63 def test_get_organisations(self, client, db):
64 response = client.get("/contacts/organisations/")
65 expected_status_code = 200
66 assert response.status_code == expected_status_code
67 expected_resp = [{"id":0,"name":"","longname":"",
68 "kind":"other","city":"","postcode":"",
69 "street_address":"","country":"N/A",
70 "homepage":"", "contact_url": "" },
71 {"id":1,"name":"UBA","longname":"Umweltbundesamt",
72 "kind":"government","city":"Dessau-Roßlau","postcode":"06844",
73 "street_address":"Wörlitzer Platz 1","country":"Germany",
74 "homepage":"https://www.umweltbundesamt.de",
75 "contact_url": "mailto:immission@uba.de"},
76 {"id":2,"name":"FZJ","longname":"Forschungszentrum Jülich",
77 "kind":"research","city":"Jülich","postcode":"52425",
78 "street_address":"Wilhelm-Johnen-Straße","country":"Germany",
79 "homepage":"https://www.fz-juelich.de",
80 "contact_url": "mailto:toar-data@fz-juelich.de"},
81 {"id": 3,"name": "EEA","longname": "European Environment Agency",
82 "kind": "university","city": "Copenhagen","postcode": "1050",
83 "street_address": "Kongens Nytorv 6","country": "Germany",
84 "homepage": "https://www.eea.europa.eu/en",
85 "contact_url": "mailto:info@eea.europa.eu"}]
86 assert response.json() == expected_resp
89 def test_get_all_organisations(self, client, db):
90 response = client.get("/contacts/organisations/?limit=None")
91 expected_status_code = 200
92 assert response.status_code == expected_status_code
93 expected_resp = [{"id":0,"name":"","longname":"",
94 "kind":"other","city":"","postcode":"",
95 "street_address":"","country":"N/A",
96 "homepage":"", "contact_url": "" },
97 {"id":1,"name":"UBA","longname":"Umweltbundesamt",
98 "kind":"government","city":"Dessau-Roßlau","postcode":"06844",
99 "street_address":"Wörlitzer Platz 1","country":"Germany",
100 "homepage":"https://www.umweltbundesamt.de",
101 "contact_url": "mailto:immission@uba.de"},
102 {"id":2,"name":"FZJ","longname":"Forschungszentrum Jülich",
103 "kind":"research","city":"Jülich","postcode":"52425",
104 "street_address":"Wilhelm-Johnen-Straße","country":"Germany",
105 "homepage":"https://www.fz-juelich.de",
106 "contact_url": "mailto:toar-data@fz-juelich.de"},
107 {"id": 3,"name": "EEA","longname": "European Environment Agency",
108 "kind": "university","city": "Copenhagen","postcode": "1050",
109 "street_address": "Kongens Nytorv 6","country": "Germany",
110 "homepage": "https://www.eea.europa.eu/en",
111 "contact_url": "mailto:info@eea.europa.eu"}]
112 assert response.json() == expected_resp
115 def test_get_special_organisation(self, client, db):
116 response = client.get("/contacts/organisations/id/2")
117 expected_status_code = 200
118 assert response.status_code == expected_status_code
119 expected_resp = {"id":2,"name":"FZJ","longname":"Forschungszentrum Jülich",
120 "kind":"research","city":"Jülich","postcode":"52425",
121 "street_address":"Wilhelm-Johnen-Straße","country":"Germany",
122 "homepage":"https://www.fz-juelich.de",
123 "contact_url": "mailto:toar-data@fz-juelich.de"}
124 assert response.json() == expected_resp
127 def test_get_special_organisation_out_of_index(self, client, db):
128 response = client.get("/contacts/organisations/id/99")
129 expected_status_code = 404
130 assert response.status_code == expected_status_code
131 expected_resp = {"detail":"Organisation not found."}
132 assert response.json() == expected_resp
135 def test_get_special_organisation_by_name(self, client, db):
136 response = client.get("/contacts/organisations/FZJ")
137 expected_status_code = 200
138 assert response.status_code == expected_status_code
139 expected_resp = {"id":2,"name":"FZJ","longname":"Forschungszentrum Jülich",
140 "kind":"research","city":"Jülich","postcode":"52425",
141 "street_address":"Wilhelm-Johnen-Straße","country":"Germany",
142 "homepage":"https://www.fz-juelich.de",
143 "contact_url": "mailto:toar-data@fz-juelich.de"}
144 assert response.json() == expected_resp
147# def test_insert_new_organisation_without_credits(self):
148#? response = client.post("/contacts/organisations/")
149# expected_status_code=401
150# assert response.status_code == expected_status_code
151#? expected_resp = ...
152# assert response.json() == expected_resp
156# def test_insert_new_organisation_wrong_credits(self):
157#? response = client.post("/contacts/organisations/")
158# expected_status_code = 401
159# assert response.status_code == expected_status_code
160#? expected_resp = ...
161# assert response.json() == expected_resp
164 def test_insert_new_organisation(self, client, db):
165 response = client.post("/contacts/organisations/",
166 json= {"organisation":
167 {"name": "FZJ2", "longname": "Forschungszentrum Test",
168 "kind": "Research", "city": "Jülich", "postcode": "52425",
169 "street_address": "Wilhelm-Johnen-Straße",
170 "country": "DE",
171 "homepage": "https://www.fz-juelich.de",
172 "contact_url": "mailto:toar-data2@fz-juelich.de"}
173 }
174 )
175 expected_status_code = 200
176 assert response.status_code == expected_status_code
177 expected_resp = {"id":4,"name":"FZJ2","longname":"Forschungszentrum Test",
178 "kind":"research","city":"Jülich","postcode":"52425",
179 "street_address":"Wilhelm-Johnen-Straße","country":"Germany",
180 "homepage":"https://www.fz-juelich.de",
181 "contact_url": "mailto:toar-data2@fz-juelich.de"}
182 assert response.json() == expected_resp
185 def test_insert_duplicate_organisation(self, client, db):
186 response = client.post("/contacts/organisations/",
187 json= {"organisation":
188 {"name": "FZJ", "longname": "Forschungszentrum Jülich",
189 "kind": "Research", "city": "Jülich", "postcode": "52425",
190 "street_address": "Wilhelm-Johnen-Straße",
191 "country": "DE",
192 "homepage": "https://www.fz-juelich.de",
193 "contact_url": "mailto:toar-data2@fz-juelich.de"}
194 }
195 )
196 expected_status_code = 400
197 assert response.status_code == expected_status_code
198 expected_resp = {'detail': 'Organisation already registered.'}
199 assert response.json() == expected_resp
202 def test_get_persons(self, client, db):
203 response = client.get("/contacts/persons/")
204 expected_status_code = 200
205 assert response.status_code == expected_status_code
206 expected_resp = [{'id': 3, 'name': 'Sabine Schröder', 'email': 's.schroeder@fz-juelich.de',
207 'phone': '+49-2461-61-6397', 'orcid': '0000-0002-0309-8010', 'isprivate': False}]
208 assert response.json() == expected_resp
211 def test_get_special_person(self, client, db):
212 response = client.get("/contacts/persons/?id=3")
213 expected_status_code = 200
214 assert response.status_code == expected_status_code
215 expected_resp = [{'id': 3, 'name': 'Sabine Schröder', 'email': 's.schroeder@fz-juelich.de',
216 'phone': '+49-2461-61-6397', 'orcid': '0000-0002-0309-8010', 'isprivate': False}]
217 assert response.json() == expected_resp
220 def test_get_special_person_out_of_index(self, client, db):
221 response = client.get("/contacts/persons/id/3")
222 expected_status_code = 200
223 assert response.status_code == expected_status_code
224 expected_resp = {'id': 3, 'name': 'Sabine Schröder', 'email': 's.schroeder@fz-juelich.de',
225 'phone': '+49-2461-61-6397', 'orcid': '0000-0002-0309-8010', 'isprivate': False}
226 assert response.json() == expected_resp
229 def test_get_special_person_out_of_index_not_found(self, client, db):
230 response = client.get("/contacts/persons/id/99")
231 expected_status_code = 404
232 assert response.status_code == expected_status_code
233 expected_resp = {"detail":"Person not found."}
234 assert response.json() == expected_resp
237 def test_get_special_person_by_name_url(self, client, db):
238 response = client.get("/contacts/persons/Sabine Schröder")
239 expected_status_code = 200
240 assert response.status_code == expected_status_code
241 expected_resp = {'id': 3, 'name': 'Sabine Schröder', 'email': 's.schroeder@fz-juelich.de',
242 'phone': '+49-2461-61-6397', 'orcid': '0000-0002-0309-8010', 'isprivate': False}
243 assert response.json() == expected_resp
246 def test_get_special_person_by_name(self, client, db):
247 response = client.get("/contacts/persons/?name=Sabine Schröder")
248 expected_status_code = 200
249 assert response.status_code == expected_status_code
250 expected_resp = [{'id': 3, 'name': 'Sabine Schröder', 'email': 's.schroeder@fz-juelich.de',
251 'phone': '+49-2461-61-6397', 'orcid': '0000-0002-0309-8010', 'isprivate': False}]
252 assert response.json() == expected_resp
255 def test_get_special_person_by_name_using_fields(self, client, db):
256 response = client.get("/contacts/persons/?name=Sabine Schröder&fields=orcid")
257 expected_status_code = 200
258 assert response.status_code == expected_status_code
259 expected_resp = [{'orcid': '0000-0002-0309-8010'}]
260 assert response.json() == expected_resp
263 def test_get_special_person_using_wrong_fieldname(self, client, db):
264 response = client.get("/contacts/persons/?name=Sabine Schröder&fields=orcid,bla")
265 expected_status_code = 400
266 assert response.status_code == expected_status_code
267 expected_resp = "Wrong field given: bla"
268 assert response.json() == expected_resp
271# def test_insert_new_person_without_credits(self):
272#? response = client.post("/contacts/persons/")
273# expected_status_code=401
274# assert response.status_code == expected_status_code
275#? expected_resp = ...
276# assert response.json() == expected_resp
280# def test_insert_new_person_wrong_credits(self):
281#? response = client.post("/contacts/persons/")
282# expected_status_code = 401
283# assert response.status_code == expected_status_code
284#? expected_resp = ...
285# assert response.json() == expected_resp
288 def test_insert_new_person(self, client, db):
289 response = client.post("/contacts/persons/",
290 json= {'person':
291 {'name': 'Martin Schultz', 'email': 'm.schultz@fz-juelich.de',
292 'phone': '+49-2461-61-96870', 'orcid': '0000-0003-3455-774X', 'isprivate': True}
293 }
294 )
295 expected_status_code = 200
296 assert response.status_code == expected_status_code
297 expected_resp = {'id': 4, 'name': 'Martin Schultz', 'email': 'm.schultz@fz-juelich.de',
298 'phone': '+49-2461-61-96870', 'orcid': '0000-0003-3455-774X', 'isprivate': True}
299 assert response.json() == expected_resp
302 def test_insert_duplicate_person(self, client, db):
303 response = client.post("/contacts/persons/",
304 json= {'person':
305 {'name': 'Sabine Schröder', 'email': 's.schroeder@fz-juelich.de',
306 'phone': '+49-2461-61-6397', 'orcid': '0000-0002-0309-8010', 'isprivate': True}
307 }
308 )
309 expected_status_code = 400
310 assert response.status_code == expected_status_code
311 expected_resp = {'detail': 'Person already registered.'}
312 assert response.json() == expected_resp
315## also test requiring all contacts (a contact is either an organisation or a person)
316 def test_get_contacts(self, client, db):
317 response = client.get("/contacts/")
318 expected_status_code = 200
319 assert response.status_code == expected_status_code
320 expected_resp = [{'id': 3,
321 'person': {'id': 3, 'name': 'Sabine Schröder', 'email': 's.schroeder@fz-juelich.de',
322 'phone': '+49-2461-61-6397', 'orcid': '0000-0002-0309-8010', 'isprivate': False},
323 'organisation': None},
324 {'id': 4,
325 'person': None,
326 'organisation': {'id': 1, 'name': 'UBA', 'longname': 'Umweltbundesamt', 'kind': 'government',
327 'city': 'Dessau-Roßlau', 'postcode': '06844', 'street_address': 'Wörlitzer Platz 1',
328 'country': 'Germany', 'homepage': 'https://www.umweltbundesamt.de',
329 'contact_url': 'mailto:immission@uba.de'}},
330 {'id': 5,
331 'person': None,
332 'organisation': {'id': 2, 'name': 'FZJ', 'longname': 'Forschungszentrum Jülich', 'kind': 'research',
333 'city': 'Jülich', 'postcode': '52425', 'street_address': 'Wilhelm-Johnen-Straße',
334 'country': 'Germany', 'homepage': 'https://www.fz-juelich.de',
335 'contact_url': 'mailto:toar-data@fz-juelich.de'}}]
336 assert response.json() == expected_resp
339 def test_get_all_contacts(self, client, db):
340 response = client.get("/contacts/?limit=None")
341 expected_status_code = 200
342 assert response.status_code == expected_status_code
343 expected_resp = [{'id': 3,
344 'person': {'id': 3, 'name': 'Sabine Schröder', 'email': 's.schroeder@fz-juelich.de',
345 'phone': '+49-2461-61-6397', 'orcid': '0000-0002-0309-8010', 'isprivate': False},
346 'organisation': None},
347 {'id': 4,
348 'person': None,
349 'organisation': {'id': 1, 'name': 'UBA', 'longname': 'Umweltbundesamt', 'kind': 'government',
350 'city': 'Dessau-Roßlau', 'postcode': '06844', 'street_address': 'Wörlitzer Platz 1',
351 'country': 'Germany', 'homepage': 'https://www.umweltbundesamt.de',
352 'contact_url': 'mailto:immission@uba.de'}},
353 {'id': 5,
354 'person': None,
355 'organisation': {'id': 2, 'name': 'FZJ', 'longname': 'Forschungszentrum Jülich', 'kind': 'research',
356 'city': 'Jülich', 'postcode': '52425', 'street_address': 'Wilhelm-Johnen-Straße',
357 'country': 'Germany', 'homepage': 'https://www.fz-juelich.de',
358 'contact_url': 'mailto:toar-data@fz-juelich.de'}}]
359 assert response.json() == expected_resp
362 def test_get_person_not_found(self, client, db):
363 response = client.get("/contacts/persons/Wrong Name")
364 expected_status_code = 404
365 assert response.status_code == expected_status_code
366 expected_resp = {"detail": "Person not found."}
367 assert response.json() == expected_resp
370 def test_get_contact_by_person_name(self, client, db):
371 response = client.get("/contacts/person_name/Sabine Schröder")
372 expected_status_code = 200
373 assert response.status_code == expected_status_code
374 expected_resp = {'id': 3,
375 'person': {'id': 3, 'name':
376 'Sabine Schröder',
377 'email': 's.schroeder@fz-juelich.de',
378 'phone': '+49-2461-61-6397',
379 'orcid': '0000-0002-0309-8010',
380 'isprivate': False
381 },
382 'organisation': None}
383 assert response.json() == expected_resp
386 def test_get_contact_by_person_email(self, client, db):
387 response = client.get("/contacts/person_email/s.schroeder@fz-juelich.de")
388 expected_status_code = 200
389 assert response.status_code == expected_status_code
390 expected_resp = {'id': 3,
391 'person': {'id': 3, 'name':
392 'Sabine Schröder',
393 'email': 's.schroeder@fz-juelich.de',
394 'phone': '+49-2461-61-6397',
395 'orcid': '0000-0002-0309-8010',
396 'isprivate': False
397 },
398 'organisation': None}
399 assert response.json() == expected_resp
402 def test_get_organisation_not_found(self, client, db):
403 response = client.get("/contacts/organisations/Wrong Organisation")
404 expected_status_code = 404
405 assert response.status_code == expected_status_code
406 expected_resp = {"detail": "Organisation not found."}
407 assert response.json() == expected_resp
410 def test_get_contact_by_id(self, client, db):
411 response = client.get("/contacts/id/3")
412 expected_status_code = 200
413 assert response.status_code == expected_status_code
414 expected_resp = {
415 'id': 3,
416 'name': 'Sabine Schröder',
417 'email': 's.schroeder@fz-juelich.de',
418 'phone': '+49-2461-61-6397',
419 'orcid': '0000-0002-0309-8010',
420 'isprivate': False
421 }
422 assert response.json() == expected_resp
425 def test_get_all_contacts_by_orga_name(self, client, db):
426 response = client.get("/contacts/orga_name/Umweltbundesamt")
427 expected_status_code = 200
428 assert response.status_code == expected_status_code
429 expected_resp = {
430 'id': 4,
431 'person': None,
432 'organisation':
433 {'id': 1,
434 'name': 'UBA',
435 'longname': 'Umweltbundesamt',
436 'kind': 'government',
437 'city': 'Dessau-Roßlau',
438 'postcode': '06844',
439 'street_address': 'Wörlitzer Platz 1',
440 'country': 'Germany',
441 'homepage': 'https://www.umweltbundesamt.de',
442 'contact_url': 'mailto:immission@uba.de'
443 }
444 }
445 assert response.json() == expected_resp
448 def test_get_contact_not_found(self, client, db):
449 response = client.get("/contacts/orga_name/Luftbundesamt")
450 expected_status_code = 404
451 assert response.status_code == expected_status_code
452 expected_resp = {'detail': 'Contact not found.'}
453 assert response.json() == expected_resp