|
| 1 | +from django.test import TestCase |
| 2 | + |
| 3 | +from django.db.models import F |
| 4 | +from django.core.exceptions import FieldError |
| 5 | + |
| 6 | +from models import Employee, Company |
| 7 | + |
| 8 | +class ExpressionsTestCase(TestCase): |
| 9 | + fixtures = ['f_expression_testdata.json'] |
| 10 | + |
| 11 | + def assertItemsEqual(self, a, b): |
| 12 | + #fixme, replace with unittest2 function |
| 13 | + return self.assertEqual(sorted(a), sorted(b)) |
| 14 | + |
| 15 | + def test_basic_f_expression(self): |
| 16 | + company_query = Company.objects.values('name','num_employees', |
| 17 | + 'num_chairs' |
| 18 | + ).order_by('name', |
| 19 | + 'num_employees', |
| 20 | + 'num_chairs') |
| 21 | + # We can filter for companies where the number of employees is |
| 22 | + # greater than the number of chairs. |
| 23 | + self.assertItemsEqual(company_query.filter( |
| 24 | + num_employees__gt=F('num_chairs')), |
| 25 | + [{'num_chairs': 5, 'name': u'Example Inc.', |
| 26 | + 'num_employees': 2300}, |
| 27 | + {'num_chairs': 1, 'name': u'Test GmbH', |
| 28 | + 'num_employees': 32}]) |
| 29 | + |
| 30 | + # We can set one field to have the value of another field Make |
| 31 | + # sure we have enough chairs |
| 32 | + company_query.update(num_chairs=F('num_employees')) |
| 33 | + self.assertItemsEqual(company_query, |
| 34 | + [{'num_chairs': 2300, 'name': u'Example Inc.', |
| 35 | + 'num_employees': 2300}, |
| 36 | + {'num_chairs': 3, 'name': u'Foobar Ltd.', |
| 37 | + 'num_employees': 3}, |
| 38 | + {'num_chairs': 32, 'name': u'Test GmbH', |
| 39 | + 'num_employees': 32}]) |
| 40 | + |
| 41 | + # We can perform arithmetic operations in expressions. Make |
| 42 | + # sure we have 2 spare chairs |
| 43 | + company_query.update(num_chairs=F('num_employees')+2) |
| 44 | + self.assertItemsEqual(company_query, |
| 45 | + [{'num_chairs': 2302, 'name': u'Example Inc.', |
| 46 | + 'num_employees': 2300}, |
| 47 | + {'num_chairs': 5, 'name': u'Foobar Ltd.', |
| 48 | + 'num_employees': 3}, |
| 49 | + {'num_chairs': 34, 'name': u'Test GmbH', |
| 50 | + 'num_employees': 32}]) |
| 51 | + |
| 52 | + # Law of order of operations is followed |
| 53 | + company_query.update(num_chairs=F('num_employees') + |
| 54 | + 2 * F('num_employees')) |
| 55 | + self.assertItemsEqual(company_query, |
| 56 | + [{'num_chairs': 6900, 'name': u'Example Inc.', |
| 57 | + 'num_employees': 2300}, |
| 58 | + {'num_chairs': 9, 'name': u'Foobar Ltd.', |
| 59 | + 'num_employees': 3}, |
| 60 | + {'num_chairs': 96, 'name': u'Test GmbH', |
| 61 | + 'num_employees': 32}]) |
| 62 | + |
| 63 | + # Law of order of operations can be overridden by parentheses |
| 64 | + company_query.update(num_chairs=((F('num_employees') + 2) * |
| 65 | + F('num_employees'))) |
| 66 | + self.assertItemsEqual(company_query, |
| 67 | + [{'num_chairs': 5294600, 'name': u'Example Inc.', |
| 68 | + 'num_employees': 2300}, |
| 69 | + {'num_chairs': 15, 'name': u'Foobar Ltd.', |
| 70 | + 'num_employees': 3}, |
| 71 | + {'num_chairs': 1088, 'name': u'Test GmbH', |
| 72 | + 'num_employees': 32}]) |
| 73 | + |
| 74 | + # The relation of a foreign key can become copied over to an |
| 75 | + # other foreign key. |
| 76 | + self.assertEqual(Company.objects.update(point_of_contact=F('ceo')), 3) |
| 77 | + |
| 78 | + |
| 79 | + self.assertEqual(repr([c.point_of_contact for |
| 80 | + c in Company.objects.all()]), |
| 81 | + '[<Employee: Joe Smith>, <Employee: Frank Meyer>, <Employee: Max Mustermann>]') |
| 82 | + |
| 83 | + def test_f_expression_spanning_join(self): |
| 84 | + # F Expressions can also span joins |
| 85 | + self.assertQuerysetEqual( |
| 86 | + Company.objects.filter( |
| 87 | + ceo__firstname=F('point_of_contact__firstname') |
| 88 | + ).distinct().order_by('name'), |
| 89 | + ['<Company: Foobar Ltd.>', '<Company: Test GmbH>']) |
| 90 | + |
| 91 | + Company.objects.exclude( |
| 92 | + ceo__firstname=F('point_of_contact__firstname') |
| 93 | + ).update(name='foo') |
| 94 | + self.assertEqual(Company.objects.exclude( |
| 95 | + ceo__firstname=F('point_of_contact__firstname') |
| 96 | + ).get().name, |
| 97 | + u'foo') |
| 98 | + |
| 99 | + self.assertRaises(FieldError, |
| 100 | + Company.objects.exclude(ceo__firstname=F('point_of_contact__firstname')).update, |
| 101 | + name=F('point_of_contact__lastname')) |
| 102 | + |
| 103 | + def test_f_expression_update_attribute(self): |
| 104 | + # F expressions can be used to update attributes on single objects |
| 105 | + test_gmbh = Company.objects.get(name='Test GmbH') |
| 106 | + self.assertEqual(test_gmbh.num_employees, 32) |
| 107 | + test_gmbh.num_employees = F('num_employees') + 4 |
| 108 | + test_gmbh.save() |
| 109 | + test_gmbh = Company.objects.get(pk=test_gmbh.pk) |
| 110 | + self.assertEqual(test_gmbh.num_employees, 36) |
| 111 | + |
| 112 | + # F expressions cannot be used to update attributes which are |
| 113 | + # foreign keys, or attributes which involve joins. |
| 114 | + test_gmbh.point_of_contact = None |
| 115 | + test_gmbh.save() |
| 116 | + self.assertEqual(test_gmbh.point_of_contact, None) |
| 117 | + self.assertRaises(ValueError, |
| 118 | + setattr, |
| 119 | + test_gmbh, 'point_of_contact', F('ceo')) |
| 120 | + |
| 121 | + test_gmbh.point_of_contact = test_gmbh.ceo |
| 122 | + test_gmbh.save() |
| 123 | + test_gmbh.name = F('ceo__last_name') |
| 124 | + self.assertRaises(FieldError, |
| 125 | + test_gmbh.save) |
| 126 | + |
| 127 | + # F expressions cannot be used to update attributes on objects |
| 128 | + # which do not yet exist in the database |
| 129 | + acme = Company(name='The Acme Widget Co.', num_employees=12, |
| 130 | + num_chairs=5, ceo=test_gmbh.ceo) |
| 131 | + acme.num_employees = F('num_employees') + 16 |
| 132 | + self.assertRaises(TypeError, |
| 133 | + acme.save) |
0 commit comments