Doc tests for queries

I am writing doc tests for the library for which I have to assert on the queries rather than the result. I came across an issue where i am asserting on the dynamics like this:

 iex> result = {__MODULE__}.like_dynamic("name", "%Joh%", true, [dynamic_type: :and])
 iex> inspect(result)
 "dynamic([q], like(fragment("\(?)::TEXT\", q.name), ^"\%Joh%\") and ^true)"

It gives me this error:

       Doctest did not compile, got: (TokenMissingError) lib/fat_ecto/query/dynamics.ex:480: missing terminator: ) (for "(" starting at line 480)
 code: "dynamic([q], like(fragment("(?)::TEXT", q.name), ^"%Joh%") and ^true)"

I debugged it and figure out whenever there is a string inside the result doc test start failing, but if there is no string it works fine like this:

          "dynamic([..., c], c.experience_years < ^2 and ^true)"

How to resolve this issue.
Any suggestions?
Thanks

In your specific case it seems you‘re not escaping quotes in the result. In the more general case I‘d go and not test ecto queries. Most of their internals are undocumented implementation details, which could break on each update of ecto.

1 Like

If you really have to test queries, just do it like this:

assert inspect(expected_query) == inspect(actual_query)

It’s a hack but it works.

2 Likes