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
7daf0067
Kaydet (Commit)
7daf0067
authored
Kas 27, 2014
tarafından
Helen ST
Kaydeden (comit)
Tim Graham
Ara 04, 2014
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Refs #18586 -- Split up order_with_respect_to tests
üst
765fa36d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
30 deletions
+42
-30
tests.py
tests/order_with_respect_to/tests.py
+42
-30
No files found.
tests/order_with_respect_to/tests.py
Dosyayı görüntüle @
7daf0067
...
...
@@ -10,61 +10,73 @@ from .models import Post, Question, Answer
class
OrderWithRespectToTests
(
TestCase
):
def
test_basic
(
self
):
q1
=
Question
.
objects
.
create
(
text
=
"Which Beatle starts with the letter 'R'?"
)
q2
=
Question
.
objects
.
create
(
text
=
"What is your name?"
)
Answer
.
objects
.
create
(
text
=
"John"
,
question
=
q1
)
Answer
.
objects
.
create
(
text
=
"Jonno"
,
question
=
q2
)
Answer
.
objects
.
create
(
text
=
"Paul"
,
question
=
q1
)
Answer
.
objects
.
create
(
text
=
"Paulo"
,
question
=
q2
)
Answer
.
objects
.
create
(
text
=
"George"
,
question
=
q1
)
Answer
.
objects
.
create
(
text
=
"Ringo"
,
question
=
q1
)
# The answers will always be ordered in the order they were inserted.
@classmethod
def
setUpTestData
(
cls
):
cls
.
q1
=
Question
.
objects
.
create
(
text
=
"Which Beatle starts with the letter 'R'?"
)
Answer
.
objects
.
create
(
text
=
"John"
,
question
=
cls
.
q1
)
Answer
.
objects
.
create
(
text
=
"Paul"
,
question
=
cls
.
q1
)
Answer
.
objects
.
create
(
text
=
"George"
,
question
=
cls
.
q1
)
Answer
.
objects
.
create
(
text
=
"Ringo"
,
question
=
cls
.
q1
)
def
test_default_to_insertion_order
(
self
):
# Answers will always be ordered in the order they were inserted.
self
.
assertQuerysetEqual
(
q1
.
answer_set
.
all
(),
[
self
.
q1
.
answer_set
.
all
(),
[
"John"
,
"Paul"
,
"George"
,
"Ringo"
,
],
attrgetter
(
"text"
),
)
def
test_previous_and_next_in_order
(
self
):
# We can retrieve the answers related to a particular object, in the
# order they were created, once we have a particular object.
a1
=
Answer
.
objects
.
filter
(
question
=
q1
)[
0
]
a1
=
Answer
.
objects
.
filter
(
question
=
self
.
q1
)[
0
]
self
.
assertEqual
(
a1
.
text
,
"John"
)
a2
=
a1
.
get_next_in_order
()
self
.
assertEqual
(
a2
.
text
,
"Paul"
)
a4
=
list
(
Answer
.
objects
.
filter
(
question
=
q1
))[
-
1
]
self
.
assertEqual
(
a4
.
text
,
"Ringo"
)
self
.
assertEqual
(
a4
.
get_previous_in_order
()
.
text
,
"George"
)
self
.
assertEqual
(
a1
.
get_next_in_order
()
.
text
,
"Paul"
)
# Determining (and setting) the ordering for a particular item is also
# possible.
id_list
=
[
o
.
pk
for
o
in
q1
.
answer_set
.
all
()]
self
.
assertEqual
(
a2
.
question
.
get_answer_order
(),
id_list
)
a2
=
list
(
Answer
.
objects
.
filter
(
question
=
self
.
q1
))[
-
1
]
self
.
assertEqual
(
a2
.
text
,
"Ringo"
)
self
.
assertEqual
(
a2
.
get_previous_in_order
()
.
text
,
"George"
)
a5
=
Answer
.
objects
.
create
(
text
=
"Number five"
,
question
=
q1
)
def
test_item_ordering
(
self
):
# We can retrieve the ordering of the queryset from a particular item.
a1
=
Answer
.
objects
.
filter
(
question
=
self
.
q1
)[
1
]
id_list
=
[
o
.
pk
for
o
in
self
.
q1
.
answer_set
.
all
()]
self
.
assertEqual
(
a1
.
question
.
get_answer_order
(),
id_list
)
# It doesn't matter which answer we use to check the order, it will
# always be the same.
a2
=
Answer
.
objects
.
create
(
text
=
"Number five"
,
question
=
self
.
q1
)
self
.
assertEqual
(
a
2
.
question
.
get_answer_order
(),
a5
.
question
.
get_answer_order
()
a
1
.
question
.
get_answer_order
(),
a2
.
question
.
get_answer_order
()
)
# The ordering can be altered:
id_list
=
[
o
.
pk
for
o
in
q1
.
answer_set
.
all
()]
def
test_change_ordering
(
self
):
# The ordering can be altered
a
=
Answer
.
objects
.
create
(
text
=
"Number five"
,
question
=
self
.
q1
)
# Swap the last two items in the order list
id_list
=
[
o
.
pk
for
o
in
self
.
q1
.
answer_set
.
all
()]
x
=
id_list
.
pop
()
id_list
.
insert
(
-
1
,
x
)
self
.
assertNotEqual
(
a5
.
question
.
get_answer_order
(),
id_list
)
a5
.
question
.
set_answer_order
(
id_list
)
# By default, the ordering is different from the swapped version
self
.
assertNotEqual
(
a
.
question
.
get_answer_order
(),
id_list
)
# Change the ordering to the swapped version -
# this changes the ordering of the queryset.
a
.
question
.
set_answer_order
(
id_list
)
self
.
assertQuerysetEqual
(
q1
.
answer_set
.
all
(),
[
self
.
q1
.
answer_set
.
all
(),
[
"John"
,
"Paul"
,
"George"
,
"Number five"
,
"Ringo"
],
attrgetter
(
"text"
)
)
class
OrderWithRespectToTests2
(
TestCase
):
def
test_recursive_ordering
(
self
):
p1
=
Post
.
objects
.
create
(
title
=
'1'
)
p2
=
Post
.
objects
.
create
(
title
=
'2'
)
...
...
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