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_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_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_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) 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_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_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) 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_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)