As part of my our test suites, we’ve designed small helpers such as:
defmacro assert_query_equal(q, right) do
quote do
unquote(q)
|> inspect(pretty: false, width: :infinity, limit: :infinity, printable_limit: :infinity)
|> assert_equal(unquote(right))
end
end
Within our tests, we use it like this:
pattern
|> Query.with_email_like()
|> assert_query_equal(
"#Ecto.Query<from u0 in EAICore.Schema.User, as: :users, " <>
"where: fragment(\"lower(f_unaccent(?)) ILIKE lower(f_unaccent(?))\", u0.email, ^\"#{similar_pattern}\"), " <>
"group_by: [u0.id], " <>
"order_by: [asc: u0.inserted_at]>"
)
We really like this way of testing, because we’re testing what our queries look like rather than hitting a database - and tests are really fast.
However, it seems that inspect
might have recently changed, because it is inserting sequences such as \n
.
It could be fine, the problem being that we’re using Faker to generate inputs for these query tests, and they’re not necessarily the same lengths - so the inspect
output changes not only on these parameters, but also on how it builds these \n
sequences…
I’ve tried forcing something a bit more “deterministic” an output by using options such as pretty: false, width: :infinity, limit: :infinity, printable_limit: :infinity
- my first intuition was that in particular pretty: false
would do the trick. But no.
It could be that the problem is in the implementation of the inspect behaviour of Ecto Query, Fragment, etc. And I am not entirely sure how to approach this. In particular, how Algebraic documents are constructed, and whether or not there is a way to pass a custom_options
to inspect that would propagate to whatever Ecto is doing…
Any ideas on how to force inspect
never to add new lines, and instead simply build a long string?
Thanks.