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
7e2c804c
Kaydet (Commit)
7e2c804c
authored
May 16, 2014
tarafından
zsoldosp
Kaydeden (comit)
Tim Graham
Haz 25, 2014
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Split tests.basic.ModelTests in several tests; refs #18586.
üst
77c0a904
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
223 additions
and
0 deletions
+223
-0
tests.py
tests/basic/tests.py
+0
-0
tests.py
tests/dates/tests.py
+39
-0
tests.py
tests/datetimes/tests.py
+57
-0
models.py
tests/queries/models.py
+4
-0
tests.py
tests/queries/tests.py
+123
-0
No files found.
tests/basic/tests.py
Dosyayı görüntüle @
7e2c804c
This diff is collapsed.
Click to expand it.
tests/dates/tests.py
Dosyayı görüntüle @
7e2c804c
...
...
@@ -2,7 +2,9 @@ from __future__ import unicode_literals
import
datetime
from
django.db.models.fields
import
FieldDoesNotExist
from
django.test
import
TestCase
from
django.utils
import
six
from
.models
import
Article
,
Comment
,
Category
...
...
@@ -81,3 +83,40 @@ class DatesTests(TestCase):
],
lambda
d
:
d
,
)
def
test_dates_fails_when_no_arguments_are_provided
(
self
):
self
.
assertRaises
(
TypeError
,
Article
.
objects
.
dates
,
)
def
test_dates_fails_when_given_invalid_field_argument
(
self
):
six
.
assertRaisesRegex
(
self
,
FieldDoesNotExist
,
"Article has no field named 'invalid_field'"
,
Article
.
objects
.
dates
,
"invalid_field"
,
"year"
,
)
def
test_dates_fails_when_given_invalid_kind_argument
(
self
):
six
.
assertRaisesRegex
(
self
,
AssertionError
,
"'kind' must be one of 'year', 'month' or 'day'."
,
Article
.
objects
.
dates
,
"pub_date"
,
"bad_kind"
,
)
def
test_dates_fails_when_given_invalid_order_argument
(
self
):
six
.
assertRaisesRegex
(
self
,
AssertionError
,
"'order' must be either 'ASC' or 'DESC'."
,
Article
.
objects
.
dates
,
"pub_date"
,
"year"
,
order
=
"bad order"
,
)
tests/datetimes/tests.py
Dosyayı görüntüle @
7e2c804c
...
...
@@ -97,3 +97,60 @@ class DateTimesTests(TestCase):
Article
.
objects
.
create
(
title
=
"First one"
,
pub_date
=
now
)
qs
=
Article
.
objects
.
datetimes
(
'pub_date'
,
'second'
)
self
.
assertEqual
(
qs
[
0
],
now
)
def
test_datetimes_returns_available_dates_for_given_scope_and_given_field
(
self
):
pub_dates
=
[
datetime
.
datetime
(
2005
,
7
,
28
,
12
,
15
),
datetime
.
datetime
(
2005
,
7
,
29
,
2
,
15
),
datetime
.
datetime
(
2005
,
7
,
30
,
5
,
15
),
datetime
.
datetime
(
2005
,
7
,
31
,
19
,
15
)]
for
i
,
pub_date
in
enumerate
(
pub_dates
):
Article
(
pub_date
=
pub_date
,
title
=
'title #{}'
.
format
(
i
))
.
save
()
self
.
assertQuerysetEqual
(
Article
.
objects
.
datetimes
(
'pub_date'
,
'year'
),
[
"datetime.datetime(2005, 1, 1, 0, 0)"
])
self
.
assertQuerysetEqual
(
Article
.
objects
.
datetimes
(
'pub_date'
,
'month'
),
[
"datetime.datetime(2005, 7, 1, 0, 0)"
])
self
.
assertQuerysetEqual
(
Article
.
objects
.
datetimes
(
'pub_date'
,
'day'
),
[
"datetime.datetime(2005, 7, 28, 0, 0)"
,
"datetime.datetime(2005, 7, 29, 0, 0)"
,
"datetime.datetime(2005, 7, 30, 0, 0)"
,
"datetime.datetime(2005, 7, 31, 0, 0)"
])
self
.
assertQuerysetEqual
(
Article
.
objects
.
datetimes
(
'pub_date'
,
'day'
,
order
=
'ASC'
),
[
"datetime.datetime(2005, 7, 28, 0, 0)"
,
"datetime.datetime(2005, 7, 29, 0, 0)"
,
"datetime.datetime(2005, 7, 30, 0, 0)"
,
"datetime.datetime(2005, 7, 31, 0, 0)"
])
self
.
assertQuerysetEqual
(
Article
.
objects
.
datetimes
(
'pub_date'
,
'day'
,
order
=
'DESC'
),
[
"datetime.datetime(2005, 7, 31, 0, 0)"
,
"datetime.datetime(2005, 7, 30, 0, 0)"
,
"datetime.datetime(2005, 7, 29, 0, 0)"
,
"datetime.datetime(2005, 7, 28, 0, 0)"
])
def
test_datetimes_has_lazy_iterator
(
self
):
pub_dates
=
[
datetime
.
datetime
(
2005
,
7
,
28
,
12
,
15
),
datetime
.
datetime
(
2005
,
7
,
29
,
2
,
15
),
datetime
.
datetime
(
2005
,
7
,
30
,
5
,
15
),
datetime
.
datetime
(
2005
,
7
,
31
,
19
,
15
)]
for
i
,
pub_date
in
enumerate
(
pub_dates
):
Article
(
pub_date
=
pub_date
,
title
=
'title #{}'
.
format
(
i
))
.
save
()
# Use iterator() with datetimes() to return a generator that lazily
# requests each result one at a time, to save memory.
dates
=
[]
with
self
.
assertNumQueries
(
0
):
article_datetimes_iterator
=
Article
.
objects
.
datetimes
(
'pub_date'
,
'day'
,
order
=
'DESC'
)
.
iterator
()
with
self
.
assertNumQueries
(
1
):
for
article
in
article_datetimes_iterator
:
dates
.
append
(
article
)
self
.
assertEqual
(
dates
,
[
datetime
.
datetime
(
2005
,
7
,
31
,
0
,
0
),
datetime
.
datetime
(
2005
,
7
,
30
,
0
,
0
),
datetime
.
datetime
(
2005
,
7
,
29
,
0
,
0
),
datetime
.
datetime
(
2005
,
7
,
28
,
0
,
0
)])
tests/queries/models.py
Dosyayı görüntüle @
7e2c804c
...
...
@@ -364,10 +364,14 @@ class Plaything(models.Model):
return
self
.
name
@python_2_unicode_compatible
class
Article
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
20
)
created
=
models
.
DateTimeField
()
def
__str__
(
self
):
return
self
.
name
@python_2_unicode_compatible
class
Food
(
models
.
Model
):
...
...
tests/queries/tests.py
Dosyayı görüntüle @
7e2c804c
...
...
@@ -2145,6 +2145,129 @@ class ValuesQuerysetTests(BaseQuerysetTest):
self
.
assertQuerysetEqual
(
qs
,
[
72
],
self
.
identity
)
class
QuerySetSupportsPythonIdioms
(
TestCase
):
def
setUp
(
self
):
some_date
=
datetime
.
datetime
(
2014
,
5
,
16
,
12
,
1
)
for
i
in
range
(
1
,
8
):
Article
.
objects
.
create
(
name
=
"Article {}"
.
format
(
i
),
created
=
some_date
)
def
get_ordered_articles
(
self
):
return
Article
.
objects
.
all
()
.
order_by
(
'name'
)
def
test_can_get_items_using_index_and_slice_notation
(
self
):
self
.
assertEqual
(
self
.
get_ordered_articles
()[
0
]
.
name
,
'Article 1'
)
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
1
:
3
],
[
"<Article: Article 2>"
,
"<Article: Article 3>"
])
def
test_slicing_with_steps_can_be_used
(
self
):
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[::
2
],
[
"<Article: Article 1>"
,
"<Article: Article 3>"
,
"<Article: Article 5>"
,
"<Article: Article 7>"
])
@unittest.skipUnless
(
six
.
PY2
,
"Python 2 only -- Python 3 doesn't have longs."
)
def
test_slicing_works_with_longs
(
self
):
self
.
assertEqual
(
self
.
get_ordered_articles
()[
long
(
0
)]
.
name
,
'Article 1'
)
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
long
(
1
):
long
(
3
)],
[
"<Article: Article 2>"
,
"<Article: Article 3>"
])
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[::
long
(
2
)],
[
"<Article: Article 1>"
,
"<Article: Article 3>"
,
"<Article: Article 5>"
,
"<Article: Article 7>"
])
# And can be mixed with ints.
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
1
:
long
(
3
)],
[
"<Article: Article 2>"
,
"<Article: Article 3>"
])
def
test_slicing_without_step_is_lazy
(
self
):
with
self
.
assertNumQueries
(
0
):
self
.
get_ordered_articles
()[
0
:
5
]
def
test_slicing_with_tests_is_not_lazy
(
self
):
with
self
.
assertNumQueries
(
1
):
self
.
get_ordered_articles
()[
0
:
5
:
3
]
def
test_slicing_can_slice_again_after_slicing
(
self
):
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
0
:
5
][
0
:
2
],
[
"<Article: Article 1>"
,
"<Article: Article 2>"
])
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
0
:
5
][
4
:],
[
"<Article: Article 5>"
])
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
0
:
5
][
5
:],
[])
# Some more tests!
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
2
:][
0
:
2
],
[
"<Article: Article 3>"
,
"<Article: Article 4>"
])
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
2
:][:
2
],
[
"<Article: Article 3>"
,
"<Article: Article 4>"
])
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
2
:][
2
:
3
],
[
"<Article: Article 5>"
])
# Using an offset without a limit is also possible.
self
.
assertQuerysetEqual
(
self
.
get_ordered_articles
()[
5
:],
[
"<Article: Article 6>"
,
"<Article: Article 7>"
])
def
test_slicing_cannot_filter_queryset_once_sliced
(
self
):
six
.
assertRaisesRegex
(
self
,
AssertionError
,
"Cannot filter a query once a slice has been taken."
,
Article
.
objects
.
all
()[
0
:
5
]
.
filter
,
id
=
1
,
)
def
test_slicing_cannot_reorder_queryset_once_sliced
(
self
):
six
.
assertRaisesRegex
(
self
,
AssertionError
,
"Cannot reorder a query once a slice has been taken."
,
Article
.
objects
.
all
()[
0
:
5
]
.
order_by
,
'id'
,
)
def
test_slicing_cannot_combine_queries_once_sliced
(
self
):
six
.
assertRaisesRegex
(
self
,
AssertionError
,
"Cannot combine queries once a slice has been taken."
,
lambda
:
Article
.
objects
.
all
()[
0
:
1
]
&
Article
.
objects
.
all
()[
4
:
5
]
)
def
test_slicing_negative_indexing_not_supported_for_single_element
(
self
):
"""hint: inverting your ordering might do what you need"""
six
.
assertRaisesRegex
(
self
,
AssertionError
,
"Negative indexing is not supported."
,
lambda
:
Article
.
objects
.
all
()[
-
1
]
)
def
test_slicing_negative_indexing_not_supported_for_range
(
self
):
"""hint: inverting your ordering might do what you need"""
six
.
assertRaisesRegex
(
self
,
AssertionError
,
"Negative indexing is not supported."
,
lambda
:
Article
.
objects
.
all
()[
0
:
-
5
]
)
def
test_can_get_number_of_items_in_queryset_using_standard_len
(
self
):
self
.
assertEqual
(
len
(
Article
.
objects
.
filter
(
name__exact
=
'Article 1'
)),
1
)
def
test_can_combine_queries_using_and_and_or_operators
(
self
):
s1
=
Article
.
objects
.
filter
(
name__exact
=
'Article 1'
)
s2
=
Article
.
objects
.
filter
(
name__exact
=
'Article 2'
)
self
.
assertQuerysetEqual
((
s1
|
s2
)
.
order_by
(
'name'
),
[
"<Article: Article 1>"
,
"<Article: Article 2>"
])
self
.
assertQuerysetEqual
(
s1
&
s2
,
[])
class
WeirdQuerysetSlicingTests
(
BaseQuerysetTest
):
def
setUp
(
self
):
Number
.
objects
.
create
(
num
=
1
)
...
...
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