def test_generate_m2m_with_intermediary_model(self): filler = Mockup(M2MModelThrough, generate_m2m=(1, 5)) all_m2m = set() for obj in filler.create(10): self.assertTrue(1 <= obj.m2m.count() <= 5) all_m2m.update(obj.m2m.all()) self.assertEqual(SimpleModel.objects.count(), len(all_m2m))
def test_follow_m2m(self): related = Mockup(SimpleModel).create()[0] self.assertEqual(SimpleModel.objects.count(), 1) filler = Mockup(M2MModel, follow_m2m=(2, 10)) for obj in filler.create(10): self.assertEqual(list(obj.m2m.all()), [related])
def test_constraints(self): filler = Mockup(BasicModel) for obj in filler.create(100): self.assertTrue(len(obj.chars) > 0) self.assertEqual(type(obj.chars), unicode) self.assertTrue(len(obj.shortchars) <= 2) self.assertEqual(type(obj.shortchars), unicode) self.assertTrue(type(obj.blankchars), unicode) self.assertEqualOr(type(obj.nullchars), unicode, None) self.assertEqual(type(obj.slugfield), unicode) self.assertEqual(type(obj.defaultint), int) self.assertEqual(obj.defaultint, 1) self.assertEqual(type(obj.intfield), int) self.assertEqual(type(obj.sintfield), int) self.assertEqual(type(obj.pintfield), int) self.assertEqual(type(obj.psintfield), int) self.assertEqual(type(obj.datefield), date) self.assertEqual(type(obj.datetimefield), datetime) self.assertEqual(type(obj.defaultdatetime), datetime) self.assertEqual(obj.defaultdatetime, y2k()) self.assertEqual(type(obj.decimalfield), Decimal) self.assertTrue("@" in obj.emailfield) self.assertTrue("." in obj.emailfield) self.assertTrue(" " not in obj.emailfield) self.assertTrue(obj.ipaddressfield.count("."), 3) self.assertTrue(len(obj.ipaddressfield) >= 7) self.assertEqual(BasicModel.objects.count(), 100)
def test_generate_foreignkeys(self): for m in RELATED_MODELS: filler = Mockup( m, generate_fk=True) for obj in filler.create(100): self.assertEqual(obj.related.__class__, BasicModel) self.assertEqual(obj.limitedfk.name, 'foo')
def test_unique_together(self): filler = Mockup(UniqueTogetherTestModel) count1 = len(filler.model._meta. get_field_by_name('choice1')[0].choices) count2 = len(filler.model._meta. get_field_by_name('choice2')[0].choices) for obj in filler.create(count1 * count2): pass
def test_generate_only_some_foreignkeys(self): for m in RELATED_MODELS: filler = Mockup( m, generate_fk=('related',)) for obj in filler.create(100): self.assertEqual(obj.related.__class__, BasicModel) self.assertEqual(obj.limitedfk, None)
def test_deep_generate_foreignkeys(self): filler = Mockup( DeepLinkModel2, generate_fk=True) for obj in filler.create(10): self.assertEqual(obj.related.__class__, DeepLinkModel1) self.assertEqual(obj.related.related.__class__, SimpleModel) self.assertEqual(obj.related.related2.__class__, SimpleModel)
def test_deep_generate_foreignkeys2(self): filler = Mockup( DeepLinkModel2, follow_fk=False, generate_fk=('related', 'related__related')) for obj in filler.create(10): self.assertEqual(obj.related.__class__, DeepLinkModel1) self.assertEqual(obj.related.related.__class__, SimpleModel) self.assertEqual(obj.related.related2, None)
def test_generate_fk_for_o2o(self): # OneToOneField is the same as a ForeignKey with unique=True filler = Mockup(O2OModel, generate_fk=True) all_o2o = set() for obj in filler.create(10): all_o2o.add(obj.o2o) self.assertEqual(set(SimpleModel.objects.all()), all_o2o)
def test_follow_fk_for_o2o(self): # OneToOneField is the same as a ForeignKey with unique=True filler = Mockup(O2OModel, follow_fk=True) simple = SimpleModel.objects.create() obj = filler.create()[0] self.assertEqual(obj.o2o, simple) self.assertRaises(CreateInstanceError, filler.create)
def test_follow_only_some_m2m(self): related = Mockup(SimpleModel).create()[0] self.assertEqual(SimpleModel.objects.count(), 1) other_related = Mockup(OtherSimpleModel).create()[0] self.assertEqual(OtherSimpleModel.objects.count(), 1) filler = Mockup(M2MModel, follow_m2m={"m2m": (2, 10)}) for obj in filler.create(10): self.assertEqual(list(obj.m2m.all()), [related]) self.assertEqual(list(obj.secondm2m.all()), [])
def test_generate_only_some_m2m(self): filler = Mockup(M2MModel, generate_m2m={"m2m": (1, 5)}) all_m2m = set() all_secondm2m = set() for obj in filler.create(10): self.assertTrue(1 <= obj.m2m.count() <= 5) self.assertEqual(0, obj.secondm2m.count()) all_m2m.update(obj.m2m.all()) all_secondm2m.update(obj.secondm2m.all()) self.assertEqual(SimpleModel.objects.count(), len(all_m2m)) self.assertEqual(OtherSimpleModel.objects.count(), len(all_secondm2m))
def test_generate_m2m(self): filler = Mockup(M2MModel, generate_m2m=(1, 5)) all_m2m = set() all_secondm2m = set() for obj in filler.create(10): self.assertTrue(1 <= obj.m2m.count() <= 5) self.assertTrue(1 <= obj.secondm2m.count() <= 5) all_m2m.update(obj.m2m.all()) all_secondm2m.update(obj.secondm2m.all()) self.assertEqual(SimpleModel.objects.count(), len(all_m2m)) self.assertEqual(OtherSimpleModel.objects.count(), len(all_secondm2m))
def test_follow_only_some_foreignkeys(self): related = Mockup(BasicModel).create()[0] self.assertEqual(BasicModel.objects.count(), 1) simple = SimpleModel.objects.create(name='foo') simple2 = SimpleModel.objects.create(name='bar') for m in RELATED_MODELS: filler = Mockup(m, follow_fk=('related', )) for obj in filler.create(100): self.assertEqual(obj.related, related) self.assertEqual(obj.limitedfk, None)
def test_follow_only_some_m2m(self): related = Mockup(SimpleModel).create()[0] self.assertEqual(SimpleModel.objects.count(), 1) other_related = Mockup(OtherSimpleModel).create()[0] self.assertEqual(OtherSimpleModel.objects.count(), 1) filler = Mockup(M2MModel, follow_m2m={ 'm2m': (2, 10), }) for obj in filler.create(10): self.assertEqual(list(obj.m2m.all()), [related]) self.assertEqual(list(obj.secondm2m.all()), [])
def test_follow_only_some_foreignkeys(self): related = Mockup(BasicModel).create()[0] self.assertEqual(BasicModel.objects.count(), 1) simple = SimpleModel.objects.create(name="foo") simple2 = SimpleModel.objects.create(name="bar") for m in RELATED_MODELS: filler = Mockup(m, follow_fk=("related",)) for obj in filler.create(100): self.assertEqual(obj.related, related) self.assertEqual(obj.limitedfk, None)
def test_follow_foreignkeys(self): related = Mockup(BasicModel).create()[0] self.assertEqual(BasicModel.objects.count(), 1) simple = SimpleModel.objects.create(name='foo') simple2 = SimpleModel.objects.create(name='bar') for m in RELATED_MODELS: filler = Mockup( m, follow_fk=True) for obj in filler.create(100): self.assertEqual(obj.related, related) self.assertEqual(obj.limitedfk, simple)
def test_create(self): filler = Mockup(BasicModel) filler.create(10) self.assertEqual(BasicModel.objects.count(), 10)
def test_unique_field(self): filler = Mockup(UniqueTestModel) count = len(filler.model._meta.get_field_by_name("choice1")[0].choices) for obj in filler.create(count): pass