Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Migrated the field_subclsasing doctests. Thanks to Alex Gaynor.

Backport of r13780 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13797 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 106600edb63cdebf5764a25a6fa5cb1ac7117c4c 1 parent f7f2c72
Russell Keith-Magee authored September 12, 2010
8  tests/modeltests/field_subclassing/fields.py
@@ -53,18 +53,18 @@ def get_prep_lookup(self, lookup_type, value):
53 53
 
54 54
 class JSONField(models.TextField):
55 55
     __metaclass__ = models.SubfieldBase
56  
-    
  56
+
57 57
     description = ("JSONField automatically serializes and desializes values to "
58 58
         "and from JSON.")
59  
-    
  59
+
60 60
     def to_python(self, value):
61 61
         if not value:
62 62
             return None
63  
-        
  63
+
64 64
         if isinstance(value, basestring):
65 65
             value = json.loads(value)
66 66
         return value
67  
-    
  67
+
68 68
     def get_db_prep_save(self, value):
69 69
         if value is None:
70 70
             return None
54  tests/modeltests/field_subclassing/models.py
@@ -2,7 +2,6 @@
2 2
 Tests for field subclassing.
3 3
 """
4 4
 
5  
-from django.core import serializers
6 5
 from django.db import models
7 6
 from django.utils.encoding import force_unicode
8 7
 
@@ -18,56 +17,3 @@ def __unicode__(self):
18 17
 
19 18
 class DataModel(models.Model):
20 19
     data = JSONField()
21  
-
22  
-__test__ = {'API_TESTS': ur"""
23  
-# Creating a model with custom fields is done as per normal.
24  
->>> s = Small(1, 2)
25  
->>> print s
26  
-12
27  
->>> m = MyModel(name='m', data=s)
28  
->>> m.save()
29  
-
30  
-# Custom fields still have normal field's attributes.
31  
->>> m._meta.get_field('data').verbose_name
32  
-'small field'
33  
-
34  
-# The m.data attribute has been initialised correctly. It's a Small object.
35  
->>> m.data.first, m.data.second
36  
-(1, 2)
37  
-
38  
-# The data loads back from the database correctly and 'data' has the right type.
39  
->>> m1 = MyModel.objects.get(pk=m.pk)
40  
->>> isinstance(m1.data, Small)
41  
-True
42  
->>> print m1.data
43  
-12
44  
-
45  
-# We can do normal filtering on the custom field (and will get an error when we
46  
-# use a lookup type that does not make sense).
47  
->>> s1 = Small(1, 3)
48  
->>> s2 = Small('a', 'b')
49  
->>> MyModel.objects.filter(data__in=[s, s1, s2])
50  
-[<MyModel: m>]
51  
->>> MyModel.objects.filter(data__lt=s)
52  
-Traceback (most recent call last):
53  
-...
54  
-TypeError: Invalid lookup type: 'lt'
55  
-
56  
-# Serialization works, too.
57  
->>> stream = serializers.serialize("json", MyModel.objects.all())
58  
->>> stream
59  
-'[{"pk": 1, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]'
60  
->>> obj = list(serializers.deserialize("json", stream))[0]
61  
->>> obj.object == m
62  
-True
63  
-
64  
-# Test retrieving custom field data
65  
->>> m.delete()
66  
->>> m1 = MyModel(name="1", data=Small(1, 2))
67  
->>> m1.save()
68  
->>> m2 = MyModel(name="2", data=Small(2, 3))
69  
->>> m2.save()
70  
->>> for m in MyModel.objects.all(): print unicode(m.data)
71  
-12
72  
-23
73  
-"""}
64  tests/modeltests/field_subclassing/tests.py
... ...
@@ -1,21 +1,75 @@
  1
+from django.core import serializers
1 2
 from django.test import TestCase
2 3
 
3  
-from models import DataModel
  4
+from fields import Small
  5
+from models import DataModel, MyModel
4 6
 
5 7
 
6 8
 class CustomField(TestCase):
7 9
     def test_defer(self):
8 10
         d = DataModel.objects.create(data=[1, 2, 3])
9  
-        
  11
+
10 12
         self.assertTrue(isinstance(d.data, list))
11  
-        
  13
+
12 14
         d = DataModel.objects.get(pk=d.pk)
13 15
         self.assertTrue(isinstance(d.data, list))
14 16
         self.assertEqual(d.data, [1, 2, 3])
15  
-        
  17
+
16 18
         d = DataModel.objects.defer("data").get(pk=d.pk)
17 19
         d.save()
18  
-        
  20
+
19 21
         d = DataModel.objects.get(pk=d.pk)
20 22
         self.assertTrue(isinstance(d.data, list))
21 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": 1, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]')
  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
+        )

0 notes on commit 106600e

Please sign in to comment.
Something went wrong with that request. Please try again.