|
| 1 | +defmodule EctoLibSql.Issue63InClauseTest do |
| 2 | + @moduledoc """ |
| 3 | + Test case for issue #63: Datatype mismatch due to JSON encoding of lists in IN statements. |
| 4 | +
|
| 5 | + The issue occurs when lists are used as parameters in IN clauses. |
| 6 | + Instead of expanding the list into individual parameters, the entire list |
| 7 | + was being JSON-encoded as a single string parameter, causing SQLite to raise |
| 8 | + a "datatype mismatch" error. |
| 9 | + """ |
| 10 | + |
| 11 | + use EctoLibSql.Integration.Case, async: false |
| 12 | + |
| 13 | + alias EctoLibSql.Integration.TestRepo |
| 14 | + alias EctoLibSql.Schemas.Product |
| 15 | + |
| 16 | + import Ecto.Query |
| 17 | + |
| 18 | + @test_db "z_ecto_libsql_test-issue_63.db" |
| 19 | + |
| 20 | + setup_all do |
| 21 | + Application.put_env(:ecto_libsql, EctoLibSql.Integration.TestRepo, |
| 22 | + adapter: Ecto.Adapters.LibSql, |
| 23 | + database: @test_db |
| 24 | + ) |
| 25 | + |
| 26 | + {:ok, _} = EctoLibSql.Integration.TestRepo.start_link() |
| 27 | + |
| 28 | + # Create test table with state column |
| 29 | + Ecto.Adapters.SQL.query!(TestRepo, """ |
| 30 | + CREATE TABLE IF NOT EXISTS test_items ( |
| 31 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 32 | + state TEXT, |
| 33 | + name TEXT, |
| 34 | + inserted_at TEXT, |
| 35 | + updated_at TEXT |
| 36 | + ) |
| 37 | + """) |
| 38 | + |
| 39 | + on_exit(fn -> |
| 40 | + EctoLibSql.TestHelpers.cleanup_db_files(@test_db) |
| 41 | + end) |
| 42 | + |
| 43 | + :ok |
| 44 | + end |
| 45 | + |
| 46 | + setup do |
| 47 | + # Clear table before each test |
| 48 | + Ecto.Adapters.SQL.query!(TestRepo, "DELETE FROM test_items", []) |
| 49 | + :ok |
| 50 | + end |
| 51 | + |
| 52 | + test "IN clause with list parameter should not JSON-encode the list" do |
| 53 | + # Insert test data with various states |
| 54 | + Ecto.Adapters.SQL.query!(TestRepo, """ |
| 55 | + INSERT INTO test_items (state, name, inserted_at, updated_at) |
| 56 | + VALUES ('scheduled', 'item1', datetime('now'), datetime('now')), |
| 57 | + ('retryable', 'item2', datetime('now'), datetime('now')), |
| 58 | + ('completed', 'item3', datetime('now'), datetime('now')), |
| 59 | + ('failed', 'item4', datetime('now'), datetime('now')) |
| 60 | + """) |
| 61 | + |
| 62 | + # This query should work without datatype mismatch error |
| 63 | + # Using a list parameter in an IN clause |
| 64 | + states = ["scheduled", "retryable"] |
| 65 | + |
| 66 | + query = |
| 67 | + from(t in "test_items", |
| 68 | + where: t.state in ^states, |
| 69 | + select: t.name |
| 70 | + ) |
| 71 | + |
| 72 | + # Execute the query - this should not raise "datatype mismatch" error |
| 73 | + result = TestRepo.all(query) |
| 74 | + |
| 75 | + # Should return the two items with scheduled or retryable state |
| 76 | + assert length(result) == 2 |
| 77 | + assert "item1" in result |
| 78 | + assert "item2" in result |
| 79 | + end |
| 80 | + |
| 81 | + test "IN clause with multiple parameter lists should work correctly" do |
| 82 | + # Insert test data |
| 83 | + Ecto.Adapters.SQL.query!(TestRepo, """ |
| 84 | + INSERT INTO test_items (state, name, inserted_at, updated_at) |
| 85 | + VALUES ('active', 'item1', datetime('now'), datetime('now')), |
| 86 | + ('inactive', 'item2', datetime('now'), datetime('now')), |
| 87 | + ('pending', 'item3', datetime('now'), datetime('now')) |
| 88 | + """) |
| 89 | + |
| 90 | + # Query with multiple filters including IN clause |
| 91 | + states = ["active", "pending"] |
| 92 | + |
| 93 | + query = |
| 94 | + from(t in "test_items", |
| 95 | + where: t.state in ^states, |
| 96 | + select: t.name |
| 97 | + ) |
| 98 | + |
| 99 | + result = TestRepo.all(query) |
| 100 | + |
| 101 | + assert length(result) == 2 |
| 102 | + assert "item1" in result |
| 103 | + assert "item3" in result |
| 104 | + end |
| 105 | + |
| 106 | + test "IN clause with empty list parameter" do |
| 107 | + # Insert test data |
| 108 | + Ecto.Adapters.SQL.query!(TestRepo, """ |
| 109 | + INSERT INTO test_items (state, name, inserted_at, updated_at) |
| 110 | + VALUES ('test', 'item1', datetime('now'), datetime('now')) |
| 111 | + """) |
| 112 | + |
| 113 | + # Query with empty list should return no results |
| 114 | + query = |
| 115 | + from(t in "test_items", |
| 116 | + where: t.state in ^[], |
| 117 | + select: t.name |
| 118 | + ) |
| 119 | + |
| 120 | + result = TestRepo.all(query) |
| 121 | + |
| 122 | + # Empty IN clause should match nothing |
| 123 | + assert result == [] |
| 124 | + end |
| 125 | +end |
0 commit comments