示例#1
0
 def test_no_entities_added(self):
     faker = fake
     seeder = Seeder(faker)
     try:
         seeder.execute()
     except Exception as e:
         self.assertTrue(isinstance(e, SeederException))
示例#2
0
    def test_pigeon_hole_principle(self):
        faker = fake
        seeder = Seeder(faker)

        seeder.add_entity(Animal, 8)

        result = seeder.execute()
示例#3
0
 def test_no_entities_added(self):
     faker = fake
     seeder = Seeder(faker)
     try:
         seeder.execute()
     except Exception as e:
         self.assertTrue(isinstance(e, SeederException))
示例#4
0
 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
示例#5
0
    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))
示例#6
0
    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)
示例#7
0
    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))
示例#8
0
    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))
示例#9
0
    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))
示例#10
0
    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)
示例#11
0
    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)
示例#12
0
 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)
示例#13
0
    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)
示例#14
0
    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)
示例#15
0
    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)
示例#16
0
    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)
示例#17
0
    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)
示例#18
0
 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()]))
示例#19
0
    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)
示例#20
0
    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)
示例#21
0
    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)
示例#22
0
    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]))
示例#23
0
    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])
示例#24
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))
示例#25
0
    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)
示例#26
0
 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()]))
示例#27
0
    def test_postgres_array_field(self):
        print("Aasdf")
        faker = fake
        seeder = Seeder(faker)
        seeder.add_entity(NotCoveredFields, 1)

        seeder.execute()
示例#28
0
    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]))
示例#29
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))
示例#30
0
 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
示例#31
0
 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))
示例#32
0
 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))
示例#33
0
    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")
示例#34
0
    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)
示例#35
0
    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)
示例#36
0
    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)
示例#37
0
 def test_population(self):
     faker = fake
     seeder = Seeder(faker)
     seeder.add_entity(Game, 10)
     self.assertEqual(len(seeder.execute()[Game]), 10)