def test_o2o_merge_with_audit_trail(self): primary_object = RestaurantFactory.create(place=None, serves_hot_dogs=True, serves_pizza=False) alias_objects = RestaurantFactory.create_batch(3) related_object = set([alias_objects[0].place]) _, audit_trail = MergedModelInstance.create_with_audit_trail( primary_object, alias_objects) assert set(audit_trail) == related_object
def test_blocked_users_are_visible_only_by_restaurant_owner(owner): owner_user, client = owner regular_user = UserFactory() restaurant = RestaurantFactory(owner=owner_user) other_restaurant = RestaurantFactory() BlockedUserFactory(user=regular_user, restaurant=restaurant) BlockedUserFactory(user=regular_user, restaurant=other_restaurant) response = client.get("/blocked_users/") assert response.status_code == 200 assert len(response.data) == 1 assert response.data[0]["restaurant"] == restaurant.id
def test_dedupe_model_with_o2o_relationship_no_merge(self): primary_object = RestaurantFactory.create(place=None, serves_hot_dogs=True, serves_pizza=False) alias_object = RestaurantFactory.create(serves_hot_dogs=False, serves_pizza=True) merged_object = MergedModelInstance.create(primary_object, [alias_object], merge_field_values=False) assert not merged_object.place assert merged_object.serves_hot_dogs and not merged_object.serves_pizza
def test_merge_model_with_o2o_relationship(self): primary_object = RestaurantFactory.create(place=None, serves_hot_dogs=True, serves_pizza=False) alias_object = RestaurantFactory.create(serves_hot_dogs=False, serves_pizza=True) alias_address = alias_object.place.address alias_name = alias_object.place.name merged_object = MergedModelInstance.create(primary_object, [alias_object]) assert merged_object.place.address == alias_address assert merged_object.place.name == alias_name assert merged_object.serves_hot_dogs and not merged_object.serves_pizza
def test_unmanaged_related_fields(self): instance = RestaurantFactory() model_meta = ModelMeta(instance) for field in model_meta.related_fields: assert field.related_model._meta.managed
def test_restaurant_cannot_be_modified_by_a_regular_user(regular): user, client = regular restaurant = RestaurantFactory() response = client.put(f"/restaurants/{restaurant.id}/", {"name": "Test"}) assert response.status_code == 403
def test_restaurant_can_be_modified_by_its_owner(client): restaurant = RestaurantFactory() client.force_login(restaurant.owner) response = client.put(f"/restaurants/{restaurant.id}/", {"name": "Test"}) assert response.status_code == 200 assert response.data["name"] == "Test"
def test_merge_model_with_o2m_relationship_and_raise_unique_validation( self): primary_object, alias_object = RestaurantFactory.create_batch(2) report = EarningsReportFactory(restaurant=primary_object) EarningsReportFactory(date=report.date, restaurant=alias_object) with pytest.raises(ValidationError): MergedModelInstance.create(primary_object, [alias_object], raise_validation_exception=True)
def test_restaurant_is_not_visible_for_blocked_user(regular): user, client = regular restaurant = RestaurantFactory() BlockedUserFactory(user=user, restaurant=restaurant) response = client.get("/restaurants/") assert response.status_code == 200 assert len(response.data) == 0
def test_user_unblocking_by_owner(owner): owner_user, client = owner regular_user = UserFactory() restaurant = RestaurantFactory(owner=owner_user) blocked_user = BlockedUserFactory(user=regular_user, restaurant=restaurant) response = client.delete(f"/blocked_users/{blocked_user.id}/") assert response.status_code == 204 assert not BlockedUser.objects.exists()
def test_regular_user_changing_order_properties_not_allowed(regular): user, client = regular order = OrderFactory(user=user) restaurant = RestaurantFactory() response = client.patch(f"/orders/{order.id}/", {"restaurant": restaurant.id}) assert response.status_code == 400 assert response.data["detail"] == "Only order status can be modified"
def test_getting_order_list_by_restaurant_owner(owner): user, client = owner restaurant = RestaurantFactory(owner=user) for _ in range(3): OrderFactory(restaurant=restaurant) response = client.get("/orders/") assert response.status_code == 200 assert len(response.data) == 3 assert response.data[0]["user"] != user.id
def test_restaurant_list(regular): user, client = regular restaurant = RestaurantFactory() meal = MealFactory(restaurant=restaurant) response = client.get("/restaurants/") assert response.status_code == 200 assert len(response.data) == 1 assert response.data[0]["name"] == restaurant.name assert len(response.data[0]["meals"]) == 1 assert response.data[0]["meals"][0]["name"] == meal.name
def test_add_meal_by_restaurant_owner(owner): user, client = owner restaurant = RestaurantFactory(owner=user) response = client.post("/meals/", { "name": "Apple", "price": 1984, "restaurant": restaurant.id }) assert response.status_code == 201 assert response.data["name"] == "Apple"
def test_owner_user_status_changes(owner, status, next_status, is_allowed): user, client = owner restaurant = RestaurantFactory(owner=user) order = OrderFactory(status=status, restaurant=restaurant) response = client.patch(f"/orders/{order.id}/", {"status": next_status}) if is_allowed: assert response.status_code == 200 assert response.data["status"] == next_status else: assert response.status_code == 403 assert "do not have permission" in response.data["detail"]
def test_modify_meal_by_restaurant_owner(owner): user, client = owner restaurant = RestaurantFactory(owner=user) meal = MealFactory(restaurant=restaurant, price=111) response = client.put(f"/meals/{meal.id}/", { "name": "Apple", "price": 222, "restaurant": restaurant.id }) assert response.status_code == 200 assert response.data["price"] == 222
def test_merge_model_with_o2m_relationship_and_unique_validation_set_null( self): primary_object, alias_object = RestaurantFactory.create_batch(2) waiter = WaiterFactory(restaurant=primary_object) duplicate_waiter = WaiterFactory(name=waiter.name, restaurant=alias_object) merged_object = MergedModelInstance.create(primary_object, [alias_object]) waiter.refresh_from_db() assert waiter.restaurant == merged_object duplicate_waiter.refresh_from_db() assert duplicate_waiter.restaurant is None
def test_merge_model_with_o2m_relationship_and_unique_validation_delete( self): primary_object, alias_object = RestaurantFactory.create_batch(2) report = EarningsReportFactory(restaurant=primary_object) other_report = EarningsReportFactory(date=report.date, restaurant=alias_object) merged_object = MergedModelInstance.create(primary_object, [alias_object]) report.refresh_from_db() assert report.restaurant == merged_object with pytest.raises(EarningsReportFactory._meta.model.DoesNotExist): other_report.refresh_from_db()