summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJosh Smeaton <josh.smeaton@gmail.com>2015-03-19 14:07:53 +1100
committerJosh Smeaton <josh.smeaton@gmail.com>2015-03-22 17:36:55 +1100
commit02a2943e4cf95dea0294e8695cfe981f34ae6180 (patch)
treeda87f399010a9c58b013a869af0ba5ba4e14879e /tests
parent127b3873d03704f77428b984de022664b268314e (diff)
Fixed #24485 -- Allowed combined expressions to set output_field
Diffstat (limited to 'tests')
-rw-r--r--tests/annotations/models.py9
-rw-r--r--tests/annotations/tests.py23
-rw-r--r--tests/expressions/tests.py8
3 files changed, 36 insertions, 4 deletions
diff --git a/tests/annotations/models.py b/tests/annotations/models.py
index 0c438b99f8..954c8ad339 100644
--- a/tests/annotations/models.py
+++ b/tests/annotations/models.py
@@ -84,3 +84,12 @@ class Company(models.Model):
return ('Company(name=%s, motto=%s, ticker_name=%s, description=%s)'
% (self.name, self.motto, self.ticker_name, self.description)
)
+
+
+@python_2_unicode_compatible
+class Ticket(models.Model):
+ active_at = models.DateTimeField()
+ duration = models.DurationField()
+
+ def __str__(self):
+ return '{} - {}'.format(self.active_at, self.duration)
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py
index 45da5e49de..1230d5c52a 100644
--- a/tests/annotations/tests.py
+++ b/tests/annotations/tests.py
@@ -5,13 +5,14 @@ from decimal import Decimal
from django.core.exceptions import FieldDoesNotExist, FieldError
from django.db.models import (
- F, BooleanField, CharField, Count, Func, IntegerField, Sum, Value,
+ F, BooleanField, CharField, Count, DateTimeField, ExpressionWrapper, Func,
+ IntegerField, Sum, Value,
)
from django.test import TestCase
from django.utils import six
from .models import (
- Author, Book, Company, DepartmentStore, Employee, Publisher, Store,
+ Author, Book, Company, DepartmentStore, Employee, Publisher, Store, Ticket,
)
@@ -135,6 +136,24 @@ class NonAggregateAnnotationTestCase(TestCase):
for book in books:
self.assertEqual(book.num_awards, book.publisher.num_awards)
+ def test_mixed_type_annotation_date_interval(self):
+ active = datetime.datetime(2015, 3, 20, 14, 0, 0)
+ duration = datetime.timedelta(hours=1)
+ expires = datetime.datetime(2015, 3, 20, 14, 0, 0) + duration
+ Ticket.objects.create(active_at=active, duration=duration)
+ t = Ticket.objects.annotate(
+ expires=ExpressionWrapper(F('active_at') + F('duration'), output_field=DateTimeField())
+ ).first()
+ self.assertEqual(t.expires, expires)
+
+ def test_mixed_type_annotation_numbers(self):
+ test = self.b1
+ b = Book.objects.annotate(
+ combined=ExpressionWrapper(F('pages') + F('rating'), output_field=IntegerField())
+ ).get(isbn=test.isbn)
+ combined = int(test.pages + test.rating)
+ self.assertEqual(b.combined, combined)
+
def test_annotate_with_aggregation(self):
books = Book.objects.annotate(
is_book=Value(1, output_field=IntegerField()),
diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
index ae2134e964..3e26be6d0b 100644
--- a/tests/expressions/tests.py
+++ b/tests/expressions/tests.py
@@ -11,8 +11,8 @@ from django.db.models.aggregates import (
Avg, Count, Max, Min, StdDev, Sum, Variance,
)
from django.db.models.expressions import (
- F, Case, Col, Date, DateTime, Func, OrderBy, Random, RawSQL, Ref, Value,
- When,
+ F, Case, Col, Date, DateTime, ExpressionWrapper, Func, OrderBy, Random,
+ RawSQL, Ref, Value, When,
)
from django.db.models.functions import (
Coalesce, Concat, Length, Lower, Substr, Upper,
@@ -855,6 +855,10 @@ class ReprTests(TestCase):
self.assertEqual(repr(DateTime('published', 'exact', utc)), "DateTime(published, exact, %s)" % utc)
self.assertEqual(repr(F('published')), "F(published)")
self.assertEqual(repr(F('cost') + F('tax')), "<CombinedExpression: F(cost) + F(tax)>")
+ self.assertEqual(
+ repr(ExpressionWrapper(F('cost') + F('tax'), models.IntegerField())),
+ "ExpressionWrapper(F(cost) + F(tax))"
+ )
self.assertEqual(repr(Func('published', function='TO_CHAR')), "Func(F(published), function=TO_CHAR)")
self.assertEqual(repr(OrderBy(Value(1))), 'OrderBy(Value(1), descending=False)')
self.assertEqual(repr(Random()), "Random()")