def test_column_choices(self): """Choices computed for the exported model When using the ColumnFormSet and the ColumnForm, all the accessible items (fields, relations, methods, attributes...) from the exported model are present in the "column" form field choices """ ColumnInlineFormSet = inlineformset_factory(Export, Column, form=ColumnForm, formset=ColumnFormSet) formset = ColumnInlineFormSet(instance=self.empty_export) # the column field has choices form = formset.forms[0] self.assertTrue(hasattr(form.fields['column'], 'choices')) # all the table items are in the column field choices self.choices = form.fields['column'].choices self.assertTrue(all([(i, i) in self.choices for i in self.im.items])) # and all the related tables items are in the fields choices # export has a FK to Format named 'export_format' im_format = [ 'export_format.%s' % i for i in InspectModel(Format).items ] self.assertTrue(all([(i, i) in self.choices for i in im_format])) # export has a FK to ContentType named 'model' im_ct = ['model.%s' % i for i in InspectModel(ContentType).items] self.assertTrue(all([(i, i) in self.choices for i in im_ct]))
def setUp(self): self.om = OtherModel.objects.create() ctype = ContentType.objects.get_for_model(OtherModel) self.mti = ModelToInspect.objects.create( foreign=self.om, one=self.om, content_type=ctype) self.mti.many.add(self.om) self.lm = LinkedModel.objects.create(toinspect=self.mti) self.im = InspectModel(self.mti)
def test_column_choices(self): """Choices computed for the exported model When using the ColumnFormSet and the ColumnForm, all the accessible items (fields, relations, methods, attributes...) from the exported model are present in the "column" form field choices """ class OneToOneToExport(models.Model): """Fake model. Make sure that get_choices works with SingleRelatedObjectDescriptor, as explained in ticket #4. """ name = models.CharField(max_length=50) o2o = models.OneToOneField(Export) # reload the model's relations, to have the OneToOneToExport's relation # taken into account self.empty_export._meta._fill_related_objects_cache() self.empty_export._meta.init_name_map() ColumnInlineFormSet = inlineformset_factory(Export, Column, form=ColumnForm, formset=ColumnFormSet) formset = ColumnInlineFormSet(instance=self.empty_export) # the column field has choices form = formset.forms[0] self.assertTrue(hasattr(form.fields['column'], 'choices')) choices = form.fields['column'].choices # all the model items are in the column field choices self.assertTrue(all([(i, i) in choices for i in self.im.items])) # and all the related model items are in the fields choices # export has a FK to Format named 'export_format' im_format = [ 'export_format.%s' % i for i in InspectModel(Format).items ] self.assertTrue(all([(i, i) in choices for i in im_format])) # export has a FK to ContentType named 'model' im_ct = ['model.%s' % i for i in InspectModel(ContentType).items] self.assertTrue(all([(i, i) in choices for i in im_ct])) # OneToOneToExport has a OneToOneField to ContentType named # 'onetoonetoexport' im_o2o = [ 'onetoonetoexport.%s' % i for i in InspectModel(OneToOneToExport).items ] self.assertTrue(all([(i, i) in choices for i in im_o2o])) # revert changes to name_map: 'unload' the OneToOneToExport relation del self.empty_export._meta._name_map['onetoonetoexport']
def setUp(self): self.om = OtherModel.objects.create() self.mti = ModelToInspect.objects.create(foreign=self.om, one=self.om) self.mti.many.add(self.om) self.lm = LinkedModel.objects.create(toinspect=self.mti) self.im = InspectModel(self.mti)
def setUp(self): self.om = OtherModel.objects.create() ctype = ContentType.objects.get_for_model(OtherModel) self.mti = ModelToInspect.objects.create(foreign=self.om, one=self.om, content_type=ctype) self.mti.many.add(self.om) self.lm = LinkedModel.objects.create(toinspect=self.mti) self.im = InspectModel(self.mti)
class ModelInspectTest(TestCase): def setUp(self): self.om = OtherModel.objects.create() ctype = ContentType.objects.get_for_model(OtherModel) self.mti = ModelToInspect.objects.create(foreign=self.om, one=self.om, content_type=ctype) self.mti.many.add(self.om) self.lm = LinkedModel.objects.create(toinspect=self.mti) self.im = InspectModel(self.mti) def test_fields(self): # 21 fields + the automatically generated id field self.assertEqual(len(self.im.fields), 22) self.assertFalse("attribute" in self.im.fields) self.assertFalse("_hidden" in self.im.fields) def test_relation_fields(self): # 2 'local' fields + a OneToOneField on LinkedModel self.assertEqual(len(self.im.relation_fields), 5) self.assertTrue("foreign" in self.im.relation_fields) self.assertTrue("content_type" in self.im.relation_fields) self.assertTrue("genericforeign" in self.im.relation_fields) self.assertTrue("linkedmodel" in self.im.relation_fields) self.assertTrue("one" in self.im.relation_fields) self.assertFalse("many" in self.im.relation_fields) def test_many_fields(self): # 1 local + 1 on the ManyRelatedModel self.assertEqual(len(self.im.many_fields), 2) self.assertTrue("manyrelatedmodel_set" in self.im.many_fields) self.assertTrue("many" in self.im.many_fields) self.assertFalse("one" in self.im.many_fields) def test_attributes(self): self.assertEqual(len(self.im.attributes), 1) def test_properties(self): self.assertEqual(len(self.im.properties), 2) def test_methods(self): self.assertEqual(len(self.im.methods), 2) self.assertFalse("method_args" in self.im.methods) self.assertFalse("_hidden_method" in self.im.methods) def test_items(self): # make sure all the items are indeed part of a ModelToInspect instance items = [getattr(self.mti, f) for f in self.im.items] self.assertEqual(len(items), 34) def test_multiple_calls(self): """Multiple calls to get_FOO""" self.im.update_fields() self.assertEqual(len(self.im.fields), 22) self.assertEqual(len(self.im.relation_fields), 5) self.assertEqual(len(self.im.many_fields), 2) self.im.update_attributes() self.assertEqual(len(self.im.attributes), 1) self.im.update_methods() self.assertEqual(len(self.im.methods), 2) self.assertEqual(len(self.im.items), 34)
def get_choices(model, prefixes=[]): choices = [] prefix = '.'.join(prefixes) if prefix: prefix = '%s.' % prefix im = InspectModel(model) items = ['%s%s' % (prefix, i) for i in im.items] choices += zip(items, items) for f in im.relation_fields: related_model = getattr(model, f).field.rel.to if f in prefixes: # we already went through this model return [] # end of recursion new_prefixes = prefixes + [f] choices += get_choices(related_model, prefixes=new_prefixes) return choices
def setUp(self): # create an export of the Export model (inception !) ct = ContentType.objects.get(app_label='data_exports', model='export') self.empty_export = Export.objects.create(name='test empty export', slug='test-empty-export', model=ct) self.im = InspectModel(Export) # create an export of the Export model with columns self.export = Export.objects.create(name='test export', slug='test-export', model=ct) for f in self.im.items: Column.objects.create(export=self.export, column=f, order=0) user = User.objects.create_user('admin', '*****@*****.**', 'admin') user.is_superuser = True user.save()
class ModelInspectTest(TestCase): def setUp(self): self.om = OtherModel.objects.create() ctype = ContentType.objects.get_for_model(OtherModel) self.mti = ModelToInspect.objects.create( foreign=self.om, one=self.om, content_type=ctype) self.mti.many.add(self.om) self.lm = LinkedModel.objects.create(toinspect=self.mti) self.im = InspectModel(self.mti) def test_fields(self): # 21 fields + the automatically generated id field self.assertEqual(len(self.im.fields), 22) self.assertFalse('attribute' in self.im.fields) self.assertFalse('_hidden' in self.im.fields) def test_relation_fields(self): # 2 'local' fields + a OneToOneField on LinkedModel + GenericFK + CT. self.assertEqual(len(self.im.relation_fields), 5) self.assertTrue('foreign' in self.im.relation_fields) self.assertTrue('content_type' in self.im.relation_fields) self.assertTrue('genericforeign' in self.im.relation_fields) self.assertTrue('linkedmodel' in self.im.relation_fields) self.assertTrue('one' in self.im.relation_fields) self.assertFalse('many' in self.im.relation_fields) def test_many_fields(self): # 1 local + 1 on the ManyRelatedModel self.assertEqual(len(self.im.many_fields), 2) self.assertTrue('manyrelatedmodel_set' in self.im.many_fields) self.assertTrue('many' in self.im.many_fields) self.assertFalse('one' in self.im.many_fields) def test_attributes(self): self.assertEqual(len(self.im.attributes), 1) def test_properties(self): self.assertEqual(len(self.im.properties), 2) def test_methods(self): self.assertEqual(len(self.im.methods), 2) self.assertFalse('method_args' in self.im.methods) self.assertFalse('_hidden_method' in self.im.methods) def test_items(self): # make sure all the items are indeed part of a ModelToInspect instance items = [getattr(self.mti, f) for f in self.im.items] self.assertEqual(len(items), 34) def test_multiple_calls(self): """Multiple calls to get_FOO""" self.im.update_fields() self.assertEqual(len(self.im.fields), 22) self.assertEqual(len(self.im.relation_fields), 5) self.assertEqual(len(self.im.many_fields), 2) self.im.update_attributes() self.assertEqual(len(self.im.attributes), 1) self.im.update_methods() self.assertEqual(len(self.im.methods), 2) self.assertEqual(len(self.im.items), 34)