Commit e445649e authored by Nicolas Noirbent's avatar Nicolas Noirbent

tests: add DRF timestamp filter tests

parent 73b732ef
[
{
"model": "tests.TestModel",
"pk": 1,
"fields": {
"id": 1,
"created": "2019-02-01T15:00:00+00:00",
"label": "toto"
}
},
{
"model": "tests.TestModel",
"pk": 2,
"fields": {
"id": 2,
"created": "2019-02-01T15:00:00+00:00",
"label": "titi"
}
},
{
"model": "tests.TestModel",
"pk": 3,
"fields": {
"id": 3,
"created": "2019-02-01T15:01:00+00:00",
"label": "tata"
}
}
]
from django.db import models
class TestModel(models.Model):
label = models.CharField(max_length=20)
created = models.DateTimeField(auto_now_add=True)
import datetime
from django.core.exceptions import ValidationError
from django.test import TestCase
from django_filters import rest_framework as filters
from hexack.utils.drf.filters import TimestampField, TimestampFilter
from hexack.utils.drf.serializers import TimestampField as SerializerTimestampField
from .models import TestModel
class SerializerTimestampFieldTestCase(TestCase):
......@@ -17,3 +21,63 @@ class SerializerTimestampFieldTestCase(TestCase):
test_timestamp = 1547814896.0
self.assertEqual(field.to_internal_value(test_timestamp),
datetime.datetime(2019, 1, 18, 12, 34, 56, tzinfo=datetime.timezone.utc))
class FilterTimestampFieldTestCase(TestCase):
def test_to_python(self):
field = TimestampField()
self.assertEqual(field.to_python(1549033200.0),
datetime.datetime(2019, 2, 1, 15, tzinfo=datetime.timezone.utc))
self.assertEqual(field.to_python(None), None)
with self.assertRaises(ValidationError) as exc_cm:
field.to_python('NaN')
self.assertEqual(exc_cm.exception.message, 'Enter a number.')
self.assertEqual(exc_cm.exception.code, 'invalid')
with self.assertRaises(ValidationError) as exc_cm:
field.to_python(-1)
self.assertEqual(exc_cm.exception.message, 'Enter a positive number.')
self.assertEqual(exc_cm.exception.code, 'positive')
def test_validate(self):
field = TimestampField(required=False)
self.assertIs(field.validate(None), None)
field = TimestampField(required=True)
with self.assertRaises(ValidationError) as exc_cm:
field.validate(None)
self.assertEqual(exc_cm.exception.code, 'required')
class TimestampFilterTestCase(TestCase):
fixtures = ['test_model']
def test_filter(self):
class MyFilterSet(filters.FilterSet):
created = TimestampFilter()
class Meta:
model = TestModel
fields = ['created']
filterset = MyFilterSet(data={'created': 1549033200.0})
self.assertTrue(filterset.is_valid())
queryset = filterset.filter_queryset(TestModel.objects.all())
self.assertEqual(queryset.count(), 2)
queryset = queryset.order_by('label')
self.assertCountEqual(queryset.values_list('id', flat=True), [2, 1])
filterset = MyFilterSet(data={'created': 1549033260.0})
self.assertTrue(filterset.is_valid())
queryset = filterset.filter_queryset(TestModel.objects.all())
self.assertEqual(queryset.count(), 1)
result = queryset.first()
self.assertEqual(result.id, 3)
self.assertEqual(result.label, 'tata')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment