forked from elixir-sqlite/ecto_sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.exs
More file actions
56 lines (46 loc) · 1.81 KB
/
json_test.exs
File metadata and controls
56 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
defmodule Ecto.Integration.JsonTest do
use Ecto.Integration.Case, async: false
alias Ecto.Adapters.SQL
alias Ecto.Integration.TestRepo
alias EctoSQLite3.Schemas.Setting
@moduletag :integration
setup do
Application.put_env(:ecto_sqlite3, :map_type, :string)
on_exit(fn -> Application.put_env(:ecto_sqlite3, :map_type, :string) end)
end
test "serializes json correctly with string format" do
# Insert a record purposefully with atoms as the map key. We are going to
# verify later they were coerced into strings.
setting =
%Setting{}
|> Setting.changeset(%{properties: %{foo: "bar", qux: "baz"}})
|> TestRepo.insert!()
# Read the record back using ecto and confirm it
assert %Setting{properties: %{"foo" => "bar", "qux" => "baz"}} =
TestRepo.get(Setting, setting.id)
assert %{num_rows: 1, rows: [["bar"]]} =
SQL.query!(
TestRepo,
"select json_extract(properties, '$.foo') from settings where id = ?1",
[setting.id]
)
end
test "serializes json correctly with binary format" do
Application.put_env(:ecto_sqlite3, :map_type, :binary)
# Insert a record purposefully with atoms as the map key. We are going to
# verify later they were coerced into strings.
setting =
%Setting{}
|> Setting.changeset(%{properties: %{foo: "bar", qux: "baz"}})
|> TestRepo.insert!()
# Read the record back using ecto and confirm it
assert %Setting{properties: %{"foo" => "bar", "qux" => "baz"}} =
TestRepo.get(Setting, setting.id)
assert %{num_rows: 1, rows: [["bar"]]} =
SQL.query!(
TestRepo,
"select json_extract(properties, '$.foo') from settings where id = ?1",
[setting.id]
)
end
end