2019-08-20 07:54:41 +00:00
|
|
|
from django.db.models import F, Sum
|
2016-08-15 01:35:12 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from .models import Company, Employee
|
|
|
|
|
|
|
|
|
|
|
|
class ValuesExpressionsTests(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
Company.objects.create(
|
2022-02-03 19:24:19 +00:00
|
|
|
name="Example Inc.",
|
|
|
|
num_employees=2300,
|
|
|
|
num_chairs=5,
|
|
|
|
ceo=Employee.objects.create(firstname="Joe", lastname="Smith", salary=10),
|
2016-08-15 01:35:12 +00:00
|
|
|
)
|
|
|
|
Company.objects.create(
|
2022-02-03 19:24:19 +00:00
|
|
|
name="Foobar Ltd.",
|
|
|
|
num_employees=3,
|
|
|
|
num_chairs=4,
|
|
|
|
ceo=Employee.objects.create(firstname="Frank", lastname="Meyer", salary=20),
|
2016-08-15 01:35:12 +00:00
|
|
|
)
|
|
|
|
Company.objects.create(
|
2022-02-03 19:24:19 +00:00
|
|
|
name="Test GmbH",
|
|
|
|
num_employees=32,
|
|
|
|
num_chairs=1,
|
|
|
|
ceo=Employee.objects.create(
|
|
|
|
firstname="Max", lastname="Mustermann", salary=30
|
|
|
|
),
|
2016-08-15 01:35:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_values_expression(self):
|
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Company.objects.values(salary=F("ceo__salary")),
|
|
|
|
[{"salary": 10}, {"salary": 20}, {"salary": 30}],
|
2016-08-15 01:35:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_values_expression_group_by(self):
|
|
|
|
# values() applies annotate() first, so values selected are grouped by
|
|
|
|
# id, not firstname.
|
2022-02-03 19:24:19 +00:00
|
|
|
Employee.objects.create(firstname="Joe", lastname="Jones", salary=2)
|
|
|
|
joes = Employee.objects.filter(firstname="Joe")
|
2016-08-15 01:35:12 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
joes.values("firstname", sum_salary=Sum("salary")).order_by("sum_salary"),
|
|
|
|
[
|
|
|
|
{"firstname": "Joe", "sum_salary": 2},
|
|
|
|
{"firstname": "Joe", "sum_salary": 10},
|
|
|
|
],
|
2016-08-15 01:35:12 +00:00
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
joes.values("firstname").annotate(sum_salary=Sum("salary")),
|
|
|
|
[{"firstname": "Joe", "sum_salary": 12}],
|
2016-08-15 01:35:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_chained_values_with_expression(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
Employee.objects.create(firstname="Joe", lastname="Jones", salary=2)
|
|
|
|
joes = Employee.objects.filter(firstname="Joe").values("firstname")
|
2016-08-15 01:35:12 +00:00
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
joes.values("firstname", sum_salary=Sum("salary")),
|
|
|
|
[{"firstname": "Joe", "sum_salary": 12}],
|
2016-08-15 01:35:12 +00:00
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
joes.values(sum_salary=Sum("salary")), [{"sum_salary": 12}]
|
2016-08-15 01:35:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_values_list_expression(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
companies = Company.objects.values_list("name", F("ceo__salary"))
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
companies, [("Example Inc.", 10), ("Foobar Ltd.", 20), ("Test GmbH", 30)]
|
|
|
|
)
|
2016-08-15 01:35:12 +00:00
|
|
|
|
|
|
|
def test_values_list_expression_flat(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
companies = Company.objects.values_list(F("ceo__salary"), flat=True)
|
2016-08-15 01:35:12 +00:00
|
|
|
self.assertSequenceEqual(companies, (10, 20, 30))
|