Refs #18586 -- Refactored expressions tests.

This commit is contained in:
Shabda Raaj 2014-11-10 19:11:17 +05:30 committed by Tim Graham
parent 079ee1ff5d
commit 43041ee48c
1 changed files with 34 additions and 20 deletions

View File

@ -13,9 +13,9 @@ from django.utils import six
from .models import Company, Employee, Number, Experiment from .models import Company, Employee, Number, Experiment
class ExpressionsTests(TestCase): class BasicExpressionsTests(TestCase):
@classmethod
def test_filter(self): def setUpTestData(cls):
Company.objects.create( Company.objects.create(
name="Example Inc.", num_employees=2300, num_chairs=5, name="Example Inc.", num_employees=2300, num_chairs=5,
ceo=Employee.objects.create(firstname="Joe", lastname="Smith") ceo=Employee.objects.create(firstname="Joe", lastname="Smith")
@ -29,16 +29,19 @@ class ExpressionsTests(TestCase):
ceo=Employee.objects.create(firstname="Max", lastname="Mustermann") ceo=Employee.objects.create(firstname="Max", lastname="Mustermann")
) )
company_query = Company.objects.values( def setUp(self):
self.company_query = Company.objects.values(
"name", "num_employees", "num_chairs" "name", "num_employees", "num_chairs"
).order_by( ).order_by(
"name", "num_employees", "num_chairs" "name", "num_employees", "num_chairs"
) )
# We can filter for companies where the number of employees is greater def test_filter_inter_attribute(self):
# We can filter on attribute relationships on same model obj, e.g.
# find companies where the number of employees is greater
# than the number of chairs. # than the number of chairs.
self.assertQuerysetEqual( self.assertQuerysetEqual(
company_query.filter(num_employees__gt=F("num_chairs")), [ self.company_query.filter(num_employees__gt=F("num_chairs")), [
{ {
"num_chairs": 5, "num_chairs": 5,
"name": "Example Inc.", "name": "Example Inc.",
@ -53,11 +56,12 @@ class ExpressionsTests(TestCase):
lambda o: o lambda o: o
) )
def test_update(self):
# We can set one field to have the value of another field # We can set one field to have the value of another field
# Make sure we have enough chairs # Make sure we have enough chairs
company_query.update(num_chairs=F("num_employees")) self.company_query.update(num_chairs=F("num_employees"))
self.assertQuerysetEqual( self.assertQuerysetEqual(
company_query, [ self.company_query, [
{ {
"num_chairs": 2300, "num_chairs": 2300,
"name": "Example Inc.", "name": "Example Inc.",
@ -77,11 +81,12 @@ class ExpressionsTests(TestCase):
lambda o: o lambda o: o
) )
def test_arithmetic(self):
# We can perform arithmetic operations in expressions # We can perform arithmetic operations in expressions
# Make sure we have 2 spare chairs # Make sure we have 2 spare chairs
company_query.update(num_chairs=F("num_employees") + 2) self.company_query.update(num_chairs=F("num_employees") + 2)
self.assertQuerysetEqual( self.assertQuerysetEqual(
company_query, [ self.company_query, [
{ {
'num_chairs': 2302, 'num_chairs': 2302,
'name': 'Example Inc.', 'name': 'Example Inc.',
@ -101,12 +106,13 @@ class ExpressionsTests(TestCase):
lambda o: o, lambda o: o,
) )
def test_order_of_operations(self):
# Law of order of operations is followed # Law of order of operations is followed
company_query.update( self. company_query.update(
num_chairs=F('num_employees') + 2 * F('num_employees') num_chairs=F('num_employees') + 2 * F('num_employees')
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
company_query, [ self.company_query, [
{ {
'num_chairs': 6900, 'num_chairs': 6900,
'name': 'Example Inc.', 'name': 'Example Inc.',
@ -126,12 +132,13 @@ class ExpressionsTests(TestCase):
lambda o: o, lambda o: o,
) )
def test_parenthesis_priority(self):
# Law of order of operations can be overridden by parentheses # Law of order of operations can be overridden by parentheses
company_query.update( self.company_query.update(
num_chairs=((F('num_employees') + 2) * F('num_employees')) num_chairs=((F('num_employees') + 2) * F('num_employees'))
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
company_query, [ self.company_query, [
{ {
'num_chairs': 5294600, 'num_chairs': 5294600,
'name': 'Example Inc.', 'name': 'Example Inc.',
@ -151,8 +158,8 @@ class ExpressionsTests(TestCase):
lambda o: o, lambda o: o,
) )
# The relation of a foreign key can become copied over to an other def test_update_with_fk(self):
# foreign key. # ForeignKey can become updated with the value of another ForeignKey.
self.assertEqual( self.assertEqual(
Company.objects.update(point_of_contact=F('ceo')), Company.objects.update(point_of_contact=F('ceo')),
3 3
@ -167,11 +174,13 @@ class ExpressionsTests(TestCase):
ordered=False ordered=False
) )
def test_filter_with_join(self):
# F Expressions can also span joins
Company.objects.update(point_of_contact=F('ceo'))
c = Company.objects.all()[0] c = Company.objects.all()[0]
c.point_of_contact = Employee.objects.create(firstname="Guido", lastname="van Rossum") c.point_of_contact = Employee.objects.create(firstname="Guido", lastname="van Rossum")
c.save() c.save()
# F Expressions can also span joins
self.assertQuerysetEqual( self.assertQuerysetEqual(
Company.objects.filter(ceo__firstname=F("point_of_contact__firstname")), [ Company.objects.filter(ceo__firstname=F("point_of_contact__firstname")), [
"Foobar Ltd.", "Foobar Ltd.",
@ -197,6 +206,7 @@ class ExpressionsTests(TestCase):
ceo__firstname=F('point_of_contact__firstname') ceo__firstname=F('point_of_contact__firstname')
).update(name=F('point_of_contact__lastname')) ).update(name=F('point_of_contact__lastname'))
def test_object_update(self):
# F expressions can be used to update attributes on single objects # F expressions can be used to update attributes on single objects
test_gmbh = Company.objects.get(name="Test GmbH") test_gmbh = Company.objects.get(name="Test GmbH")
self.assertEqual(test_gmbh.num_employees, 32) self.assertEqual(test_gmbh.num_employees, 32)
@ -205,11 +215,10 @@ class ExpressionsTests(TestCase):
test_gmbh = Company.objects.get(pk=test_gmbh.pk) test_gmbh = Company.objects.get(pk=test_gmbh.pk)
self.assertEqual(test_gmbh.num_employees, 36) self.assertEqual(test_gmbh.num_employees, 36)
def test_object_update_fk(self):
# F expressions cannot be used to update attributes which are foreign # F expressions cannot be used to update attributes which are foreign
# keys, or attributes which involve joins. # keys, or attributes which involve joins.
test_gmbh.point_of_contact = None test_gmbh = Company.objects.get(name="Test GmbH")
test_gmbh.save()
self.assertIsNone(test_gmbh.point_of_contact)
def test(): def test():
test_gmbh.point_of_contact = F("ceo") test_gmbh.point_of_contact = F("ceo")
@ -220,8 +229,10 @@ class ExpressionsTests(TestCase):
test_gmbh.name = F("ceo__last_name") test_gmbh.name = F("ceo__last_name")
self.assertRaises(FieldError, test_gmbh.save) self.assertRaises(FieldError, test_gmbh.save)
def test_object_update_unsaved_objects(self):
# F expressions cannot be used to update attributes on objects which do # F expressions cannot be used to update attributes on objects which do
# not yet exist in the database # not yet exist in the database
test_gmbh = Company.objects.get(name="Test GmbH")
acme = Company( acme = Company(
name="The Acme Widget Co.", num_employees=12, num_chairs=5, name="The Acme Widget Co.", num_employees=12, num_chairs=5,
ceo=test_gmbh.ceo ceo=test_gmbh.ceo
@ -287,6 +298,9 @@ class ExpressionsTests(TestCase):
) )
self.assertEqual(str(qs.query).count('JOIN'), 2) self.assertEqual(str(qs.query).count('JOIN'), 2)
class ExpressionsTests(TestCase):
def test_F_object_deepcopy(self): def test_F_object_deepcopy(self):
""" """
Make sure F objects can be deepcopied (#23492) Make sure F objects can be deepcopied (#23492)