File tree 3 files changed +43
-0
lines changed
3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -37,5 +37,9 @@ defmodule EctoSQLite3.Integration.Migration do
37
37
add ( :y , :float )
38
38
add ( :z , :float )
39
39
end
40
+
41
+ create table ( :settings ) do
42
+ add ( :properties , :map )
43
+ end
40
44
end
41
45
end
Original file line number Diff line number Diff line change @@ -112,3 +112,16 @@ defmodule EctoSQLite3.Integration.Vec3f do
112
112
field ( :z , :float )
113
113
end
114
114
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
You can’t perform that action at this time.
0 commit comments