mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	refactor migration test case
This commit is contained in:
		| @@ -1,52 +1,11 @@ | ||||
| import os | ||||
| import shutil | ||||
| from pathlib import Path | ||||
| 
 | ||||
| from django.apps import apps | ||||
| from django.conf import settings | ||||
| from django.db import connection | ||||
| from django.db.migrations.executor import MigrationExecutor | ||||
| from django.test import TestCase, TransactionTestCase, override_settings | ||||
| from django.test import override_settings | ||||
| 
 | ||||
| from documents.models import Document | ||||
| from documents.parsers import get_default_file_extension | ||||
| from documents.tests.utils import DirectoriesMixin | ||||
| 
 | ||||
| 
 | ||||
| class TestMigrations(TransactionTestCase): | ||||
| 
 | ||||
|     @property | ||||
|     def app(self): | ||||
|         return apps.get_containing_app_config(type(self).__module__).name | ||||
| 
 | ||||
|     migrate_from = None | ||||
|     migrate_to = None | ||||
| 
 | ||||
|     def setUp(self): | ||||
|         super(TestMigrations, self).setUp() | ||||
| 
 | ||||
|         assert self.migrate_from and self.migrate_to, \ | ||||
|             "TestCase '{}' must define migrate_from and migrate_to     properties".format(type(self).__name__) | ||||
|         self.migrate_from = [(self.app, self.migrate_from)] | ||||
|         self.migrate_to = [(self.app, self.migrate_to)] | ||||
|         executor = MigrationExecutor(connection) | ||||
|         old_apps = executor.loader.project_state(self.migrate_from).apps | ||||
| 
 | ||||
|         # Reverse to the original migration | ||||
|         executor.migrate(self.migrate_from) | ||||
| 
 | ||||
|         self.setUpBeforeMigration(old_apps) | ||||
| 
 | ||||
|         # Run the migration to test | ||||
|         executor = MigrationExecutor(connection) | ||||
|         executor.loader.build_graph()  # reload. | ||||
|         executor.migrate(self.migrate_to) | ||||
| 
 | ||||
|         self.apps = executor.loader.project_state(self.migrate_to).apps | ||||
| 
 | ||||
|     def setUpBeforeMigration(self, apps): | ||||
|         pass | ||||
| 
 | ||||
| from documents.tests.utils import DirectoriesMixin, TestMigrations | ||||
| 
 | ||||
| STORAGE_TYPE_UNENCRYPTED = "unencrypted" | ||||
| STORAGE_TYPE_GPG = "gpg" | ||||
| @@ -4,7 +4,10 @@ import tempfile | ||||
| from collections import namedtuple | ||||
| from contextlib import contextmanager | ||||
|  | ||||
| from django.test import override_settings | ||||
| from django.apps import apps | ||||
| from django.db import connection | ||||
| from django.db.migrations.executor import MigrationExecutor | ||||
| from django.test import override_settings, TransactionTestCase | ||||
|  | ||||
|  | ||||
| def setup_directories(): | ||||
| @@ -79,3 +82,38 @@ class DirectoriesMixin: | ||||
|     def tearDown(self) -> None: | ||||
|         super(DirectoriesMixin, self).tearDown() | ||||
|         remove_dirs(self.dirs) | ||||
|  | ||||
|  | ||||
| class TestMigrations(TransactionTestCase): | ||||
|  | ||||
|     @property | ||||
|     def app(self): | ||||
|         return apps.get_containing_app_config(type(self).__module__).name | ||||
|  | ||||
|     migrate_from = None | ||||
|     migrate_to = None | ||||
|  | ||||
|     def setUp(self): | ||||
|         super(TestMigrations, self).setUp() | ||||
|  | ||||
|         assert self.migrate_from and self.migrate_to, \ | ||||
|             "TestCase '{}' must define migrate_from and migrate_to     properties".format(type(self).__name__) | ||||
|         self.migrate_from = [(self.app, self.migrate_from)] | ||||
|         self.migrate_to = [(self.app, self.migrate_to)] | ||||
|         executor = MigrationExecutor(connection) | ||||
|         old_apps = executor.loader.project_state(self.migrate_from).apps | ||||
|  | ||||
|         # Reverse to the original migration | ||||
|         executor.migrate(self.migrate_from) | ||||
|  | ||||
|         self.setUpBeforeMigration(old_apps) | ||||
|  | ||||
|         # Run the migration to test | ||||
|         executor = MigrationExecutor(connection) | ||||
|         executor.loader.build_graph()  # reload. | ||||
|         executor.migrate(self.migrate_to) | ||||
|  | ||||
|         self.apps = executor.loader.project_state(self.migrate_to).apps | ||||
|  | ||||
|     def setUpBeforeMigration(self, apps): | ||||
|         pass | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 jonaswinkler
					jonaswinkler