diff options
| author | Alex Hill <alex@hill.net.au> | 2015-08-04 00:34:19 +1000 |
|---|---|---|
| committer | Josh Smeaton <josh.smeaton@gmail.com> | 2015-09-22 23:35:24 +1000 |
| commit | 134ca4d438bd7cbe8f0f287a00d545f96fa04a01 (patch) | |
| tree | 075ce7ffcc95819a0584e80f6611462894faea0c /tests | |
| parent | 6e51d5d0e531c6aead9ebd638a63ffdc32245e5a (diff) | |
Fixed #24509 -- Added Expression support to SQLInsertCompiler
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/bulk_create/tests.py | 11 | ||||
| -rw-r--r-- | tests/expressions/tests.py | 43 | ||||
| -rw-r--r-- | tests/model_fields/tests.py | 7 |
3 files changed, 59 insertions, 2 deletions
diff --git a/tests/bulk_create/tests.py b/tests/bulk_create/tests.py index 3a0c654112..ce069504d0 100644 --- a/tests/bulk_create/tests.py +++ b/tests/bulk_create/tests.py @@ -3,6 +3,8 @@ from __future__ import unicode_literals from operator import attrgetter from django.db import connection +from django.db.models import Value +from django.db.models.functions import Lower from django.test import ( TestCase, override_settings, skipIfDBFeature, skipUnlessDBFeature, ) @@ -183,3 +185,12 @@ class BulkCreateTests(TestCase): TwoFields.objects.all().delete() with self.assertNumQueries(1): TwoFields.objects.bulk_create(objs, len(objs)) + + @skipUnlessDBFeature('has_bulk_insert') + def test_bulk_insert_expressions(self): + Restaurant.objects.bulk_create([ + Restaurant(name="Sam's Shake Shack"), + Restaurant(name=Lower(Value("Betty's Beetroot Bar"))) + ]) + bbb = Restaurant.objects.filter(name="betty's beetroot bar") + self.assertEqual(bbb.count(), 1) 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") diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index 3d791980ba..23e90cccb8 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -98,8 +98,13 @@ class BasicFieldTests(test.TestCase): self.assertTrue(instance.id) # Set field to object on saved instance instance.size = instance + msg = ( + "Tried to update field model_fields.FloatModel.size with a model " + "instance, <FloatModel: FloatModel object>. Use a value " + "compatible with FloatField." + ) with transaction.atomic(): - with self.assertRaises(TypeError): + with self.assertRaisesMessage(TypeError, msg): instance.save() # Try setting field to object on retrieved object obj = FloatModel.objects.get(pk=instance.id) |
