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_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_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_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_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_new_query_returns_eloquent_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_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(), '*_*.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 get_connection(self): return flexmock(Connection(None))