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"]
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()]
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()
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()
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)
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
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"]
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
def test_field_error_validation(field_class: Field, values): for value in values: with pytest.raises(FieldValidationError): field_class.validate(value)
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."
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
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())
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
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
def test_set_column(): field = Field() mocked_column = Mock() field._column = mocked_column field.__set_name__(None, "name") assert field._column is mocked_column