def test_applies_new_migration_to_sqlite_database(self, fake_get): if self._db_type != 'sqlite': self.skipTest('SQLite tests are disabled.') # replace real migrations with tests migrations. test_migrations = [ (100, 'test.functional.migrations.0100_init'), (101, 'test.functional.migrations.0101_add_column'), (102, 'test.functional.migrations.0102_create_table'), (103, 'test.functional.migrations.0103_not_ready') # that should not apply ] fake_get.return_value = test_migrations # create database with initial schema with patch.object(database, 'SCHEMA_VERSION', 100): db = Database('sqlite:///{}'.format(self.sqlite_db_file)) db.create_tables() db.close() # switch version and reconnect. Now both migrations should apply. with patch.object(database, 'SCHEMA_VERSION', 102): db = Database('sqlite:///{}'.format(self.sqlite_db_file)) try: # check column created by migration 101. db.connection.execute('SELECT column1 FROM datasets;').fetchall() # check table created by migration 102. db.connection.execute('SELECT column1 FROM table1;').fetchall() # db version changed to 102 self.assertEqual(db.connection.execute('PRAGMA user_version').fetchone()[0], 102) finally: db.close()
def test_closes_session_and_connection(self): db = Database('sqlite://') with patch.object(db.session, 'close', Mock()) as fake_session_close: with patch.object(db.connection, 'close', Mock()) as fake_connection_close: db.close() fake_session_close.assert_called_once_with() fake_connection_close.assert_called_once_with() self.assertIsNone(db._session) self.assertIsNone(db._connection)
def test_applies_new_migration_to_postgresql_database(self, fake_get): if self._db_type != 'postgres': self.skipTest('Postgres tests are disabled.') # replace real migrations with tests migrations. test_migrations = [ (100, 'test.test_orm.functional.migrations.0100_init'), (101, 'test.test_orm.functional.migrations.0101_add_column'), (102, 'test.test_orm.functional.migrations.0102_create_table'), (103, 'test.test_orm.functional.migrations.0103_not_ready' ) # that should not apply ] fake_get.return_value = test_migrations # create postgresql db postgres_test_db_dsn = self.config.library.database # PostgreSQLTestBase._create_postgres_test_db(get_runconfig())['test_db_dsn'] # populate database with initial schema with patch.object(database, 'SCHEMA_VERSION', 100): db = Database(postgres_test_db_dsn, engine_kwargs={'poolclass': NullPool}) db.create() db.close() # switch version and reconnect. Now both migrations should apply. with patch.object(database, 'SCHEMA_VERSION', 102): db = Database(postgres_test_db_dsn, engine_kwargs={'poolclass': NullPool}) try: # check column created by migration 101. db.connection\ .execute('SELECT column1 FROM {}.datasets;'.format(POSTGRES_SCHEMA_NAME))\ .fetchall() # check table created by migration 102. db.connection\ .execute('SELECT column1 FROM {}.table1;'.format(POSTGRES_SCHEMA_NAME))\ .fetchall() # db version changed to 102 db_version = db.connection\ .execute('SELECT version FROM {}.user_version;'.format(POSTGRES_SCHEMA_NAME))\ .fetchone()[0] self.assertEqual(db_version, 102) finally: db.close()
def test_applies_new_migration_to_postgresql_database(self, fake_get): if self._db_type != 'postgres': self.skipTest('Postgres tests are disabled.') # replace real migrations with tests migrations. test_migrations = [ (100, 'test.test_orm.functional.migrations.0100_init'), (101, 'test.test_orm.functional.migrations.0101_add_column'), (102, 'test.test_orm.functional.migrations.0102_create_table'), (103, 'test.test_orm.functional.migrations.0103_not_ready') # that should not apply ] fake_get.return_value = test_migrations # create postgresql db postgres_test_db_dsn = self.config.library.database # PostgreSQLTestBase._create_postgres_test_db(get_runconfig())['test_db_dsn'] # populate database with initial schema with patch.object(database, 'SCHEMA_VERSION', 100): db = Database(postgres_test_db_dsn, engine_kwargs={'poolclass': NullPool}) db.create() db.close() # switch version and reconnect. Now both migrations should apply. with patch.object(database, 'SCHEMA_VERSION', 102): db = Database(postgres_test_db_dsn, engine_kwargs={'poolclass': NullPool}) try: # check column created by migration 101. db.connection\ .execute('SELECT column1 FROM {}.datasets;'.format(POSTGRES_SCHEMA_NAME))\ .fetchall() # check table created by migration 102. db.connection\ .execute('SELECT column1 FROM {}.table1;'.format(POSTGRES_SCHEMA_NAME))\ .fetchall() # db version changed to 102 db_version = db.connection\ .execute('SELECT version FROM {}.user_version;'.format(POSTGRES_SCHEMA_NAME))\ .fetchone()[0] self.assertEqual(db_version, 102) finally: db.close()
def test_applies_new_migration_to_sqlite_database(self, fake_get): if self._db_type != 'sqlite': self.skipTest('SQLite tests are disabled.') # replace real migrations with tests migrations. test_migrations = [ (100, 'test.functional.migrations.0100_init'), (101, 'test.functional.migrations.0101_add_column'), (102, 'test.functional.migrations.0102_create_table'), (103, 'test.functional.migrations.0103_not_ready' ) # that should not apply ] fake_get.return_value = test_migrations # create database with initial schema with patch.object(database, 'SCHEMA_VERSION', 100): db = Database('sqlite:///{}'.format(self.sqlite_db_file)) db.create_tables() db.close() # switch version and reconnect. Now both migrations should apply. with patch.object(database, 'SCHEMA_VERSION', 102): db = Database('sqlite:///{}'.format(self.sqlite_db_file)) try: # check column created by migration 101. db.connection.execute( 'SELECT column1 FROM datasets;').fetchall() # check table created by migration 102. db.connection.execute('SELECT column1 FROM table1;').fetchall() # db version changed to 102 self.assertEqual( db.connection.execute('PRAGMA user_version').fetchone()[0], 102) finally: db.close()