mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 10:03:57 +03:00
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
"""
|
|
General serializer field tests.
|
|
"""
|
|
|
|
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)
|
|
|
|
|
|
class CharPrimaryKeyModel(models.Model):
|
|
id = models.CharField(max_length=20, primary_key=True)
|
|
|
|
|
|
class TimestampedModelSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = TimestampedModel
|
|
|
|
|
|
class CharPrimaryKeyModelSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = CharPrimaryKeyModel
|
|
|
|
|
|
class ReadOnlyFieldTests(TestCase):
|
|
def test_auto_now_fields_read_only(self):
|
|
"""
|
|
auto_now and auto_now_add fields should be read_only by default.
|
|
"""
|
|
serializer = TimestampedModelSerializer()
|
|
self.assertEquals(serializer.fields['added'].read_only, True)
|
|
|
|
def test_auto_pk_fields_read_only(self):
|
|
"""
|
|
AutoField fields should be read_only by default.
|
|
"""
|
|
serializer = TimestampedModelSerializer()
|
|
self.assertEquals(serializer.fields['id'].read_only, True)
|
|
|
|
def test_non_auto_pk_fields_not_read_only(self):
|
|
"""
|
|
PK fields other than AutoField fields should not be read_only by default.
|
|
"""
|
|
serializer = CharPrimaryKeyModelSerializer()
|
|
self.assertEquals(serializer.fields['id'].read_only, False)
|