diff --git a/tests/modeltests/expressions/fixtures/f_expression_testdata.json b/tests/modeltests/expressions/fixtures/f_expression_testdata.json new file mode 100644 index 0000000000..76fd1396e3 --- /dev/null +++ b/tests/modeltests/expressions/fixtures/f_expression_testdata.json @@ -0,0 +1,67 @@ +[ + { + "pk": 1, + "model": "expressions.employee", + "fields": { + "lastname": "Smith", + "firstname": "Joe" + } + }, + { + "pk": 2, + "model": "expressions.employee", + "fields": { + "lastname": "Meyer", + "firstname": "Frank" + } + }, + { + "pk": 3, + "model": "expressions.employee", + "fields": { + "lastname": "Mustermann", + "firstname": "Max" + } + }, + { + "pk": 4, + "model": "expressions.employee", + "fields": { + "lastname": "van Rossum", + "firstname": "Guido" + } + }, + { + "pk": 1, + "model": "expressions.company", + "fields": { + "ceo": 1, + "num_chairs": 5, + "point_of_contact": 4, + "name": "Example Inc.", + "num_employees": 2300 + } + }, + { + "pk": 2, + "model": "expressions.company", + "fields": { + "ceo": 2, + "num_chairs": 3, + "point_of_contact": 2, + "name": "Foobar Ltd.", + "num_employees": 3 + } + }, + { + "pk": 3, + "model": "expressions.company", + "fields": { + "ceo": 3, + "num_chairs": 1, + "point_of_contact": 3, + "name": "Test GmbH", + "num_employees": 32 + } + } +] diff --git a/tests/modeltests/expressions/models.py b/tests/modeltests/expressions/models.py index f6292f5d9b..b004408536 100644 --- a/tests/modeltests/expressions/models.py +++ b/tests/modeltests/expressions/models.py @@ -25,108 +25,3 @@ class Company(models.Model): def __unicode__(self): return self.name - - -__test__ = {'API_TESTS': """ ->>> from django.db.models import F - ->>> Company(name='Example Inc.', num_employees=2300, num_chairs=5, -... ceo=Employee.objects.create(firstname='Joe', lastname='Smith')).save() ->>> Company(name='Foobar Ltd.', num_employees=3, num_chairs=3, -... ceo=Employee.objects.create(firstname='Frank', lastname='Meyer')).save() ->>> Company(name='Test GmbH', num_employees=32, num_chairs=1, -... ceo=Employee.objects.create(firstname='Max', lastname='Mustermann')).save() - ->>> company_query = Company.objects.values('name','num_employees','num_chairs').order_by('name','num_employees','num_chairs') - -# We can filter for companies where the number of employees is greater than the -# number of chairs. ->>> company_query.filter(num_employees__gt=F('num_chairs')) -[{'num_chairs': 5, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 1, 'name': u'Test GmbH', 'num_employees': 32}] - -# We can set one field to have the value of another field -# Make sure we have enough chairs ->>> _ = company_query.update(num_chairs=F('num_employees')) ->>> company_query -[{'num_chairs': 2300, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 3, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 32, 'name': u'Test GmbH', 'num_employees': 32}] - -# We can perform arithmetic operations in expressions -# Make sure we have 2 spare chairs ->>> _ =company_query.update(num_chairs=F('num_employees')+2) ->>> company_query -[{'num_chairs': 2302, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 5, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 34, 'name': u'Test GmbH', 'num_employees': 32}] - -# Law of order of operations is followed ->>> _ =company_query.update(num_chairs=F('num_employees') + 2 * F('num_employees')) ->>> company_query -[{'num_chairs': 6900, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 9, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 96, 'name': u'Test GmbH', 'num_employees': 32}] - -# Law of order of operations can be overridden by parentheses ->>> _ =company_query.update(num_chairs=((F('num_employees') + 2) * F('num_employees'))) ->>> company_query -[{'num_chairs': 5294600, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 15, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 1088, 'name': u'Test GmbH', 'num_employees': 32}] - -# The relation of a foreign key can become copied over to an other foreign key. ->>> Company.objects.update(point_of_contact=F('ceo')) -3 - ->>> [c.point_of_contact for c in Company.objects.all()] -[, , ] - ->>> c = Company.objects.all()[0] ->>> c.point_of_contact = Employee.objects.create(firstname="Guido", lastname="van Rossum") ->>> c.save() - -# F Expressions can also span joins ->>> Company.objects.filter(ceo__firstname=F('point_of_contact__firstname')).distinct().order_by('name') -[, ] - ->>> _ = Company.objects.exclude(ceo__firstname=F('point_of_contact__firstname')).update(name='foo') ->>> Company.objects.exclude(ceo__firstname=F('point_of_contact__firstname')).get().name -u'foo' - ->>> _ = Company.objects.exclude(ceo__firstname=F('point_of_contact__firstname')).update(name=F('point_of_contact__lastname')) -Traceback (most recent call last): -... -FieldError: Joined field references are not permitted in this query - -# F expressions can be used to update attributes on single objects ->>> test_gmbh = Company.objects.get(name='Test GmbH') ->>> test_gmbh.num_employees -32 ->>> test_gmbh.num_employees = F('num_employees') + 4 ->>> test_gmbh.save() ->>> test_gmbh = Company.objects.get(pk=test_gmbh.pk) ->>> test_gmbh.num_employees -36 - -# F expressions cannot be used to update attributes which are foreign keys, or -# attributes which involve joins. ->>> test_gmbh.point_of_contact = None ->>> test_gmbh.save() ->>> test_gmbh.point_of_contact is None -True ->>> test_gmbh.point_of_contact = F('ceo') -Traceback (most recent call last): -... -ValueError: Cannot assign "": "Company.point_of_contact" must be a "Employee" instance. - ->>> test_gmbh.point_of_contact = test_gmbh.ceo ->>> test_gmbh.save() ->>> test_gmbh.name = F('ceo__last_name') ->>> test_gmbh.save() -Traceback (most recent call last): -... -FieldError: Joined field references are not permitted in this query - -# F expressions cannot be used to update attributes on objects which do not yet -# exist in the database ->>> acme = Company(name='The Acme Widget Co.', num_employees=12, num_chairs=5, -... ceo=test_gmbh.ceo) ->>> acme.num_employees = F('num_employees') + 16 ->>> acme.save() -Traceback (most recent call last): -... -TypeError: ... - -"""} diff --git a/tests/modeltests/expressions/tests.py b/tests/modeltests/expressions/tests.py new file mode 100644 index 0000000000..f11224d690 --- /dev/null +++ b/tests/modeltests/expressions/tests.py @@ -0,0 +1,133 @@ +from django.test import TestCase + +from django.db.models import F +from django.core.exceptions import FieldError + +from models import Employee, Company + +class ExpressionsTestCase(TestCase): + fixtures = ['f_expression_testdata.json'] + + def assertItemsEqual(self, a, b): + #fixme, replace with unittest2 function + return self.assertEqual(sorted(a), sorted(b)) + + def test_basic_f_expression(self): + company_query = Company.objects.values('name','num_employees', + 'num_chairs' + ).order_by('name', + 'num_employees', + 'num_chairs') + # We can filter for companies where the number of employees is + # greater than the number of chairs. + self.assertItemsEqual(company_query.filter( + num_employees__gt=F('num_chairs')), + [{'num_chairs': 5, 'name': u'Example Inc.', + 'num_employees': 2300}, + {'num_chairs': 1, 'name': u'Test GmbH', + 'num_employees': 32}]) + + # We can set one field to have the value of another field Make + # sure we have enough chairs + company_query.update(num_chairs=F('num_employees')) + self.assertItemsEqual(company_query, + [{'num_chairs': 2300, 'name': u'Example Inc.', + 'num_employees': 2300}, + {'num_chairs': 3, 'name': u'Foobar Ltd.', + 'num_employees': 3}, + {'num_chairs': 32, 'name': u'Test GmbH', + 'num_employees': 32}]) + + # We can perform arithmetic operations in expressions. Make + # sure we have 2 spare chairs + company_query.update(num_chairs=F('num_employees')+2) + self.assertItemsEqual(company_query, + [{'num_chairs': 2302, 'name': u'Example Inc.', + 'num_employees': 2300}, + {'num_chairs': 5, 'name': u'Foobar Ltd.', + 'num_employees': 3}, + {'num_chairs': 34, 'name': u'Test GmbH', + 'num_employees': 32}]) + + # Law of order of operations is followed + company_query.update(num_chairs=F('num_employees') + + 2 * F('num_employees')) + self.assertItemsEqual(company_query, + [{'num_chairs': 6900, 'name': u'Example Inc.', + 'num_employees': 2300}, + {'num_chairs': 9, 'name': u'Foobar Ltd.', + 'num_employees': 3}, + {'num_chairs': 96, 'name': u'Test GmbH', + 'num_employees': 32}]) + + # Law of order of operations can be overridden by parentheses + company_query.update(num_chairs=((F('num_employees') + 2) * + F('num_employees'))) + self.assertItemsEqual(company_query, + [{'num_chairs': 5294600, 'name': u'Example Inc.', + 'num_employees': 2300}, + {'num_chairs': 15, 'name': u'Foobar Ltd.', + 'num_employees': 3}, + {'num_chairs': 1088, 'name': u'Test GmbH', + 'num_employees': 32}]) + + # The relation of a foreign key can become copied over to an + # other foreign key. + self.assertEqual(Company.objects.update(point_of_contact=F('ceo')), 3) + + + self.assertEqual(repr([c.point_of_contact for + c in Company.objects.all()]), + '[, , ]') + + def test_f_expression_spanning_join(self): + # F Expressions can also span joins + self.assertQuerysetEqual( + Company.objects.filter( + ceo__firstname=F('point_of_contact__firstname') + ).distinct().order_by('name'), + ['', '']) + + Company.objects.exclude( + ceo__firstname=F('point_of_contact__firstname') + ).update(name='foo') + self.assertEqual(Company.objects.exclude( + ceo__firstname=F('point_of_contact__firstname') + ).get().name, + u'foo') + + self.assertRaises(FieldError, + Company.objects.exclude(ceo__firstname=F('point_of_contact__firstname')).update, + name=F('point_of_contact__lastname')) + + def test_f_expression_update_attribute(self): + # F expressions can be used to update attributes on single objects + test_gmbh = Company.objects.get(name='Test GmbH') + self.assertEqual(test_gmbh.num_employees, 32) + test_gmbh.num_employees = F('num_employees') + 4 + test_gmbh.save() + test_gmbh = Company.objects.get(pk=test_gmbh.pk) + self.assertEqual(test_gmbh.num_employees, 36) + + # F expressions cannot be used to update attributes which are + # foreign keys, or attributes which involve joins. + test_gmbh.point_of_contact = None + test_gmbh.save() + self.assertEqual(test_gmbh.point_of_contact, None) + self.assertRaises(ValueError, + setattr, + test_gmbh, 'point_of_contact', F('ceo')) + + test_gmbh.point_of_contact = test_gmbh.ceo + test_gmbh.save() + test_gmbh.name = F('ceo__last_name') + self.assertRaises(FieldError, + test_gmbh.save) + + # F expressions cannot be used to update attributes on objects + # which do not yet exist in the database + acme = Company(name='The Acme Widget Co.', num_employees=12, + num_chairs=5, ceo=test_gmbh.ceo) + acme.num_employees = F('num_employees') + 16 + self.assertRaises(TypeError, + acme.save)