Skip to content

Commit

Permalink
Tests for #59 - a manually specified OneToOne should behave
Browse files Browse the repository at this point in the history
the same as an implicit one.
  • Loading branch information
kezabelle committed Oct 26, 2013
1 parent 06d3b7a commit ae71f90
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
9 changes: 9 additions & 0 deletions model_utils/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,29 @@ class InheritanceManagerTestChild1(InheritanceManagerTestParent):
objects = InheritanceManager()



class InheritanceManagerTestGrandChild1(InheritanceManagerTestChild1):
text_field = models.TextField()



class InheritanceManagerTestGrandChild1_2(InheritanceManagerTestChild1):
text_field = models.TextField()



class InheritanceManagerTestChild2(InheritanceManagerTestParent):
non_related_field_using_descriptor_2 = models.FileField(upload_to="test")
normal_field_2 = models.TextField()



class InheritanceManagerTestChild3(InheritanceManagerTestParent):
parent_ptr = models.OneToOneField(
InheritanceManagerTestParent, related_name='manual_onetoone',
parent_link=True)


class TimeStamp(TimeStampedModel):
pass

Expand Down
75 changes: 74 additions & 1 deletion model_utils/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
TimeFrameManagerAdded, Dude, SplitFieldAbstractParent, Car, Spot,
ModelTracked, ModelTrackedFK, ModelTrackedNotDefault, ModelTrackedMultiple, InheritedModelTracked,
Tracked, TrackedFK, TrackedNotDefault, TrackedNonFieldAttr, TrackedMultiple,
InheritedTracked, StatusFieldDefaultFilled, StatusFieldDefaultNotFilled)
InheritedTracked, StatusFieldDefaultFilled, StatusFieldDefaultNotFilled,
InheritanceManagerTestChild3)


class GetExcerptTests(TestCase):
Expand Down Expand Up @@ -683,6 +684,55 @@ def test_version_determining_only_child_depth(self):
self.assertEqual(1, self.get_manager().all()._get_maximum_depth())


@skipUnless(django.VERSION < (1, 6, 0), "test only applies to Django < 1.6")
def test_manually_specifying_parent_fk_only_children(self):
"""
given a Model which inherits from another Model, but also declares
the OneToOne link manually using `related_name` and `parent_link`,
ensure that the relation names and subclasses are obtained correctly.
"""
child3 = InheritanceManagerTestChild3.objects.create()
results = InheritanceManagerTestParent.objects.all().select_subclasses()

expected_objs = [self.child1, self.child2,
InheritanceManagerTestChild1(pk=3),
InheritanceManagerTestChild1(pk=4), child3]
self.assertEqual(list(results), expected_objs)

expected_related_names = [
'inheritancemanagertestchild1',
'inheritancemanagertestchild2',
'manual_onetoone', # this was set via parent_link & related_name
]
self.assertEqual(set(results.subclasses),
set(expected_related_names))

@skipUnless(django.VERSION >= (1, 6, 0), "test only applies to Django 1.6+")
def test_manually_specifying_parent_fk_including_grandchildren(self):
"""
given a Model which inherits from another Model, but also declares
the OneToOne link manually using `related_name` and `parent_link`,
ensure that the relation names and subclasses are obtained correctly.
"""
child3 = InheritanceManagerTestChild3.objects.create()
results = InheritanceManagerTestParent.objects.all().select_subclasses()

expected_objs = [self.child1, self.child2, self.grandchild1,
self.grandchild1_2, child3]
self.assertEqual(list(results), expected_objs)

expected_related_names = [
'inheritancemanagertestchild1__inheritancemanagertestgrandchild1',
'inheritancemanagertestchild1__inheritancemanagertestgrandchild1_2',
'inheritancemanagertestchild1',
'inheritancemanagertestchild2',
'manual_onetoone', # this was set via parent_link & related_name
]
self.assertEqual(set(results.subclasses),
set(expected_related_names))



class InheritanceManagerUsingModelsTests(TestCase):

def setUp(self):
Expand Down Expand Up @@ -873,6 +923,29 @@ def test_child_doesnt_accidentally_get_parent(self):
], list(objs))


def test_manually_specifying_parent_fk_only_specific_child(self):
"""
given a Model which inherits from another Model, but also declares
the OneToOne link manually using `related_name` and `parent_link`,
ensure that the relation names and subclasses are obtained correctly.
"""
child3 = InheritanceManagerTestChild3.objects.create()
results = InheritanceManagerTestParent.objects.all().select_subclasses(
InheritanceManagerTestChild3)

expected_objs = [InheritanceManagerTestParent(pk=1),
InheritanceManagerTestParent(pk=2),
InheritanceManagerTestParent(pk=3),
InheritanceManagerTestParent(pk=4),
InheritanceManagerTestParent(pk=5),
child3]
self.assertEqual(list(results), expected_objs)

expected_related_names = ['manual_onetoone']
self.assertEqual(set(results.subclasses),
set(expected_related_names))



class InheritanceManagerRelatedTests(InheritanceManagerTests):
def setUp(self):
Expand Down

0 comments on commit ae71f90

Please sign in to comment.