summaryrefslogtreecommitdiff
path: root/tests/expressions
diff options
context:
space:
mode:
Diffstat (limited to 'tests/expressions')
-rw-r--r--tests/expressions/tests.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
index a18084e06f..1af0b6e7a2 100644
--- a/tests/expressions/tests.py
+++ b/tests/expressions/tests.py
@@ -249,6 +249,32 @@ class BasicExpressionsTests(TestCase):
test_gmbh = Company.objects.get(pk=test_gmbh.pk)
self.assertEqual(test_gmbh.num_employees, 36)
+ def test_new_object_save(self):
+ # We should be able to use Funcs when inserting new data
+ test_co = Company(
+ name=Lower(Value("UPPER")), num_employees=32, num_chairs=1,
+ ceo=Employee.objects.create(firstname="Just", lastname="Doit", salary=30),
+ )
+ test_co.save()
+ test_co.refresh_from_db()
+ self.assertEqual(test_co.name, "upper")
+
+ def test_new_object_create(self):
+ test_co = Company.objects.create(
+ name=Lower(Value("UPPER")), num_employees=32, num_chairs=1,
+ ceo=Employee.objects.create(firstname="Just", lastname="Doit", salary=30),
+ )
+ test_co.refresh_from_db()
+ self.assertEqual(test_co.name, "upper")
+
+ def test_object_create_with_aggregate(self):
+ # Aggregates are not allowed when inserting new data
+ with self.assertRaisesMessage(FieldError, 'Aggregate functions are not allowed in this query'):
+ Company.objects.create(
+ name='Company', num_employees=Max(Value(1)), num_chairs=1,
+ ceo=Employee.objects.create(firstname="Just", lastname="Doit", salary=30),
+ )
+
def test_object_update_fk(self):
# F expressions cannot be used to update attributes which are foreign
# keys, or attributes which involve joins.
@@ -272,7 +298,22 @@ class BasicExpressionsTests(TestCase):
ceo=test_gmbh.ceo
)
acme.num_employees = F("num_employees") + 16
- self.assertRaises(TypeError, acme.save)
+ msg = (
+ 'Failed to insert expression "Col(expressions_company, '
+ 'expressions.Company.num_employees) + Value(16)" on '
+ 'expressions.Company.num_employees. F() expressions can only be '
+ 'used to update, not to insert.'
+ )
+ self.assertRaisesMessage(ValueError, msg, acme.save)
+
+ acme.num_employees = 12
+ acme.name = Lower(F('name'))
+ msg = (
+ 'Failed to insert expression "Lower(Col(expressions_company, '
+ 'expressions.Company.name))" on expressions.Company.name. F() '
+ 'expressions can only be used to update, not to insert.'
+ )
+ self.assertRaisesMessage(ValueError, msg, acme.save)
def test_ticket_11722_iexact_lookup(self):
Employee.objects.create(firstname="John", lastname="Doe")