4
4
import datetime
5
5
6
6
from django .apps .registry import Apps , apps
7
- from django .contrib .contenttypes import management
7
+ from django .conf import settings
8
+ from django .contrib .contenttypes import management as contenttypes_management
8
9
from django .contrib .contenttypes .fields import (
9
10
GenericForeignKey , GenericRelation ,
10
11
)
11
12
from django .contrib .contenttypes .models import ContentType
12
13
from django .contrib .sites .models import Site
13
- from django .core import checks
14
- from django .db import connections , models
15
- from django .test import SimpleTestCase , TestCase , mock , override_settings
14
+ from django .core import checks , management
15
+ from django .db import connections , migrations , models
16
+ from django .test import (
17
+ SimpleTestCase , TestCase , TransactionTestCase , mock , override_settings ,
18
+ )
16
19
from django .test .utils import captured_stdout , isolate_apps
17
20
from django .utils .encoding import force_str , force_text
18
21
@@ -388,9 +391,9 @@ def test_interactive_true(self):
388
391
interactive mode of update_contenttypes() (the default) should delete
389
392
stale contenttypes.
390
393
"""
391
- management .input = lambda x : force_str ("yes" )
394
+ contenttypes_management .input = lambda x : force_str ("yes" )
392
395
with captured_stdout () as stdout :
393
- management .update_contenttypes (self .app_config )
396
+ contenttypes_management .update_contenttypes (self .app_config )
394
397
self .assertIn ("Deleting stale content type" , stdout .getvalue ())
395
398
self .assertEqual (ContentType .objects .count (), self .before_count )
396
399
@@ -400,7 +403,7 @@ def test_interactive_false(self):
400
403
content types.
401
404
"""
402
405
with captured_stdout () as stdout :
403
- management .update_contenttypes (self .app_config , interactive = False )
406
+ contenttypes_management .update_contenttypes (self .app_config , interactive = False )
404
407
self .assertIn ("Stale content types remain." , stdout .getvalue ())
405
408
self .assertEqual (ContentType .objects .count (), self .before_count + 1 )
406
409
@@ -411,7 +414,7 @@ def test_unavailable_content_type_model(self):
411
414
"""
412
415
apps = Apps ()
413
416
with self .assertNumQueries (0 ):
414
- management .update_contenttypes (self .app_config , interactive = False , verbosity = 0 , apps = apps )
417
+ contenttypes_management .update_contenttypes (self .app_config , interactive = False , verbosity = 0 , apps = apps )
415
418
self .assertEqual (ContentType .objects .count (), self .before_count + 1 )
416
419
417
420
@@ -445,3 +448,68 @@ def test_multidb(self):
445
448
with self .assertNumQueries (0 , using = 'default' ), \
446
449
self .assertNumQueries (1 , using = 'other' ):
447
450
ContentType .objects .get_for_model (Author )
451
+
452
+
453
+ @override_settings (
454
+ MIGRATION_MODULES = dict (settings .MIGRATION_MODULES , contenttypes_tests = 'contenttypes_tests.operations_migrations' ),
455
+ )
456
+ class ContentTypeOperationsTests (TransactionTestCase ):
457
+ available_apps = ['django.contrib.contenttypes' , 'contenttypes_tests' ]
458
+
459
+ def setUp (self ):
460
+ app_config = apps .get_app_config ('contenttypes_tests' )
461
+ models .signals .post_migrate .connect (self .assertOperationsInjected , sender = app_config )
462
+
463
+ def tearDown (self ):
464
+ app_config = apps .get_app_config ('contenttypes_tests' )
465
+ models .signals .post_migrate .disconnect (self .assertOperationsInjected , sender = app_config )
466
+
467
+ def assertOperationsInjected (self , plan , ** kwargs ):
468
+ for migration , _backward in plan :
469
+ operations = iter (migration .operations )
470
+ for operation in operations :
471
+ if isinstance (operation , migrations .RenameModel ):
472
+ next_operation = next (operations )
473
+ self .assertIsInstance (next_operation , contenttypes_management .RenameContentType )
474
+ self .assertEqual (next_operation .app_label , migration .app_label )
475
+ self .assertEqual (next_operation .old_model , operation .old_name_lower )
476
+ self .assertEqual (next_operation .new_model , operation .new_name_lower )
477
+
478
+ def test_existing_content_type_rename (self ):
479
+ ContentType .objects .create (app_label = 'contenttypes_tests' , model = 'foo' )
480
+ management .call_command (
481
+ 'migrate' , 'contenttypes_tests' , database = 'default' , interactive = False , verbosity = 0 ,
482
+ )
483
+ self .assertFalse (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'foo' ).exists ())
484
+ self .assertTrue (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'renamedfoo' ).exists ())
485
+ management .call_command (
486
+ 'migrate' , 'contenttypes_tests' , 'zero' , database = 'default' , interactive = False , verbosity = 0 ,
487
+ )
488
+ self .assertTrue (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'foo' ).exists ())
489
+ self .assertFalse (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'renamedfoo' ).exists ())
490
+
491
+ def test_missing_content_type_rename_ignore (self ):
492
+ management .call_command (
493
+ 'migrate' , 'contenttypes_tests' , database = 'default' , interactive = False , verbosity = 0 ,
494
+ )
495
+ self .assertFalse (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'foo' ).exists ())
496
+ self .assertTrue (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'renamedfoo' ).exists ())
497
+ management .call_command (
498
+ 'migrate' , 'contenttypes_tests' , 'zero' , database = 'default' , interactive = False , verbosity = 0 ,
499
+ )
500
+ self .assertTrue (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'foo' ).exists ())
501
+ self .assertFalse (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'renamedfoo' ).exists ())
502
+
503
+ def test_content_type_rename_conflict (self ):
504
+ ContentType .objects .create (app_label = 'contenttypes_tests' , model = 'foo' )
505
+ ContentType .objects .create (app_label = 'contenttypes_tests' , model = 'renamedfoo' )
506
+ management .call_command (
507
+ 'migrate' , 'contenttypes_tests' , database = 'default' , interactive = False , verbosity = 0 ,
508
+ )
509
+ self .assertTrue (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'foo' ).exists ())
510
+ self .assertTrue (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'renamedfoo' ).exists ())
511
+ management .call_command (
512
+ 'migrate' , 'contenttypes_tests' , 'zero' , database = 'default' , interactive = False , verbosity = 0 ,
513
+ )
514
+ self .assertTrue (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'foo' ).exists ())
515
+ self .assertTrue (ContentType .objects .filter (app_label = 'contenttypes_tests' , model = 'renamedfoo' ).exists ())
0 commit comments