def test_room_descriptor_DATA_migration(migrator: dtmm.Migrator): #--- create migration state before introducing Room.descriptor: old_state = migrator.apply_initial_migration( ('room', '0010_visit_status_3g')) #--- create a Room: User = old_state.apps.get_model('users', 'User') user = User.objects.create(name="x") # needed for Importstep Importstep = old_state.apps.get_model('room', 'Importstep') importstep = Importstep.objects.create(user=user) # needed for Room Room = old_state.apps.get_model('room', 'Room') to_be_migrated = Room.objects.create(organization="myorg", department="mydep", building="mybldg", room="myroom", row_dist=1.3, seat_dist=0.8, seat_last="r2s3", importstep=importstep) assert getattr(to_be_migrated, 'descriptor', None) is None #--- migrate: new_state = migrator.apply_tested_migration([ ('room', '0011_room_descriptor'), ('room', '0012_room_descriptor_DATA'), ]) #--- assert descriptor is filled correctly: Room = new_state.apps.get_model('room', 'Room') newroom = Room.objects.get() assert newroom.descriptor == "myorg;mydep;mybldg;myroom"
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
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)
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[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_initial0001(migrator: Migrator) -> None: """Tests the initial migration forward application.""" old_state = migrator.apply_initial_migration((app_name, None)) with pytest.raises(LookupError): # This model does not exist before this migration: old_state.apps.get_model(app_name, 'Video') new_state = migrator.apply_tested_migration((app_name, '0001_initial')) model = new_state.apps.get_model(app_name, 'Video') assert model.objects.create()
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)
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()
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 factory(database_name: Optional[str] = None) -> Migrator: migrator = Migrator(database_name) request.addfinalizer(migrator.reset) return migrator
def factory(database_name=None): migrator = Migrator(database_name) request.addfinalizer(migrator.reset) return migrator
def factory(database_name: Optional[str] = None) -> Migrator: # ``Migrator.reset`` is not registered as finalizer here, because # database is flushed by ``transactional_db`` fixture's finalizers return Migrator(database_name)