def test_faker_cache_generator(self): self.assertEqual(DjangoFaker().get_generator(), DjangoFaker().get_generator()) self.assertIs(DjangoFaker().get_generator(), DjangoFaker().get_generator()) self.assertIs(DjangoFaker().get_generator(codename='default'), DjangoFaker().get_generator(codename='default')) self.assertEqual(DjangoFaker().get_generator(locale='it_IT'), DjangoFaker().get_generator(locale='it_IT')) self.assertIs(DjangoFaker().get_generator(locale='it_IT'), DjangoFaker().get_generator(locale='it_IT'))
def test_faker_cache_populator(self): self.assertEqual(DjangoFaker().get_generator(), DjangoFaker().get_generator()) self.assertIs(DjangoFaker().get_generator(), DjangoFaker().get_generator()) self.assertIs(DjangoFaker().get_generator(), DjangoFaker().get_generator()) self.assertEqual(DjangoFaker().get_generator(locale='it_IT'), DjangoFaker().get_generator(locale='it_IT')) self.assertIs(DjangoFaker().get_generator(locale='it_IT'), DjangoFaker().get_generator(locale='it_IT'))
def testFakerCachePopulator(self): self.assertEqual( DjangoFaker().getPopulator(), DjangoFaker().getPopulator() ) self.assertIs( DjangoFaker().getPopulator(), DjangoFaker().getPopulator() ) self.assertIs( DjangoFaker().getPopulator().generator, DjangoFaker().getPopulator().generator ) self.assertEqual( DjangoFaker().getPopulator(locale='it_IT'), DjangoFaker().getPopulator(locale='it_IT') ) self.assertIs( DjangoFaker().getPopulator(locale='it_IT'), DjangoFaker().getPopulator(locale='it_IT') )
def test_foreign_key(self): """Should be able to use one key for all the foreigns""" populator = DjangoFaker.get_populator() populator.add_entity(Key, 1) populator.add_entity(Foreign, 5) populator.execute() self.assertEqual(5, len(Key.objects.all()[0].foreigns.all()))
def test_many_to_many_existing(self): """Should be able to use existing db values if none added to populator""" populator = DjangoFaker.get_populator() to_many = ToMany(thing='stuff') to_many.save() populator.add_entity(Many, 1) populator.execute() self.assertEqual(1, len(Many.objects.all()))
def test_many_to_many_fail(self): """Non-null many-to-many fields will raise exceptions if there aren't available objects""" try: populator = DjangoFaker.get_populator() populator.add_entity(Many, 1) populator.execute() self.fail("All Many's require at least one ToMany to exist") except InvalidConstraint: pass
def test_many_to_many(self): """Each many-to-many field assigns 1 or more object if they exist. many-to-many fields won't error out if there are no available objects""" populator = DjangoFaker.get_populator() populator.add_entity(ToMany, 5) populator.add_entity(Many, 5) populator.execute() for many in Many.objects.all(): self.assertTrue(len(many.to_many.all()) > 0)
def fake_tag_str(formatter, *args, **kwargs): """ call a faker format uses: {% fakestr "formatterName" *values **kwargs %} """ if formatter == 'dateTimeThisCentury': print(args, kwargs) return DjangoFaker.get_generator().format(formatter, *args, **kwargs)
def test_one_to_one(self): """Should preserve the one-to-one constraint (each one should have a different to one)""" populator = DjangoFaker.get_populator() populator.add_entity(ToOne, 5) populator.add_entity(One, 5) populator.execute() to_ones = ToOne.objects.all() for one in One.objects.all(): to_ones = to_ones.exclude(one=one) self.assertEqual(0, len(to_ones))
def do_or_fake_filter(value, formatter): """ call a faker if value is None uses: {{ myint|or_fake:'randomInt' }} """ if not value: value = DjangoFaker.get_generator().format(formatter) return value
def test_one_to_one_existing(self): """Should be able to use a mix of existing db and new entities""" populator = DjangoFaker.get_populator() to_one = ToOne(thing='stuff') to_one.save() populator.add_entity(ToOne, 1) populator.add_entity(One, 2) populator.execute() to_ones = ToOne.objects.all() for one in One.objects.all(): to_ones = to_ones.exclude(one=one) self.assertEqual(0, len(to_ones))
def test_one_to_one_fail(self): """Should throw an exception if """ try: populator = DjangoFaker.get_populator() populator.add_entity(ToOne, 4) populator.add_entity(One, 5) populator.execute() self.fail( "each One should need an unused ToOne which should be impossible here" ) except InvalidConstraint: pass
def do_fake(formatter, *args, **kwargs): """ call a faker format uses: {% fake "formatterName" *args **kwargs as myvar %} {{ myvar }} or: {% fake 'name' %} """ return DjangoFaker.get_generator().format(formatter, *args, **kwargs)
def do_fake_filter(formatter, arg=None): """ call a faker format uses: {{ 'randomElement'|fake:mylist }} {% if 'boolean'|fake:30 %} .. {% endif %} {% for word in 'words'|fake:times %}{{ word }}\n{% endfor %} """ args = [] if not arg is None: args.append(arg) return DjangoFaker.get_generator().format(formatter, *args)
def test_complex(self): """Should be able to populate a complex set of models""" populator = DjangoFaker.get_populator() populator.add_entity(Assessment, 5) populator.add_entity(Question, 5) populator.add_entity(AnswerSubmission, 5) populator.add_entity(Company, 5) populator.add_entity(Answer, 5) results = populator.execute() self.assertEqual(len(results[Assessment]), 5) self.assertEqual(len(results[Question]), 5) self.assertEqual(len(results[AnswerSubmission]), 5) self.assertEqual(len(results[Company]), 5) self.assertEqual(len(results[Answer]), 5)
def test_all_names(self): """""" populator = DjangoFaker.get_populator() populator.add_entity(NamedFields, 1) populator.execute() named = NamedFields.objects.all()[0] self.assertRegexpMatches(named.is_boolean, '(True)|(False)') self.assertRegexpMatches(named.created_at, '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}') self.assertRegexpMatches(named.first_name, '[A-Z][a-z]+?') self.assertRegexpMatches(named.last_name, '[A-Z][a-z]+?') self.assertRegexpMatches(named.name, '\w') self.assertRegexpMatches(named.login, '\w') self.assertRegexpMatches(named.email, '.+@.*\..*') self.assertRegexpMatches(named.phone_number, '[\d\-x]+') self.assertRegexpMatches(named.city, '([A-Z][a-z]+? ?)+') self.assertRegexpMatches(named.zipcode, '\d+') self.assertRegexpMatches(named.state, '[A-Z\']{2}') self.assertRegexpMatches(named.country, '([A-Z][a-z]+? ?)+') self.assertRegexpMatches(named.title, '[\w ]+.')
def testDjangoFakerSingleton(self): self.assertEqual(DjangoFaker(), DjangoFaker()) self.assertIs(DjangoFaker(), DjangoFaker())
def test_django_faker_singleton(self): self.assertEqual(DjangoFaker(), DjangoFaker()) self.assertIs(DjangoFaker(), DjangoFaker())
def setUp(self): # populator gets shared, need to reset it populator = DjangoFaker.get_populator() populator.clear()
def setUp(self): populator = DjangoFaker.get_populator() populator.clear()