| | 64 | |
| | 65 | def test_remove_stale_contenttypes_with_no_migrations(self): |
| | 66 | project_path = tempfile.mkdtemp() |
| | 67 | try: |
| | 68 | databases = { |
| | 69 | 'default': { |
| | 70 | 'ENGINE': 'django.db.backends.sqlite3', |
| | 71 | 'NAME': os.path.join(project_path, 'db.sqlite3'), |
| | 72 | } |
| | 73 | } |
| | 74 | |
| | 75 | with open(os.path.join(project_path, 'no_migrations.py'), 'w') as fp: |
| | 76 | fp.write(SETTINGS % {'databases': databases}) |
| | 77 | |
| | 78 | with open(os.devnull, 'wb') as devnull: |
| | 79 | cmd = [ |
| | 80 | sys.executable, |
| | 81 | '-m', 'django', |
| | 82 | 'migrate', |
| | 83 | '--settings', 'no_migrations', |
| | 84 | '--pythonpath', project_path, |
| | 85 | ] |
| | 86 | returncode = subprocess.call(cmd, stdout=devnull, stderr=devnull) |
| | 87 | |
| | 88 | # Migrate command ran without errors. |
| | 89 | self.assertEqual(returncode, 0) |
| | 90 | |
| | 91 | connections.databases['no_migrations'] = databases['default'] |
| | 92 | try: |
| | 93 | ct_manager = ContentType.objects.db_manager('no_migrations') |
| | 94 | ct_manager.create(app_label='contenttypes', model='stale_model') |
| | 95 | |
| | 96 | with open(os.devnull, 'wb') as devnull: |
| | 97 | cmd = [ |
| | 98 | sys.executable, |
| | 99 | '-m', 'django', |
| | 100 | 'remove_stale_contenttypes', |
| | 101 | '--settings', 'no_migrations', |
| | 102 | '--pythonpath', project_path, |
| | 103 | ] |
| | 104 | returncode = subprocess.call(cmd, stdout=devnull, stderr=devnull) |
| | 105 | |
| | 106 | # Command ran without errors. |
| | 107 | self.assertEqual(returncode, 0) |
| | 108 | |
| | 109 | # Stale ContentType removed. |
| | 110 | self.assertEqual( |
| | 111 | {(ct.app_label, ct.model) for ct in ct_manager.all()}, |
| | 112 | { |
| | 113 | ('auth', 'group'), |
| | 114 | ('auth', 'permission'), |
| | 115 | ('auth', 'user'), |
| | 116 | ('contenttypes', 'contenttype'), |
| | 117 | }, |
| | 118 | ) |
| | 119 | |
| | 120 | finally: |
| | 121 | connections['no_migrations'].close() |
| | 122 | del connections['no_migrations'] |
| | 123 | del connections.databases['no_migrations'] |
| | 124 | finally: |
| | 125 | shutil.rmtree(project_path) |