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
23 # This is the example from pages 2 and 3 of RFC-7396
24 do_execsql_test json104-100 {
37 } {{{"a":"z","c":{"d":"e"}}}}
40 # This is the example from pages 4 and 5 of RFC-7396
41 do_execsql_test json104-110 {
48 "tags":[ "example", "sample" ],
49 "content": "This will be unchanged"
52 "phoneNumber": "+01-123-456-7890",
58 } {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}}
60 do_execsql_test json104-200 {
61 SELECT json_patch('[1,2,3]','{"x":null}');
63 do_execsql_test json104-210 {
64 SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}');
66 do_execsql_test json104-220 {
67 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
69 do_execsql_test json104-221 {
70 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,null,3]}}}');
71 } {{{"a":{"bb":{"ccc":[1,null,3]}}}}}
72 do_execsql_test json104-222 {
73 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}');
74 } {{{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}}}
76 # Example test cases at the end of the RFC-7396 document
77 do_execsql_test json104-300 {
78 SELECT json_patch('{"a":"b"}','{"a":"c"}');
80 do_execsql_test json104-300a {
81 SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null');
83 do_execsql_test json104-301 {
84 SELECT json_patch('{"a":"b"}','{"b":"c"}');
85 } {{{"a":"b","b":"c"}}}
86 do_execsql_test json104-302 {
87 SELECT json_patch('{"a":"b"}','{"a":null}');
89 do_execsql_test json104-303 {
90 SELECT json_patch('{"a":"b","b":"c"}','{"a":null}');
92 do_execsql_test json104-304 {
93 SELECT json_patch('{"a":["b"]}','{"a":"c"}');
95 do_execsql_test json104-305 {
96 SELECT json_patch('{"a":"c"}','{"a":["b"]}');
98 do_execsql_test json104-306 {
99 SELECT json_patch('{"a":{"b":"c"}}','{"a":{"b":"d","c":null}}');
100 } {{{"a":{"b":"d"}}}}
101 do_execsql_test json104-307 {
102 SELECT json_patch('{"a":[{"b":"c"}]}','{"a":[1]}');
104 do_execsql_test json104-308 {
105 SELECT json_patch('["a","b"]','["c","d"]');
107 do_execsql_test json104-309 {
108 SELECT json_patch('{"a":"b"}','["c"]');
110 do_execsql_test json104-310 {
111 SELECT json_patch('{"a":"foo"}','null');
113 do_execsql_test json104-310a {
114 SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null');
116 do_execsql_test json104-311 {
117 SELECT json_patch('{"a":"foo"}','"bar"');
119 do_execsql_test json104-312 {
120 SELECT json_patch('{"e":null}','{"a":1}');
121 } {{{"e":null,"a":1}}}
122 do_execsql_test json104-313 {
123 SELECT json_patch('[1,2]','{"a":"b","c":null}');
125 do_execsql_test json104-314 {
126 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
127 } {{{"a":{"bb":{}}}}}
129 #-------------------------------------------------------------------------
131 do_execsql_test 401 {
133 INSERT INTO obj VALUES('{"a":1,"b":2}');
136 do_execsql_test 402 {
137 UPDATE obj SET x = json_insert(x, '$.c', 3);
139 } {{{"a":1,"b":2,"c":3}}}
140 do_execsql_test 403 {
141 SELECT json_extract(x, '$.b') FROM obj;
142 SELECT json_extract(x, '$."b"') FROM obj;
144 do_execsql_test 404 {
145 UPDATE obj SET x = json_set(x, '$."b"', 555);
146 SELECT json_extract(x, '$.b') FROM obj;
147 SELECT json_extract(x, '$."b"') FROM obj;
149 do_execsql_test 405 {
150 UPDATE obj SET x = json_set(x, '$."d"', 4);
151 SELECT json_extract(x, '$."d"') FROM obj;