From 6ef28f31558714773f4ab0d7e48afe539457ea2e Mon Sep 17 00:00:00 2001 From: Andreas Tolfsen <andretol@usit.uio.no> Date: Tue, 27 Jul 2021 14:58:14 +0200 Subject: [PATCH] greg.tests: convert Person tests to pytest This is a first stab at converting the tests for the Person model to pytest. It is more than likely that we will want to create shared fixtures for these in the future. Fixes: GREG-14 --- greg/tests/models/__init__.py | 0 greg/tests/models/test_person.py | 76 ++++++++++++++++++++++++++++++++ greg/tests/test_models.py | 74 ------------------------------- 3 files changed, 76 insertions(+), 74 deletions(-) create mode 100644 greg/tests/models/__init__.py create mode 100644 greg/tests/models/test_person.py diff --git a/greg/tests/models/__init__.py b/greg/tests/models/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/greg/tests/models/test_person.py b/greg/tests/models/test_person.py new file mode 100644 index 00000000..fb851f06 --- /dev/null +++ b/greg/tests/models/test_person.py @@ -0,0 +1,76 @@ +from functools import partial + +import pytest + +from django.db.models import ProtectedError + +from greg.models import ( + OrganizationalUnit, + Person, + PersonRole, + Role, + Sponsor, +) + + +person_role_with = partial( + PersonRole.objects.create, + start_date="2020-03-05", + end_date="2020-06-10", + contact_person_unit="Contact Person", + available_in_search=True, +) + + +@pytest.fixture +def role_foo() -> Role: + return Role.objects.create(type="role_foo", name_en="Role Foo") + + +@pytest.fixture +def role_bar() -> Role: + return Role.objects.create(type="role_bar", name_en="Role Bar") + + +@pytest.fixture +def person(role_foo, role_bar) -> Person: + person = Person.objects.create( + first_name="Test", + last_name="Tester", + date_of_birth="2000-01-27", + email="test@example.org", + mobile_phone="123456789", + ) + + ou = OrganizationalUnit.objects.create(orgreg_id="12345", name_en="Test unit") + person_role_with( + person=person, + role=role_foo, + unit=ou, + registered_by=Sponsor.objects.create(feide_id="foosponsor@uio.no"), + ) + person_role_with( + person=person, + role=role_bar, + unit=ou, + registered_by=Sponsor.objects.create(feide_id="barsponsor@uio.no"), + ) + + return person + + +@pytest.mark.django_db +def test_add_multiple_roles_to_person(person, role_foo, role_bar): + person_roles = person.roles.all() + assert len(person_roles) == 2 + assert role_foo in person_roles + assert role_bar in person_roles + + +@pytest.mark.django_db +def test_delete_person_with_roles(person): + # it is not clear what cleanup needs to be done when removing a person, + # so for now it is prohibited to delete a person with role relationships + # attached in other tables + with pytest.raises(ProtectedError): + person.delete() diff --git a/greg/tests/test_models.py b/greg/tests/test_models.py index 0c587924..26fdbbe1 100644 --- a/greg/tests/test_models.py +++ b/greg/tests/test_models.py @@ -1,10 +1,7 @@ -from django.db.models import ProtectedError from django.test import TestCase from greg.models import ( Person, - Role, - PersonRole, OrganizationalUnit, Sponsor, SponsorOrganizationalUnit, @@ -12,77 +9,6 @@ from greg.models import ( ) -class PersonModelTests(TestCase): - test_person = dict( - first_name="Test", - last_name="Tester", - date_of_birth="2000-01-27", - email="test@example.org", - mobile_phone="123456789", - ) - - def test_add_multiple_roles_to_person(self): - role1 = Role.objects.create(type="role1", name_en="Role 1") - role2 = Role.objects.create(type="role2", name_en="Role 2") - - unit = OrganizationalUnit.objects.create(orgreg_id="12345", name_en="Test unit") - sponsor = Sponsor.objects.create(feide_id="test@uio.no") - sponsor2 = Sponsor.objects.create(feide_id="test2@uio.no") - - person = Person.objects.create(**self.test_person) - - # Add two roles to the person and check that they appear when listing the roles for the person - PersonRole.objects.create( - person=person, - role=role1, - unit=unit, - start_date="2020-03-05", - end_date="2020-06-10", - contact_person_unit="Contact Person", - available_in_search=True, - registered_by=sponsor, - ) - - PersonRole.objects.create( - person=person, - role=role2, - unit=unit, - start_date="2021-03-05", - end_date="2021-06-10", - contact_person_unit="Contact Person", - available_in_search=True, - registered_by=sponsor2, - ) - - person_roles = person.roles.all() - - self.assertEqual(2, len(person_roles)) - self.assertIn(role1, person_roles) - self.assertIn(role2, person_roles) - - def test_person_not_allowed_deleted_when_roles_are_present(self): - person = Person.objects.create(**self.test_person) - role = Role.objects.create(type="role1", name_en="Role 1") - unit = OrganizationalUnit.objects.create(orgreg_id="12345", name_en="Test unit") - sponsor = Sponsor.objects.create(feide_id="test@uio.no") - - PersonRole.objects.create( - person=person, - role=role, - unit=unit, - start_date="2020-03-05", - end_date="2020-06-10", - contact_person_unit="Contact Person", - available_in_search=True, - registered_by=sponsor, - ) - - # It is not clear what cleanup needs to be done when a person is going to be - # removed, so for now is prohibited to delete a person if there is data - # attached to him in other tables - self.assertRaises(ProtectedError, person.delete) - - class SponsorModelTests(TestCase): def test_add_sponsor_to_multiple_units(self): sponsor = Sponsor.objects.create(feide_id="test@uio.no") -- GitLab