示例#1
0
    def test_select_modifier(self, request_ctx):
        item = MagicMock()
        Product.query.paginate().items = [item]

        with request_ctx('/?select={"id": 1}'):
            collection()(lambda: Product)()

        item.export_data.assert_called_with(["id"], ())
示例#2
0
    def test_basic_request_with_org_model(self, app):

        with app.test_request_context("/"):
            with auth_ctx("coyote", "acme"):
                current_org_id = current_org.id
                collection()(lambda: Todo)()

        Todo.query.filter_by.assert_called_with(org_id=current_org_id)
示例#3
0
    def test_limit_modifier(self, request_ctx, query_string, expected):
        # The mock is created just one time, so reset it
        Product.query.paginate.reset_mock()

        with request_ctx(f"/?{query_string}"):
            collection()(lambda: Product)()

        Product.query.paginate.assert_called_with(*expected)
示例#4
0
    def test_filter_modifier(self, request_ctx):

        with request_ctx('/?filter={"name": "tnt", "enabled": true}'):
            collection()(lambda: Product)()

        Product.query.filter_by.assert_called_with(**{
            "name": "tnt",
            "enabled": True
        })
示例#5
0
    def test_sort_modifier_with_descending_order(self, request_ctx):
        with request_ctx("/?sort=-name"):
            collection()(lambda: Product)()

        Product.query.order_by.assert_called()

        expected = Product.name.desc()
        original = Product.query.order_by.call_args[0][0]
        assert original.compare(expected)
示例#6
0
    def test_filter_modifier_with_org_model(self, request_ctx):

        with request_ctx('/?filter={"task": "Do something"}'):
            with auth_ctx("coyote", "acme"):
                current_org_id = current_org.id
                collection()(lambda: Todo)()

        Todo.query.filter_by.assert_called_with(org_id=current_org_id,
                                                task="Do something")
示例#7
0
    def test_search_modifier(self, request_ctx):

        with request_ctx('/?search={"t": "black", "f": ["name", "color"]}'):
            collection()(lambda: Product)()

        Product.query.filter.assert_called()

        expected = or_(Product.name.ilike("%black%"),
                       Product.color.ilike("%black%"))
        original = Product.query.filter.call_args[0][0]

        assert original.compare(expected)
示例#8
0
    def test_sort_modifier_with_multiple_fields(self, request_ctx):
        with request_ctx("/?sort=name,color"):
            collection()(lambda: Product)()

        Product.query.order_by.assert_called()

        expected = Product.name.asc()
        original = Product.query.order_by.call_args[0][0]
        assert original.compare(expected)

        expected = Product.color.asc()
        original = Product.query.order_by.call_args[0][1]
        assert original.compare(expected)
示例#9
0
    def test_model_class_export_data_with_exception(self, request_ctx):
        class TestTable(Model):
            id = Column(Integer, primary_key=True)

            def export_data(self, *args, **kargs):
                raise AttributeError("Inside of export_data method")

        TestTable.query = MagicMock()
        TestTable.query.paginate().items = [TestTable(id=1)]

        with request_ctx("/"):
            with pytest.raises(AttributeError,
                               match="Inside of export_data method"):
                collection()(lambda: TestTable)()
示例#10
0
    def test_search_modifier_on_non_text_column(self, request_ctx):

        with request_ctx('/?search={"t": "black", "f": ["price", "color"]}'):
            collection()(lambda: Product)()

        Product.query.filter.assert_called()

        expected = or_(
            func.cast(Product.price, Text).ilike("%black%"),
            Product.color.ilike("%black%"),
        )
        original = Product.query.filter.call_args[0][0]

        # clauseelement.compare() is not implemented for cast()
        # so use string comparison instead
        assert str(original) == str(expected)
示例#11
0
    def test_search_modifier_with_org_model(self, request_ctx):

        with request_ctx('/?search={"t": "black", "f": ["task"]}'):
            with auth_ctx("coyote", "acme"):
                current_org_id = current_org.id
                collection()(lambda: Todo)()

        Todo.query.filter_by.assert_called_with(org_id=current_org_id)
        Todo.query.filter.assert_called()

        expected = or_(Todo.task.ilike("%black%"))
        original = Todo.query.filter.call_args[0][0]
        assert original.compare(expected)

        # The query should be filtered by the organization id first
        assert Todo.query.method_calls[0] == call.filter_by(
            org_id=current_org_id)
示例#12
0
    def test_model_class_with_export_data_method(self, query, request_ctx):
        item = Person(id=300, firstname="John", lastname="Connor")

        query.paginate().items = [item]
        query.paginate().total = 1

        with request_ctx('/?select={"firstname": 1}&page=3'):
            rv = collection()(lambda: Person)()

        assert rv[0] == [{"firstname": "John"}]
        assert rv[1] == {"X-Total": 1, "X-Page": 3}
示例#13
0
    def test_model_class_without_export_data_method(self, query, request_ctx):
        item = Product(id=4, name="Acme explosive tennis balls")

        query.paginate().items = [item]
        query.paginate().total = 1

        with request_ctx('/?select={"id": 1}&page=3'):
            rv = collection()(lambda: Product)()

        assert rv[0] == [{"id": 4}]
        assert rv[1] == {"X-Total": 1, "X-Page": 3}
示例#14
0
    def test_returned_value(self, query, request_ctx):
        item = MagicMock()
        item.export_data.return_value = {"id": 300}

        query.paginate().items = [item]
        query.paginate().total = 1

        with request_ctx('/?select={"id": 1}&page=3'):
            rv = collection()(lambda: Product)()

        assert rv[0] == [{"id": 300}]
        assert rv[1] == {"X-Total": 1, "X-Page": 3}
示例#15
0
 def test_unknown_modifier(self, request_ctx):
     with request_ctx("/?unknown=value"):
         with pytest.raises(ValidationError):
             collection()(lambda: Product)()
示例#16
0
    def test_page_modifier(self, request_ctx):
        with request_ctx("/?page=50"):
            collection()(lambda: Product)()

        Product.query.paginate.assert_called_with(50, 30)
示例#17
0
 def test_invalid_modifier(self, request_ctx, qs):
     with request_ctx(f"/?{qs}"):
         with pytest.raises(ValidationError):
             collection()(lambda: Product)()