Test postgres field mapping

This commit is contained in:
Ryan P Kilby 2017-12-05 00:34:43 -05:00
parent 7855d3bd8b
commit 7df7e252f6
2 changed files with 50 additions and 1 deletions

View File

@ -1,5 +1,6 @@
# Optional packages which may be used with REST framework. # Optional packages which may be used with REST framework.
pytz==2017.2 pytz==2017.2
psycopg2==2.7.3
markdown==2.6.4 markdown==2.6.4
django-guardian==1.4.9 django-guardian==1.4.9
django-filter==1.1.0 django-filter==1.1.0

View File

@ -21,7 +21,7 @@ from django.test import TestCase
from django.utils import six from django.utils import six
from rest_framework import serializers from rest_framework import serializers
from rest_framework.compat import unicode_repr from rest_framework.compat import postgres_fields, unicode_repr
def dedent(blocktext): def dedent(blocktext):
@ -379,6 +379,54 @@ class TestGenericIPAddressFieldValidation(TestCase):
'{0}'.format(s.errors)) '{0}'.format(s.errors))
@pytest.mark.skipUnless(postgres_fields, 'postgres is required')
class TestPosgresFieldsMapping(TestCase):
def test_hstore_field(self):
class HStoreFieldModel(models.Model):
hstore_field = postgres_fields.HStoreField()
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = HStoreFieldModel
fields = ['hstore_field']
expected = dedent("""
TestSerializer():
hstore_field = CharMappingField()
""")
self.assertEqual(unicode_repr(TestSerializer()), expected)
def test_array_field(self):
class ArrayFieldModel(models.Model):
array_field = postgres_fields.ArrayField(base_field=models.CharField())
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = ArrayFieldModel
fields = ['array_field']
expected = dedent("""
TestSerializer():
array_field = ListField(child=CharField(label='Array field', validators=[<django.core.validators.MaxLengthValidator object>]))
""")
self.assertEqual(unicode_repr(TestSerializer()), expected)
def test_json_field(self):
class JSONFieldModel(models.Model):
json_field = postgres_fields.JSONField()
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = JSONFieldModel
fields = ['json_field']
expected = dedent("""
TestSerializer():
json_field = JSONField(style={'base_template': 'textarea.html'})
""")
self.assertEqual(unicode_repr(TestSerializer()), expected)
# Tests for relational field mappings. # Tests for relational field mappings.
# ------------------------------------ # ------------------------------------