Skip to content

Commit

Permalink
Improve potential name collision handling on taxonomy fixtures
Browse files Browse the repository at this point in the history
`title` has a unique constraint in our DB, so we're relying on the
randomness of Faker's sentence generator not to create duplicates.
With `django_get_or_create` though, we can avoid the problem by not
creating a new row if a duplicate name happens to be generated.
  • Loading branch information
chigby committed Sep 10, 2018
1 parent a1740d5 commit 06f76b8
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions directory/tests/factories/taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
from directory.models import Language, Country, Topic


class LanguageFactory(factory.Factory):
class LanguageFactory(factory.django.DjangoModelFactory):
class Meta:
model = Language
django_get_or_create = ('title',)
# there were not suffient random words in faker, so we're using sentences
title = factory.Faker('sentence', nb_words=3)


class CountryFactory(factory.Factory):
class CountryFactory(factory.django.DjangoModelFactory):
class Meta:
model = Country
django_get_or_create = ('title',)
title = factory.Faker('sentence', nb_words=3)


class TopicFactory(factory.Factory):
class TopicFactory(factory.django.DjangoModelFactory):
class Meta:
model = Topic
django_get_or_create = ('title',)
title = factory.Faker('sentence', nb_words=3)

0 comments on commit 06f76b8

Please sign in to comment.