Skip to content

Commit d9d256b

Browse files
authored
Add Django model tests (#812)
2 parents e397578 + 9e8dc94 commit d9d256b

File tree

1 file changed

+211
-2
lines changed

1 file changed

+211
-2
lines changed

tests/pytest/core/test_models.py

+211-2
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,231 @@
11
import pytest
22

3+
from benefits.core.models import EligibilityType, EligibilityVerifier, TransitAgency
4+
5+
6+
@pytest.mark.django_db
7+
def test_PemData_str(model_PemData):
8+
assert str(model_PemData) == model_PemData.label
9+
10+
11+
@pytest.mark.django_db
12+
def test_EligibilityType_str(model_EligibilityType):
13+
assert str(model_EligibilityType) == model_EligibilityType.label
14+
15+
16+
@pytest.mark.django_db
17+
def test_EligibilityType_get_matching(model_EligibilityType):
18+
eligibility = EligibilityType.get(model_EligibilityType.id)
19+
20+
assert eligibility == model_EligibilityType
21+
22+
23+
@pytest.mark.django_db
24+
def test_EligibilityType_get_nonmatching():
25+
with pytest.raises(EligibilityType.DoesNotExist):
26+
EligibilityType.get(99999)
27+
28+
29+
@pytest.mark.django_db
30+
def test_EligibilityType_get_many_matching(model_EligibilityType):
31+
new_type = EligibilityType.get(model_EligibilityType.id)
32+
new_type.pk = None
33+
new_type.save()
34+
35+
result = EligibilityType.get_many([model_EligibilityType.id, new_type.id])
36+
37+
assert len(result) == 2
38+
assert model_EligibilityType in result
39+
assert new_type in result
40+
41+
42+
@pytest.mark.django_db
43+
def test_EligibilityType_get_many_nonmatching():
44+
result = EligibilityType.get_many([99998, 99999])
45+
46+
assert len(result) == 0
47+
48+
49+
@pytest.mark.django_db
50+
def test_EligibilityType_get_many_somematching(model_EligibilityType):
51+
result = EligibilityType.get_many([model_EligibilityType.id, 99999])
52+
53+
assert len(result) == 1
54+
assert model_EligibilityType in result
55+
356

457
@pytest.mark.django_db
5-
def test_EligibilityVerifier_with_auth_verification(model_EligibilityVerifier, model_AuthProvider_with_verification):
58+
def test_EligibilityVerifier_str(model_EligibilityVerifier):
59+
assert str(model_EligibilityVerifier) == model_EligibilityVerifier.name
60+
61+
62+
@pytest.mark.django_db
63+
def test_EligibilityVerifier_by_id_matching(model_EligibilityVerifier):
64+
verifier = EligibilityVerifier.by_id(model_EligibilityVerifier.id)
65+
66+
assert verifier == model_EligibilityVerifier
67+
68+
69+
@pytest.mark.django_db
70+
def test_EligibilityVerifier_by_id_nonmatching():
71+
with pytest.raises(EligibilityVerifier.DoesNotExist):
72+
EligibilityVerifier.by_id(99999)
73+
74+
75+
@pytest.mark.django_db
76+
def test_EligibilityVerifier_with_AuthProvider_with_verification(
77+
model_EligibilityVerifier, model_AuthProvider_with_verification
78+
):
679
model_EligibilityVerifier.auth_provider = model_AuthProvider_with_verification
780

81+
assert model_EligibilityVerifier.is_auth_required
882
assert model_EligibilityVerifier.uses_auth_verification
983

1084

1185
@pytest.mark.django_db
12-
def test_EligibilityVerifier_without_auth_verification(model_EligibilityVerifier, model_AuthProvider_without_verification):
86+
def test_EligibilityVerifier_with_AuthProvider_without_verification(
87+
model_EligibilityVerifier, model_AuthProvider_without_verification
88+
):
1389
model_EligibilityVerifier.auth_provider = model_AuthProvider_without_verification
1490

91+
assert model_EligibilityVerifier.is_auth_required
1592
assert not model_EligibilityVerifier.uses_auth_verification
1693

1794

1895
@pytest.mark.django_db
1996
def test_EligibilityVerifier_without_AuthProvider(model_EligibilityVerifier):
2097
model_EligibilityVerifier.auth_provider = None
2198

99+
assert not model_EligibilityVerifier.is_auth_required
22100
assert not model_EligibilityVerifier.uses_auth_verification
101+
102+
103+
@pytest.mark.django_db
104+
def test_PaymentProcessor_str(model_PaymentProcessor):
105+
assert str(model_PaymentProcessor) == model_PaymentProcessor.name
106+
107+
108+
@pytest.mark.django_db
109+
def test_TransitAgency_str(model_TransitAgency):
110+
assert str(model_TransitAgency) == model_TransitAgency.long_name
111+
112+
113+
@pytest.mark.django_db
114+
def test_TransitAgency_get_type_id_matching(model_TransitAgency):
115+
eligibility = model_TransitAgency.eligibility_types.first()
116+
result = model_TransitAgency.get_type_id(eligibility.name)
117+
118+
assert result == eligibility.id
119+
120+
121+
@pytest.mark.django_db
122+
def test_TransitAgency_get_type_id_manymatching(model_TransitAgency):
123+
eligibility = model_TransitAgency.eligibility_types.first()
124+
new_eligibility = EligibilityType.get(eligibility.id)
125+
new_eligibility.pk = None
126+
new_eligibility.save()
127+
model_TransitAgency.eligibility_types.add(new_eligibility)
128+
129+
with pytest.raises(Exception, match=r"name"):
130+
model_TransitAgency.get_type_id(eligibility.name)
131+
132+
133+
@pytest.mark.django_db
134+
def test_TransitAgency_get_type_id_nonmatching(model_TransitAgency):
135+
with pytest.raises(Exception, match=r"name"):
136+
model_TransitAgency.get_type_id("something")
137+
138+
139+
@pytest.mark.django_db
140+
def test_TransitAgency_supports_type_matching(model_TransitAgency):
141+
eligibility = model_TransitAgency.eligibility_types.first()
142+
143+
assert model_TransitAgency.supports_type(eligibility)
144+
145+
146+
@pytest.mark.django_db
147+
def test_TransitAgency_supports_type_nonmatching(model_TransitAgency):
148+
eligibility = model_TransitAgency.eligibility_types.first()
149+
new_eligibility = EligibilityType.get(eligibility.id)
150+
new_eligibility.pk = None
151+
new_eligibility.save()
152+
153+
assert not model_TransitAgency.supports_type(new_eligibility)
154+
155+
156+
@pytest.mark.django_db
157+
def test_TransitAgency_supports_type_wrongtype(model_TransitAgency):
158+
eligibility = model_TransitAgency.eligibility_types.first()
159+
160+
assert not model_TransitAgency.supports_type(eligibility.name)
161+
162+
163+
@pytest.mark.django_db
164+
def test_TransitAgency_types_to_verify(model_TransitAgency):
165+
eligibility = model_TransitAgency.eligibility_types.first()
166+
new_eligibility = EligibilityType.get(eligibility.id)
167+
new_eligibility.pk = None
168+
new_eligibility.save()
169+
170+
assert eligibility != new_eligibility
171+
172+
model_TransitAgency.eligibility_types.add(new_eligibility)
173+
assert model_TransitAgency.eligibility_types.count() == 2
174+
175+
verifier = model_TransitAgency.eligibility_verifiers.first()
176+
assert verifier.eligibility_type == eligibility
177+
178+
result = model_TransitAgency.types_to_verify(verifier)
179+
assert len(result) == 1
180+
assert eligibility in result
181+
182+
183+
@pytest.mark.django_db
184+
def test_TransitAgency_index_url(model_TransitAgency):
185+
result = model_TransitAgency.index_url
186+
187+
assert result.endswith(model_TransitAgency.slug)
188+
189+
190+
@pytest.mark.django_db
191+
def test_TransitAgency_by_id_matching(model_TransitAgency):
192+
result = TransitAgency.by_id(model_TransitAgency.id)
193+
194+
assert result == model_TransitAgency
195+
196+
197+
@pytest.mark.django_db
198+
def test_TransitAgency_by_id_nonmatching():
199+
with pytest.raises(TransitAgency.DoesNotExist):
200+
TransitAgency.by_id(99999)
201+
202+
203+
@pytest.mark.django_db
204+
def test_TransitAgency_by_slug_matching(model_TransitAgency):
205+
result = TransitAgency.by_slug(model_TransitAgency.slug)
206+
207+
assert result == model_TransitAgency
208+
209+
210+
@pytest.mark.django_db
211+
def test_TransitAgency_by_slug_nonmatching():
212+
result = TransitAgency.by_slug("nope")
213+
214+
assert not result
215+
216+
217+
@pytest.mark.django_db
218+
def test_TransitAgency_all_active(model_TransitAgency):
219+
assert TransitAgency.objects.count() == 1
220+
221+
inactive_agency = TransitAgency.by_id(model_TransitAgency.id)
222+
inactive_agency.pk = None
223+
inactive_agency.active = False
224+
inactive_agency.save()
225+
226+
assert TransitAgency.objects.count() == 2
227+
228+
result = TransitAgency.all_active()
229+
230+
assert len(result) == 1
231+
assert model_TransitAgency in result

0 commit comments

Comments
 (0)