Testing Filters

I'm writing a function that dynamically adds filters to an Ash.Query for AshBackpex. I'm not sure how to assert that I'm getting the filters back that I want. I can't figure out how to assert against this structure:
#Ash.Query<
resource: AshBackpex.TestDomain.Post,
action: :read,
filter: #Ash.Filter<contains(title, "foo bar")>
>
#Ash.Query<
resource: AshBackpex.TestDomain.Post,
action: :read,
filter: #Ash.Filter<contains(title, "foo bar")>
>
I can do this:
result = MyMod.my_func(query, ...)
dbg(result.filter.expression)
result = MyMod.my_func(query, ...)
dbg(result.filter.expression)
And I get: contains(title, "foo bar") But I don't know how to pattern match or make assertions against that. I can also use Ash.Filter.to_simple_filter(...) and assert against the length filter.predicates , which might actually be enough for this use case. But I'd like to get more specific if possible.
1 Reply
⿻ eileen
⿻ eileenOP2mo ago
I think I got it! Macro.escape!!!!! Ah I see instead of to_simple_filter I can also use Ash.Filter.list_predicates(query.filter) Helper function:
defp predicate_has_key_val(predicate, :contains, key, val) do
{_, _, keys} = predicate |> Macro.escape()
assert Keyword.get(keys, :name) == :contains

[{_, _, arg}, result_val] = Keyword.get(keys, :arguments)
assert result_val == val

{_, _, attribute} = Keyword.get(arg, :attribute)
assert Keyword.get(attribute, :name) == key
assert Keyword.get(attribute, :source) == key
end
defp predicate_has_key_val(predicate, :contains, key, val) do
{_, _, keys} = predicate |> Macro.escape()
assert Keyword.get(keys, :name) == :contains

[{_, _, arg}, result_val] = Keyword.get(keys, :arguments)
assert result_val == val

{_, _, attribute} = Keyword.get(arg, :attribute)
assert Keyword.get(attribute, :name) == key
assert Keyword.get(attribute, :source) == key
end
There's probably a more elegant way to do this using the Access module but I'm ok with it as is

Did you find this page helpful?