diff --git a/src/open_producten/locations/tests/api/contact.py b/src/open_producten/locations/tests/api/contact.py index 20e08c3..930aa3e 100644 --- a/src/open_producten/locations/tests/api/contact.py +++ b/src/open_producten/locations/tests/api/contact.py @@ -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) @@ -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) diff --git a/src/open_producten/locations/tests/api/location.py b/src/open_producten/locations/tests/api/location.py index 6e7b2fd..4c42c50 100644 --- a/src/open_producten/locations/tests/api/location.py +++ b/src/open_producten/locations/tests/api/location.py @@ -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) @@ -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) diff --git a/src/open_producten/locations/tests/api/neighbourhood.py b/src/open_producten/locations/tests/api/neighbourhood.py index 3ecdb11..6d089d0 100644 --- a/src/open_producten/locations/tests/api/neighbourhood.py +++ b/src/open_producten/locations/tests/api/neighbourhood.py @@ -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) @@ -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) diff --git a/src/open_producten/locations/tests/api/organisation.py b/src/open_producten/locations/tests/api/organisation.py index 65298ce..c1edeb9 100644 --- a/src/open_producten/locations/tests/api/organisation.py +++ b/src/open_producten/locations/tests/api/organisation.py @@ -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) @@ -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) diff --git a/src/open_producten/locations/tests/api/organisation_type.py b/src/open_producten/locations/tests/api/organisation_type.py index 985b672..b95e084 100644 --- a/src/open_producten/locations/tests/api/organisation_type.py +++ b/src/open_producten/locations/tests/api/organisation_type.py @@ -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) @@ -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) diff --git a/src/open_producten/products/tests/api/test_product.py b/src/open_producten/products/tests/api/test_product.py index 7369f40..338151b 100644 --- a/src/open_producten/products/tests/api/test_product.py +++ b/src/open_producten/products/tests/api/test_product.py @@ -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() diff --git a/src/open_producten/producttypes/serializers/category.py b/src/open_producten/producttypes/serializers/category.py index 05b59c1..c075218 100644 --- a/src/open_producten/producttypes/serializers/category.py +++ b/src/open_producten/producttypes/serializers/category.py @@ -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( diff --git a/src/open_producten/producttypes/serializers/children.py b/src/open_producten/producttypes/serializers/children.py index a1ec37c..217773f 100644 --- a/src/open_producten/producttypes/serializers/children.py +++ b/src/open_producten/producttypes/serializers/children.py @@ -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 diff --git a/src/open_producten/producttypes/serializers/producttype.py b/src/open_producten/producttypes/serializers/producttype.py index d65bec0..1480689 100644 --- a/src/open_producten/producttypes/serializers/producttype.py +++ b/src/open_producten/producttypes/serializers/producttype.py @@ -100,22 +100,23 @@ class Meta: model = ProductType fields = "__all__" - def validate_category_ids(self, category_ids): - if len(category_ids) == 0: + def validate_category_ids(self, categories: list[Category]) -> list[Category]: + if len(categories) == 0: raise serializers.ValidationError("At least one category is required") - return category_ids + return categories def _handle_relations( self, + *, instance, - related_product_types, - categories, - tags, - conditions, - locations, - organisations, - contacts, - ): + related_product_types: list[ProductType], + categories: list[Category], + tags: list[Tag], + conditions: list[Condition], + locations: list[Condition], + organisations: list[Organisation], + contacts: list[Contact], + ) -> None: errors = dict() if related_product_types is not None: build_array_duplicates_error_message( @@ -159,14 +160,14 @@ def create(self, validated_data): product_type = ProductType.objects.create(**validated_data) self._handle_relations( - product_type, - related_product_types, - categories, - tags, - conditions, - locations, - organisations, - contacts, + instance=product_type, + related_product_types=related_product_types, + categories=categories, + tags=tags, + conditions=conditions, + locations=locations, + organisations=organisations, + contacts=contacts, ) product_type.save() @@ -184,14 +185,14 @@ def update(self, instance, validated_data): instance = super().update(instance, validated_data) self._handle_relations( - instance, - related_product_types, - categories, - tags, - conditions, - locations, - organisations, - contacts, + instance=instance, + related_product_types=related_product_types, + categories=categories, + tags=tags, + conditions=conditions, + locations=locations, + organisations=organisations, + contacts=contacts, ) instance.save() diff --git a/src/open_producten/producttypes/tests/api/test_category.py b/src/open_producten/producttypes/tests/api/test_category.py index 71e8434..b1b9e0d 100644 --- a/src/open_producten/producttypes/tests/api/test_category.py +++ b/src/open_producten/producttypes/tests/api/test_category.py @@ -74,7 +74,10 @@ def test_create_category_with_product_type(self): self.assertEqual(response.status_code, 201) self.assertEqual(Category.objects.count(), 1) - self.assertEqual(Category.objects.first().product_types.first(), product_type) + self.assertEqual( + list(Category.objects.values_list("product_types", flat=True)), + [product_type.id], + ) def test_create_parent_with_duplicate_product_types_returns_error(self): product_type = ProductTypeFactory.create() diff --git a/src/open_producten/producttypes/tests/api/test_category_question.py b/src/open_producten/producttypes/tests/api/test_category_question.py index 14c4b2a..a5f6102 100644 --- a/src/open_producten/producttypes/tests/api/test_category_question.py +++ b/src/open_producten/producttypes/tests/api/test_category_question.py @@ -17,12 +17,11 @@ class TestCategoryQuestion(BaseApiTestCase): def setUp(self): super().setUp() - self.category = CategoryFactory.create() + category = CategoryFactory.create() self.data = {"question": "18?", "answer": "eligible"} - self.path = f"/api/v1/categories/{self.category.id}/questions/" + self.path = f"/api/v1/categories/{category.id}/questions/" - def _create_question(self): - return QuestionFactory.create(category=self.category) + self.question = QuestionFactory.create(category=category) def test_read_question_without_credentials_returns_error(self): response = APIClient().get(self.path) @@ -32,49 +31,39 @@ def test_create_question(self): response = self.post(self.data) self.assertEqual(response.status_code, 201) - self.assertEqual(Question.objects.count(), 1) - self.assertEqual(self.category.questions.first().question, "18?") + self.assertEqual(Question.objects.count(), 2) def test_update_question(self): - question = self._create_question() - data = self.data | {"question": "21?"} - response = self.put(question.id, data) + response = self.put(self.question.id, data) self.assertEqual(response.status_code, 200) self.assertEqual(Question.objects.count(), 1) self.assertEqual(Category.objects.first().questions.first().question, "21?") def test_partial_update_question(self): - question = self._create_question() - data = {"question": "21?"} - response = self.patch(question.id, data) + response = self.patch(self.question.id, data) self.assertEqual(response.status_code, 200) self.assertEqual(Question.objects.count(), 1) self.assertEqual(Category.objects.first().questions.first().question, "21?") def test_read_questions(self): - question = self._create_question() - response = self.get() self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 1) - self.assertEqual(response.data["results"], [question_to_dict(question)]) + self.assertEqual(response.data["results"], [question_to_dict(self.question)]) def test_read_question(self): - question = self._create_question() - - response = self.get(question.id) + response = self.get(self.question.id) self.assertEqual(response.status_code, 200) - self.assertEqual(response.data, question_to_dict(question)) + self.assertEqual(response.data, question_to_dict(self.question)) def test_delete_question(self): - question = self._create_question() - response = self.delete(question.id) + response = self.delete(self.question.id) self.assertEqual(response.status_code, 204) self.assertEqual(Question.objects.count(), 0) diff --git a/src/open_producten/producttypes/tests/api/test_product_type_field.py b/src/open_producten/producttypes/tests/api/test_product_type_field.py index 603c209..6cfd4d9 100644 --- a/src/open_producten/producttypes/tests/api/test_product_type_field.py +++ b/src/open_producten/producttypes/tests/api/test_product_type_field.py @@ -19,8 +19,7 @@ def setUp(self): self.data = {"name": "test field", "description": "test", "type": "textfield"} self.path = f"/api/v1/producttypes/{self.product_type.id}/fields/" - def _create_field(self): - return FieldFactory.create(product_type=self.product_type) + self.field = FieldFactory.create(product_type=self.product_type) def test_read_field_without_credentials_returns_error(self): response = APIClient().get(self.path) @@ -30,8 +29,7 @@ def test_create_field(self): response = self.post(self.data) self.assertEqual(response.status_code, 201) - self.assertEqual(Field.objects.count(), 1) - self.assertEqual(ProductType.objects.first().fields.first().name, "test field") + self.assertEqual(Field.objects.count(), 2) def test_create_normal_field_with_choices_returns_error(self): response = self.post(self.data | {"choices": ["a", "b"]}) @@ -62,20 +60,16 @@ def test_create_choice_field_without_choices_returns_error(self): ) def test_update_field(self): - field = self._create_field() - data = self.data | {"name": "updated"} - response = self.put(field.id, data) + response = self.put(self.field.id, data) self.assertEqual(response.status_code, 200) self.assertEqual(Field.objects.count(), 1) self.assertEqual(ProductType.objects.first().fields.first().name, "updated") def test_partial_update_field(self): - field = self._create_field() - data = {"name": "updated"} - response = self.patch(field.id, data) + response = self.patch(self.field.id, data) self.assertEqual(response.status_code, 200) self.assertEqual(Field.objects.count(), 1) @@ -90,29 +84,26 @@ def test_partial_update_change_choices(self): response = self.patch(field.id, data) self.assertEqual(response.status_code, 200) - self.assertEqual(Field.objects.count(), 1) - self.assertEqual(ProductType.objects.first().fields.first().choices, ["a"]) + self.assertEqual(Field.objects.count(), 2) + self.assertEqual( + ProductType.objects.first().fields.get(id=field.id).choices, ["a"] + ) def test_read_fields(self): - field = self._create_field() - response = self.get() self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 1) - self.assertEqual(response.data["results"], [field_to_dict(field)]) + self.assertEqual(response.data["results"], [field_to_dict(self.field)]) def test_read_field(self): - field = self._create_field() - - response = self.get(field.id) + response = self.get(self.field.id) self.assertEqual(response.status_code, 200) - self.assertEqual(response.data, field_to_dict(field)) + self.assertEqual(response.data, field_to_dict(self.field)) def test_delete_field(self): - field = self._create_field() - response = self.delete(field.id) + response = self.delete(self.field.id) self.assertEqual(response.status_code, 204) self.assertEqual(Field.objects.count(), 0) diff --git a/src/open_producten/producttypes/tests/api/test_product_type_link.py b/src/open_producten/producttypes/tests/api/test_product_type_link.py index b5617d1..219e52b 100644 --- a/src/open_producten/producttypes/tests/api/test_product_type_link.py +++ b/src/open_producten/producttypes/tests/api/test_product_type_link.py @@ -15,12 +15,11 @@ class TestProductTypeLink(BaseApiTestCase): def setUp(self): super().setUp() - self.product_type = ProductTypeFactory.create() + product_type = ProductTypeFactory.create() self.data = {"name": "test link", "url": "https://www.google.com"} - self.path = f"/api/v1/producttypes/{self.product_type.id}/links/" + self.path = f"/api/v1/producttypes/{product_type.id}/links/" - def _create_link(self): - return LinkFactory.create(product_type=self.product_type) + self.link = LinkFactory.create(product_type=product_type) def test_read_link_without_credentials_returns_error(self): response = APIClient().get(self.path) @@ -30,49 +29,39 @@ def test_create_link(self): response = self.post(self.data) self.assertEqual(response.status_code, 201) - self.assertEqual(Link.objects.count(), 1) - self.assertEqual(ProductType.objects.first().links.first().name, "test link") + self.assertEqual(Link.objects.count(), 2) def test_update_link(self): - link = self._create_link() - data = self.data | {"name": "updated"} - response = self.put(link.id, data) + response = self.put(self.link.id, data) self.assertEqual(response.status_code, 200) self.assertEqual(Link.objects.count(), 1) self.assertEqual(ProductType.objects.first().links.first().name, "updated") def test_partial_update_link(self): - link = self._create_link() - data = {"name": "updated"} - response = self.patch(link.id, data) + response = self.patch(self.link.id, data) self.assertEqual(response.status_code, 200) self.assertEqual(Link.objects.count(), 1) self.assertEqual(ProductType.objects.first().links.first().name, "updated") def test_read_links(self): - link = self._create_link() - response = self.get() self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 1) - self.assertEqual(response.data["results"], [link_to_dict(link)]) + self.assertEqual(response.data["results"], [link_to_dict(self.link)]) def test_read_link(self): - link = self._create_link() - - response = self.get(link.id) + response = self.get(self.link.id) self.assertEqual(response.status_code, 200) - self.assertEqual(response.data, link_to_dict(link)) + self.assertEqual(response.data, link_to_dict(self.link)) def test_delete_link(self): - link = self._create_link() - response = self.delete(link.id) + response = self.delete(self.link.id) self.assertEqual(response.status_code, 204) self.assertEqual(Link.objects.count(), 0) diff --git a/src/open_producten/producttypes/tests/api/test_product_type_question.py b/src/open_producten/producttypes/tests/api/test_product_type_question.py index 3e9616d..9a7c66f 100644 --- a/src/open_producten/producttypes/tests/api/test_product_type_question.py +++ b/src/open_producten/producttypes/tests/api/test_product_type_question.py @@ -24,8 +24,7 @@ def setUp(self): self.data = {"question": "18?", "answer": "eligible"} self.path = f"/api/v1/producttypes/{self.product_type.id}/questions/" - def _create_question(self): - return QuestionFactory.create(product_type=self.product_type) + self.question = QuestionFactory.create(product_type=self.product_type) def test_read_question_without_credentials_returns_error(self): response = APIClient().get(self.path) @@ -35,49 +34,39 @@ def test_create_question(self): response = self.post(self.data) self.assertEqual(response.status_code, 201) - self.assertEqual(Question.objects.count(), 1) - self.assertEqual(self.product_type.questions.first().question, "18?") + self.assertEqual(Question.objects.count(), 2) def test_update_question(self): - question = self._create_question() - data = self.data | {"question": "21?"} - response = self.put(question.id, data) + response = self.put(self.question.id, data) self.assertEqual(response.status_code, 200) self.assertEqual(Question.objects.count(), 1) self.assertEqual(ProductType.objects.first().questions.first().question, "21?") def test_partial_update_question(self): - question = self._create_question() - data = {"question": "21?"} - response = self.patch(question.id, data) + response = self.patch(self.question.id, data) self.assertEqual(response.status_code, 200) self.assertEqual(Question.objects.count(), 1) self.assertEqual(ProductType.objects.first().questions.first().question, "21?") def test_read_questions(self): - question = self._create_question() - response = self.get() self.assertEqual(response.status_code, 200) self.assertEqual(response.data["count"], 1) - self.assertEqual(response.data["results"], [question_to_dict(question)]) + self.assertEqual(response.data["results"], [question_to_dict(self.question)]) def test_read_question(self): - question = self._create_question() - - response = self.get(question.id) + response = self.get(self.question.id) self.assertEqual(response.status_code, 200) - self.assertEqual(response.data, question_to_dict(question)) + self.assertEqual(response.data, question_to_dict(self.question)) def test_delete_question(self): - question = self._create_question() - response = self.delete(question.id) + response = self.delete(self.question.id) self.assertEqual(response.status_code, 204) self.assertEqual(Question.objects.count(), 0) diff --git a/src/open_producten/producttypes/tests/api/test_producttype.py b/src/open_producten/producttypes/tests/api/test_producttype.py index 837fa05..be1e828 100644 --- a/src/open_producten/producttypes/tests/api/test_producttype.py +++ b/src/open_producten/producttypes/tests/api/test_producttype.py @@ -132,10 +132,8 @@ def test_create_product_type_with_category(self): self.assertEqual(response.status_code, 201) self.assertEqual(ProductType.objects.count(), 1) self.assertEqual( - ProductType.objects.first().categories.first().name, category.name - ) - self.assertEqual( - response.data, product_type_to_dict(ProductType.objects.first()) + list(ProductType.objects.values_list("categories__name", flat=True)), + [category.name], ) def test_create_product_type_with_tag(self): @@ -146,7 +144,9 @@ def test_create_product_type_with_tag(self): self.assertEqual(response.status_code, 201) self.assertEqual(ProductType.objects.count(), 1) - self.assertEqual(ProductType.objects.first().tags.first().name, tag.name) + self.assertEqual( + list(ProductType.objects.values_list("tags__name", flat=True)), [tag.name] + ) def test_create_product_type_with_condition(self): condition = ConditionFactory.create() @@ -157,7 +157,8 @@ def test_create_product_type_with_condition(self): self.assertEqual(response.status_code, 201) self.assertEqual(ProductType.objects.count(), 1) self.assertEqual( - ProductType.objects.first().conditions.first().name, condition.name + list(ProductType.objects.values_list("conditions__name", flat=True)), + [condition.name], ) def test_create_product_type_with_location(self): @@ -169,7 +170,8 @@ def test_create_product_type_with_location(self): self.assertEqual(response.status_code, 201) self.assertEqual(ProductType.objects.count(), 1) self.assertEqual( - ProductType.objects.first().locations.first().name, location.name + list(ProductType.objects.values_list("locations__name", flat=True)), + [location.name], ) def test_create_product_type_with_organisation(self): @@ -181,7 +183,8 @@ def test_create_product_type_with_organisation(self): self.assertEqual(response.status_code, 201) self.assertEqual(ProductType.objects.count(), 1) self.assertEqual( - ProductType.objects.first().organisations.first().name, organisation.name + list(ProductType.objects.values_list("organisations__name", flat=True)), + [organisation.name], ) def test_create_product_type_with_contact(self): @@ -193,7 +196,8 @@ def test_create_product_type_with_contact(self): self.assertEqual(response.status_code, 201) self.assertEqual(ProductType.objects.count(), 1) self.assertEqual( - ProductType.objects.first().contacts.first().first_name, contact.first_name + list(ProductType.objects.values_list("contacts__first_name", flat=True)), + [contact.first_name], ) def test_create_product_type_with_duplicate_ids_returns_error(self): @@ -274,7 +278,10 @@ def test_update_product_type_with_category(self): self.assertEqual(response.status_code, 200) self.assertEqual(ProductType.objects.count(), 1) - self.assertEqual(product_type.categories.first().name, category.name) + self.assertEqual( + list(ProductType.objects.values_list("categories__name", flat=True)), + [category.name], + ) def test_update_product_type_with_tag(self): product_type = ProductTypeFactory.create() @@ -285,7 +292,9 @@ def test_update_product_type_with_tag(self): self.assertEqual(response.status_code, 200) self.assertEqual(ProductType.objects.count(), 1) - self.assertEqual(product_type.tags.first().name, tag.name) + self.assertEqual( + list(ProductType.objects.values_list("tags__name", flat=True)), [tag.name] + ) def test_update_product_type_with_condition(self): product_type = ProductTypeFactory.create() @@ -296,7 +305,52 @@ def test_update_product_type_with_condition(self): self.assertEqual(response.status_code, 200) self.assertEqual(ProductType.objects.count(), 1) - self.assertEqual(product_type.conditions.first().name, condition.name) + self.assertEqual( + list(ProductType.objects.values_list("conditions__name", flat=True)), + [condition.name], + ) + + def test_update_product_type_with_location(self): + product_type = ProductTypeFactory.create() + location = LocationFactory.create() + + data = self.data | {"location_ids": [location.id]} + response = self.put(product_type.id, data) + + self.assertEqual(response.status_code, 200) + self.assertEqual(ProductType.objects.count(), 1) + self.assertEqual( + list(ProductType.objects.values_list("locations__name", flat=True)), + [location.name], + ) + + def test_update_product_type_with_organisation(self): + product_type = ProductTypeFactory.create() + organisation = OrganisationFactory.create() + + data = self.data | {"organisation_ids": [organisation.id]} + response = self.put(product_type.id, data) + + self.assertEqual(response.status_code, 200) + self.assertEqual(ProductType.objects.count(), 1) + self.assertEqual( + list(ProductType.objects.values_list("organisations__name", flat=True)), + [organisation.name], + ) + + def test_update_product_type_with_contact(self): + product_type = ProductTypeFactory.create() + contact = ContactFactory.create() + + data = self.data | {"contact_ids": [contact.id]} + response = self.put(product_type.id, data) + + self.assertEqual(response.status_code, 200) + self.assertEqual(ProductType.objects.count(), 1) + self.assertEqual( + list(ProductType.objects.values_list("contacts__first_name", flat=True)), + [contact.first_name], + ) def test_update_product_type_with_duplicate_ids_returns_error(self): product_type = ProductTypeFactory.create()