~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/field_subclassing/tests.py

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2014-09-17 14:15:11 UTC
  • mfrom: (1.3.17) (6.2.18 experimental)
  • Revision ID: package-import@ubuntu.com-20140917141511-icneokthe9ww5sk4
Tags: 1.7-2
* Release to unstable.
* Add a migrate-south sample script to help users apply their South
  migrations. Thanks to Brian May.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from __future__ import absolute_import
2
 
 
3
 
from django.core import serializers
 
1
from __future__ import unicode_literals
 
2
 
 
3
import inspect
 
4
 
 
5
from django.core import exceptions, serializers
 
6
from django.db import connection
4
7
from django.test import TestCase
5
8
 
6
 
from .fields import Small
7
 
from .models import DataModel, MyModel, OtherModel
 
9
from .fields import Small, CustomTypedField
 
10
from .models import ChoicesModel, DataModel, MyModel, OtherModel
8
11
 
9
12
 
10
13
class CustomField(TestCase):
37
40
        # Custom fields still have normal field's attributes.
38
41
        self.assertEqual(m._meta.get_field("data").verbose_name, "small field")
39
42
 
40
 
        # The m.data attribute has been initialised correctly. It's a Small
 
43
        # The m.data attribute has been initialized correctly. It's a Small
41
44
        # object.
42
45
        self.assertEqual((m.data.first, m.data.second), (1, 2))
43
46
 
74
77
        m.delete()
75
78
 
76
79
        m1 = MyModel.objects.create(name="1", data=Small(1, 2))
77
 
        m2 = MyModel.objects.create(name="2", data=Small(2, 3))
 
80
        MyModel.objects.create(name="2", data=Small(2, 3))
78
81
 
79
82
        self.assertQuerysetEqual(
80
83
            MyModel.objects.all(), [
90
93
        o = OtherModel.objects.get()
91
94
        self.assertEqual(o.data.first, "a")
92
95
        self.assertEqual(o.data.second, "b")
 
96
 
 
97
    def test_subfieldbase_plays_nice_with_module_inspect(self):
 
98
        """
 
99
        Custom fields should play nice with python standard module inspect.
 
100
 
 
101
        http://users.rcn.com/python/download/Descriptor.htm#properties
 
102
        """
 
103
        # Even when looking for totally different properties, SubfieldBase's
 
104
        # non property like behavior made inspect crash. Refs #12568.
 
105
        data = dict(inspect.getmembers(MyModel))
 
106
        self.assertIn('__module__', data)
 
107
        self.assertEqual(data['__module__'], 'field_subclassing.models')
 
108
 
 
109
    def test_validation_of_choices_for_custom_field(self):
 
110
        # a valid choice
 
111
        o = ChoicesModel.objects.create(data=Small('a', 'b'))
 
112
        o.full_clean()
 
113
 
 
114
        # an invalid choice
 
115
        o = ChoicesModel.objects.create(data=Small('d', 'e'))
 
116
        with self.assertRaises(exceptions.ValidationError):
 
117
            o.full_clean()
 
118
 
 
119
 
 
120
class TestDbType(TestCase):
 
121
 
 
122
    def test_db_parameters_respects_db_type(self):
 
123
        f = CustomTypedField()
 
124
        self.assertEqual(f.db_parameters(connection)['type'], 'custom_field')