Current section
Files
Jump to
Current section
Files
lib/sql_formatter/assertions.ex
defmodule SQLFormatter.Assertions do
@moduledoc """
Helper functions for SQL formatting tests.
This module provides utilities for testing SQL formatting, particularly useful
for libraries that extend or customize SQLFormatter.
## Example
defmodule MyFormatterTest do
use ExUnit.Case
import SQLFormatter.Assertions, only: [assert_sql_equal: 2]
test "formats my custom SQL" do
assert_sql_equal(
MyFormatter.format("SELECT * FROM table"),
\"\"\"
SELECT
*
FROM
table
\"\"\"
)
end
end
"""
import ExUnit.Assertions
@doc """
Asserts that two SQL strings are equal, ignoring insignificant whitespace differences.
Provides a detailed diff output when the assertion fails, making it easier to spot
formatting differences between the expected and actual SQL.
## Examples
assert_sql_equal(
"SELECT * FROM users",
\"\"\"
SELECT
*
FROM
users
\"\"\"
)
"""
defmacro assert_sql_equal(actual, expected) do
quote do
actual_formatted = unquote(actual) |> SQLFormatter.format() |> String.trim()
expected_formatted = unquote(expected) |> SQLFormatter.format() |> String.trim()
assert actual_formatted == expected_formatted,
message: "SQL strings do not match",
left: "<SQL>" <> actual_formatted <> "</SQL>",
right: "<SQL>" <> expected_formatted <> "</SQL>"
end
end
@doc """
Asserts that two SQL strings are not equal, ignoring insignificant whitespace differences.
Provides a detailed diff output when the assertion fails, making it easier to spot
formatting differences between the expected and actual SQL.
## Examples
refute_sql_equal(
"SELECT * FROM users",
"SELECT * FROM user"
)
"""
defmacro refute_sql_equal(actual, expected) do
quote do
actual_formatted = unquote(actual) |> SQLFormatter.format() |> String.trim()
expected_formatted = unquote(expected) |> SQLFormatter.format() |> String.trim()
refute actual_formatted == expected_formatted,
message: "SQL strings should not match",
left: "<SQL>" <> actual_formatted <> "</SQL>",
right: "<SQL>" <> expected_formatted <> "</SQL>"
end
end
end