Exemplo n.º 1
0
def test_resolve_foreign_key_as_property_argument():
    field = Field(field_name="user.email")
    parent_filter = Mock(Meta=Mock(model=Article))
    field.parent_filter = parent_filter
    field.resolve_fk()
    assert field._column is User.email
    assert field.foreign_model is User
    class F(Filter):
        name = Field(field_name="first_name")
        t = NestedFilter(MyNestedFilter)
        method = MethodField(method="some_method")
        email = Field()

        def some_method(self, value):
            pass

        class Meta:
            model = User
            fields = ["age", "name"]
Exemplo n.º 3
0
def test_resolve_fk_is_called():
    field = Field()
    field.__set_name__(None, "name")
    field.is_foreign_key = True
    field.resolve_fk = Mock()
    parent_filter = Mock()
    field.post_init(parent_filter)
    assert field.parent_filter is parent_filter
    assert field.resolve_fk.call_args_list == [call()]
Exemplo n.º 4
0
def test_field_allow_none_false_apply_exists():
    filter_obj = Mock(validated_data={"name": None})
    field = Field(allow_none=False)
    field.__set_name__(None, "name")
    field._apply_filter = Mock()
    field.apply_filter(filter_obj)
    field._apply_filter.assert_not_called()
Exemplo n.º 5
0
def test_field_allow_none_apply_does_not_exist_in_data(allow_none):
    filter_obj = Mock(validated_data={})
    field = Field(allow_none=allow_none)
    field.__set_name__(None, "name")
    field._apply_filter = Mock()
    field.apply_filter(filter_obj)
    field._apply_filter.assert_not_called()
Exemplo n.º 6
0
def test_field_allow_none_true_apply_exists():
    filter_obj = Mock(validated_data={"name": None})
    field = Field(allow_none=True)
    field.__set_name__(None, "name")
    field._apply_filter = Mock()
    field.apply_filter(filter_obj)
    field._apply_filter.assert_called_once_with(filter_obj, None)
Exemplo n.º 7
0
def test_resolve_fk_is_not_called():
    field = Field()
    field.__set_name__(None, "name")
    field.resolve_fk = Mock()
    parent_filter = Mock()
    field.post_init(parent_filter)
    assert field.parent_filter is parent_filter
    assert field.resolve_fk.call_count == 0
Exemplo n.º 8
0
def test_field_apply_with_default_lookup_operator():
    field = Field()
    field.__set_name__(None, "first_name")
    parent_filter = Mock(validated_data={"first_name": "test"},
                         Meta=Mock(model=User))
    field.post_init(parent_filter)
    assert compares_expressions(field.apply_filter(parent_filter),
                                User.first_name == "test")
    class MyFilter(Filter):
        test = MethodField(method="filter_first_name", data_source_name="custom_key")
        test2 = MethodField(method="filter_last_name")
        age = Field(lookup_operator=GTEOperator)

        def filter_first_name(self, value):
            return User.first_name == value

        def filter_last_name(self, value):
            return User.last_name.startswith(value)

        class Meta:
            model = User
            session = db_session
        class _(Filter):
            age = Field()

            class Meta:
                pass
 class _(Filter):
     age = Field()
        class _(Filter):
            name = Field(field_name="first_name")

            class Meta:
                model = User
                fields = ["last_login_time", "name"]
        class _(Filter):
            name = Field(field_name="first_name")

            class Meta:
                model = User
                fields = ["x", "name"]
Exemplo n.º 14
0
def test_set_column_with_custom_column_as_str():
    field = Field(custom_column="my_column")
    field.__set_name__(None, "name")
    field._set_column()
    assert isinstance(field._column, column("a").__class__)
    assert str(field._column) == "my_column"
        class _(Filter):
            f = Field(field_name=field_name)

            class Meta:
                model = User
Exemplo n.º 16
0
def test_field_error_validation(field_class: Field, values):
    for value in values:
        with pytest.raises(FieldValidationError):
            field_class.validate(value)
Exemplo n.º 17
0
def test_define_field_set_foreign_key_dept_greater_than_one():
    with pytest.raises(ValueError) as exc:
        Field(field_name="model.other_model.column")
    assert str(exc.value) == "Dept greater than 2 not supported yet."
Exemplo n.º 18
0
def test_set_column_with_custom_column():
    mocked_column = Mock()
    field = Field(custom_column=mocked_column)
    field.__set_name__(None, "name")

    assert field._column is mocked_column
    class F(Filter):
        age = Field()

        class Meta:
            model = User
            session = db_session
Exemplo n.º 20
0
def test_field_apply_does_not_exist_in_data():
    field = Field()
    field.__set_name__(None, "name")
    assert field.apply_filter(Mock(validated_data={})).compare(empty_sql())
Exemplo n.º 21
0
def test_field_validation(field_class: Field, values):
    for value, expected in values:
        assert field_class.validate(value) == expected
        class _(Filter):
            age = Field()

            class Meta:
                model = User
Exemplo n.º 23
0
def test_define_field_for_foreign_model_column():
    field = Field(field_name="model.column")
    assert field.is_foreign_key is True
    class F(Filter):
        age = Field()

        class Meta:
            model = User
            marshmallow_schema = ma_schema
Exemplo n.º 25
0
def test_set_column():
    field = Field()
    mocked_column = Mock()
    field._column = mocked_column
    field.__set_name__(None, "name")
    assert field._column is mocked_column