Exemple #1
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.")
Exemple #2
0
 def test(self):
     Y = list_of_fields(TestModelY)
     X = list_of_fields(TestModelX)
     A = list_of_fields(TestModelA)
     B = list_of_fields(TestModelB)
     C = list_of_fields(TestModelC)
     D = list_of_fields(TestModelD)
     E = list_of_fields(TestModelE)
     self.assertFalse(field_sample_values(X[0]))
     self.assertEqual(field_sample_values(Y[1]), [2, 3, 5, 7, 11, 13])
     self.assertEqual(field_sample_values(Y[2]), ['MMa', 'XXa', 'azz'])
     self.assertEqual(field_sample_values(X[1]),
                      [x * x * x for x in range(10)])
     self.assertEqual(field_sample_values(E[3]), [1000000009, 1000003, 101])
     self.assertEqual(field_sample_values(D[1]),
                      [x * x * x for x in range(10)])
     self.assertEqual(field_sample_values(C[1]),
                      ['Hello I am C', 'MUHAHAHAHAHA', 'CCCC', '^_^'])
     self.assertEqual(field_sample_values(B[1]),
                      ['Hello Universe', 'Hello Parallel Universe!'])
     self.assertEqual(field_sample_values(A[1]),
                      ['Hello World', 'Hello Africa', 'axxx!!'])
     self.assertEqual(field_sample_values(A[2]),
                      ['Hello Second Field', 'field 2'])
     a = TestModelX(field1X=12)
     b = TestModelX(field1X=15)
     a.save()
     b.save()
     self.assertEqual((field_sample_values(models.ForeignKey(TestModelX))),
                      ([a, b]))
     fld = models.ManyToManyField(TestModelX)
     self.assertTrue(all([x in [a, b]
                          for x in field_sample_values(fld)[0]]))
     vals = [int(x) for x in field_sample_values(list_of_fields(CycleF)[2])]
     self.assertEqual(vals, range(4000, 5000))