示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    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)
示例#4
0
    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)
示例#5
0
    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)
示例#6
0
    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)
示例#7
0
    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)
示例#8
0
    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'}])
示例#9
0
    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)
示例#10
0
    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)
示例#11
0
    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'
            }]
        )
示例#12
0
    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)
示例#13
0
    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)