PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/field_subclassing/tests.py

https://code.google.com/p/mango-py/
Python | 81 lines | 53 code | 18 blank | 10 comment | 0 complexity | e3f259383f93ab3793ee6cc023e8c8e9 MD5 | raw file
 1from django.core import serializers
 2from django.test import TestCase
 3
 4from fields import Small
 5from models import DataModel, MyModel, OtherModel
 6
 7
 8class CustomField(TestCase):
 9    def test_defer(self):
10        d = DataModel.objects.create(data=[1, 2, 3])
11
12        self.assertTrue(isinstance(d.data, list))
13
14        d = DataModel.objects.get(pk=d.pk)
15        self.assertTrue(isinstance(d.data, list))
16        self.assertEqual(d.data, [1, 2, 3])
17
18        d = DataModel.objects.defer("data").get(pk=d.pk)
19        d.save()
20
21        d = DataModel.objects.get(pk=d.pk)
22        self.assertTrue(isinstance(d.data, list))
23        self.assertEqual(d.data, [1, 2, 3])
24
25    def test_custom_field(self):
26        # Creating a model with custom fields is done as per normal.
27        s = Small(1, 2)
28        self.assertEqual(str(s), "12")
29
30        m = MyModel.objects.create(name="m", data=s)
31        # Custom fields still have normal field's attributes.
32        self.assertEqual(m._meta.get_field("data").verbose_name, "small field")
33
34        # The m.data attribute has been initialised correctly. It's a Small
35        # object.
36        self.assertEqual((m.data.first, m.data.second), (1, 2))
37
38        # The data loads back from the database correctly and 'data' has the
39        # right type.
40        m1 = MyModel.objects.get(pk=m.pk)
41        self.assertTrue(isinstance(m1.data, Small))
42        self.assertEqual(str(m1.data), "12")
43
44        # We can do normal filtering on the custom field (and will get an error
45        # when we use a lookup type that does not make sense).
46        s1 = Small(1, 3)
47        s2 = Small("a", "b")
48        self.assertQuerysetEqual(
49            MyModel.objects.filter(data__in=[s, s1, s2]), [
50                "m",
51            ],
52            lambda m: m.name,
53        )
54        self.assertRaises(TypeError, lambda: MyModel.objects.filter(data__lt=s))
55
56        # Serialization works, too.
57        stream = serializers.serialize("json", MyModel.objects.all())
58        self.assertEqual(stream, '[{"pk": %d, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]' % m1.pk)
59
60        obj = list(serializers.deserialize("json", stream))[0]
61        self.assertEqual(obj.object, m)
62
63        # Test retrieving custom field data
64        m.delete()
65
66        m1 = MyModel.objects.create(name="1", data=Small(1, 2))
67        m2 = MyModel.objects.create(name="2", data=Small(2, 3))
68
69        self.assertQuerysetEqual(
70            MyModel.objects.all(), [
71                "12",
72                "23",
73            ],
74            lambda m: str(m.data)
75        )
76
77    def test_field_subclassing(self):
78        o = OtherModel.objects.create(data=Small("a", "b"))
79        o = OtherModel.objects.get()
80        self.assertEqual(o.data.first, "a")
81        self.assertEqual(o.data.second, "b")