def test_no_entities_added(self): faker = fake seeder = Seeder(faker) try: seeder.execute() except Exception as e: self.assertTrue(isinstance(e, SeederException))
def test_pigeon_hole_principle(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Animal, 8) result = seeder.execute()
def test_null_foreign_key(self): faker = fake seeder = Seeder(faker) try: seeder.add_entity(Action, 1) seeder.execute() except Exception as e: self.assertTrue(isinstance(e, SeederException)) pass
def test_auto_now(self): date = datetime(1957, 3, 6, 13, 13) faker = fake seeder = Seeder(faker) seeder.add_entity(Game, 10, {'updated_at': lambda x: date}) inserted_pks = seeder.execute()[Game] games = Game.objects.filter(pk__in=inserted_pks) self.assertTrue(all(game.updated_at == date for game in games))
def test_many_to_many_population_success(self): faker = fake seeder = Seeder(faker) mtm_count = 10 mtmt_count = 12 payload = "Payload" def resolver(obj, id): Through.objects.create(parent_id=obj.id, subobject_id=id, payload=payload) seeder.add_entity(ThroughSubobject, 50) seeder.add_entity(ManySubobject, 50) seeder.add_entity(Object, 5, many_to_many_count_dict={ 'many_to_many': mtm_count, 'many_to_many_through': mtmt_count }, many_to_many_through_resolver_dict={ 'many_to_many_through': resolver }) inserted_ids = seeder.execute() for object_id in inserted_ids[Object]: object_model = Object.objects.get(id=object_id) mtm_subobjects = list(object_model.many_to_many.all()) mtmt_subobjects = list( object_model.many_to_many_through.filter( through__payload=payload)) self.assertTrue(len(mtm_subobjects) == mtm_count) self.assertTrue(len(mtmt_subobjects) == mtmt_count)
def test_auto_now(self): date = datetime(1957, 3, 6, 13, 13) faker = fake seeder = Seeder(faker) seeder.add_entity(Game, 10, { 'updated_at': lambda x: date }) inserted_pks = seeder.execute()[Game] games = Game.objects.filter(pk__in=inserted_pks) self.assertTrue(all(game.updated_at == date for game in games))
def test_default_with_max_length(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Product, 1) _id = seeder.execute() product = Product.objects.get(id=_id[Product][0]) self.assertTrue(len(DEF_LD) == len(product.description))
def test_default_value_guessed_by_field_name(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Product, 1, {'name':'Great Product'}) _id = seeder.execute() self.assertIsNotNone(_id) product = Product.objects.get(id=_id[Product][0]) self.assertEquals(product.description, DEF_LD)
def test_many_to_one(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Pen, 1) seeder.add_entity(Reporter, 1) seeder.add_entity(Article, 1) seeder.execute() self.assertNotEqual(Reporter.objects.get(id=1), None) self.assertNotEqual(Article.objects.get(id=1), None) self.assertEqual(Article.objects.get(id=1).reporter.pk, 1)
def test_guesser(self): faker = fake def title_fake(arg): title_fake.count += 1 name = faker.company() return name title_fake.count = 0 seeder = Seeder(faker) seeder.add_entity(Game, 10, { 'title': title_fake }) self.assertEqual(len(seeder.execute()[Game]), title_fake.count)
def test_guesser(self): faker = fake def title_fake(arg): title_fake.count += 1 name = faker.company() return name title_fake.count = 0 seeder = Seeder(faker) seeder.add_entity(Game, 10, {'title': title_fake}) self.assertEqual(len(seeder.execute()[Game]), title_fake.count)
def test_default_value_guessed_by_field_type(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Product, 1, {'name':'Awesome Product'}) _id = seeder.execute() self.assertIsNotNone(_id) product = Product.objects.get(id=_id[Product][0]) self.assertEquals(product.short_description, DEF_SD) self.assertTrue(product.enabled)
def test_fields(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Animal, 10) result = seeder.execute() animal_object = Animal.objects.get(id=result[Animal][0]) self.assertTrue(animal_object.species in [x[0] for x in Animal.SPECIES_CHOICES]) self.assertTrue(animal_object.first_color in [x[0] for x in Animal.COLOR_CHOICES]) self.assertTrue(animal_object.second_color in [x[0] for x in Animal.COLOR_CHOICES]) self.assertTrue(animal_object.farm <= 5)
def test_not_covered_fields(self): """ Tell the django-seed how to work with fields which are not covered by the code. Avoids AttributeError(field). :return: """ faker = fake seeder = Seeder(faker) seeder.add_entity(NotCoveredFields, 10, { 'json': lambda x: {seeder.faker.domain_name(): {'description': seeder.faker.text()}}, }) inserted_pks = seeder.execute() self.assertTrue(len(inserted_pks[NotCoveredFields]) == 10) self.assertTrue(all([field.json for field in NotCoveredFields.objects.all()]))
def test_one_to_one(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Pen, 1) seeder.add_entity(Reporter, 1) seeder.execute() self.assertEqual(Reporter.objects.get(id=1).pen.pk, 1)
def test_many_to_one_wrong_order(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Article, 1) seeder.add_entity(Pen, 1) seeder.add_entity(Reporter, 1) self.assertRaises(SeederException, seeder.execute)
def test_population(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Game, 10) self.assertEqual(len(seeder.execute()[Game]), 10) self.assertEqual(len(Game.objects.all()), 10) seeder.add_entity(Game, 40) self.assertEqual(len(seeder.execute()[Game]), 40) self.assertEqual(len(Game.objects.all()), 50)
def test_formatter(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Game,5) seeder.add_entity(Player, 10, { 'score': lambda x: random.randint(0,1000), 'nickname': lambda x: fake.email() }) seeder.add_entity(Action,30) inserted_pks = seeder.execute() self.assertTrue(len(inserted_pks[Game]) == 5) self.assertTrue(len(inserted_pks[Player]) == 10) players = Player.objects.all() self.assertTrue(any([self.valid_player(p) for p in players]))
def test_one_to_one(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Pen, 1) seeder.add_entity(Reporter, 1) result = seeder.execute() self.assertEqual(Reporter.objects.get(id=result[Reporter][0]).pen.pk, result[Pen][0])
def test_max_length(self): faker = fake seeder = Seeder(faker) name_max_len = Customer._meta.get_field('name').max_length country_max_len = Customer._meta.get_field('country').max_length address_max_len = Customer._meta.get_field('address').max_length comments_max_len = Customer._meta.get_field('comments').max_length rand = random.randint(1, 10) data = { 'name': 'x' * (name_max_len + rand), 'country': 'p' * (country_max_len + rand), 'address': 't' * (address_max_len + rand), 'comments': 'o' * (comments_max_len + rand), } seeder.add_entity(Customer, 1, data) _id = seeder.execute() customer = Customer.objects.get(id=_id[Customer][0]) self.assertTrue( len(customer.name) <= name_max_len, "name with length {}, does not respect max length restriction of {}" .format(len(customer.name), name_max_len)) self.assertTrue( len(customer.country) <= country_max_len, "country with length {}, does not respect max length restriction of {}" .format(len(customer.name), country_max_len)) self.assertTrue( len(customer.address) <= address_max_len, "address with length {}, does not respect max length restriction of {}" .format(len(customer.name), address_max_len)) self.assertTrue( len(customer.comments) <= comments_max_len, "comments with length {}, does not respect max length restriction of {}" .format(len(customer.comments), comments_max_len))
def test_one_to_one_population_always_success(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Subobject, 10) seeder.add_entity(Object, 10) inserted_pks = seeder.execute() self.assertEqual(len(inserted_pks[Subobject]), 10) self.assertEqual(len(inserted_pks[Object]), 10)
def test_locale(self): ad = AlphabetDetector() faker = Faker('ru_RU') seeder = Seeder(faker) seeder.add_entity(Game, 5) seeder.execute() self.assertTrue(all([ad.is_cyrillic(game.title) for game in Game.objects.all()]))
def test_postgres_array_field(self): print("Aasdf") faker = fake seeder = Seeder(faker) seeder.add_entity(NotCoveredFields, 1) seeder.execute()
def test_max_length(self): faker = fake seeder = Seeder(faker) name_max_len = Customer._meta.get_field('name').max_length country_max_len = Customer._meta.get_field('country').max_length address_max_len = Customer._meta.get_field('address').max_length comments_max_len = Customer._meta.get_field('comments').max_length rand = random.randint(1, 10) data = { 'name': 'x' * (name_max_len + rand), 'country': 'p' * (country_max_len + rand), 'address': 't' * (address_max_len + rand), 'comments': 'o' * (comments_max_len + rand), } seeder.add_entity(Customer, 1, data) _id = seeder.execute() customer = Customer.objects.get(id=_id[Customer][0]) self.assertTrue(len(customer.name) <= name_max_len, "name with length {}, does not respect max length restriction of {}" .format(len(customer.name), name_max_len)) self.assertTrue(len(customer.country) <= country_max_len, "country with length {}, does not respect max length restriction of {}" .format(len(customer.name), country_max_len)) self.assertTrue(len(customer.address) <= address_max_len, "address with length {}, does not respect max length restriction of {}" .format(len(customer.name), address_max_len)) self.assertTrue(len(customer.comments) <= comments_max_len, "comments with length {}, does not respect max length restriction of {}" .format(len(customer.comments), comments_max_len))
def test_timezone(self): """test if datetime objects are created timezone aware based on USE_TZ in settings.py """ faker = fake seeder = Seeder(faker) try: # import django settings from django.conf import settings from django.utils import timezone except ImportError: pass # check if timezone is active if not getattr(settings, 'USE_TZ', False): setattr(settings, 'USE_TZ', True) deactivate_tz = True else: deactivate_tz = False seeder.add_entity(Game, 1) game = Game.objects.get(pk=seeder.execute()[Game][0]) if deactivate_tz: # reset timezone settings setattr(settings, 'USE_TZ', False) self.assertTrue(timezone.is_aware(game.created_at))
def test_same_model_unique_fields(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Game, 10, {"title": "First Game"}) seeder.add_entity(Game, 20, {"title": "Second Game"}) inserted_pks = seeder.execute() self.assertEqual(len(inserted_pks[Game]), 30) self.assertEqual(len(Game.objects.all()), 30) self.assertEqual( Game.objects.get(id=inserted_pks[Game][0]).title, "First Game") self.assertEqual( Game.objects.get(id=inserted_pks[Game][-1]).title, "Second Game")
def test_many_to_many(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Pen, 1) seeder.add_entity(Reporter, 1) seeder.add_entity(Article, 1) seeder.add_entity(Newspaper, 1) results = seeder.execute() self.assertNotEqual(Newspaper.objects.get(id=1), None) self.assertNotEqual(Reporter.objects.get(id=1), None) self.assertNotEqual(Article.objects.get(id=1), None) self.assertEqual(len(Reporter.objects.get(id=1).newspaper_set.all()), 1)
def test_many_to_many(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Pen, 1) seeder.add_entity(Reporter, 1) seeder.add_entity(Article, 1) seeder.add_entity(Newspaper, 1) results = seeder.execute() self.assertNotEqual(Newspaper.objects.get(id=1), None) self.assertNotEqual(Reporter.objects.get(id=1), None) self.assertNotEqual(Article.objects.get(id=1), None) self.assertEqual(len(Reporter.objects.get(id=1).newspaper_set.all()), 1) # TODO: This test should work once # https://github.com/Brobin/django-seed/issues/79 is resolved # def test_many_to_many_separate_executes(self): # faker = fake # seeder = Seeder(faker) # seeder.add_entity(Pen, 1) # seeder.add_entity(Reporter, 1) # seeder.add_entity(Article, 1) # seeder.execute() # seeder.add_entity(Newspaper, 1) # seeder.execute() # self.assertNotEqual(Newspaper.objects.get(id=1), None) # self.assertNotEqual(Reporter.objects.get(id=1), None) # self.assertNotEqual(Article.objects.get(id=1), None) # self.assertEqual(len(Reporter.objects.get(id=1).newspaper_set.all()), 1)
def test_impossible_uniqueness(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Animal, 1, {"first_color": 1}) seeder.add_entity(Animal, 1, {"first_color": 2}) seeder.add_entity(Animal, 1, {"first_color": 3}) result = seeder.execute() # This fourth animal cannot have a unique first color seeder.add_entity(Animal, 1) self.assertRaises(IntegrityError, seeder.execute)
def test_population(self): faker = fake seeder = Seeder(faker) seeder.add_entity(Game, 10) self.assertEqual(len(seeder.execute()[Game]), 10)