Skip to content

Commit 9c5f5a4

Browse files
committed
Add quick test to check json serialization
1 parent 98c8940 commit 9c5f5a4

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

test/ecto/integration/json_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule Ecto.Integration.JsonTest do
2+
use Ecto.Integration.Case
3+
4+
alias EctoSQLite3.Integration.Setting
5+
alias Ecto.Integration.TestRepo
6+
7+
test "serializes json correctly" do
8+
# Insert a record purposefully with atoms as the map key. We are going to
9+
# verify later they were coerced into strings.
10+
setting =
11+
%Setting{}
12+
|> Setting.changeset(%{properties: %{foo: "bar", qux: "baz"}})
13+
|> TestRepo.insert!()
14+
15+
# Read the record back using ecto and confirm it
16+
assert %Setting{properties: %{"foo" => "bar", "qux" => "baz"}} =
17+
TestRepo.get(Setting, setting.id)
18+
19+
assert %{num_rows: 1, rows: [["bar"]]} =
20+
Ecto.Adapters.SQL.query!(
21+
TestRepo,
22+
"select json_extract(properties, '$.foo') from settings where id = ?1",
23+
[setting.id]
24+
)
25+
end
26+
end

test/support/migration.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,9 @@ defmodule EctoSQLite3.Integration.Migration do
3737
add(:y, :float)
3838
add(:z, :float)
3939
end
40+
41+
create table(:settings) do
42+
add(:properties, :map)
43+
end
4044
end
4145
end

test/support/schemas.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,16 @@ defmodule EctoSQLite3.Integration.Vec3f do
112112
field(:z, :float)
113113
end
114114
end
115+
116+
defmodule EctoSQLite3.Integration.Setting do
117+
use Ecto.Schema
118+
import Ecto.Changeset
119+
120+
schema "settings" do
121+
field(:properties, :map)
122+
end
123+
124+
def changeset(struct, attrs) do
125+
cast(struct, attrs, [:properties])
126+
end
127+
end

0 commit comments

Comments
 (0)