Ticket #30246: test_regression.py

File test_regression.py, 3.9 KB (added by Jan Baryła, 6 years ago)
Line 
1import datetime
2from decimal import Decimal
3from django.db.models import (
4 Q, Sum,
5)
6from django.db.models.functions import Extract
7from django.test import TestCase
8
9from .models import (
10 Author, Book, Publisher, Store
11)
12
13class DateTimesTests(TestCase):
14 @classmethod
15 def setUpTestData(cls):
16 cls.a1 = Author.objects.create(name='Adrian Holovaty', age=34)
17 cls.a2 = Author.objects.create(name='Jacob Kaplan-Moss', age=35)
18 cls.a3 = Author.objects.create(name='Brad Dayley', age=45)
19 cls.a4 = Author.objects.create(name='James Bennett', age=29)
20 cls.a5 = Author.objects.create(name='Jeffrey Forcier', age=37)
21 cls.a6 = Author.objects.create(name='Paul Bissex', age=29)
22 cls.a7 = Author.objects.create(name='Wesley J. Chun', age=25)
23 cls.a8 = Author.objects.create(name='Peter Norvig', age=57)
24 cls.a9 = Author.objects.create(name='Stuart Russell', age=46)
25 cls.a1.friends.add(cls.a2, cls.a4)
26 cls.a2.friends.add(cls.a1, cls.a7)
27 cls.a4.friends.add(cls.a1)
28 cls.a5.friends.add(cls.a6, cls.a7)
29 cls.a6.friends.add(cls.a5, cls.a7)
30 cls.a7.friends.add(cls.a2, cls.a5, cls.a6)
31 cls.a8.friends.add(cls.a9)
32 cls.a9.friends.add(cls.a8)
33
34 cls.p1 = Publisher.objects.create(name='Apress', num_awards=3)
35 cls.p2 = Publisher.objects.create(name='Sams', num_awards=1)
36 cls.p3 = Publisher.objects.create(name='Prentice Hall', num_awards=7)
37 cls.p4 = Publisher.objects.create(name='Morgan Kaufmann', num_awards=9)
38 cls.p5 = Publisher.objects.create(name="Jonno's House of Books", num_awards=0)
39
40 cls.b1 = Book.objects.create(
41 isbn='159059725', name='The Definitive Guide to Django: Web Development Done Right',
42 pages=447, rating=4.5, price=Decimal('30.00'), contact=cls.a1, publisher=cls.p1,
43 pubdate=datetime.date(2007, 12, 6)
44 )
45 cls.b2 = Book.objects.create(
46 isbn='067232959', name='Sams Teach Yourself Django in 24 Hours',
47 pages=528, rating=3.0, price=Decimal('23.09'), contact=cls.a3, publisher=cls.p2,
48 pubdate=datetime.date(2008, 3, 3)
49 )
50 cls.b3 = Book.objects.create(
51 isbn='159059996', name='Practical Django Projects',
52 pages=300, rating=4.0, price=Decimal('29.69'), contact=cls.a4, publisher=cls.p1,
53 pubdate=datetime.date(2008, 6, 23)
54 )
55 cls.b4 = Book.objects.create(
56 isbn='013235613', name='Python Web Development with Django',
57 pages=350, rating=4.0, price=Decimal('29.69'), contact=cls.a5, publisher=cls.p3,
58 pubdate=datetime.date(2008, 11, 3)
59 )
60
61 cls.b1.authors.add(cls.a1, cls.a2)
62 cls.b2.authors.add(cls.a3)
63 cls.b3.authors.add(cls.a4)
64 cls.b4.authors.add(cls.a5, cls.a6, cls.a7)
65
66 s1 = Store.objects.create(
67 name='Amazon.com',
68 original_opening=datetime.datetime(1994, 4, 23, 9, 17, 42),
69 friday_night_closing=datetime.time(23, 59, 59)
70 )
71 s2 = Store.objects.create(
72 name='Books.com',
73 original_opening=datetime.datetime(2001, 3, 15, 11, 23, 37),
74 friday_night_closing=datetime.time(23, 59, 59)
75 )
76 s3 = Store.objects.create(
77 name="Mamma and Pappa's Books",
78 original_opening=datetime.datetime(1945, 4, 25, 16, 24, 14),
79 friday_night_closing=datetime.time(21, 30)
80 )
81 s1.books.add(cls.b1, cls.b2, cls.b3, cls.b4,)
82 s2.books.add(cls.b1, cls.b3)
83 s3.books.add(cls.b3, cls.b4)
84
85 def test_annotation_with_value(self):
86 hour_aggregates = {}
87 for i in range(24):
88 hour_aggregates['{}_{}'.format("am" if i < 12 else "pm", i)] = Sum("books__pages", filter=Q(hour=i))
89 usages = Store.objects.annotate(hour=Extract("friday_night_closing", "hour")).aggregate(**hour_aggregates)
90 self.assertEqual(usages['am_0'], 3022)
Back to Top