Tests annotation DurationField ¶
Contents
Classe Ticket ¶
1 class Ticket(models.Model):
2 active_at = models.DateTimeField()
3 duration = models.DurationField()
4
5 def __str__(self):
6 return '{} - {}'.format(self.active_at, self.duration)
class NonAggregateAnnotationTestCase(TestCase) ¶
1 import datetime
2 from decimal import Decimal
3
4 from django.core.exceptions import FieldDoesNotExist, FieldError
5 from django.db.models import (
6 BooleanField, CharField, Count, DateTimeField, ExpressionWrapper, F, Func,
7 IntegerField, NullBooleanField, OuterRef, Q, Subquery, Sum, Value,
8 )
9 from django.db.models.expressions import RawSQL
10 from django.db.models.functions import Length, Lower
11 from django.test import TestCase, skipUnlessDBFeature
12
13 from .models import (
14 Author, Book, Company, DepartmentStore, Employee, Publisher, Store, Ticket,
15 )
16
17
18 def cxOracle_py3_bug(func):
19 """
20 There's a bug in Django/cx_Oracle with respect to string handling under
21 Python 3 (essentially, they treat Python 3 strings as Python 2 strings
22 rather than unicode). This makes some tests here fail under Python 3, so
23 we mark them as expected failures until someone fixes them in #23843.
24 """
25 from unittest import expectedFailure
26 from django.db import connection
27 return expectedFailure(func) if connection.vendor == 'oracle' else func
28
29
30 class NonAggregateAnnotationTestCase(TestCase):
31
32 @classmethod
33 def setUpTestData(cls):
34 cls.a1 = Author.objects.create(name='Adrian Holovaty', age=34)
35 cls.a2 = Author.objects.create(name='Jacob Kaplan-Moss', age=35)
36 cls.a3 = Author.objects.create(name='Brad Dayley', age=45)
37 cls.a4 = Author.objects.create(name='James Bennett', age=29)
38 cls.a5 = Author.objects.create(name='Jeffrey Forcier', age=37)
39 cls.a6 = Author.objects.create(name='Paul Bissex', age=29)
40 cls.a7 = Author.objects.create(name='Wesley J. Chun', age=25)
41 cls.a8 = Author.objects.create(name='Peter Norvig', age=57)
42 cls.a9 = Author.objects.create(name='Stuart Russell', age=46)
test_mixed_type_annotation_date_interval ¶
1 def test_mixed_type_annotation_date_interval(self):
2 active = datetime.datetime(2015, 3, 20, 14, 0, 0)
3 duration = datetime.timedelta(hours=1)
4 expires = datetime.datetime(2015, 3, 20, 14, 0, 0) + duration
5 Ticket.objects.create(active_at=active, duration=duration)
6 t = Ticket.objects.annotate(
7 expires=ExpressionWrapper(F('active_at') + F('duration'), output_field=DateTimeField())
8 ).first()
9 self.assertEqual(t.expires, expires)