def test_apply_scope_to_a_builder(self): scope = SoftDeletingScope() query = flexmock(QueryBuilder(None, None, None)) builder = Builder(query) model = flexmock(ModelStub()) model.should_receive('get_qualified_deleted_at_column').once().and_return('table.deleted_at') builder.get_query().should_receive('where_null').once().with_args('table.deleted_at') scope.apply(builder, model)
def test_force_delete_extension(self): scope = SoftDeletingScope() builder = Builder(None) scope.extend(builder) callback = builder.get_macro('force_delete') query = flexmock(QueryBuilder(None, None, None)) given_builder = Builder(query) query.should_receive('delete').once() callback(given_builder)
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_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_delete_sets_soft_deleted_column(self): model = flexmock(SoftDeleteModelStub()) model.set_exists(True) builder = flexmock(Builder) query_builder = flexmock(QueryBuilder(None, None, None)) query = Builder(query_builder) model.should_receive("new_query").and_return(query) builder.should_receive("where").once().with_args("id", 1).and_return(query) builder.should_receive("update").once().with_args({"deleted_at": t}) model.delete() self.assertIsInstance(model.deleted_at, datetime.datetime)
def test_with_trashed_extension(self): scope = flexmock(SoftDeletingScope()) builder = Builder(None) scope.extend(builder) callback = builder.get_macro('with_trashed') query = flexmock(QueryBuilder(None, None, None)) given_builder = Builder(query) model = flexmock(ModelStub()) given_builder.set_model(model) scope.should_receive('remove').once().with_args(given_builder, model) result = callback(given_builder) self.assertEqual(given_builder, result)
def test_restore_extension(self): scope = SoftDeletingScope() builder = Builder(None) scope.extend(builder) callback = builder.get_macro('restore') query = flexmock(QueryBuilder(None, None, None)) builder_mock = flexmock(BuilderWithTrashedStub) given_builder = BuilderWithTrashedStub(query) builder_mock.should_receive('with_trashed').once() builder_mock.should_receive('get_model').once().and_return(ModelStub()) builder_mock.should_receive('update').once().with_args({'deleted_at': None}) callback(given_builder)
def test_with_trashed_extension(self): scope = flexmock(SoftDeletingScope()) builder = Builder(None) scope.extend(builder) callback = builder.get_macro('with_trashed') query = flexmock(QueryBuilder(None, None, None)) given_builder = Builder(query) model = flexmock(ModelStub()) given_builder.set_model(model) given_builder.remove_global_scope = mock.MagicMock(return_value=given_builder) result = callback(given_builder) self.assertEqual(given_builder, result) given_builder.remove_global_scope.assert_called_with(scope)
def test_scope_can_remove_deleted_at_constraint(self): scope = SoftDeletingScope() query = flexmock(QueryBuilder(None, None, None)) model = flexmock(ModelStub()) builder = Builder(query) builder.set_model(model) model.should_receive('get_qualified_deleted_at_column').once( ).and_return('table.deleted_at') query.wheres = [{ 'type': 'null', 'column': 'foo' }, { 'type': 'null', 'column': 'table.deleted_at' }] scope.remove(builder, model) self.assertEqual(query.wheres, [{'type': 'null', 'column': 'foo'}])
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_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_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())