Example #1
0
    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)
Example #2
0
class ModelInspectTest(TestCase):

    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 test_fields(self):
        # 22 fields + the automatically generated id field
        self.assertEqual(len(self.im.fields), 23)
        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), 3)
        self.assertTrue('foreign' 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_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), 31)

    def test_multiple_calls(self):
        """Multiple calls to get_FOO"""
        self.im.update_fields()
        self.assertEqual(len(self.im.fields), 23)
        self.assertEqual(len(self.im.relation_fields), 3)
        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), 31)