Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Floris272 committed Oct 9, 2024
1 parent 5beac4f commit 00ec85c
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 216 deletions.
32 changes: 12 additions & 20 deletions src/open_producten/locations/tests/api/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def setUp(self):
}
self.path = "/api/v1/contacts/"

def _create_contact(self):
return ContactFactory.create()
self.contact = ContactFactory.create()

def test_read_contact_without_credentials_returns_error(self):
response = APIClient().get(self.path)
Expand All @@ -42,46 +41,39 @@ def test_create_contact(self):
response = self.post(self.data)

self.assertEqual(response.status_code, 201)
self.assertEqual(Contact.objects.count(), 1)
self.assertEqual(Contact.objects.count(), 2)

def test_update_contact(self):
contact = self._create_contact()

data = self.data | {"name": "updated"}
response = self.put(contact.id, data)
data = self.data | {"first_name": "updated"}
response = self.put(self.contact.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(Contact.objects.count(), 1)
self.assertEqual(Contact.objects.first().first_name, "updated")

def test_partial_update_contact(self):
contact = self._create_contact()

data = {"name": "updated"}
response = self.patch(contact.id, data)
data = {"first_name": "updated"}
response = self.patch(self.contact.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(Contact.objects.count(), 1)
self.assertEqual(Contact.objects.first().first_name, "updated")

def test_read_contacts(self):
contact = self._create_contact()

response = self.get()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 1)
self.assertEqual(response.data["results"], [contact_to_dict(contact)])
self.assertEqual(response.data["results"], [contact_to_dict(self.contact)])

def test_read_contact(self):
contact = self._create_contact()

response = self.get(contact.id)
response = self.get(self.contact.id)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, contact_to_dict(contact))
self.assertEqual(response.data, contact_to_dict(self.contact))

def test_delete_contact(self):
contact = self._create_contact()
response = self.delete(contact.id)
response = self.delete(self.contact.id)

self.assertEqual(response.status_code, 204)
self.assertEqual(Contact.objects.count(), 0)
28 changes: 10 additions & 18 deletions src/open_producten/locations/tests/api/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def setUp(self):
self.data = {"name": "locatie", "postcode": "1111 AA", "city": "Amsterdam"}
self.path = "/api/v1/locations/"

def _create_location(self):
return LocationFactory.create()
self.location = LocationFactory.create()

def test_read_location_without_credentials_returns_error(self):
response = APIClient().get(self.path)
Expand All @@ -29,46 +28,39 @@ def test_create_location(self):
response = self.post(self.data)

self.assertEqual(response.status_code, 201)
self.assertEqual(Location.objects.count(), 1)
self.assertEqual(Location.objects.count(), 2)

def test_update_location(self):
location = self._create_location()

data = self.data | {"name": "updated"}
response = self.put(location.id, data)
response = self.put(self.location.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(Location.objects.count(), 1)
self.assertEqual(Location.objects.first().name, "updated")

def test_partial_update_location(self):
location = self._create_location()

data = {"name": "updated"}
response = self.patch(location.id, data)
response = self.patch(self.location.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(Location.objects.count(), 1)
self.assertEqual(Location.objects.first().name, "updated")

def test_read_locations(self):
location = self._create_location()

response = self.get()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 1)
self.assertEqual(response.data["results"], [location_to_dict(location)])
self.assertEqual(response.data["results"], [location_to_dict(self.location)])

def test_read_location(self):
location = self._create_location()

response = self.get(location.id)
response = self.get(self.location.id)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, location_to_dict(location))
self.assertEqual(response.data, location_to_dict(self.location))

def test_delete_location(self):
location = self._create_location()
response = self.delete(location.id)
response = self.delete(self.location.id)

self.assertEqual(response.status_code, 204)
self.assertEqual(Location.objects.count(), 0)
28 changes: 10 additions & 18 deletions src/open_producten/locations/tests/api/neighbourhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def setUp(self):
self.data = {"name": "buurt"}
self.path = "/api/v1/neighbourhoods/"

def _create_neighbourhood(self):
return NeighbourhoodFactory.create()
self.neighbourhood = NeighbourhoodFactory.create()

def test_read_neighbourhood_without_credentials_returns_error(self):
response = APIClient().get(self.path)
Expand All @@ -29,48 +28,41 @@ def test_create_neighbourhood(self):
response = self.post(self.data)

self.assertEqual(response.status_code, 201)
self.assertEqual(Neighbourhood.objects.count(), 1)
self.assertEqual(Neighbourhood.objects.count(), 2)

def test_update_neighbourhood(self):
neighbourhood = self._create_neighbourhood()

data = self.data | {"name": "updated"}
response = self.put(neighbourhood.id, data)
response = self.put(self.neighbourhood.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(Neighbourhood.objects.count(), 1)
self.assertEqual(Neighbourhood.objects.first().name, "updated")

def test_partial_update_neighbourhood(self):
neighbourhood = self._create_neighbourhood()

data = {"name": "updated"}
response = self.patch(neighbourhood.id, data)
response = self.patch(self.neighbourhood.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(Neighbourhood.objects.count(), 1)
self.assertEqual(Neighbourhood.objects.first().name, "updated")

def test_read_neighbourhoods(self):
neighbourhood = self._create_neighbourhood()

response = self.get()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 1)
self.assertEqual(
response.data["results"], [neighbourhood_to_dict(neighbourhood)]
response.data["results"], [neighbourhood_to_dict(self.neighbourhood)]
)

def test_read_neighbourhood(self):
neighbourhood = self._create_neighbourhood()

response = self.get(neighbourhood.id)
response = self.get(self.neighbourhood.id)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, neighbourhood_to_dict(neighbourhood))
self.assertEqual(response.data, neighbourhood_to_dict(self.neighbourhood))

def test_delete_neighbourhood(self):
neighbourhood = self._create_neighbourhood()
response = self.delete(neighbourhood.id)
response = self.delete(self.neighbourhood.id)

self.assertEqual(response.status_code, 204)
self.assertEqual(Neighbourhood.objects.count(), 0)
30 changes: 12 additions & 18 deletions src/open_producten/locations/tests/api/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def setUp(self):
}
self.path = "/api/v1/organisations/"

def _create_organisation(self):
return OrganisationFactory.create()
self.organisation = OrganisationFactory.create()

def test_read_organisation_without_credentials_returns_error(self):
response = APIClient().get(self.path)
Expand All @@ -46,46 +45,41 @@ def test_create_organisation(self):
response = self.post(self.data)

self.assertEqual(response.status_code, 201)
self.assertEqual(Organisation.objects.count(), 1)
self.assertEqual(Organisation.objects.count(), 2)

def test_update_organisation(self):
organisation = self._create_organisation()

data = self.data | {"name": "updated"}
response = self.put(organisation.id, data)
response = self.put(self.organisation.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(Organisation.objects.count(), 1)
self.assertEqual(Organisation.objects.first().name, "updated")

def test_partial_update_organisation(self):
organisation = self._create_organisation()

data = {"name": "updated"}
response = self.patch(organisation.id, data)
response = self.patch(self.organisation.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(Organisation.objects.count(), 1)
self.assertEqual(Organisation.objects.first().name, "updated")

def test_read_organisations(self):
organisation = self._create_organisation()

response = self.get()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 1)
self.assertEqual(response.data["results"], [organisation_to_dict(organisation)])
self.assertEqual(
response.data["results"], [organisation_to_dict(self.organisation)]
)

def test_read_organisation(self):
organisation = self._create_organisation()

response = self.get(organisation.id)
response = self.get(self.organisation.id)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, organisation_to_dict(organisation))
self.assertEqual(response.data, organisation_to_dict(self.organisation))

def test_delete_organisation(self):
organisation = self._create_organisation()
response = self.delete(organisation.id)
response = self.delete(self.organisation.id)

self.assertEqual(response.status_code, 204)
self.assertEqual(Organisation.objects.count(), 0)
31 changes: 13 additions & 18 deletions src/open_producten/locations/tests/api/organisation_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def setUp(self):
self.data = {"name": "type"}
self.path = "/api/v1/organisationtypes/"

def _create_organisation_type(self):
return OrganisationTypeFactory.create()
self.organisation_type = OrganisationTypeFactory.create()

def test_read_organisation_type_without_credentials_returns_error(self):
response = APIClient().get(self.path)
Expand All @@ -29,48 +28,44 @@ def test_create_organisation_type(self):
response = self.post(self.data)

self.assertEqual(response.status_code, 201)
self.assertEqual(OrganisationType.objects.count(), 1)
self.assertEqual(OrganisationType.objects.count(), 2)

def test_update_organisation_type(self):
organisation_type = self._create_organisation_type()

data = self.data | {"name": "updated"}
response = self.put(organisation_type.id, data)
response = self.put(self.organisation_type.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(OrganisationType.objects.count(), 1)
self.assertEqual(OrganisationType.objects.first().name, "updated")

def test_partial_update_organisation_type(self):
organisation_type = self._create_organisation_type()

data = {"name": "updated"}
response = self.patch(organisation_type.id, data)
response = self.patch(self.organisation_type.id, data)

self.assertEqual(response.status_code, 200)
self.assertEqual(OrganisationType.objects.count(), 1)
self.assertEqual(OrganisationType.objects.first().name, "updated")

def test_read_organisation_types(self):
organisation_type = self._create_organisation_type()

response = self.get()

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 1)
self.assertEqual(
response.data["results"], [organisation_type_to_dict(organisation_type)]
response.data["results"],
[organisation_type_to_dict(self.organisation_type)],
)

def test_read_organisation_type(self):
organisation_type = self._create_organisation_type()

response = self.get(organisation_type.id)
response = self.get(self.organisation_type.id)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, organisation_type_to_dict(organisation_type))
self.assertEqual(
response.data, organisation_type_to_dict(self.organisation_type)
)

def test_delete_organisation_type(self):
organisation_type = self._create_organisation_type()
response = self.delete(organisation_type.id)
response = self.delete(self.organisation_type.id)

self.assertEqual(response.status_code, 204)
self.assertEqual(OrganisationType.objects.count(), 0)
1 change: 1 addition & 0 deletions src/open_producten/products/tests/api/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def test_update_product(self):

self.assertEqual(response.status_code, 200)
self.assertEqual(Product.objects.count(), 1)
self.assertEqual(Product.objects.first().end_date, data["end_date"])

def test_update_product_without_bsn_or_kvk(self):
product = self._create_product()
Expand Down
2 changes: 1 addition & 1 deletion src/open_producten/producttypes/serializers/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Meta:
model = Category
exclude = ("path", "depth", "numchild")

def _handle_relations(self, instance, product_types):
def _handle_relations(self, instance, product_types: list[ProductType]):
errors = dict()
if product_types is not None:
build_array_duplicates_error_message(
Expand Down
2 changes: 1 addition & 1 deletion src/open_producten/producttypes/serializers/children.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Meta:
model = Price
exclude = ("product_type",)

def validate_options(self, options):
def validate_options(self, options: list[PriceOption]) -> list[PriceOption]:
if len(options) == 0:
raise serializers.ValidationError("At least one option is required")
return options
Expand Down
Loading

0 comments on commit 00ec85c

Please sign in to comment.