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
0dab07e5
Kaydet (Commit)
0dab07e5
authored
Eyl 22, 2014
tarafından
Loic Bistuer
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Merged custom_managers_regress into the custom_managers test package.
üst
d42a45de
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
86 additions
and
109 deletions
+86
-109
models.py
tests/custom_managers/models.py
+40
-11
tests.py
tests/custom_managers/tests.py
+46
-1
__init__.py
tests/custom_managers_regress/__init__.py
+0
-0
models.py
tests/custom_managers_regress/models.py
+0
-48
tests.py
tests/custom_managers_regress/tests.py
+0
-49
No files found.
tests/custom_managers/models.py
Dosyayı görüntüle @
0dab07e5
...
...
@@ -11,28 +11,20 @@ returns.
from
__future__
import
unicode_literals
from
django.contrib.contenttypes.fields
import
(
GenericForeignKey
,
GenericRelation
)
from
django.contrib.contenttypes.fields
import
GenericForeignKey
,
GenericRelation
from
django.db
import
models
from
django.utils.encoding
import
python_2_unicode_compatible
# An example of a custom manager called "objects".
class
PersonManager
(
models
.
Manager
):
def
get_fun_people
(
self
):
return
self
.
filter
(
fun
=
True
)
# An example of a custom manager that sets get_queryset().
class
PublishedBookManager
(
models
.
Manager
):
def
get_queryset
(
self
):
return
super
(
PublishedBookManager
,
self
)
.
get_queryset
()
.
filter
(
is_published
=
True
)
# An example of a custom queryset that copies its methods onto the manager.
class
CustomQuerySet
(
models
.
QuerySet
):
def
filter
(
self
,
*
args
,
**
kwargs
):
...
...
@@ -138,8 +130,6 @@ class Book(models.Model):
def
__str__
(
self
):
return
self
.
title
# An example of providing multiple custom managers.
class
FastCarManager
(
models
.
Manager
):
def
get_queryset
(
self
):
...
...
@@ -156,3 +146,42 @@ class Car(models.Model):
def
__str__
(
self
):
return
self
.
name
class
RestrictedManager
(
models
.
Manager
):
def
get_queryset
(
self
):
return
super
(
RestrictedManager
,
self
)
.
get_queryset
()
.
filter
(
is_public
=
True
)
@python_2_unicode_compatible
class
RelatedModel
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
50
)
def
__str__
(
self
):
return
self
.
name
@python_2_unicode_compatible
class
RestrictedModel
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
50
)
is_public
=
models
.
BooleanField
(
default
=
False
)
related
=
models
.
ForeignKey
(
RelatedModel
)
objects
=
RestrictedManager
()
plain_manager
=
models
.
Manager
()
def
__str__
(
self
):
return
self
.
name
@python_2_unicode_compatible
class
OneToOneRestrictedModel
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
50
)
is_public
=
models
.
BooleanField
(
default
=
False
)
related
=
models
.
OneToOneField
(
RelatedModel
)
objects
=
RestrictedManager
()
plain_manager
=
models
.
Manager
()
def
__str__
(
self
):
return
self
.
name
tests/custom_managers/tests.py
Dosyayı görüntüle @
0dab07e5
...
...
@@ -3,7 +3,8 @@ from __future__ import unicode_literals
from
django.test
import
TestCase
from
django.utils
import
six
from
.models
import
Person
,
FunPerson
,
Book
,
Car
,
PersonManager
,
PublishedBookManager
from
.models
import
(
Book
,
Car
,
FunPerson
,
OneToOneRestrictedModel
,
Person
,
PersonManager
,
PublishedBookManager
,
RelatedModel
,
RestrictedModel
)
class
CustomManagerTests
(
TestCase
):
...
...
@@ -463,3 +464,47 @@ class CustomManagerTests(TestCase):
lambda
c
:
c
.
first_name
,
ordered
=
False
,
)
class
CustomManagersRegressTestCase
(
TestCase
):
def
test_filtered_default_manager
(
self
):
"""Even though the default manager filters out some records,
we must still be able to save (particularly, save by updating
existing records) those filtered instances. This is a
regression test for #8990, #9527"""
related
=
RelatedModel
.
objects
.
create
(
name
=
"xyzzy"
)
obj
=
RestrictedModel
.
objects
.
create
(
name
=
"hidden"
,
related
=
related
)
obj
.
name
=
"still hidden"
obj
.
save
()
# If the hidden object wasn't seen during the save process,
# there would now be two objects in the database.
self
.
assertEqual
(
RestrictedModel
.
plain_manager
.
count
(),
1
)
def
test_delete_related_on_filtered_manager
(
self
):
"""Deleting related objects should also not be distracted by a
restricted manager on the related object. This is a regression
test for #2698."""
related
=
RelatedModel
.
objects
.
create
(
name
=
"xyzzy"
)
for
name
,
public
in
((
'one'
,
True
),
(
'two'
,
False
),
(
'three'
,
False
)):
RestrictedModel
.
objects
.
create
(
name
=
name
,
is_public
=
public
,
related
=
related
)
obj
=
RelatedModel
.
objects
.
get
(
name
=
"xyzzy"
)
obj
.
delete
()
# All of the RestrictedModel instances should have been
# deleted, since they *all* pointed to the RelatedModel. If
# the default manager is used, only the public one will be
# deleted.
self
.
assertEqual
(
len
(
RestrictedModel
.
plain_manager
.
all
()),
0
)
def
test_delete_one_to_one_manager
(
self
):
# The same test case as the last one, but for one-to-one
# models, which are implemented slightly different internally,
# so it's a different code path.
obj
=
RelatedModel
.
objects
.
create
(
name
=
"xyzzy"
)
OneToOneRestrictedModel
.
objects
.
create
(
name
=
"foo"
,
is_public
=
False
,
related
=
obj
)
obj
=
RelatedModel
.
objects
.
get
(
name
=
"xyzzy"
)
obj
.
delete
()
self
.
assertEqual
(
len
(
OneToOneRestrictedModel
.
plain_manager
.
all
()),
0
)
tests/custom_managers_regress/__init__.py
deleted
100644 → 0
Dosyayı görüntüle @
d42a45de
tests/custom_managers_regress/models.py
deleted
100644 → 0
Dosyayı görüntüle @
d42a45de
"""
Regression tests for custom manager classes.
"""
from
django.db
import
models
from
django.utils.encoding
import
python_2_unicode_compatible
class
RestrictedManager
(
models
.
Manager
):
"""
A manager that filters out non-public instances.
"""
def
get_queryset
(
self
):
return
super
(
RestrictedManager
,
self
)
.
get_queryset
()
.
filter
(
is_public
=
True
)
@python_2_unicode_compatible
class
RelatedModel
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
50
)
def
__str__
(
self
):
return
self
.
name
@python_2_unicode_compatible
class
RestrictedModel
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
50
)
is_public
=
models
.
BooleanField
(
default
=
False
)
related
=
models
.
ForeignKey
(
RelatedModel
)
objects
=
RestrictedManager
()
plain_manager
=
models
.
Manager
()
def
__str__
(
self
):
return
self
.
name
@python_2_unicode_compatible
class
OneToOneRestrictedModel
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
50
)
is_public
=
models
.
BooleanField
(
default
=
False
)
related
=
models
.
OneToOneField
(
RelatedModel
)
objects
=
RestrictedManager
()
plain_manager
=
models
.
Manager
()
def
__str__
(
self
):
return
self
.
name
tests/custom_managers_regress/tests.py
deleted
100644 → 0
Dosyayı görüntüle @
d42a45de
from
__future__
import
unicode_literals
from
django.test
import
TestCase
from
.models
import
RelatedModel
,
RestrictedModel
,
OneToOneRestrictedModel
class
CustomManagersRegressTestCase
(
TestCase
):
def
test_filtered_default_manager
(
self
):
"""Even though the default manager filters out some records,
we must still be able to save (particularly, save by updating
existing records) those filtered instances. This is a
regression test for #8990, #9527"""
related
=
RelatedModel
.
objects
.
create
(
name
=
"xyzzy"
)
obj
=
RestrictedModel
.
objects
.
create
(
name
=
"hidden"
,
related
=
related
)
obj
.
name
=
"still hidden"
obj
.
save
()
# If the hidden object wasn't seen during the save process,
# there would now be two objects in the database.
self
.
assertEqual
(
RestrictedModel
.
plain_manager
.
count
(),
1
)
def
test_delete_related_on_filtered_manager
(
self
):
"""Deleting related objects should also not be distracted by a
restricted manager on the related object. This is a regression
test for #2698."""
related
=
RelatedModel
.
objects
.
create
(
name
=
"xyzzy"
)
for
name
,
public
in
((
'one'
,
True
),
(
'two'
,
False
),
(
'three'
,
False
)):
RestrictedModel
.
objects
.
create
(
name
=
name
,
is_public
=
public
,
related
=
related
)
obj
=
RelatedModel
.
objects
.
get
(
name
=
"xyzzy"
)
obj
.
delete
()
# All of the RestrictedModel instances should have been
# deleted, since they *all* pointed to the RelatedModel. If
# the default manager is used, only the public one will be
# deleted.
self
.
assertEqual
(
len
(
RestrictedModel
.
plain_manager
.
all
()),
0
)
def
test_delete_one_to_one_manager
(
self
):
# The same test case as the last one, but for one-to-one
# models, which are implemented slightly different internally,
# so it's a different code path.
obj
=
RelatedModel
.
objects
.
create
(
name
=
"xyzzy"
)
OneToOneRestrictedModel
.
objects
.
create
(
name
=
"foo"
,
is_public
=
False
,
related
=
obj
)
obj
=
RelatedModel
.
objects
.
get
(
name
=
"xyzzy"
)
obj
.
delete
()
self
.
assertEqual
(
len
(
OneToOneRestrictedModel
.
plain_manager
.
all
()),
0
)
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