Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
43041ee4
Kaydet (Commit)
43041ee4
authored
Kas 10, 2014
tarafından
Shabda Raaj
Kaydeden (comit)
Tim Graham
Ara 11, 2014
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Refs #18586 -- Refactored expressions tests.
üst
079ee1ff
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
20 deletions
+34
-20
tests.py
tests/expressions/tests.py
+34
-20
No files found.
tests/expressions/tests.py
Dosyayı görüntüle @
43041ee4
...
...
@@ -13,9 +13,9 @@ from django.utils import six
from
.models
import
Company
,
Employee
,
Number
,
Experiment
class
ExpressionsTests
(
TestCase
):
def
test_filter
(
self
):
class
Basic
ExpressionsTests
(
TestCase
):
@classmethod
def
setUpTestData
(
cls
):
Company
.
objects
.
create
(
name
=
"Example Inc."
,
num_employees
=
2300
,
num_chairs
=
5
,
ceo
=
Employee
.
objects
.
create
(
firstname
=
"Joe"
,
lastname
=
"Smith"
)
...
...
@@ -29,16 +29,19 @@ class ExpressionsTests(TestCase):
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"
)
.
order_by
(
"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.
self
.
assertQuerysetEqual
(
company_query
.
filter
(
num_employees__gt
=
F
(
"num_chairs"
)),
[
self
.
company_query
.
filter
(
num_employees__gt
=
F
(
"num_chairs"
)),
[
{
"num_chairs"
:
5
,
"name"
:
"Example Inc."
,
...
...
@@ -53,11 +56,12 @@ class ExpressionsTests(TestCase):
lambda
o
:
o
)
def
test_update
(
self
):
# 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
.
company_query
.
update
(
num_chairs
=
F
(
"num_employees"
))
self
.
assertQuerysetEqual
(
company_query
,
[
self
.
company_query
,
[
{
"num_chairs"
:
2300
,
"name"
:
"Example Inc."
,
...
...
@@ -77,11 +81,12 @@ class ExpressionsTests(TestCase):
lambda
o
:
o
)
def
test_arithmetic
(
self
):
# We can perform arithmetic operations in expressions
# 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
(
company_query
,
[
self
.
company_query
,
[
{
'num_chairs'
:
2302
,
'name'
:
'Example Inc.'
,
...
...
@@ -101,12 +106,13 @@ class ExpressionsTests(TestCase):
lambda
o
:
o
,
)
def
test_order_of_operations
(
self
):
# Law of order of operations is followed
company_query
.
update
(
self
.
company_query
.
update
(
num_chairs
=
F
(
'num_employees'
)
+
2
*
F
(
'num_employees'
)
)
self
.
assertQuerysetEqual
(
company_query
,
[
self
.
company_query
,
[
{
'num_chairs'
:
6900
,
'name'
:
'Example Inc.'
,
...
...
@@ -126,12 +132,13 @@ class ExpressionsTests(TestCase):
lambda
o
:
o
,
)
def
test_parenthesis_priority
(
self
):
# 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'
))
)
self
.
assertQuerysetEqual
(
company_query
,
[
self
.
company_query
,
[
{
'num_chairs'
:
5294600
,
'name'
:
'Example Inc.'
,
...
...
@@ -151,8 +158,8 @@ class ExpressionsTests(TestCase):
lambda
o
:
o
,
)
# The relation of a foreign key can become copied over to an other
#
foreign k
ey.
def
test_update_with_fk
(
self
):
#
ForeignKey can become updated with the value of another ForeignK
ey.
self
.
assertEqual
(
Company
.
objects
.
update
(
point_of_contact
=
F
(
'ceo'
)),
3
...
...
@@ -167,11 +174,13 @@ class ExpressionsTests(TestCase):
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
.
point_of_contact
=
Employee
.
objects
.
create
(
firstname
=
"Guido"
,
lastname
=
"van Rossum"
)
c
.
save
()
# F Expressions can also span joins
self
.
assertQuerysetEqual
(
Company
.
objects
.
filter
(
ceo__firstname
=
F
(
"point_of_contact__firstname"
)),
[
"Foobar Ltd."
,
...
...
@@ -197,6 +206,7 @@ class ExpressionsTests(TestCase):
ceo__firstname
=
F
(
'point_of_contact__firstname'
)
)
.
update
(
name
=
F
(
'point_of_contact__lastname'
))
def
test_object_update
(
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
)
...
...
@@ -205,11 +215,10 @@ class ExpressionsTests(TestCase):
test_gmbh
=
Company
.
objects
.
get
(
pk
=
test_gmbh
.
pk
)
self
.
assertEqual
(
test_gmbh
.
num_employees
,
36
)
def
test_object_update_fk
(
self
):
# 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
.
assertIsNone
(
test_gmbh
.
point_of_contact
)
test_gmbh
=
Company
.
objects
.
get
(
name
=
"Test GmbH"
)
def
test
():
test_gmbh
.
point_of_contact
=
F
(
"ceo"
)
...
...
@@ -220,8 +229,10 @@ class ExpressionsTests(TestCase):
test_gmbh
.
name
=
F
(
"ceo__last_name"
)
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
# not yet exist in the database
test_gmbh
=
Company
.
objects
.
get
(
name
=
"Test GmbH"
)
acme
=
Company
(
name
=
"The Acme Widget Co."
,
num_employees
=
12
,
num_chairs
=
5
,
ceo
=
test_gmbh
.
ceo
...
...
@@ -287,6 +298,9 @@ class ExpressionsTests(TestCase):
)
self
.
assertEqual
(
str
(
qs
.
query
)
.
count
(
'JOIN'
),
2
)
class
ExpressionsTests
(
TestCase
):
def
test_F_object_deepcopy
(
self
):
"""
Make sure F objects can be deepcopied (#23492)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment