Skip to content

Commit df276d0

Browse files
committed
fix: support non-serial id in Elixir tests
1 parent df332a9 commit df276d0

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

tests/integration/elixir_test/lib/elixir_test/encrypted.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule ElixirTest.Encrypted do
22
use Ecto.Schema
33
import Ecto.Changeset
44

5-
@primary_key {:id, :id, autogenerate: true}
5+
@primary_key {:id, :id, autogenerate: false}
66
schema "encrypted" do
77
field(:plaintext, :string)
88
field(:plaintext_date, :date)

tests/integration/elixir_test/test/elixir_test_test.exs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ defmodule ElixirTestTest do
77

88
setup do
99
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
10+
max_id = Repo.aggregate(Encrypted, :max, :id) || 1
11+
%{next_id: max_id + 1}
1012
end
1113

1214
test "db connection test" do
@@ -15,9 +17,9 @@ defmodule ElixirTestTest do
1517
assert result.rows == [[1]]
1618
end
1719

18-
test "plaintext save and load" do
20+
test "plaintext save and load", %{next_id: next_id} do
1921
{:ok, result} =
20-
%Encrypted{plaintext: "plaintext content", plaintext_date: ~D[2025-06-02]}
22+
%Encrypted{id: next_id, plaintext: "plaintext content", plaintext_date: ~D[2025-06-02]}
2123
|> Repo.insert()
2224

2325
fetched = Encrypted |> Repo.get(result.id)
@@ -26,19 +28,20 @@ defmodule ElixirTestTest do
2628
assert fetched.plaintext_date == ~D[2025-06-02]
2729
end
2830

29-
test "encrypted text save and load" do
31+
test "encrypted text save and load", %{next_id: next_id} do
3032
{:ok, result} =
31-
%Encrypted{encrypted_text: "encrypted text content"}
33+
%Encrypted{id: next_id, encrypted_text: "encrypted text content"}
3234
|> Repo.insert()
3335

3436
fetched = Encrypted |> Repo.get(result.id)
3537

3638
assert fetched.encrypted_text == "encrypted text content"
3739
end
3840

39-
test "encrypted fields save and load" do
41+
test "encrypted fields save and load", %{next_id: next_id} do
4042
{:ok, result} =
4143
%Encrypted{
44+
id: next_id,
4245
encrypted_bool: false,
4346
encrypted_int2: 2,
4447
encrypted_int4: 4,
@@ -60,12 +63,12 @@ defmodule ElixirTestTest do
6063
assert fetched.encrypted_jsonb == %{"top" => %{"array" => [1, 2, 3]}}
6164
end
6265

63-
test "find by exact text" do
66+
test "find by exact text", %{next_id: next_id} do
6467
{2, _} =
6568
Encrypted
6669
|> Repo.insert_all([
67-
%{encrypted_text: "encrypted text content"},
68-
%{encrypted_text: "some other encrypted text"}
70+
%{id: next_id, encrypted_text: "encrypted text content"},
71+
%{id: next_id + 1, encrypted_text: "some other encrypted text"}
6972
])
7073

7174
q =
@@ -79,12 +82,12 @@ defmodule ElixirTestTest do
7982
assert Enum.at(fetched, 0) == ["encrypted text content"]
8083
end
8184

82-
test "find by text match" do
85+
test "find by text match", %{next_id: next_id} do
8386
{2, _} =
8487
Encrypted
8588
|> Repo.insert_all([
86-
%{encrypted_text: "encrypted text content"},
87-
%{encrypted_text: "some other encrypted text"}
89+
%{id: next_id, encrypted_text: "encrypted text content"},
90+
%{id: next_id + 1, encrypted_text: "some other encrypted text"}
8891
])
8992

9093
q =
@@ -98,10 +101,13 @@ defmodule ElixirTestTest do
98101
assert Enum.at(fetched, 0) == ["encrypted text content"]
99102
end
100103

101-
test "find by float value - currently not supported" do
104+
test "find by float value - currently not supported", %{next_id: next_id} do
102105
{2, _} =
103106
Encrypted
104-
|> Repo.insert_all([%{encrypted_float8: 0.0}, %{encrypted_float8: 7.5}])
107+
|> Repo.insert_all([
108+
%{id: next_id, encrypted_float8: 0.0},
109+
%{id: next_id + 1, encrypted_float8: 7.5}
110+
])
105111

106112
# Ecto appends explicit cast to `7.5`, making it `7.5::float` and causes
107113
# the "operator does not exist" error
@@ -114,10 +120,13 @@ defmodule ElixirTestTest do
114120
assert_raise(Postgrex.Error, fn -> Repo.all(q) end)
115121
end
116122

117-
test "find by float value" do
123+
test "find by float value", %{next_id: next_id} do
118124
{2, _} =
119125
Encrypted
120-
|> Repo.insert_all([%{id: 1, encrypted_float8: 0.0}, %{id: 2, encrypted_float8: 7.5}])
126+
|> Repo.insert_all([
127+
%{id: next_id, encrypted_float8: 0.0},
128+
%{id: next_id + 1, encrypted_float8: 7.5}
129+
])
121130

122131
q =
123132
from(e in "encrypted",
@@ -127,13 +136,16 @@ defmodule ElixirTestTest do
127136

128137
fetched = Repo.all(q)
129138

130-
assert Enum.at(fetched, 0) == [2, 7.5]
139+
assert Enum.at(fetched, 0) == [next_id + 1, 7.5]
131140
end
132141

133-
test "find by float value gt" do
142+
test "find by float value gt", %{next_id: next_id} do
134143
{2, _} =
135144
Encrypted
136-
|> Repo.insert_all([%{id: 1, encrypted_float8: 0.0}, %{id: 2, encrypted_float8: 7.5}])
145+
|> Repo.insert_all([
146+
%{id: next_id, encrypted_float8: 0.0},
147+
%{id: next_id + 1, encrypted_float8: 7.5}
148+
])
137149

138150
q =
139151
from(e in "encrypted",
@@ -143,16 +155,16 @@ defmodule ElixirTestTest do
143155

144156
fetched = Repo.all(q)
145157

146-
assert Enum.at(fetched, 0) == [2, 7.5]
158+
assert Enum.at(fetched, 0) == [next_id + 1, 7.5]
147159
end
148160

149-
test "order by integer" do
161+
test "order by integer", %{next_id: next_id} do
150162
{3, _} =
151163
Encrypted
152164
|> Repo.insert_all([
153-
%{id: 1, encrypted_int2: 7},
154-
%{id: 2, encrypted_int2: 9},
155-
%{id: 3, encrypted_int2: 0}
165+
%{id: next_id, encrypted_int2: 7},
166+
%{id: next_id + 1, encrypted_int2: 9},
167+
%{id: next_id + 2, encrypted_int2: 0}
156168
])
157169

158170
q =
@@ -166,13 +178,13 @@ defmodule ElixirTestTest do
166178
assert fetched == [0, 7, 9]
167179
end
168180

169-
test "find by text and float" do
181+
test "find by text and float", %{next_id: next_id} do
170182
{3, _} =
171183
Encrypted
172184
|> Repo.insert_all([
173-
%{encrypted_text: "encrypted text content", encrypted_float8: 1.0},
174-
%{encrypted_text: "encrypted text content", encrypted_float8: 3.0},
175-
%{encrypted_text: "some other encrypted text", encrypted_float8: 5.0}
185+
%{id: next_id, encrypted_text: "encrypted text content", encrypted_float8: 1.0},
186+
%{id: next_id + 1, encrypted_text: "encrypted text content", encrypted_float8: 3.0},
187+
%{id: next_id + 2, encrypted_text: "some other encrypted text", encrypted_float8: 5.0}
176188
])
177189

178190
q =

0 commit comments

Comments
 (0)