def test_rollback_migration_can_be_pretended(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive("connection").and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock(Connection(None)) connection.should_receive("get_logged_queries").twice().and_return([]) resolver.should_receive("connection").with_args(None).and_return( connection) migrator = flexmock( Migrator( flexmock(DatabaseMigrationRepository(resolver, "migrations")), resolver)) foo_migration = flexmock(MigrationStub("foo")) foo_migration.should_receive("get_connection").and_return(connection) bar_migration = flexmock(MigrationStub("bar")) bar_migration.should_receive("get_connection").and_return(connection) migrator.get_repository().should_receive("get_last").once().and_return( [foo_migration, bar_migration]) migrator.should_receive("_resolve").with_args( os.getcwd(), "bar").once().and_return(bar_migration) migrator.should_receive("_resolve").with_args( os.getcwd(), "foo").once().and_return(foo_migration) migrator.rollback(os.getcwd(), True) self.assertTrue(foo_migration.downed) self.assertFalse(foo_migration.upped) self.assertTrue(foo_migration.downed) self.assertFalse(foo_migration.upped)
def test_rollback_migration_can_be_pretended(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock(Connection(None)) connection.should_receive('pretend').replace_with( lambda callback: callback(None)) resolver.should_receive('connection').with_args(None).and_return( connection) migrator = flexmock( Migrator( flexmock(DatabaseMigrationRepository(resolver, 'migrations')), resolver)) foo_migration = MigrationStub('foo') bar_migration = MigrationStub('bar') migrator.get_repository().should_receive('get_last').once().and_return( [foo_migration, bar_migration]) bar_mock = flexmock(MigrationStub()) bar_mock.should_receive('down').once() foo_mock = flexmock(MigrationStub()) foo_mock.should_receive('down').once() migrator.should_receive('_resolve').with_args( os.getcwd(), 'bar').once().and_return(bar_mock) migrator.should_receive('_resolve').with_args( os.getcwd(), 'foo').once().and_return(foo_mock) migrator.rollback(os.getcwd(), True)
def test_rollback_migration_can_be_pretended(self): import orator.migrations.migrator as migrator d = flexmock(migrator) resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock(Connection(None)) connection.should_receive('get_logged_queries').twice().and_return([]) resolver.should_receive('connection').with_args(None).and_return( connection) d.should_receive('dump').with_args(connection).and_return('') migrator = flexmock( Migrator( flexmock(DatabaseMigrationRepository(resolver, 'migrations')), resolver)) foo_migration = flexmock(MigrationStub('foo')) foo_migration.should_receive('get_connection').and_return(connection) bar_migration = flexmock(MigrationStub('bar')) bar_migration.should_receive('get_connection').and_return(connection) migrator.get_repository().should_receive('get_last').once().and_return( [foo_migration, bar_migration]) migrator.should_receive('_resolve').with_args( os.getcwd(), 'bar').once().and_return(bar_migration) migrator.should_receive('_resolve').with_args( os.getcwd(), 'foo').once().and_return(foo_migration) migrator.rollback(os.getcwd(), True) self.assertTrue(foo_migration.downed) self.assertFalse(foo_migration.upped) self.assertTrue(foo_migration.downed) self.assertFalse(foo_migration.upped)
def get_connection(self, version=None): if version is None: version = (5, 7) conn = flexmock(Connection(None)) conn.should_receive('get_server_version').and_return(version) return conn
def get_connection(self, version=None): if version is None: version = (5, 7, 11, '') connector = flexmock(MySQLConnector()) connector.should_receive('get_server_version').and_return(version) conn = flexmock(Connection(connector)) return conn
def test_has_table_correctly_calls_grammar(self): connection = flexmock(Connection(None)) grammar = flexmock() connection.should_receive('get_schema_grammar').and_return(grammar) builder = SchemaBuilder(connection) grammar.should_receive('compile_table_exists').once().and_return('sql') connection.should_receive('get_table_prefix').once().and_return('prefix_') connection.should_receive('select').once().with_args('sql', ['prefix_table']).and_return(['prefix_table']) self.assertTrue(builder.has_table('table'))
def test_to_sql_runs_commands_from_blueprint(self): conn = flexmock(Connection(None)) conn.should_receive("statement").once().with_args("foo") conn.should_receive("statement").once().with_args("bar") grammar = flexmock(SchemaGrammar(conn)) blueprint = flexmock(Blueprint("table")) blueprint.should_receive("to_sql").once().with_args( conn, grammar).and_return(["foo", "bar"]) blueprint.build(conn, grammar)
def test_to_sql_runs_commands_from_blueprint(self): conn = flexmock(Connection(None)) conn.should_receive('statement').once().with_args('foo') conn.should_receive('statement').once().with_args('bar') grammar = flexmock(SchemaGrammar()) blueprint = flexmock(Blueprint('table')) blueprint.should_receive('to_sql').once().with_args( conn, grammar).and_return(['foo', 'bar']) blueprint.build(conn, grammar)
def test_get_last_batch_number_returns_max_batch(self): repo = self.get_repository() connection = flexmock(Connection(None)) query = flexmock(QueryBuilder(connection, None, None)) repo.get_connection_resolver().should_receive("connection").with_args( None).and_return(connection) repo.get_connection().should_receive("table").once().with_args( "migrations").and_return(query) query.should_receive("max").and_return(1) self.assertEqual(1, repo.get_last_batch_number())
def test_get_ran_migrations_list_migrations_by_package(self): repo = self.get_repository() connection = flexmock(Connection(None)) query = flexmock(QueryBuilder(connection, None, None)) repo.get_connection_resolver().should_receive("connection").with_args( None).and_return(connection) repo.get_connection().should_receive("table").once().with_args( "migrations").and_return(query) query.should_receive("lists").once().with_args("migration").and_return( "bar") self.assertEqual("bar", repo.get_ran())
def test_has_table_correctly_calls_grammar(self): connection = flexmock(Connection(None)) grammar = flexmock() connection.should_receive("get_schema_grammar").and_return(grammar) builder = SchemaBuilder(connection) grammar.should_receive("compile_table_exists").once().and_return("sql") connection.should_receive("get_table_prefix").once().and_return( "prefix_") connection.should_receive("select").once().with_args( "sql", ["prefix_table"]).and_return(["prefix_table"]) self.assertTrue(builder.has_table("table"))
def test_log_inserts_record_into_migration_table(self): repo = self.get_repository() connection = flexmock(Connection(None)) query = flexmock(QueryBuilder(connection, None, None)) repo.get_connection_resolver().should_receive('connection').with_args( None).and_return(connection) repo.get_connection().should_receive('table').once().with_args( 'migrations').and_return(query) query.should_receive('insert').once().with_args(migration='bar', batch=1) repo.log('bar', 1)
def test_get_ran_migrations_list_migrations_by_package(self): repo = self.get_repository() connection = flexmock(Connection(None)) query = flexmock(QueryBuilder(connection, None, None)) repo.get_connection_resolver().should_receive('connection').with_args( None).and_return(connection) repo.get_connection().should_receive('table').once().with_args( 'migrations').and_return(query) query.should_receive('lists').once().with_args('migration').and_return( 'bar') self.assertEqual('bar', repo.get_ran())
def test_log_inserts_record_into_migration_table(self): repo = self.get_repository() connection = flexmock(Connection(None)) query = flexmock(QueryBuilder(connection, None, None)) repo.get_connection_resolver().should_receive("connection").with_args( None).and_return(connection) repo.get_connection().should_receive("table").once().with_args( "migrations").and_return(query) query.should_receive("insert").once().with_args(migration="bar", batch=1) repo.log("bar", 1)
def test_new_query_returns_orator_query_builder(self): conn = flexmock(Connection) grammar = flexmock(QueryGrammar) processor = flexmock(QueryProcessor) conn.should_receive('get_query_grammar').and_return(grammar) conn.should_receive('get_post_processor').and_return(processor) resolver = flexmock(DatabaseManager) resolver.should_receive('connection').and_return(Connection(None)) OrmModelStub.set_connection_resolver(DatabaseManager({})) model = OrmModelStub() builder = model.new_query() self.assertIsInstance(builder, Builder)
def test_get_last_migrations(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return(None) resolver = flexmock(resolver_mock({})) repo = flexmock(DatabaseMigrationRepository(resolver, 'migrations')) connection = flexmock(Connection(None)) query = flexmock(QueryBuilder(connection, None, None)) repo.should_receive('get_last_batch_number').and_return(1) repo.get_connection_resolver().should_receive('connection').with_args( None).and_return(connection) repo.get_connection().should_receive('table').once().with_args( 'migrations').and_return(query) query.should_receive('where').once().with_args('batch', 1).and_return(query) query.should_receive('order_by').once().with_args( 'migration', 'desc').and_return(query) query.should_receive('get').once().and_return('foo') self.assertEqual('foo', repo.get_last())
def test_get_last_migrations(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive("connection").and_return(None) resolver = flexmock(resolver_mock({})) repo = flexmock(DatabaseMigrationRepository(resolver, "migrations")) connection = flexmock(Connection(None)) query = flexmock(QueryBuilder(connection, None, None)) repo.should_receive("get_last_batch_number").and_return(1) repo.get_connection_resolver().should_receive("connection").with_args( None).and_return(connection) repo.get_connection().should_receive("table").once().with_args( "migrations").and_return(query) query.should_receive("where").once().with_args("batch", 1).and_return(query) query.should_receive("order_by").once().with_args( "migration", "desc").and_return(query) query.should_receive("get").once().and_return("foo") self.assertEqual("foo", repo.get_last())
def test_call_resolve_class_and_calls_run(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock(Connection(None)) resolver.should_receive('connection').with_args(None).and_return( connection) seeder = Seeder(resolver) command = flexmock(Command('foo')) command.should_receive('line').once() seeder.set_command(command) child = flexmock() child.__name__ = 'foo' child.should_receive('set_command').once().with_args(command) child.should_receive('set_connection_resolver').once().with_args( resolver) child.should_receive('run').once() seeder.call(child)
def test_call_resolve_class_and_calls_run(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive("connection").and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock(Connection(None)) resolver.should_receive("connection").with_args(None).and_return( connection) seeder = Seeder(resolver) command = flexmock(Command("foo")) command.should_receive("line").once() seeder.set_command(command) child = flexmock() child.__name__ = "foo" child.should_receive("set_command").once().with_args(command) child.should_receive("set_connection_resolver").once().with_args( resolver) child.should_receive("run").once() seeder.call(child)
def test_delete_removes_migration_from_table(self): repo = self.get_repository() connection = flexmock(Connection(None)) query = flexmock(QueryBuilder(connection, None, None)) repo.get_connection_resolver().should_receive('connection').with_args( None).and_return(connection) repo.get_connection().should_receive('table').once().with_args( 'migrations').and_return(query) query.should_receive('where').once().with_args('migration', 'foo').and_return(query) query.should_receive('delete').once() class Migration: migration = 'foo' def __getitem__(self, item): return self.migration repo.delete(Migration())
def test_delete_removes_migration_from_table(self): repo = self.get_repository() connection = flexmock(Connection(None)) query = flexmock(QueryBuilder(connection, None, None)) repo.get_connection_resolver().should_receive("connection").with_args( None).and_return(connection) repo.get_connection().should_receive("table").once().with_args( "migrations").and_return(query) query.should_receive("where").once().with_args("migration", "foo").and_return(query) query.should_receive("delete").once() class Migration(object): migration = "foo" def __getitem__(self, item): return self.migration repo.delete(Migration())
def test_up_migration_can_be_pretended(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock(Connection(None)) connection.should_receive('pretend').replace_with( lambda callback: callback(None)) resolver.should_receive('connection').with_args(None).and_return( connection) migrator = flexmock( Migrator( flexmock(DatabaseMigrationRepository(resolver, 'migrations')), resolver)) g = flexmock(glob) g.should_receive('glob').with_args( os.path.join(os.getcwd(), '[0-9]*_*.py')).and_return([ os.path.join(os.getcwd(), '2_bar.py'), os.path.join(os.getcwd(), '1_foo.py'), os.path.join(os.getcwd(), '3_baz.py') ]) migrator.get_repository().should_receive('get_ran').once().and_return( ['1_foo']) migrator.get_repository().should_receive( 'get_next_batch_number').once().and_return(1) bar_mock = flexmock(MigrationStub()) bar_mock.should_receive('get_connection').once().and_return(None) bar_mock.should_receive('up').once() baz_mock = flexmock(MigrationStub()) baz_mock.should_receive('get_connection').once().and_return(None) baz_mock.should_receive('up').once() migrator.should_receive('_resolve').with_args( os.getcwd(), '2_bar').once().and_return(bar_mock) migrator.should_receive('_resolve').with_args( os.getcwd(), '3_baz').once().and_return(baz_mock) migrator.run(os.getcwd(), True)
def test_up_migration_can_be_pretended(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive("connection").and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock(Connection(None)) connection.should_receive("get_logged_queries").twice().and_return([]) resolver.should_receive("connection").with_args(None).and_return( connection) migrator = flexmock( Migrator( flexmock(DatabaseMigrationRepository(resolver, "migrations")), resolver)) g = flexmock(glob) g.should_receive("glob").with_args( os.path.join(os.getcwd(), "[0-9]*_*.py")).and_return([ os.path.join(os.getcwd(), "2_bar.py"), os.path.join(os.getcwd(), "1_foo.py"), os.path.join(os.getcwd(), "3_baz.py"), ]) migrator.get_repository().should_receive("get_ran").once().and_return( ["1_foo"]) migrator.get_repository().should_receive( "get_next_batch_number").once().and_return(1) bar_mock = flexmock(MigrationStub()) bar_mock.should_receive("get_connection").once().and_return(connection) bar_mock.should_receive("up").once() baz_mock = flexmock(MigrationStub()) baz_mock.should_receive("get_connection").once().and_return(connection) baz_mock.should_receive("up").once() migrator.should_receive("_resolve").with_args( os.getcwd(), "2_bar").once().and_return(bar_mock) migrator.should_receive("_resolve").with_args( os.getcwd(), "3_baz").once().and_return(baz_mock) migrator.run(os.getcwd(), True)
def get_connection(self): return flexmock(Connection(None))
def resolve_connection(cls, connection=None): return flexmock(Connection(None))
def test_migrations_are_run_up_directly_if_transactional_is_false(self): import orator.migrations.migrator as migrator d = flexmock(migrator) resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock() connection.should_receive('transaction').never() resolver.should_receive('connection').and_return(connection) d.should_receive('dump').and_return('') migrator = flexmock( Migrator( flexmock(DatabaseMigrationRepository(resolver, 'migrations')), resolver)) g = flexmock(glob) g.should_receive('glob').with_args( os.path.join(os.getcwd(), '[0-9]*_*.py')).and_return([ os.path.join(os.getcwd(), '2_bar.py'), os.path.join(os.getcwd(), '1_foo.py'), os.path.join(os.getcwd(), '3_baz.py') ]) migrator.get_repository().should_receive('get_ran').once().and_return( ['1_foo']) migrator.get_repository().should_receive( 'get_next_batch_number').once().and_return(1) migrator.get_repository().should_receive('log').once().with_args( '2_bar', 1) migrator.get_repository().should_receive('log').once().with_args( '3_baz', 1) bar_mock = flexmock(MigrationStub()) bar_mock.transactional = False bar_mock.set_connection(connection) bar_mock.should_receive('up').once() baz_mock = flexmock(MigrationStub()) baz_mock.transactional = False baz_mock.set_connection(connection) baz_mock.should_receive('up').once() migrator.should_receive('_resolve').with_args( os.getcwd(), '2_bar').once().and_return(bar_mock) migrator.should_receive('_resolve').with_args( os.getcwd(), '3_baz').once().and_return(baz_mock) migrator.run(os.getcwd()) resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock(Connection(None)) connection.should_receive('get_logged_queries').twice().and_return([]) resolver.should_receive('connection').with_args(None).and_return( connection) migrator = flexmock( Migrator( flexmock(DatabaseMigrationRepository(resolver, 'migrations')), resolver)) g = flexmock(glob) g.should_receive('glob').with_args( os.path.join(os.getcwd(), '[0-9]*_*.py')).and_return([ os.path.join(os.getcwd(), '2_bar.py'), os.path.join(os.getcwd(), '1_foo.py'), os.path.join(os.getcwd(), '3_baz.py') ]) migrator.get_repository().should_receive('get_ran').once().and_return( ['1_foo']) migrator.get_repository().should_receive( 'get_next_batch_number').once().and_return(1) bar_mock = flexmock(MigrationStub()) bar_mock.should_receive('get_connection').once().and_return(connection) bar_mock.should_receive('up').once() baz_mock = flexmock(MigrationStub()) baz_mock.should_receive('get_connection').once().and_return(connection) baz_mock.should_receive('up').once() migrator.should_receive('_resolve').with_args( os.getcwd(), '2_bar').once().and_return(bar_mock) migrator.should_receive('_resolve').with_args( os.getcwd(), '3_baz').once().and_return(baz_mock) migrator.run(os.getcwd(), True)