def test_shopping_public_list(self) -> None: client = APIClient() BaseLocationFactory(type=LocationTypeChoices.SHOPPING, is_submission=True, closed=None) BaseLocationFactory( type=LocationTypeChoices.SHOPPING, is_submission=False, closed=timezone.now(), vegan=OMNIVORE_VEGAN, ) BaseLocationFactory( type=LocationTypeChoices.SHOPPING, is_submission=False, closed=timezone.now(), vegan=VEGETARIAN_VEGAN, ) BaseLocationFactory( type=LocationTypeChoices.SHOPPING, is_submission=False, vegan=VEGAN_VEGAN, closed=timezone.now() - timedelta(weeks=21, days=1), ) response_get_list = client.get(reverse("data:api-v2-shopping-list")) self.assertEqual(response_get_list.status_code, 200) self.assertEqual(len(response_get_list.json()), 0)
def test_no_closed(self) -> None: BaseLocationFactory(type=LocationTypeChoices.GASTRO, is_submission=False, vegan=OMNIVORE_VEGAN) BaseLocationFactory(type=LocationTypeChoices.GASTRO, is_submission=False, closed=None) response = self.client.get(reverse(self.view_name)) self.assertEqual(len(response.json()), 1)
def test_only_gastro_type(self) -> None: BaseLocationFactory( type=LocationTypeChoices.SHOPPING, closed=None, is_submission=False, ) BaseLocationFactory( type=LocationTypeChoices.GASTRO, closed=None, is_submission=False, ) response = self.client.get(reverse(self.view_name)) self.assertEqual(len(response.json()), 1)
def test_no_closed_vegan(self) -> None: BaseLocationFactory( type=LocationTypeChoices.GASTRO, is_submission=False, vegan=VEGAN_VEGAN, closed=timezone.now() - timedelta(weeks=22), ) BaseLocationFactory( type=LocationTypeChoices.GASTRO, is_submission=False, vegan=VEGAN_VEGAN, closed=timezone.now() - timedelta(weeks=20, days=6, hours=23), ) response = self.client.get(reverse(self.view_name)) self.assertEqual(len(response.json()), 1)
def test_update(self) -> None: instance = BaseLocationFactory(type=LocationTypeChoices.GASTRO) location_input = baselocation_input_fixture( opening_hours_input=opening_hours_fixture(), attribute_input=gastro_attribute_input_fixture(), tags_input=gastro_tags_input_fixture(), ) serializer = PrivateGastroDetailSerializer(instance=instance, data=location_input) self.assertTrue(serializer.is_valid()) updated_instance = serializer.save() for key, value in location_input.items(): if key == "tags": self.assertEqual(updated_instance.tags.all().count(), len(value)) elif key == "attributes": self.assertEqual( updated_instance.boolean_attributes.all().count() + updated_instance.positive_integer_attributes.all().count(), len(value), ) elif key == "opening_hours": self.assertEqual( updated_instance.openinghours_set.all().count(), len(value)) else: self.assertEqual(getattr(updated_instance, key), value)
def baselocation_input_fixture(opening_hours_input=None, tags_input=None, attribute_input=None, **kwargs): location_factory = BaseLocationFactory.build(**kwargs) location_input = dict( name=location_factory.name, street=location_factory.street, postal_code=location_factory.postal_code, city=location_factory.city, vegan=location_factory.vegan, latitude=location_factory.latitude, longitude=location_factory.longitude, telephone=location_factory.telephone, website=location_factory.website, email=location_factory.email, comment=location_factory.comment, comment_english=location_factory.comment_english, comment_opening_hours=location_factory.comment_opening_hours, closed=location_factory.closed, text_intern=location_factory.text_intern, review=None, ) if attribute_input is not None: location_input["attributes"] = attribute_input if tags_input is not None: location_input["tags"] = tags_input if opening_hours_input is not None: location_input["opening_hours"] = opening_hours_input return location_input
def test_weekday_location_unique_together(self) -> None: location = BaseLocationFactory() OpeningHoursFactory(location=location, weekday=WeekdayChoices.choices[0][0]) with pytest.raises(IntegrityError): OpeningHoursFactory(location=location, weekday=WeekdayChoices.choices[0][0])
def test_shopping_viewset_update_set_last_editor(self) -> None: location = BaseLocationFactory(type=LocationTypeChoices.SHOPPING) data = baselocation_input_fixture() response = self.client.put( reverse("data:api-v2-shopping-detail", args=[location.id_string]), data=data, format="json", ) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["lastEditor"], self.user.username)
def test_to_representation_data_exists(self) -> None: location = BaseLocationFactory() expected_data = {} for day in WeekdayChoices.choices: opening_hour = OpeningHoursFactory(location=location, weekday=day[0]) expected_data[opening_hour.weekday.lower()] = dict( opening=opening_hour.opening, closing=opening_hour.closing) data_model_manager = OpeningHoursSerializer( instance=location.openinghours_set).data self.assertDictEqual(data_model_manager, expected_data) data_without_model_manager = OpeningHoursSerializer( instance=location.openinghours_set.all()).data self.assertDictEqual(data_without_model_manager, expected_data)
def test_closed_location_to_dict(self): location = BaseLocationFactory( type=LocationTypeChoices.GASTRO, vegan=VEGAN_VEGAN, ) OpeningHoursFactory( location=location, weekday=WeekdayChoices.MONDAY, ) OpeningHoursFactory( location=location, weekday=WeekdayChoices.TUESDAY, ) OpeningHoursFactory( location=location, weekday=WeekdayChoices.WEDNESDAY, ) OpeningHoursFactory( location=location, weekday=WeekdayChoices.THURSDAY, ) OpeningHoursFactory( location=location, weekday=WeekdayChoices.FRIDAY, ) OpeningHoursFactory( location=location, weekday=WeekdayChoices.SATURDAY, ) OpeningHoursFactory( location=location, weekday=WeekdayChoices.SUNDAY, ) location_dict = _build_base( location=location, data_to_field_name=GASTRO_DATA_FIELD_NAME_TO_FIELD_NAME ) self.assertEqual( f"{location.name} - GESCHLOSSEN / CLOSED", location_dict["name"] ) self.assertNotIn("telephone", location_dict) for _, legacy_key in OPENING_HOUR_FIELD_NAME.items(): self.assertEqual("", location_dict[legacy_key])
def test_shopping_view_public_no_private_fields(self) -> None: client = APIClient() location = BaseLocationFactory(type=LocationTypeChoices.SHOPPING, is_submission=False, closed=None) response_get_list = client.get(reverse("data:api-v2-shopping-list")) location_data_list = response_get_list.json()[0] private_fields = [ "text_intern", "has_sticker", "is_submission", "submit_email", "last_editor", ] for field in private_fields: self.assertTrue(field not in location_data_list) response_get_list = client.get( reverse("data:api-v2-shopping-detail", args=[location.id_string])) location_data = response_get_list.json() for field in private_fields: self.assertTrue(field not in location_data)
def test_shopping_view_anonymouse_can_access_list_detail(self) -> None: client = APIClient() response_get_list = client.get(reverse("data:api-v2-shopping-list")) self.assertEqual(response_get_list.status_code, 200) response_post = client.post(reverse("data:api-v2-shopping-list"), data={}, format="json") self.assertEqual(response_post.status_code, 403) location = BaseLocationFactory(type=LocationTypeChoices.SHOPPING, is_submission=False, closed=None) location_url = reverse("data:api-v2-shopping-detail", args=[location.id_string]) response_get_detail = client.get(location_url) self.assertEqual(response_get_detail.status_code, 200) response_put = client.put(location_url, data={}, format="json") self.assertEqual(response_put.status_code, 403) response_patch = client.patch(location_url, data={}, format="json") self.assertEqual(response_patch.status_code, 403) response_delete = client.delete(location_url) self.assertEqual(response_delete.status_code, 403)
def test_id_string_unique(self) -> None: BaseLocationFactory(id_string="abc") with pytest.raises(IntegrityError): BaseLocationFactory(id_string="abc")
def test_check_constrain_type_valid(self) -> None: with pytest.raises(IntegrityError): BaseLocationFactory(type="abc")
def test_gastro_to_dict(self): review = ReviewFactory( url= "https://www.berlin-vegan.de/essen-und-trinken/rezensionen/test-restaurant/" ) location = BaseLocationFactory( id_string="6zd67nov4gjk8wv24trgxd9sr1r5f413", type=LocationTypeChoices.GASTRO, name="Test Restaurant", street="Teststraße", postal_code="12345", city="Berlin", latitude=5.555, longitude=4.444, telephone="012345678", website="https://test.de", email="*****@*****.**", vegan=5, comment= "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed ", comment_english= "English, Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed ", closed=None, text_intern= "Intern, Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed ", comment_public_transport= "Public Transport, Lorem ipsum dolor sit amet, consetetur ", comment_opening_hours= "Open Comment, Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed", submit_email="*****@*****.**", has_sticker=True, is_submission=False, review=review, ) OpeningHours.objects.create( location=location, weekday=WeekdayChoices.MONDAY, opening="09:00:00", closing="15:00:00", ) OpeningHours.objects.create( location=location, weekday=WeekdayChoices.TUESDAY, opening="10:00:00", closing="16:00:00", ) OpeningHours.objects.create( location=location, weekday=WeekdayChoices.WEDNESDAY, opening="11:00:00", closing="17:00:00", ) OpeningHours.objects.create( location=location, weekday=WeekdayChoices.THURSDAY, opening="12:00:00", closing="18:00:00", ) OpeningHours.objects.create( location=location, weekday=WeekdayChoices.FRIDAY, opening="13:00:00", closing="19:00:00", ) OpeningHours.objects.create( location=location, weekday=WeekdayChoices.SATURDAY, opening="14:00:00", closing="20:00:00", ) OpeningHours.objects.create( location=location, weekday=WeekdayChoices.SUNDAY, opening="15:00:00", closing="21:00:00", ) tags = [ Tag.objects.create(tag="cafe"), Tag.objects.create(tag="restaurant") ] location.tags.set(tags) pos_int_attributes = [ PositiveIntegerAttribute.objects.create(name="seats_outdoor", state=40), PositiveIntegerAttribute.objects.create(name="seats_indoor", state=50), ] location.positive_integer_attributes.set(pos_int_attributes) boolean_attributes = [ BooleanAttribute.objects.create(name="handicapped_accessible", state=True), BooleanAttribute.objects.create(name="handicapped_accessible_wc", state=False), BooleanAttribute.objects.create(name="dog", state=None), BooleanAttribute.objects.create(name="child_chair", state=True), BooleanAttribute.objects.create(name="catering", state=False), BooleanAttribute.objects.create(name="delivery", state=None), BooleanAttribute.objects.create(name="organic", state=False), BooleanAttribute.objects.create(name="wlan", state=True), BooleanAttribute.objects.create(name="gluten_free", state=None), BooleanAttribute.objects.create(name="breakfast", state=True), BooleanAttribute.objects.create(name="brunch", state=False), ] location.boolean_attributes.set(boolean_attributes) api_fixture = [{ "id": "6zd67nov4gjk8wv24trgxd9sr1r5f413", "name": "Test Restaurant", "street": "Teststraße", "cityCode": "12345", "city": "Berlin", "latCoord": 5.555, "longCoord": 4.444, "vegan": 5, "district": "Berlin", "telephone": "012345678", "website": "https://test.de", "email": "*****@*****.**", "otMon": "09:00 - 15:00", "otTue": "10:00 - 16:00", "otWed": "11:00 - 17:00", "otThu": "12:00 - 18:00", "otFri": "13:00 - 19:00", "otSat": "14:00 - 20:00", "otSun": "15:00 - 21:00", "comment": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed ", "commentEnglish": "English, Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed ", "openComment": "Open Comment, Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed", "publicTransport": "Public Transport, Lorem ipsum dolor sit amet, consetetur ", "handicappedAccessible": 1, "handicappedAccessibleWc": 0, "dog": -1, "childChair": 1, "catering": 0, "delivery": -1, "organic": 0, "wlan": 1, "glutenFree": -1, "breakfast": 1, "brunch": 0, "seatsOutdoor": 40, "seatsIndoor": 50, "tags": ["Cafe", "Restaurant"], "reviewURL": "test-restaurant", "created": timezone.now().strftime("%Y-%m-%d"), }] response = self.client.get(reverse(self.view_name)) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), api_fixture)
def test_sanity(self) -> None: BaseLocationFactory()
def test_check_constrain_weekday_valid(self) -> None: location = BaseLocationFactory() with pytest.raises(IntegrityError): OpeningHoursFactory(location=location, weekday="abc")
def test_is_not_closed_filter_base_location(self) -> None: BaseLocationFactory(closed=None) BaseLocationFactory(closed=dt.now()) qs = BaseLocation.objects.all() closed_filter = BaseLocationFilter(data=dict(closed=False), queryset=qs) self.assertEqual(closed_filter.qs.count(), 1)
def test_save_id_string_always_the_same(self, get_random_mock): BaseLocationFactory() with pytest.raises(RecursionError): BaseLocationFactory()