2013-01-12 13:32:53 +04:00
|
|
|
"""
|
2013-01-15 14:51:10 +04:00
|
|
|
General serializer field tests.
|
2013-01-12 13:32:53 +04:00
|
|
|
"""
|
2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2013-01-12 13:32:53 +04:00
|
|
|
from django.db import models
|
|
|
|
from django.test import TestCase
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
|
|
|
|
class TimestampedModel(models.Model):
|
|
|
|
added = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
|
2013-01-12 13:43:07 +04:00
|
|
|
class CharPrimaryKeyModel(models.Model):
|
|
|
|
id = models.CharField(max_length=20, primary_key=True)
|
|
|
|
|
|
|
|
|
2013-01-12 13:32:53 +04:00
|
|
|
class TimestampedModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = TimestampedModel
|
|
|
|
|
|
|
|
|
2013-01-12 13:43:07 +04:00
|
|
|
class CharPrimaryKeyModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = CharPrimaryKeyModel
|
|
|
|
|
|
|
|
|
2013-01-12 13:32:53 +04:00
|
|
|
class ReadOnlyFieldTests(TestCase):
|
|
|
|
def test_auto_now_fields_read_only(self):
|
|
|
|
"""
|
2013-01-15 14:51:10 +04:00
|
|
|
auto_now and auto_now_add fields should be read_only by default.
|
2013-01-12 13:32:53 +04:00
|
|
|
"""
|
|
|
|
serializer = TimestampedModelSerializer()
|
|
|
|
self.assertEquals(serializer.fields['added'].read_only, True)
|
2013-01-12 13:43:07 +04:00
|
|
|
|
|
|
|
def test_auto_pk_fields_read_only(self):
|
2013-01-15 14:51:10 +04:00
|
|
|
"""
|
|
|
|
AutoField fields should be read_only by default.
|
|
|
|
"""
|
2013-01-12 13:43:07 +04:00
|
|
|
serializer = TimestampedModelSerializer()
|
|
|
|
self.assertEquals(serializer.fields['id'].read_only, True)
|
|
|
|
|
|
|
|
def test_non_auto_pk_fields_not_read_only(self):
|
2013-01-15 14:51:10 +04:00
|
|
|
"""
|
|
|
|
PK fields other than AutoField fields should not be read_only by default.
|
|
|
|
"""
|
2013-01-12 13:43:07 +04:00
|
|
|
serializer = CharPrimaryKeyModelSerializer()
|
|
|
|
self.assertEquals(serializer.fields['id'].read_only, False)
|