Skip to content

Commit 65dec2b

Browse files
committed
Update decimal SQL query assertions to accept both numeric and string representations
Modified the decimal type tests to be more flexible when validating raw SQL query results from Ecto.Adapters.SQL.query: - 'decimal fields load and dump as strings': Now uses case pattern matching to accept either float/integer or binary string representations - 'handles negative decimals and zero': Normalizes all rows to strings for comparison, accepting both numeric and string formats This accommodates different SQLite drivers that may return decimals as either floats or strings. The loader tests (using TestRepo.get with Decimal.equal?) remain unchanged and continue to verify proper Ecto loader behavior.
1 parent 8b1d2de commit 65dec2b

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

test/type_loader_dumper_test.exs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,17 @@ defmodule EctoLibSql.TypeLoaderDumperTest do
370370

371371
{:ok, result} = Ecto.Adapters.SQL.query(TestRepo, "SELECT decimal_field FROM all_types")
372372

373-
# SQLite's NUMERIC type affinity stores decimals as numbers when possible
374-
assert [[123.45]] = result.rows
373+
# SQLite's NUMERIC type affinity stores decimals as numbers when possible,
374+
# but we need to accept either float or string representation from the query result
375+
assert [[value]] = result.rows
376+
377+
case value do
378+
v when is_float(v) or is_integer(v) ->
379+
assert abs(v - 123.45) < 0.001
380+
381+
v when is_binary(v) ->
382+
assert v == "123.45"
383+
end
375384
end
376385

377386
test "decimal loader parses strings, integers, and floats" do
@@ -399,8 +408,31 @@ defmodule EctoLibSql.TypeLoaderDumperTest do
399408
"SELECT decimal_field FROM all_types ORDER BY decimal_field"
400409
)
401410

402-
# SQLite's NUMERIC type affinity stores decimals as numbers
403-
assert [[-123.45], [0], [999.999]] = result.rows
411+
# SQLite's NUMERIC type affinity stores decimals as numbers, but accept
412+
# both numeric and string representations from the query result
413+
assert 3 = length(result.rows)
414+
415+
# Normalize rows by converting to strings for comparison
416+
normalized_rows =
417+
Enum.map(result.rows, fn [value] ->
418+
case value do
419+
v when is_float(v) or is_integer(v) -> to_string(v)
420+
v when is_binary(v) -> v
421+
end
422+
end)
423+
424+
# Verify values in sorted order (by parsed numeric value)
425+
assert length(normalized_rows) == 3
426+
[first, second, third] = normalized_rows
427+
428+
# Check first is -123.45 (or 123.45 with leading -)
429+
assert String.contains?(first, "-123.45") or first == "-123.45"
430+
431+
# Check second is 0
432+
assert second == "0" or String.to_float(second) == 0.0
433+
434+
# Check third is 999.999
435+
assert String.contains?(third, "999.999") or third == "999.999"
404436
end
405437
end
406438

0 commit comments

Comments
 (0)