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_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_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()