Beispiel #1
0
 def test(self):
     generate_model(TestModelX, 5)
     self.assertEqual(len(TestModelX.objects.all()), 5)
     generate_model(TestModelY, 95)
     generated_models = list(TestModelY.objects.all())
     length = len(generated_models)
     self.assertEqual(len(TestModelX.objects.all()) * 18, length)
     generate_model(TestModelA, 7)
     self.assertEqual(len(TestModelA.objects.all()), 6)
     generate_model(TestModelB, 17)
     self.assertEqual(len(TestModelB.objects.all()), 12)
     generate_model(TestModelC, 53)
     self.assertEqual(len(TestModelC.objects.all()), 12)
     for model in generated_models:
         self.assertTrue(isinstance(model, TestModelY))
         self.assertTrue(model.field1Y in [2, 3, 5, 7, 11, 13])
         self.assertTrue(model.field2Y in ['MMa', 'XXa', 'azz'])
         self.assertTrue(model.field3Y in TestModelX.objects.all())
     to_be_computed_test = generate_model(TestModelFieldsTwo, 50)
     self.assertTrue(to_be_computed_test)
     self.assertEqual(TestModelFieldsTwo, to_be_computed_test[0])
     self.assertTrue(to_be_computed_test[1])
     for fld in to_be_computed_test[1]:
         self.assertTrue(is_related(fld)
                         and 'ManyToMany' in relation_type(fld))
         self.assertEqual(fld.rel.to, TestModelE)
     generate_model(TestModelE, 2, shuffle=False)[0]
     generated_models = list(TestModelE.objects.all())
     for model in generated_models:
         self.assertTrue(isinstance(model, TestModelE))
         self.assertTrue(model.field4E in [1000000009, 1000003, 101])
         self.assertTrue(model.field1E in TestModelB.objects.all())
         self.assertTrue(all([x in TestModelA.objects.all()
                              for x in model.field2E.all()]))
         self.assertTrue(model.field3E in TestModelC.objects.all())
Beispiel #2
0
 def test(self):
     self.assertTrue(is_related(models.ForeignKey))
     self.assertTrue(is_related(models.OneToOneField))
     self.assertTrue(is_related(models.ManyToManyField))
     self.assertFalse(is_related(models.CharField))
     self.assertFalse(is_related(models.BooleanField))
     self.assertFalse(is_related(models.EmailField))
     self.assertFalse(is_related(models.IntegerField))
Beispiel #3
0
 def test(self):
     generate_test_data('tests.models', 10)
     length = len(list_of_models(mdls))
     visited = dict(zip(list_of_models(mdls), length * [False]))
     pairs = []
     data_base = dict([(mdl, list(mdl.objects.all()))
                       for mdl in list_of_models(mdls)])
     generated_data = data_base.values()
     nodes = 0
     edges = 0
     for list_model in generated_data:
         for model in list_model:
             visited[model.__class__] = True
             fields = list_of_fields(model.__class__)
             nodes += 1
             for field in fields:
                 if (not is_auto_field(field) and
                    not is_reverse_related(field)):
                     val = getattr(model, field.name)
                     if is_related(field):
                         if 'ManyToMany' in relation_type(field):
                             r = data_base[field.rel.to]
                             self.assertTrue(list(val.all()))
                             self.assertTrue(all([x in r for
                                                  x in list(val.all())]))
                         else:
                             r = data_base[field.rel.to]
                             self.assertTrue(val in r)
                         edges += 1
                     else:
                         this_model = field.model
                         while (this_model != Model and not
                                (hasattr(this_model, 'TestData') and
                                 hasattr(this_model.TestData, field.name))
                                and not os.path.exists(
                                 '%s/TestTemplates/sample__%s__%s' %
                                 (this_model._meta.app_label,
                                  this_model.__name__, field.name))):
                             this_model = this_model.__base__
                         if this_model == Model:
                             self.assertEqual(model.__class__,
                                              AllFieldsModel)
                             sample_values = field_sample_values(field)
                             if val.__class__ == unicode:
                                 val = str(val)
                             self.assertTrue(val.__class__ in
                                             map(lambda val: val.__class__,
                                                 sample_values))
                         elif (field.__class__.__name__ == 'DecimalField' or
                               field.__class__.__name__ == 'FloatField'):
                             sample_values = map(float,
                                                 field_sample_values(field))
                             val = float(val)
                             self.assertTrue(any(abs(val - fld_value) < 1e-5
                                                 for fld_value in
                                                 sample_values))
                         else:
                             sample_values = map(str,
                                                 field_sample_values(field))
                             val = str(val)
                             self.assertTrue(val in sample_values)
             if model.__class__ == TestModelFields:
                 pr = (model.fieldC, model.fieldA)
                 self.assertFalse(pr in pairs)
                 pairs.append(pr)
                 self.assertTrue((model.fieldB < 50)
                                 or (model.fieldD / 2 % 2 == 1))
     self.assertTrue(all(visited.values()),
                     "Not all the models with sample data are generated.")