Skip to content

Instantly share code, notes, and snippets.

@dkuku
Last active June 17, 2024 05:48
Show Gist options
  • Save dkuku/0928b89064df320765b957a6a3c4b3cd to your computer and use it in GitHub Desktop.
Save dkuku/0928b89064df320765b957a6a3c4b3cd to your computer and use it in GitHub Desktop.
Debugging flaky tests in elixir:
file_path = "/Users/you/debug"
# open file
{:ok, io_device} = File.open(file_path, [:append])
# get all schemas
:code.all_loaded()
|> Enum.map(&elem(&1, 0))
|> Enum.filter(&(to_string(&1) =~ "Schemas"))
|> Enum.map(fn schema ->
# get all elements for a schema
schema
|> Repo.all()
|> tap(
&if &1 != [] do
IO.write(io_device, "\n-----\n")
# display overview
IO.inspect(length(&1), label: schema)
IO.write(io_device, "#{schema}: #{length(&1)}")
end
)
|> Enum.map(fn el ->
el
|> Map.from_struct()
|> Enum.reject(fn {k, v} ->
match?(%Ecto.Association.NotLoaded{}, v) or k == :__meta__
end)
# format the data
|> Keyword.values()
|> Enum.map_join(", ", &inspect/1)
# write to file
|> then(fn formated_data -> IO.write(io_device, ["\n", formated_data]) end)
end)
end)
File.close(io_device)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment