class TestEndpointMigrationBroken(TransactionTestCase): migrate_from = ('dojo', '0104_endpoint_userinfo_creation') migrate_to = ('dojo', '0105_endpoint_host_migration') def setUp(self): super().setUp() self.migrator = Migrator() self.old_state = self.migrator.apply_initial_migration( self.migrate_from) Endpoint = self.old_state.apps.get_model('dojo', 'Endpoint') self.endpoints = { 'empty': Endpoint.objects.create().pk, 'empty_host': Endpoint.objects.create(host='').pk, 'invalid_host': Endpoint.objects.create(host='foo bar').pk, 'invalid_ip': Endpoint.objects.create(host='127.0.1').pk, 'invalid_port_high': Endpoint.objects.create(host='127.0.0.1:66666').pk, 'invalid_port_low': Endpoint.objects.create(host='127.0.0.1:-1').pk, 'invalid_port_word': Endpoint.objects.create(host='127.0.0.1:port').pk, 'protocol_mismatch': Endpoint.objects.create(protocol='http', host='https://foo.bar').pk, 'port_mismatch': Endpoint.objects.create(host='https://foo.bar', port=80).pk, 'path_mismatch': Endpoint.objects.create(host='https://foo.bar/path1', path='/path1').pk, 'query_mismatch': Endpoint.objects.create(host='https://foo.bar/?key1=value&key2', query='?key1=value&' 'key2=None').pk, 'fragment_mismatch': Endpoint.objects.create(host='https://foo.bar/#fragment', fragment='#fragment').pk, 'missing_host': Endpoint.objects.create(host='file:///etc/passwd').pk, } def tearDown(self): self.migrator.reset() super().tearDown() def test_migration_endpoint_broken(self): with self.assertLogs('dojo.endpoint.utils', 'ERROR') as cm: self.migrator.apply_tested_migration(self.migrate_to) self.assertIn( 'ERROR:dojo.endpoint.utils:It is not possible to migrate database because there is/are {} broken ' 'endpoint(s). Please check logs.'.format(len(self.endpoints)), cm.output)
class MigratorTestCase(TransactionTestCase): """Used when using raw ``unitest`` library for test.""" database_name: ClassVar[Optional[str]] = None old_state: ProjectState new_state: ProjectState #: Part of the end-user API. Used to tell what migrations we are using. migrate_from: ClassVar[MigrationSpec] migrate_to: ClassVar[MigrationSpec] def setUp(self) -> None: """ Regular ``unittest`` styled setup case. What it does? - It starts with defining the initial migration state - Then it allows to run custom method to prepare some data before the migration will happen - Then it applies the migration and saves all states """ super().setUp() self._migrator = Migrator(self.database_name) self.old_state = self._migrator.apply_initial_migration( self.migrate_from, ) self.prepare() self.new_state = self._migrator.apply_tested_migration(self.migrate_to) def prepare(self) -> None: """ Part of the end-user API. Used to prepare some data before the migration process. """ def tearDown(self) -> None: """Used to clean mess up after each test.""" self._migrator.reset() super().tearDown() def _pre_setup(self): self._pre_migrate_receivers, pre_migrate.receivers = ( # noqa: WPS414 pre_migrate.receivers, [], ) self._post_migrate_receivers, post_migrate.receivers = ( # noqa: WPS414 post_migrate.receivers, [], ) super()._pre_setup() def _post_teardown(self): super()._post_teardown() pre_migrate.receivers = self._pre_migrate_receivers post_migrate.receivers = self._post_migrate_receivers
def test_migrator(transactional_db): """We only need this test for coverage.""" migrator = Migrator() old_state = migrator.apply_initial_migration(('main_app', None)) new_state = migrator.apply_tested_migration(('main_app', '0001_initial')) assert isinstance(old_state, ProjectState) assert isinstance(new_state, ProjectState) assert migrator.reset() is None
def test_migrator_list(transactional_db): """We only need this test for coverage.""" migrator = Migrator() old_state = migrator.before([('main_app', None)]) new_state = migrator.after([('main_app', '0001_initial')]) assert isinstance(old_state, ProjectState) assert isinstance(new_state, ProjectState) assert migrator.reset() is None
class MigratorTestCase(TransactionTestCase): """Used when using raw ``unitest`` library for test.""" database_name: ClassVar[Optional[str]] = None old_state: ProjectState new_state: ProjectState #: Part of the end-user API. Used to tell what migrations we are using. migrate_from: ClassVar[_Migration] migrate_to: ClassVar[_Migration] def __init__(self, *args, **kwargs) -> None: """Initializes our :class:`Migrator` instance.""" super().__init__(*args, **kwargs) def setUp(self) -> None: """ Regular ``unittest`` styled setup case. What it does? - It starts with defining the initial migration state - Then it allows to run custom method to prepare some data before the migration will happen - Then it applies the migration and saves all states """ super().setUp() self._pre_setup() self._migrator = Migrator(self.database_name) self.old_state = self._migrator.before(self.migrate_from) self.prepare() self.new_state = self._migrator.after(self.migrate_to) def prepare(self) -> None: """ Part of the end-user API. Used to prepare some data before the migration process. """ def tearDown(self) -> None: """Used to clean mess up after each test.""" self._migrator.reset() super().tearDown()