|
8 | 8 | from django.utils.dateparse import parse_time, parse_datetime |
9 | 9 |
|
10 | 10 | from django.db import models |
11 | | -from django.test import TestCase |
| 11 | +from django.test import TestCase, override_settings |
12 | 12 | from django.contrib.auth.models import User |
13 | 13 |
|
14 | 14 | from . import filters |
@@ -632,6 +632,35 @@ class Meta: |
632 | 632 | p = list(f)[0] |
633 | 633 | self.assertEqual(p.name, "John") |
634 | 634 |
|
| 635 | + @override_settings(USE_TZ=True) |
| 636 | + def test_datetime_timezone_awareness(self): |
| 637 | + # Addresses issue #24 - ensure that datetime strings terminating |
| 638 | + # in 'Z' are correctly handled. |
| 639 | + from rest_framework import serializers |
| 640 | + from rest_framework.renderers import JSONRenderer |
| 641 | + |
| 642 | + class PersonSerializer(serializers.ModelSerializer): |
| 643 | + class Meta: |
| 644 | + model = Person |
| 645 | + |
| 646 | + # Figure out what the date strings should look like based on the |
| 647 | + # serializer output. |
| 648 | + john = Person.objects.get(name="John") |
| 649 | + data = PersonSerializer(john).data |
| 650 | + datetime_str = JSONRenderer().render(parse_datetime(data['datetime_joined']) + datetime.timedelta(seconds=0.6)).decode('utf-8').strip('"') |
| 651 | + |
| 652 | + # This is more for documentation - DRF appends a 'Z' to timezone aware UTC datetimes when rendering: |
| 653 | + # https://github.com/tomchristie/django-rest-framework/blob/3.2.0/rest_framework/fields.py#L1002-L1006 |
| 654 | + self.assertTrue(datetime_str.endswith('Z')) |
| 655 | + |
| 656 | + GET = { |
| 657 | + 'datetime_joined__lte': datetime_str, |
| 658 | + } |
| 659 | + f = AllLookupsPersonDateFilter(GET, queryset=Person.objects.all()) |
| 660 | + self.assertEqual(len(list(f)), 1) |
| 661 | + p = list(f)[0] |
| 662 | + self.assertEqual(p.name, "John") |
| 663 | + |
635 | 664 | def test_inset_filter(self): |
636 | 665 | p1 = Person.objects.get(name="John").pk |
637 | 666 | p2 = Person.objects.get(name="Mark").pk |
|
0 commit comments