Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mockups for unique foreign keys #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mockups/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def __init__(self, model, constraints=None, follow_fk=None,

``generate_fk``: A boolean which indicates if related models should
also be created with random values. The *follow_fk* parameter will
be ignored if *generate_fk* is set to ``True``.
be ignored if *generate_fk* is set to ``True`` or foreign field is
unique.

``follow_m2m``: A tuple containing minium and maximum of model
instances that are assigned to ``ManyToManyField``. No new
Expand Down
3 changes: 2 additions & 1 deletion mockups/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ def get_generator(self, field, *args, **kwargs):
class ForeignKeyFieldGenerator(FieldGenerator):
def get_generator(self, field, generate_fk=None, follow_fk=None, **kwargs):
# if generate_fk is set, follow_fk is ignored.
if field.name in generate_fk:
# also generate fk if field is unique to ensure uniqueness.
if field.name in generate_fk or self.field.unique:
mockup = get_mockup(
field.rel.to,
follow_fk=follow_fk.get_deep_links(field.name),
Expand Down
12 changes: 6 additions & 6 deletions mockups/tests/mockups_test/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ def test_follow_only_some_foreignkeys(self):
self.assertEqual(obj.related, related)
self.assertEqual(obj.limitedfk, None)

def test_follow_fk_for_o2o(self):
def test_unique_fk_for_o2o(self):
# OneToOneField is the same as a ForeignKey with unique=True
filler = Mockup(O2OModel, follow_fk=True)
filler = Mockup(O2OModel)

simple = SimpleModel.objects.create()
obj = filler.create()[0]
self.assertEqual(obj.o2o, simple)
all_o2o = set()
for obj in filler.create(10):
all_o2o.add(obj.o2o)

self.assertRaises(CreateInstanceError, filler.create)
self.assertEqual(set(SimpleModel.objects.all()), all_o2o)

def test_generate_fk_for_o2o(self):
# OneToOneField is the same as a ForeignKey with unique=True
Expand Down