| 1 |
from django.apps import apps |
| 2 |
from django.contrib.contenttypes.models import ContentType, ContentTypeManager |
| 3 |
from django.contrib.contenttypes.prefetch import GenericPrefetch |
| 4 |
from django.db import models |
| 5 |
from django.db.migrations.state import ModelState, ProjectState |
| 6 |
from django.test import TestCase, override_settings |
| 7 |
from django.test.utils import isolate_apps |
| 8 |
|
| 9 |
from .models import Author, ConcreteModel, FooWithUrl, ProxyModel |
| 10 |
|
| 11 |
|
| 12 |
class ContentTypesTests(TestCase): |
| 13 |
def setUp(self): |
| 14 |
ContentType.objects.clear_cache() |
| 15 |
self.addCleanup(ContentType.objects.clear_cache) |
| 16 |
|
| 17 |
def test_lookup_cache(self): |
| 18 |
""" |
| 19 |
The content type cache (see ContentTypeManager) works correctly. |
| 20 |
Lookups for a particular content type -- by model, ID, or natural key |
| 21 |
-- should hit the database only on the first lookup. |
| 22 |
""" |
| 23 |
# At this point, a lookup for a ContentType should hit the DB |
| 24 |
with self.assertNumQueries(1): |
| 25 |
ContentType.objects.get_for_model(ContentType) |
| 26 |
|
| 27 |
# A second hit, though, won't hit the DB, nor will a lookup by ID |
| 28 |
# or natural key |
| 29 |
with self.assertNumQueries(0): |
| 30 |
ct = ContentType.objects.get_for_model(ContentType) |
| 31 |
with self.assertNumQueries(0): |
| 32 |
ContentType.objects.get_for_id(ct.id) |
| 33 |
with self.assertNumQueries(0): |
| 34 |
ContentType.objects.get_by_natural_key("contenttypes", "contenttype") |
| 35 |
|
| 36 |
# Once we clear the cache, another lookup will again hit the DB |
| 37 |
ContentType.objects.clear_cache() |
| 38 |
with self.assertNumQueries(1): |
| 39 |
ContentType.objects.get_for_model(ContentType) |
| 40 |
|
| 41 |
# The same should happen with a lookup by natural key |
| 42 |
ContentType.objects.clear_cache() |
| 43 |
with self.assertNumQueries(1): |
| 44 |
ContentType.objects.get_by_natural_key("contenttypes", "contenttype") |
| 45 |
# And a second hit shouldn't hit the DB |
| 46 |
with self.assertNumQueries(0): |
| 47 |
ContentType.objects.get_by_natural_key("contenttypes", "contenttype") |
| 48 |
|
| 49 |
def test_get_for_models_creation(self): |
| 50 |
ContentType.objects.all().delete() |
| 51 |
with self.assertNumQueries(4): |
| 52 |
cts = ContentType.objects.get_for_models( |
| 53 |
ContentType, FooWithUrl, ProxyModel, ConcreteModel |
| 54 |
) |
| 55 |
self.assertEqual( |
| 56 |
cts, |
| 57 |
{ |
| 58 |
ContentType: ContentType.objects.get_for_model(ContentType), |
| 59 |
FooWithUrl: ContentType.objects.get_for_model(FooWithUrl), |
| 60 |
ProxyModel: ContentType.objects.get_for_model(ProxyModel), |
| 61 |
ConcreteModel: ContentType.objects.get_for_model(ConcreteModel), |
| 62 |
}, |
| 63 |
) |
| 64 |
|
| 65 |
def test_get_for_models_empty_cache(self): |
| 66 |
# Empty cache. |
| 67 |
with self.assertNumQueries(1): |
| 68 |
cts = ContentType.objects.get_for_models( |
| 69 |
ContentType, FooWithUrl, ProxyModel, ConcreteModel |
| 70 |
) |
| 71 |
self.assertEqual( |
| 72 |
cts, |
| 73 |
{ |
| 74 |
ContentType: ContentType.objects.get_for_model(ContentType), |
| 75 |
FooWithUrl: ContentType.objects.get_for_model(FooWithUrl), |
| 76 |
ProxyModel: ContentType.objects.get_for_model(ProxyModel), |
| 77 |
ConcreteModel: ContentType.objects.get_for_model(ConcreteModel), |
| 78 |
}, |
| 79 |
) |
| 80 |
|
| 81 |
def test_get_for_models_partial_cache(self): |
| 82 |
# Partial cache |
| 83 |
ContentType.objects.get_for_model(ContentType) |
| 84 |
with self.assertNumQueries(1): |
| 85 |
cts = ContentType.objects.get_for_models(ContentType, FooWithUrl) |
| 86 |
self.assertEqual( |
| 87 |
cts, |
| 88 |
{ |
| 89 |
ContentType: ContentType.objects.get_for_model(ContentType), |
| 90 |
FooWithUrl: ContentType.objects.get_for_model(FooWithUrl), |
| 91 |
}, |
| 92 |
) |
| 93 |
|
| 94 |
def test_get_for_models_migrations(self): |
| 95 |
state = ProjectState.from_apps(apps.get_app_config("contenttypes")) |
| 96 |
ContentType = state.apps.get_model("contenttypes", "ContentType") |
| 97 |
cts = ContentType.objects.get_for_models(ContentType) |
| 98 |
self.assertEqual( |
| 99 |
cts, {ContentType: ContentType.objects.get_for_model(ContentType)} |
| 100 |
) |
| 101 |
|
| 102 |
@isolate_apps("contenttypes_tests") |
| 103 |
def test_get_for_models_migrations_create_model(self): |
| 104 |
state = ProjectState.from_apps(apps.get_app_config("contenttypes")) |
| 105 |
|
| 106 |
class Foo(models.Model): |
| 107 |
class Meta: |
| 108 |
app_label = "contenttypes_tests" |
| 109 |
|
| 110 |
state.add_model(ModelState.from_model(Foo)) |
| 111 |
ContentType = state.apps.get_model("contenttypes", "ContentType") |
| 112 |
cts = ContentType.objects.get_for_models(FooWithUrl, Foo) |
| 113 |
self.assertEqual( |
| 114 |
cts, |
| 115 |
{ |
| 116 |
Foo: ContentType.objects.get_for_model(Foo), |
| 117 |
FooWithUrl: ContentType.objects.get_for_model(FooWithUrl), |
| 118 |
}, |
| 119 |
) |
| 120 |
|
| 121 |
def test_get_for_models_full_cache(self): |
| 122 |
# Full cache |
| 123 |
ContentType.objects.get_for_model(ContentType) |
| 124 |
ContentType.objects.get_for_model(FooWithUrl) |
| 125 |
with self.assertNumQueries(0): |
| 126 |
cts = ContentType.objects.get_for_models(ContentType, FooWithUrl) |
| 127 |
self.assertEqual( |
| 128 |
cts, |
| 129 |
{ |
| 130 |
ContentType: ContentType.objects.get_for_model(ContentType), |
| 131 |
FooWithUrl: ContentType.objects.get_for_model(FooWithUrl), |
| 132 |
}, |
| 133 |
) |
| 134 |
|
| 135 |
@isolate_apps("contenttypes_tests") |
| 136 |
def test_get_for_model_create_contenttype(self): |
| 137 |
""" |
| 138 |
ContentTypeManager.get_for_model() creates the corresponding content |
| 139 |
type if it doesn't exist in the database. |
| 140 |
""" |
| 141 |
|
| 142 |
class ModelCreatedOnTheFly(models.Model): |
| 143 |
name = models.CharField() |
| 144 |
|
| 145 |
ct = ContentType.objects.get_for_model(ModelCreatedOnTheFly) |
| 146 |
self.assertEqual(ct.app_label, "contenttypes_tests") |
| 147 |
self.assertEqual(ct.model, "modelcreatedonthefly") |
| 148 |
self.assertEqual(str(ct), "modelcreatedonthefly") |
| 149 |
|
| 150 |
def test_get_for_concrete_model(self): |
| 151 |
""" |
| 152 |
Make sure the `for_concrete_model` kwarg correctly works |
| 153 |
with concrete, proxy and deferred models |
| 154 |
""" |
| 155 |
concrete_model_ct = ContentType.objects.get_for_model(ConcreteModel) |
| 156 |
self.assertEqual( |
| 157 |
concrete_model_ct, ContentType.objects.get_for_model(ProxyModel) |
| 158 |
) |
| 159 |
self.assertEqual( |
| 160 |
concrete_model_ct, |
| 161 |
ContentType.objects.get_for_model(ConcreteModel, for_concrete_model=False), |
| 162 |
) |
| 163 |
|
| 164 |
proxy_model_ct = ContentType.objects.get_for_model( |
| 165 |
ProxyModel, for_concrete_model=False |
| 166 |
) |
| 167 |
self.assertNotEqual(concrete_model_ct, proxy_model_ct) |
| 168 |
|
| 169 |
# Make sure deferred model are correctly handled |
| 170 |
ConcreteModel.objects.create(name="Concrete") |
| 171 |
DeferredConcreteModel = ConcreteModel.objects.only("pk").get().__class__ |
| 172 |
DeferredProxyModel = ProxyModel.objects.only("pk").get().__class__ |
| 173 |
|
| 174 |
self.assertEqual( |
| 175 |
concrete_model_ct, ContentType.objects.get_for_model(DeferredConcreteModel) |
| 176 |
) |
| 177 |
self.assertEqual( |
| 178 |
concrete_model_ct, |
| 179 |
ContentType.objects.get_for_model( |
| 180 |
DeferredConcreteModel, for_concrete_model=False |
| 181 |
), |
| 182 |
) |
| 183 |
self.assertEqual( |
| 184 |
concrete_model_ct, ContentType.objects.get_for_model(DeferredProxyModel) |
| 185 |
) |
| 186 |
self.assertEqual( |
| 187 |
proxy_model_ct, |
| 188 |
ContentType.objects.get_for_model( |
| 189 |
DeferredProxyModel, for_concrete_model=False |
| 190 |
), |
| 191 |
) |
| 192 |
|
| 193 |
def test_get_for_concrete_models(self): |
| 194 |
""" |
| 195 |
Make sure the `for_concrete_models` kwarg correctly works |
| 196 |
with concrete, proxy and deferred models. |
| 197 |
""" |
| 198 |
concrete_model_ct = ContentType.objects.get_for_model(ConcreteModel) |
| 199 |
|
| 200 |
cts = ContentType.objects.get_for_models(ConcreteModel, ProxyModel) |
| 201 |
self.assertEqual( |
| 202 |
cts, |
| 203 |
{ |
| 204 |
ConcreteModel: concrete_model_ct, |
| 205 |
ProxyModel: concrete_model_ct, |
| 206 |
}, |
| 207 |
) |
| 208 |
|
| 209 |
proxy_model_ct = ContentType.objects.get_for_model( |
| 210 |
ProxyModel, for_concrete_model=False |
| 211 |
) |
| 212 |
cts = ContentType.objects.get_for_models( |
| 213 |
ConcreteModel, ProxyModel, for_concrete_models=False |
| 214 |
) |
| 215 |
self.assertEqual( |
| 216 |
cts, |
| 217 |
{ |
| 218 |
ConcreteModel: concrete_model_ct, |
| 219 |
ProxyModel: proxy_model_ct, |
| 220 |
}, |
| 221 |
) |
| 222 |
|
| 223 |
# Make sure deferred model are correctly handled |
| 224 |
ConcreteModel.objects.create(name="Concrete") |
| 225 |
DeferredConcreteModel = ConcreteModel.objects.only("pk").get().__class__ |
| 226 |
DeferredProxyModel = ProxyModel.objects.only("pk").get().__class__ |
| 227 |
|
| 228 |
cts = ContentType.objects.get_for_models( |
| 229 |
DeferredConcreteModel, DeferredProxyModel |
| 230 |
) |
| 231 |
self.assertEqual( |
| 232 |
cts, |
| 233 |
{ |
| 234 |
DeferredConcreteModel: concrete_model_ct, |
| 235 |
DeferredProxyModel: concrete_model_ct, |
| 236 |
}, |
| 237 |
) |
| 238 |
|
| 239 |
cts = ContentType.objects.get_for_models( |
| 240 |
DeferredConcreteModel, DeferredProxyModel, for_concrete_models=False |
| 241 |
) |
| 242 |
self.assertEqual( |
| 243 |
cts, |
| 244 |
{ |
| 245 |
DeferredConcreteModel: concrete_model_ct, |
| 246 |
DeferredProxyModel: proxy_model_ct, |
| 247 |
}, |
| 248 |
) |
| 249 |
|
| 250 |
def test_cache_not_shared_between_managers(self): |
| 251 |
with self.assertNumQueries(1): |
| 252 |
ContentType.objects.get_for_model(ContentType) |
| 253 |
with self.assertNumQueries(0): |
| 254 |
ContentType.objects.get_for_model(ContentType) |
| 255 |
other_manager = ContentTypeManager() |
| 256 |
other_manager.model = ContentType |
| 257 |
with self.assertNumQueries(1): |
| 258 |
other_manager.get_for_model(ContentType) |
| 259 |
with self.assertNumQueries(0): |
| 260 |
other_manager.get_for_model(ContentType) |
| 261 |
|
| 262 |
def test_missing_model(self): |
| 263 |
""" |
| 264 |
Displaying content types in admin (or anywhere) doesn't break on |
| 265 |
leftover content type records in the DB for which no model is defined |
| 266 |
anymore. |
| 267 |
""" |
| 268 |
ct = ContentType.objects.create( |
| 269 |
app_label="contenttypes", |
| 270 |
model="OldModel", |
| 271 |
) |
| 272 |
self.assertEqual(str(ct), "OldModel") |
| 273 |
self.assertIsNone(ct.model_class()) |
| 274 |
|
| 275 |
# Stale ContentTypes can be fetched like any other object. |
| 276 |
ct_fetched = ContentType.objects.get_for_id(ct.pk) |
| 277 |
self.assertIsNone(ct_fetched.model_class()) |
| 278 |
|
| 279 |
def test_missing_model_with_existing_model_name(self): |
| 280 |
""" |
| 281 |
Displaying content types in admin (or anywhere) doesn't break on |
| 282 |
leftover content type records in the DB for which no model is defined |
| 283 |
anymore, even if a model with the same name exists in another app. |
| 284 |
""" |
| 285 |
# Create a stale ContentType that matches the name of an existing |
| 286 |
# model. |
| 287 |
ContentType.objects.create(app_label="contenttypes", model="author") |
| 288 |
ContentType.objects.clear_cache() |
| 289 |
# get_for_models() should work as expected for existing models. |
| 290 |
cts = ContentType.objects.get_for_models(ContentType, Author) |
| 291 |
self.assertEqual( |
| 292 |
cts, |
| 293 |
{ |
| 294 |
ContentType: ContentType.objects.get_for_model(ContentType), |
| 295 |
Author: ContentType.objects.get_for_model(Author), |
| 296 |
}, |
| 297 |
) |
| 298 |
|
| 299 |
def test_str(self): |
| 300 |
ct = ContentType.objects.get(app_label="contenttypes_tests", model="site") |
| 301 |
self.assertEqual(str(ct), "Contenttypes_Tests | site") |
| 302 |
|
| 303 |
def test_str_auth(self): |
| 304 |
ct = ContentType.objects.get(app_label="auth", model="group") |
| 305 |
self.assertEqual(str(ct), "Authentication and Authorization | group") |
| 306 |
|
| 307 |
def test_name(self): |
| 308 |
ct = ContentType.objects.get(app_label="contenttypes_tests", model="site") |
| 309 |
self.assertEqual(ct.name, "site") |
| 310 |
|
| 311 |
def test_app_labeled_name(self): |
| 312 |
ct = ContentType.objects.get(app_label="contenttypes_tests", model="site") |
| 313 |
self.assertEqual(ct.app_labeled_name, "Contenttypes_Tests | site") |
| 314 |
|
| 315 |
def test_name_unknown_model(self): |
| 316 |
ct = ContentType(app_label="contenttypes_tests", model="unknown") |
| 317 |
self.assertEqual(ct.name, "unknown") |
| 318 |
|
| 319 |
def test_app_labeled_name_unknown_model(self): |
| 320 |
ct = ContentType(app_label="contenttypes_tests", model="unknown") |
| 321 |
self.assertEqual(ct.app_labeled_name, "unknown") |
| 322 |
|
| 323 |
|
| 324 |
class TestRouter: |
| 325 |
def db_for_read(self, model, **hints): |
| 326 |
return "other" |
| 327 |
|
| 328 |
def db_for_write(self, model, **hints): |
| 329 |
return "default" |
| 330 |
|
| 331 |
def allow_relation(self, obj1, obj2, **hints): |
| 332 |
return True |
| 333 |
|
| 334 |
|
| 335 |
@override_settings(DATABASE_ROUTERS=[TestRouter()]) |
| 336 |
class ContentTypesMultidbTests(TestCase): |
| 337 |
databases = {"default", "other"} |
| 338 |
|
| 339 |
def test_multidb(self): |
| 340 |
""" |
| 341 |
When using multiple databases, ContentType.objects.get_for_model() uses |
| 342 |
db_for_read(). |
| 343 |
""" |
| 344 |
ContentType.objects.clear_cache() |
| 345 |
with ( |
| 346 |
self.assertNumQueries(0, using="default"), |
| 347 |
self.assertNumQueries(1, using="other"), |
| 348 |
): |
| 349 |
ContentType.objects.get_for_model(Author) |
| 350 |
|
| 351 |
|
| 352 |
class GenericPrefetchTests(TestCase): |
| 353 |
def test_querysets_required(self): |
| 354 |
msg = ( |
| 355 |
"GenericPrefetch.__init__() missing 1 required " |
| 356 |
"positional argument: 'querysets'" |
| 357 |
) |
| 358 |
with self.assertRaisesMessage(TypeError, msg): |
| 359 |
GenericPrefetch("question") |
| 360 |
|
| 361 |
def test_values_queryset(self): |
| 362 |
msg = "Prefetch querysets cannot use raw(), values(), and values_list()." |
| 363 |
with self.assertRaisesMessage(ValueError, msg): |
| 364 |
GenericPrefetch("question", [Author.objects.values("pk")]) |
| 365 |
with self.assertRaisesMessage(ValueError, msg): |
| 366 |
GenericPrefetch("question", [Author.objects.values_list("pk")]) |
| 367 |
|
| 368 |
def test_raw_queryset(self): |
| 369 |
msg = "Prefetch querysets cannot use raw(), values(), and values_list()." |
| 370 |
with self.assertRaisesMessage(ValueError, msg): |
| 371 |
GenericPrefetch("question", [Author.objects.raw("select pk from author")]) |