Skip to content

Commit

Permalink
Make test less fragile and improve test name
Browse files Browse the repository at this point in the history
The `test_returns_400_for_active_infractions_of_type_that_cannot_be_
active` test relied on the order in which the validation was done
since it contained incompatible combinations of arguments. The test
has been changed to make sure the data is valid except for the thing
we actually want to test.

I have also tried to improve the name of the test that tests the
`test_unique_constraint_accepts_active_infraction_after_inactive_
infraction` test. It now includes the logic of what it does, but
not the entire name of the test it's testing.
  • Loading branch information
SebastiaanZ committed Oct 19, 2019
1 parent 13083b3 commit 9989170
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pydis_site/apps/api/tests/test_infractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,18 +316,22 @@ def test_returns_400_for_non_hidden_required_hidden_type(self):
def test_returns_400_for_active_infraction_of_type_that_cannot_be_active(self):
"""Test if the API rejects active infractions for types that cannot be active."""
url = reverse('bot:infraction-list', host='api')
restricted_types = ('note', 'warning', 'kick')
restricted_types = (
('note', True),
('warning', False),
('kick', False),
)

for infraction_type in restricted_types:
for infraction_type, hidden in restricted_types:
with self.subTest(infraction_type=infraction_type):
invalid_infraction = {
'user': self.user.id,
'actor': self.user.id,
'type': infraction_type,
'reason': 'Take me on!',
'hidden': True,
'hidden': hidden,
'active': True,
'expires_at': '2019-10-04T12:52:00+00:00'
'expires_at': None,
}
response = self.client.post(url, data=invalid_infraction)
self.assertEqual(response.status_code, 400)
Expand Down Expand Up @@ -450,7 +454,7 @@ def test_unique_constraint_accepts_active_infraction_after_inactive_infraction(s
self.fail("An unexpected IntegrityError was raised.")

@patch(f"{__name__}.Infraction")
def test_the_accepts_active_infraction_after_inactive_infractions_test(self, infraction_patch):
def test_if_accepts_active_infraction_test_catches_integrity_error(self, infraction_patch):
"""Does the test properly catch the IntegrityError and raise an AssertionError?"""
infraction_patch.objects.create.side_effect = IntegrityError
with self.assertRaises(AssertionError, msg="An unexpected IntegrityError was raised."):
Expand Down

0 comments on commit 9989170

Please sign in to comment.