Skip to content

Commit

Permalink
test(models): consolidate EnrollmentFlow.clean() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman committed Nov 12, 2024
1 parent e26b479 commit 7759cc7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 68 deletions.
20 changes: 0 additions & 20 deletions tests/pytest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,6 @@ def model_EnrollmentFlow_does_not_support_expiration(model_EnrollmentFlow):
return model_EnrollmentFlow


@pytest.fixture
def model_EnrollmentFlow_zero_expiration_days(model_EnrollmentFlow):
model_EnrollmentFlow.supports_expiration = True
model_EnrollmentFlow.expiration_days = 0
model_EnrollmentFlow.expiration_reenrollment_days = 14
model_EnrollmentFlow.save()

return model_EnrollmentFlow


@pytest.fixture
def model_EnrollmentFlow_zero_expiration_reenrollment_days(model_EnrollmentFlow):
model_EnrollmentFlow.supports_expiration = True
model_EnrollmentFlow.expiration_days = 14
model_EnrollmentFlow.expiration_reenrollment_days = 0
model_EnrollmentFlow.save()

return model_EnrollmentFlow


@pytest.fixture
def model_EnrollmentFlow_supports_expiration(model_EnrollmentFlow):
model_EnrollmentFlow.supports_expiration = True
Expand Down
97 changes: 49 additions & 48 deletions tests/pytest/core/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,54 +160,6 @@ def test_EnrollmentFlow_agency_card_name__claims(model_EnrollmentFlow_with_scope
assert model_EnrollmentFlow_with_scope_and_claim.agency_card_name == ""


@pytest.mark.django_db
def test_EnrollmentFlow_supports_expiration_False(model_EnrollmentFlow, model_EnrollmentFlow_does_not_support_expiration):
# test will fail if any error is raised
model_EnrollmentFlow_does_not_support_expiration.full_clean()


@pytest.mark.django_db
def test_EnrollmentFlow_zero_expiration_days(model_EnrollmentFlow_zero_expiration_days):
with pytest.raises(ValidationError) as exception_info:
model_EnrollmentFlow_zero_expiration_days.full_clean()

error_dict = exception_info.value.error_dict
assert len(error_dict["expiration_days"]) == 1
assert error_dict["expiration_days"][0].message == "When support_expiration is True, this value must be greater than 0."


@pytest.mark.django_db
def test_EnrollmentFlow_zero_expiration_reenrollment_days(model_EnrollmentFlow_zero_expiration_reenrollment_days):
with pytest.raises(ValidationError) as exception_info:
model_EnrollmentFlow_zero_expiration_reenrollment_days.full_clean()

error_dict = exception_info.value.error_dict
assert len(error_dict["expiration_reenrollment_days"]) == 1
assert (
error_dict["expiration_reenrollment_days"][0].message
== "When support_expiration is True, this value must be greater than 0."
)


@pytest.mark.django_db
def test_EnrollmentFlow_missing_reenrollment_template(model_EnrollmentFlow_supports_expiration):
model_EnrollmentFlow_supports_expiration.reenrollment_error_template = None
model_EnrollmentFlow_supports_expiration.save()

with pytest.raises(ValidationError) as exception_info:
model_EnrollmentFlow_supports_expiration.full_clean()

error_dict = exception_info.value.error_dict
assert len(error_dict["reenrollment_error_template"]) == 1
assert error_dict["reenrollment_error_template"][0].message == "Required when supports expiration is True."


@pytest.mark.django_db
def test_EnrollmentFlow_supports_expiration(model_EnrollmentFlow_supports_expiration):
# test will fail if any error is raised
model_EnrollmentFlow_supports_expiration.full_clean()


@pytest.mark.django_db
def test_EnrollmentFlow_enrollment_index_template(model_EnrollmentFlow_with_scope_and_claim):
assert model_EnrollmentFlow_with_scope_and_claim.enrollment_index_template == "enrollment/index.html"
Expand Down Expand Up @@ -420,6 +372,55 @@ def test_EnrollmentFlow_template_overrides_eligibility_api(model_EnrollmentFlow_
)


@pytest.mark.django_db
def test_EnrollmentFlow_clean_does_not_supports_expiration(model_EnrollmentFlow_does_not_support_expiration):
# test will fail if any error is raised
model_EnrollmentFlow_does_not_support_expiration.clean()


@pytest.mark.django_db
def test_EnrollmentFlow_clean_supports_expiration(model_EnrollmentFlow_supports_expiration):
# test will fail if any error is raised
model_EnrollmentFlow_supports_expiration.clean()

model_EnrollmentFlow_supports_expiration.expiration_days = 0
model_EnrollmentFlow_supports_expiration.expiration_reenrollment_days = 0
model_EnrollmentFlow_supports_expiration.reenrollment_error_template = None

with pytest.raises(ValidationError) as e:
model_EnrollmentFlow_supports_expiration.clean()

assert "expiration_days" in e.value.error_dict
assert "expiration_reenrollment_days" in e.value.error_dict
assert "reenrollment_error_template" in e.value.error_dict


@pytest.mark.django_db
def test_EnrollmentFlow_clean_templates(model_EnrollmentFlow_supports_expiration, model_TransitAgency_inactive):
model_EnrollmentFlow_supports_expiration.transit_agency = model_TransitAgency_inactive
test_template = "does/not/exist.html"
model_EnrollmentFlow_supports_expiration.selection_label_template_override = test_template
model_EnrollmentFlow_supports_expiration.eligibility_start_template_override = test_template
model_EnrollmentFlow_supports_expiration.eligibility_unverified_template_override = test_template
model_EnrollmentFlow_supports_expiration.enrollment_index_template_override = test_template
model_EnrollmentFlow_supports_expiration.enrollment_success_template_override = test_template
model_EnrollmentFlow_supports_expiration.reenrollment_error_template = test_template
# agency is inactive, OK to have bad template fields
model_EnrollmentFlow_supports_expiration.clean()

# now mark it active and expect failure on clean()
model_TransitAgency_inactive.active = True
with pytest.raises(ValidationError) as e:
model_EnrollmentFlow_supports_expiration.clean()

assert "selection_label_template" in e.value.error_dict
assert "eligibility_start_template" in e.value.error_dict
assert "eligibility_unverified_template" in e.value.error_dict
assert "enrollment_index_template" in e.value.error_dict
assert "enrollment_success_template" in e.value.error_dict
assert "reenrollment_error_template" in e.value.error_dict


@pytest.mark.django_db
def test_TransitProcessor_str(model_TransitProcessor):
assert str(model_TransitProcessor) == model_TransitProcessor.name
Expand Down

0 comments on commit 7759cc7

Please sign in to comment.