blob: 56dd2738c387499693e1dfe6f38c900b1fa9e2d0 [file] [log] [blame]
drh633647a2017-03-22 21:24:31 +00001# 2017-03-22
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
drh37f03df2017-03-23 20:33:49 +000011# This file implements tests for json_patch(A,B) SQL function.
drh633647a2017-03-22 21:24:31 +000012#
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
danfe9a8322019-06-17 14:50:33 +000016set testprefix json104
drh633647a2017-03-22 21:24:31 +000017
drh633647a2017-03-22 21:24:31 +000018# This is the example from pages 2 and 3 of RFC-7396
19do_execsql_test json104-100 {
drh37f03df2017-03-23 20:33:49 +000020 SELECT json_patch('{
drh633647a2017-03-22 21:24:31 +000021 "a": "b",
22 "c": {
23 "d": "e",
24 "f": "g"
25 }
drhf07b2492017-03-22 21:45:20 +000026 }','{
drh633647a2017-03-22 21:24:31 +000027 "a":"z",
28 "c": {
29 "f": null
30 }
drhf07b2492017-03-22 21:45:20 +000031 }');
drh633647a2017-03-22 21:24:31 +000032} {{{"a":"z","c":{"d":"e"}}}}
33
34
35# This is the example from pages 4 and 5 of RFC-7396
36do_execsql_test json104-110 {
drh37f03df2017-03-23 20:33:49 +000037 SELECT json_patch('{
drh633647a2017-03-22 21:24:31 +000038 "title": "Goodbye!",
39 "author" : {
40 "givenName" : "John",
41 "familyName" : "Doe"
42 },
43 "tags":[ "example", "sample" ],
44 "content": "This will be unchanged"
drhf07b2492017-03-22 21:45:20 +000045 }','{
drh633647a2017-03-22 21:24:31 +000046 "title": "Hello!",
47 "phoneNumber": "+01-123-456-7890",
48 "author": {
49 "familyName": null
50 },
51 "tags": [ "example" ]
drhf07b2492017-03-22 21:45:20 +000052 }');
drhbb7aa2d2017-03-23 00:13:52 +000053} {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}}
54
55do_execsql_test json104-200 {
drh37f03df2017-03-23 20:33:49 +000056 SELECT json_patch('[1,2,3]','{"x":null}');
drhbb7aa2d2017-03-23 00:13:52 +000057} {{{}}}
58do_execsql_test json104-210 {
drh37f03df2017-03-23 20:33:49 +000059 SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}');
drhbb7aa2d2017-03-23 00:13:52 +000060} {{{"y":1}}}
drh29c99692017-03-24 12:35:17 +000061do_execsql_test json104-220 {
62 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
63} {{{"a":{"bb":{}}}}}
64do_execsql_test json104-221 {
65 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,null,3]}}}');
66} {{{"a":{"bb":{"ccc":[1,null,3]}}}}}
67do_execsql_test json104-222 {
68 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}');
69} {{{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}}}
70
drhf9e91972017-03-24 13:31:47 +000071# Example test cases at the end of the RFC-7396 document
72do_execsql_test json104-300 {
73 SELECT json_patch('{"a":"b"}','{"a":"c"}');
74} {{{"a":"c"}}}
75do_execsql_test json104-300a {
76 SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null');
77} {{real-null}}
78do_execsql_test json104-301 {
79 SELECT json_patch('{"a":"b"}','{"b":"c"}');
80} {{{"a":"b","b":"c"}}}
81do_execsql_test json104-302 {
82 SELECT json_patch('{"a":"b"}','{"a":null}');
83} {{{}}}
84do_execsql_test json104-303 {
85 SELECT json_patch('{"a":"b","b":"c"}','{"a":null}');
86} {{{"b":"c"}}}
87do_execsql_test json104-304 {
88 SELECT json_patch('{"a":["b"]}','{"a":"c"}');
89} {{{"a":"c"}}}
90do_execsql_test json104-305 {
91 SELECT json_patch('{"a":"c"}','{"a":["b"]}');
92} {{{"a":["b"]}}}
93do_execsql_test json104-306 {
94 SELECT json_patch('{"a":{"b":"c"}}','{"a":{"b":"d","c":null}}');
95} {{{"a":{"b":"d"}}}}
96do_execsql_test json104-307 {
97 SELECT json_patch('{"a":[{"b":"c"}]}','{"a":[1]}');
98} {{{"a":[1]}}}
99do_execsql_test json104-308 {
100 SELECT json_patch('["a","b"]','["c","d"]');
101} {{["c","d"]}}
102do_execsql_test json104-309 {
103 SELECT json_patch('{"a":"b"}','["c"]');
104} {{["c"]}}
105do_execsql_test json104-310 {
106 SELECT json_patch('{"a":"foo"}','null');
107} {{null}}
108do_execsql_test json104-310a {
109 SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null');
110} {{real-null}}
111do_execsql_test json104-311 {
112 SELECT json_patch('{"a":"foo"}','"bar"');
113} {{"bar"}}
114do_execsql_test json104-312 {
115 SELECT json_patch('{"e":null}','{"a":1}');
116} {{{"e":null,"a":1}}}
117do_execsql_test json104-313 {
118 SELECT json_patch('[1,2]','{"a":"b","c":null}');
119} {{{"a":"b"}}}
120do_execsql_test json104-314 {
121 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
122} {{{"a":{"bb":{}}}}}
drha2852ac2021-11-15 01:45:11 +0000123do_execsql_test json104-320 {
124 SELECT json_patch('{"x":{"one":1}}','{"x":{"two":2},"x":"three"}');
125} {{{"x":"three"}}}
drhf9e91972017-03-24 13:31:47 +0000126
danfe9a8322019-06-17 14:50:33 +0000127#-------------------------------------------------------------------------
128
129do_execsql_test 401 {
130 CREATE TABLE obj(x);
131 INSERT INTO obj VALUES('{"a":1,"b":2}');
132 SELECT * FROM obj;
133} {{{"a":1,"b":2}}}
134do_execsql_test 402 {
135 UPDATE obj SET x = json_insert(x, '$.c', 3);
136 SELECT * FROM obj;
137} {{{"a":1,"b":2,"c":3}}}
138do_execsql_test 403 {
139 SELECT json_extract(x, '$.b') FROM obj;
140 SELECT json_extract(x, '$."b"') FROM obj;
141} {2 2}
142do_execsql_test 404 {
143 UPDATE obj SET x = json_set(x, '$."b"', 555);
144 SELECT json_extract(x, '$.b') FROM obj;
145 SELECT json_extract(x, '$."b"') FROM obj;
146} {555 555}
147do_execsql_test 405 {
148 UPDATE obj SET x = json_set(x, '$."d"', 4);
149 SELECT json_extract(x, '$."d"') FROM obj;
150} {4}
drhf9e91972017-03-24 13:31:47 +0000151
drh633647a2017-03-22 21:24:31 +0000152
153finish_test