blatyo
I’m testing some complex ecto sql builder stuff. So I pass data to generate an ecto query and I want to validate that it produces the right query. So, I’m using the Repo.to_sql function to generate SQL and then asserting that matches the expected SQL.
That works OK, but when the SQL is long, it makes it hard to understand which parts of the query differ. I wrote something that can format SQL, because I thought it’d improve the diff output that exunit provides. However, that just added some more whitespace to a single line diff between the two SQL queries. Here’s an example:
What I would like is to have a test failure that would wrap on the newlines instead. Ideally, it would look something like this with the diff coloring elixir does:
1) test formats SQL (SQLFormatterTest)
test/sql_formatter_test.exs:5
Assertion with == failed
code: assert SQLFormatter.format("SELECT * FROM users") == "SELECT\n *\nFROM\n user\nWHERE\n id = 1\n"
left: """
SELECT
*
FROM
users
"""
right: """
SELECT
*
FROM
user
WHERE
id = 1
"""
stacktrace:
test/sql_formatter_test.exs:6: (test)
I’m curious if anyone has any suggestions for how to get better test output?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
codeanpeace
It sounds like something you could do with the
ExUnit.Formattermodule.source: docs for format_test_failure/5
sodapopcan
The problem I run into here is that
format_test_failureseems to escape\nas\\n. AFAIK (and I would love to be proved wrong) I think you have to do the formatting yourself.This is a cobbled together version from a formatter I have. I was hooking into the standard formatter and using
format_test_failureso I had to remove that which of course removes all the meta data like test name and line number (though it’s easily added as all of that info is available betweenerrorandmeta). All this has isleftandrightbut it does work if you want to use it as a starting point!Again, hopefully I’m wrong and there is an easier way.
egze
I’ve been doing similar things at work for showing meaningful diffs between 2 JSON files. I had to normalize both: sort keys (json keys don’t have order), pretty print, and only then diff.
See these links for inspiration:
blatyo
Thanks for the suggestions. I was definitely in the headspace of thinking I’d have to write a custom assertion. I forgot ex_unit even had formatters, so that will be a useful area to explore.