|
5 | 5 | import time_machine |
6 | 6 | from pytest_django.asserts import assertQuerySetEqual |
7 | 7 |
|
| 8 | +from manage_breast_screening.auth.models import Role |
8 | 9 | from manage_breast_screening.clinics import models |
9 | 10 |
|
10 | 11 | from .factories import ( |
@@ -38,37 +39,72 @@ def test_status_filtering(): |
38 | 39 | assertQuerySetEqual(models.Clinic.objects.completed(), {past}, ordered=False) |
39 | 40 |
|
40 | 41 |
|
41 | | -@pytest.mark.django_db |
42 | | -def test_user_assignment_creation(): |
43 | | - assignment = UserAssignmentFactory() |
44 | | - assert assignment.user is not None |
45 | | - assert assignment.provider is not None |
46 | | - assert assignment.pk is not None |
47 | | - |
48 | | - |
49 | | -@pytest.mark.django_db |
50 | | -def test_user_assignment_str(): |
51 | | - user = UserFactory(first_name="John", last_name="Doe") |
52 | | - provider = ProviderFactory(name="Test Provider") |
53 | | - assignment = UserAssignmentFactory(user=user, provider=provider) |
54 | | - assert str(assignment) == "John Doe → Test Provider" |
55 | | - |
| 42 | +class TestUserAssignment: |
| 43 | + def test_str(self): |
| 44 | + user = UserFactory.build(first_name="John", last_name="Doe") |
| 45 | + provider = ProviderFactory.build(name="Test Provider") |
| 46 | + assignment = UserAssignmentFactory.build(user=user, provider=provider) |
| 47 | + assert str(assignment) == "John Doe → Test Provider" |
56 | 48 |
|
57 | | -@pytest.mark.django_db |
58 | | -def test_user_assignment_unique_constraint(): |
59 | | - user = UserFactory() |
60 | | - provider = ProviderFactory() |
61 | | - UserAssignmentFactory(user=user, provider=provider) |
62 | | - |
63 | | - with pytest.raises(Exception): |
| 49 | + @pytest.mark.django_db |
| 50 | + def test_unique_constraint(self): |
| 51 | + user = UserFactory() |
| 52 | + provider = ProviderFactory() |
64 | 53 | UserAssignmentFactory(user=user, provider=provider) |
65 | 54 |
|
66 | | - |
67 | | -@pytest.mark.django_db |
68 | | -def test_user_assignment_related_names(): |
69 | | - user = UserFactory() |
70 | | - provider = ProviderFactory() |
71 | | - assignment = UserAssignmentFactory(user=user, provider=provider) |
72 | | - |
73 | | - assert assignment in user.assignments.all() |
74 | | - assert assignment in provider.assignments.all() |
| 55 | + with pytest.raises(Exception): |
| 56 | + UserAssignmentFactory(user=user, provider=provider) |
| 57 | + |
| 58 | + @pytest.mark.django_db |
| 59 | + def test_related_names(self): |
| 60 | + user = UserFactory() |
| 61 | + provider = ProviderFactory() |
| 62 | + assignment = UserAssignmentFactory(user=user, provider=provider) |
| 63 | + |
| 64 | + assert assignment in user.assignments.all() |
| 65 | + assert assignment in provider.assignments.all() |
| 66 | + |
| 67 | + @pytest.mark.django_db |
| 68 | + def test_roles_ordering(self): |
| 69 | + """Test that roles are sorted alphabetically for consistent ordering.""" |
| 70 | + user = UserFactory() |
| 71 | + provider = ProviderFactory() |
| 72 | + |
| 73 | + # Create assignment with roles in reverse alphabetical order |
| 74 | + assignment = UserAssignmentFactory( |
| 75 | + user=user, |
| 76 | + provider=provider, |
| 77 | + roles=[Role.CLINICAL.value, Role.ADMINISTRATIVE.value], |
| 78 | + ) |
| 79 | + |
| 80 | + assert assignment.roles == [Role.ADMINISTRATIVE.value, Role.CLINICAL.value] |
| 81 | + |
| 82 | + @pytest.mark.django_db |
| 83 | + def test_roles_single_role(self): |
| 84 | + """Test that single role works correctly.""" |
| 85 | + user = UserFactory() |
| 86 | + provider = ProviderFactory() |
| 87 | + |
| 88 | + assignment = UserAssignmentFactory( |
| 89 | + user=user, provider=provider, roles=[Role.CLINICAL.value] |
| 90 | + ) |
| 91 | + |
| 92 | + assert assignment.roles == [Role.CLINICAL.value] |
| 93 | + |
| 94 | + @pytest.mark.django_db |
| 95 | + def test_roles_duplicate_values(self): |
| 96 | + """Test that duplicate roles are deduplicated and sorted on save.""" |
| 97 | + user = UserFactory() |
| 98 | + provider = ProviderFactory() |
| 99 | + |
| 100 | + assignment = UserAssignmentFactory( |
| 101 | + user=user, |
| 102 | + provider=provider, |
| 103 | + roles=[Role.CLINICAL.value, Role.CLINICAL.value, Role.ADMINISTRATIVE.value], |
| 104 | + ) |
| 105 | + |
| 106 | + # Should be sorted and deduplicated |
| 107 | + assert assignment.roles == [ |
| 108 | + Role.ADMINISTRATIVE.value, |
| 109 | + Role.CLINICAL.value, |
| 110 | + ] |
0 commit comments