3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements tests for json_patch(A,B) SQL function.
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix json104
18 # This is the example from pages 2 and 3 of RFC-7396
19 do_execsql_test json104-100 {
32 } {{{"a":"z","c":{"d":"e"}}}}
33 do_execsql_test json104-101 {
46 } {{{"a":"z","c":{"d":"e"}}}}
47 do_execsql_test json104-102 {
60 } {{{"a":"z","c":{"d":"e"}}}}
61 do_execsql_test json104-103 {
74 } {{{"a":"z","c":{"d":"e"}}}}
77 # This is the example from pages 4 and 5 of RFC-7396
78 do_execsql_test json104-110 {
85 "tags":[ "example", "sample" ],
86 "content": "This will be unchanged"
89 "phoneNumber": "+01-123-456-7890",
95 } {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}}
97 do_execsql_test json104-200 {
98 SELECT json_patch('[1,2,3]','{"x":null}');
100 do_execsql_test json104-210 {
101 SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}');
103 do_execsql_test json104-220 {
104 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
105 } {{{"a":{"bb":{}}}}}
106 do_execsql_test json104-221 {
107 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,null,3]}}}');
108 } {{{"a":{"bb":{"ccc":[1,null,3]}}}}}
109 do_execsql_test json104-222 {
110 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}');
111 } {{{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}}}
113 # Example test cases at the end of the RFC-7396 document
114 do_execsql_test json104-300 {
115 SELECT json_patch('{"a":"b"}','{"a":"c"}');
117 do_execsql_test json104-300a {
118 SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null');
120 do_execsql_test json104-301 {
121 SELECT json_patch('{"a":"b"}','{"b":"c"}');
122 } {{{"a":"b","b":"c"}}}
123 do_execsql_test json104-302 {
124 SELECT json_patch('{"a":"b"}','{"a":null}');
126 do_execsql_test json104-303 {
127 SELECT json_patch('{"a":"b","b":"c"}','{"a":null}');
129 do_execsql_test json104-304 {
130 SELECT json_patch('{"a":["b"]}','{"a":"c"}');
132 do_execsql_test json104-305 {
133 SELECT json_patch('{"a":"c"}','{"a":["b"]}');
135 do_execsql_test json104-306 {
136 SELECT json_patch('{"a":{"b":"c"}}','{"a":{"b":"d","c":null}}');
137 } {{{"a":{"b":"d"}}}}
138 do_execsql_test json104-307 {
139 SELECT json_patch('{"a":[{"b":"c"}]}','{"a":[1]}');
141 do_execsql_test json104-308 {
142 SELECT json_patch('["a","b"]','["c","d"]');
144 do_execsql_test json104-309 {
145 SELECT json_patch('{"a":"b"}','["c"]');
147 do_execsql_test json104-310 {
148 SELECT json_patch('{"a":"foo"}','null');
150 do_execsql_test json104-310a {
151 SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null');
153 do_execsql_test json104-311 {
154 SELECT json_patch('{"a":"foo"}','"bar"');
156 do_execsql_test json104-312 {
157 SELECT json_patch('{"e":null}','{"a":1}');
158 } {{{"e":null,"a":1}}}
159 do_execsql_test json104-313 {
160 SELECT json_patch('[1,2]','{"a":"b","c":null}');
162 do_execsql_test json104-314 {
163 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
164 } {{{"a":{"bb":{}}}}}
165 do_execsql_test json104-320 {
166 SELECT json_patch('{"x":{"one":1}}','{"x":{"two":2},"x":"three"}');
169 #-------------------------------------------------------------------------
171 do_execsql_test 401 {
173 INSERT INTO obj VALUES('{"a":1,"b":2}');
176 do_execsql_test 402 {
177 UPDATE obj SET x = json_insert(x, '$.c', 3);
179 } {{{"a":1,"b":2,"c":3}}}
180 do_execsql_test 403 {
181 SELECT json_extract(x, '$.b') FROM obj;
182 SELECT json_extract(x, '$."b"') FROM obj;
184 do_execsql_test 404 {
185 UPDATE obj SET x = json_set(x, '$."b"', 555);
186 SELECT json_extract(x, '$.b') FROM obj;
187 SELECT json_extract(x, '$."b"') FROM obj;
189 do_execsql_test 405 {
190 UPDATE obj SET x = json_set(x, '$."d"', 4);
191 SELECT json_extract(x, '$."d"') FROM obj;