def test_by_primary_entity_returns_related_models(self):
        add_related(self.cat, self.dog)

        entities = RelatedEntity.objects.by_primary(self.cat)

        self.assertEqual(len(entities), 1)
        self.assertEqual(entities.first().primary_object, self.cat)
        self.assertEqual(entities.first().related_object, self.dog)
    def test_delete_removes_both_sides_of_relationship(self):
        cat = Cat.objects.create(name='Test Cat')
        dog = Dog.objects.create(name='Test Dog')

        related = add_related(cat, dog)
        rev_related = add_related(dog, cat)

        related.delete()

        self.assertFalse(RelatedEntity.objects.filter(pk=related.pk).exists())
        self.assertFalse(RelatedEntity.objects.filter(pk=rev_related.pk).exists())
    def test_is_related_to_works_with_reverse_relationship(self):
        cat = Cat.objects.create(name='Test Cat')
        dog = Dog.objects.create(name='Test Dog')

        related = add_related(cat, dog)

        self.assertTrue(dog.is_related_to(cat))
    def test_is_related_to(self):
        cat = Cat.objects.create(name='Test Cat')
        dog = Dog.objects.create(name='Test Dog')

        related = add_related(cat, dog)

        self.assertTrue(cat.is_related_to(dog))
    def test_add_related_returns_instance_of_related_entity(self):
        related_entity = add_related(self.cat, self.dog)

        self.assertIsInstance(related_entity, RelatedEntity)
    def test_add_related_adds_one_record(self):
        related_entity = add_related(self.cat, self.dog)

        self.assertEqual(RelatedEntity.objects.count(), 1)