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
b49ed4be
Kaydet (Commit)
b49ed4be
authored
Haz 21, 2018
tarafından
Tim Graham
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Corrected location of some contenttypes_tests.
üst
238ed313
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
83 deletions
+61
-83
test_models.py
tests/contenttypes_tests/test_models.py
+17
-65
test_views.py
tests/contenttypes_tests/test_views.py
+44
-18
No files found.
tests/contenttypes_tests/test_models.py
Dosyayı görüntüle @
b49ed4be
from
django.contrib.contenttypes.models
import
ContentType
,
ContentTypeManager
from
django.contrib.contenttypes.models
import
ContentType
,
ContentTypeManager
from
django.contrib.contenttypes.views
import
shortcut
from
django.db
import
models
from
django.contrib.sites.shortcuts
import
get_current_site
from
django.http
import
Http404
,
HttpRequest
from
django.test
import
TestCase
,
override_settings
from
django.test
import
TestCase
,
override_settings
from
django.test.utils
import
isolate_apps
from
.models
import
(
from
.models
import
Author
,
ConcreteModel
,
FooWithUrl
,
ProxyModel
Author
,
ConcreteModel
,
FooWithBrokenAbsoluteUrl
,
FooWithoutUrl
,
FooWithUrl
,
ProxyModel
,
)
class
ContentTypesTests
(
TestCase
):
class
ContentTypesTests
(
TestCase
):
...
@@ -93,6 +89,20 @@ class ContentTypesTests(TestCase):
...
@@ -93,6 +89,20 @@ class ContentTypesTests(TestCase):
FooWithUrl
:
ContentType
.
objects
.
get_for_model
(
FooWithUrl
),
FooWithUrl
:
ContentType
.
objects
.
get_for_model
(
FooWithUrl
),
})
})
@isolate_apps
(
'contenttypes_tests'
)
def
test_get_for_model_create_contenttype
(
self
):
"""
ContentTypeManager.get_for_model() creates the corresponding content
type if it doesn't exist in the database.
"""
class
ModelCreatedOnTheFly
(
models
.
Model
):
name
=
models
.
CharField
()
ct
=
ContentType
.
objects
.
get_for_model
(
ModelCreatedOnTheFly
)
self
.
assertEqual
(
ct
.
app_label
,
'contenttypes_tests'
)
self
.
assertEqual
(
ct
.
model
,
'modelcreatedonthefly'
)
self
.
assertEqual
(
str
(
ct
),
'modelcreatedonthefly'
)
def
test_get_for_concrete_model
(
self
):
def
test_get_for_concrete_model
(
self
):
"""
"""
Make sure the `for_concrete_model` kwarg correctly works
Make sure the `for_concrete_model` kwarg correctly works
...
@@ -172,64 +182,6 @@ class ContentTypesTests(TestCase):
...
@@ -172,64 +182,6 @@ class ContentTypesTests(TestCase):
with
self
.
assertNumQueries
(
0
):
with
self
.
assertNumQueries
(
0
):
other_manager
.
get_for_model
(
ContentType
)
other_manager
.
get_for_model
(
ContentType
)
@override_settings
(
ALLOWED_HOSTS
=
[
'example.com'
])
def
test_shortcut_view
(
self
):
"""
The shortcut view (used for the admin "view on site" functionality)
returns a complete URL regardless of whether the sites framework is
installed.
"""
request
=
HttpRequest
()
request
.
META
=
{
"SERVER_NAME"
:
"Example.com"
,
"SERVER_PORT"
:
"80"
,
}
user_ct
=
ContentType
.
objects
.
get_for_model
(
FooWithUrl
)
obj
=
FooWithUrl
.
objects
.
create
(
name
=
"john"
)
with
self
.
modify_settings
(
INSTALLED_APPS
=
{
'append'
:
'django.contrib.sites'
}):
response
=
shortcut
(
request
,
user_ct
.
id
,
obj
.
id
)
self
.
assertEqual
(
"http://
%
s/users/john/"
%
get_current_site
(
request
)
.
domain
,
response
.
_headers
.
get
(
"location"
)[
1
]
)
with
self
.
modify_settings
(
INSTALLED_APPS
=
{
'remove'
:
'django.contrib.sites'
}):
response
=
shortcut
(
request
,
user_ct
.
id
,
obj
.
id
)
self
.
assertEqual
(
"http://Example.com/users/john/"
,
response
.
_headers
.
get
(
"location"
)[
1
])
def
test_shortcut_view_without_get_absolute_url
(
self
):
"""
The shortcut view (used for the admin "view on site" functionality)
returns 404 when get_absolute_url is not defined.
"""
request
=
HttpRequest
()
request
.
META
=
{
"SERVER_NAME"
:
"Example.com"
,
"SERVER_PORT"
:
"80"
,
}
user_ct
=
ContentType
.
objects
.
get_for_model
(
FooWithoutUrl
)
obj
=
FooWithoutUrl
.
objects
.
create
(
name
=
"john"
)
with
self
.
assertRaises
(
Http404
):
shortcut
(
request
,
user_ct
.
id
,
obj
.
id
)
def
test_shortcut_view_with_broken_get_absolute_url
(
self
):
"""
The shortcut view does not catch an AttributeError raised by
the model's get_absolute_url() method (#8997).
"""
request
=
HttpRequest
()
request
.
META
=
{
"SERVER_NAME"
:
"Example.com"
,
"SERVER_PORT"
:
"80"
,
}
user_ct
=
ContentType
.
objects
.
get_for_model
(
FooWithBrokenAbsoluteUrl
)
obj
=
FooWithBrokenAbsoluteUrl
.
objects
.
create
(
name
=
"john"
)
with
self
.
assertRaises
(
AttributeError
):
shortcut
(
request
,
user_ct
.
id
,
obj
.
id
)
def
test_missing_model
(
self
):
def
test_missing_model
(
self
):
"""
"""
Displaying content types in admin (or anywhere) doesn't break on
Displaying content types in admin (or anywhere) doesn't break on
...
...
tests/contenttypes_tests/test_views.py
Dosyayı görüntüle @
b49ed4be
...
@@ -2,14 +2,15 @@ import datetime
...
@@ -2,14 +2,15 @@ import datetime
from
unittest
import
mock
from
unittest
import
mock
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.contenttypes.views
import
shortcut
from
django.contrib.sites.models
import
Site
from
django.contrib.sites.models
import
Site
from
django.db
import
models
from
django.contrib.sites.shortcuts
import
get_current_site
from
django.http
import
Http404
,
HttpRequest
from
django.test
import
TestCase
,
override_settings
from
django.test
import
TestCase
,
override_settings
from
django.test.utils
import
isolate_apps
from
.models
import
(
from
.models
import
(
Article
,
Author
,
ModelWithNullFKToSite
,
SchemeIncludedURL
,
Article
,
Author
,
FooWithBrokenAbsoluteUrl
,
FooWithoutUrl
,
FooWithUrl
,
Site
as
MockSite
,
ModelWithNullFKToSite
,
SchemeIncludedURL
,
Site
as
MockSite
,
)
)
...
@@ -106,19 +107,44 @@ class ContentTypesViewsTests(TestCase):
...
@@ -106,19 +107,44 @@ class ContentTypesViewsTests(TestCase):
response
=
self
.
client
.
get
(
url
)
response
=
self
.
client
.
get
(
url
)
self
.
assertRedirects
(
response
,
'
%
s'
%
obj
.
get_absolute_url
(),
fetch_redirect_response
=
False
)
self
.
assertRedirects
(
response
,
'
%
s'
%
obj
.
get_absolute_url
(),
fetch_redirect_response
=
False
)
@isolate_apps
(
'contenttypes_tests'
)
def
test_create_contenttype_on_the_spot
(
self
):
"""
ContentTypeManager.get_for_model() creates the corresponding content
type if it doesn't exist in the database.
"""
class
ModelCreatedOnTheFly
(
models
.
Model
):
name
=
models
.
CharField
()
class
Meta
:
class
ShortcutViewTests
(
TestCase
):
verbose_name
=
'a model created on the fly'
def
setUp
(
self
):
self
.
request
=
HttpRequest
()
self
.
request
.
META
=
{
'SERVER_NAME'
:
'Example.com'
,
'SERVER_PORT'
:
'80'
}
ct
=
ContentType
.
objects
.
get_for_model
(
ModelCreatedOnTheFly
)
@override_settings
(
ALLOWED_HOSTS
=
[
'example.com'
])
self
.
assertEqual
(
ct
.
app_label
,
'contenttypes_tests'
)
def
test_not_dependent_on_sites_app
(
self
):
self
.
assertEqual
(
ct
.
model
,
'modelcreatedonthefly'
)
"""
self
.
assertEqual
(
str
(
ct
),
'modelcreatedonthefly'
)
The view returns a complete URL regardless of whether the sites
framework is installed.
"""
user_ct
=
ContentType
.
objects
.
get_for_model
(
FooWithUrl
)
obj
=
FooWithUrl
.
objects
.
create
(
name
=
'john'
)
with
self
.
modify_settings
(
INSTALLED_APPS
=
{
'append'
:
'django.contrib.sites'
}):
response
=
shortcut
(
self
.
request
,
user_ct
.
id
,
obj
.
id
)
self
.
assertEqual
(
'http://
%
s/users/john/'
%
get_current_site
(
self
.
request
)
.
domain
,
response
.
_headers
.
get
(
'location'
)[
1
]
)
with
self
.
modify_settings
(
INSTALLED_APPS
=
{
'remove'
:
'django.contrib.sites'
}):
response
=
shortcut
(
self
.
request
,
user_ct
.
id
,
obj
.
id
)
self
.
assertEqual
(
'http://Example.com/users/john/'
,
response
.
_headers
.
get
(
'location'
)[
1
])
def
test_model_without_get_absolute_url
(
self
):
"""The view returns 404 when Model.get_absolute_url() isn't defined."""
user_ct
=
ContentType
.
objects
.
get_for_model
(
FooWithoutUrl
)
obj
=
FooWithoutUrl
.
objects
.
create
(
name
=
'john'
)
with
self
.
assertRaises
(
Http404
):
shortcut
(
self
.
request
,
user_ct
.
id
,
obj
.
id
)
def
test_model_with_broken_get_absolute_url
(
self
):
"""
The view doesn't catch an AttributeError raised by
Model.get_absolute_url() (#8997).
"""
user_ct
=
ContentType
.
objects
.
get_for_model
(
FooWithBrokenAbsoluteUrl
)
obj
=
FooWithBrokenAbsoluteUrl
.
objects
.
create
(
name
=
'john'
)
with
self
.
assertRaises
(
AttributeError
):
shortcut
(
self
.
request
,
user_ct
.
id
,
obj
.
id
)
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