Ticket #24171: 24171-test.diff

File 24171-test.diff, 2.6 KB (added by Tim Graham, 10 years ago)
  • tests/expressions/models.py

    diff --git a/tests/expressions/models.py b/tests/expressions/models.py
    index 69de52c..1c1e924 100644
    a b from django.utils.encoding import python_2_unicode_compatible  
    1212class Employee(models.Model):
    1313    firstname = models.CharField(max_length=50)
    1414    lastname = models.CharField(max_length=50)
     15    salary = models.IntegerField(blank=True, null=True)
    1516
    1617    def __str__(self):
    1718        return '%s %s' % (self.firstname, self.lastname)
  • tests/expressions/tests.py

    diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
    index 7233fa0..e3073a4 100644
    a b import uuid  
    55from copy import deepcopy
    66
    77from django.core.exceptions import FieldError
    8 from django.db import DatabaseError, connection, transaction
     8from django.db import DatabaseError, connection, models, transaction
    99from django.db.models import TimeField, UUIDField
    1010from django.db.models.aggregates import (
    1111    Avg, Count, Max, Min, StdDev, Sum, Variance,
    class BasicExpressionsTests(TestCase):  
    3030    def setUpTestData(cls):
    3131        Company.objects.create(
    3232            name="Example Inc.", num_employees=2300, num_chairs=5,
    33             ceo=Employee.objects.create(firstname="Joe", lastname="Smith")
     33            ceo=Employee.objects.create(firstname="Joe", lastname="Smith", salary=10)
    3434        )
    3535        Company.objects.create(
    3636            name="Foobar Ltd.", num_employees=3, num_chairs=4,
    37             ceo=Employee.objects.create(firstname="Frank", lastname="Meyer")
     37            ceo=Employee.objects.create(firstname="Frank", lastname="Meyer", salary=20)
    3838        )
    3939        Company.objects.create(
    4040            name="Test GmbH", num_employees=32, num_chairs=1,
    41             ceo=Employee.objects.create(firstname="Max", lastname="Mustermann")
     41            ceo=Employee.objects.create(firstname="Max", lastname="Mustermann", salary=30)
    4242        )
    4343
    4444    def setUp(self):
    class BasicExpressionsTests(TestCase):  
    4848            "name", "num_employees", "num_chairs"
    4949        )
    5050
     51    def test_annotate_values_aggregate(self):
     52        companies = Company.objects.annotate(
     53            salaries=F('ceo__salary'),
     54        ).values('num_employees', 'salaries').aggregate(
     55            result=Sum(F('salaries') + F('num_employees'),
     56            output_field=models.IntegerField()),
     57        )
     58        self.assertEqual(companies['result'], 60)  # 2395 without values
     59
    5160    def test_filter_inter_attribute(self):
    5261        # We can filter on attribute relationships on same model obj, e.g.
    5362        # find companies where the number of employees is greater
Back to Top