xref: /sqlite-3.40.0/test/json104.test (revision e229ca03)
1633647afSdrh# 2017-03-22
2633647afSdrh#
3633647afSdrh# The author disclaims copyright to this source code.  In place of
4633647afSdrh# a legal notice, here is a blessing:
5633647afSdrh#
6633647afSdrh#    May you do good and not evil.
7633647afSdrh#    May you find forgiveness for yourself and forgive others.
8633647afSdrh#    May you share freely, never taking more than you give.
9633647afSdrh#
10633647afSdrh#***********************************************************************
1137f03dfbSdrh# This file implements tests for json_patch(A,B) SQL function.
12633647afSdrh#
13633647afSdrh
14633647afSdrhset testdir [file dirname $argv0]
15633647afSdrhsource $testdir/tester.tcl
16fe9a832eSdanset testprefix json104
17633647afSdrh
18633647afSdrh# This is the example from pages 2 and 3 of RFC-7396
19633647afSdrhdo_execsql_test json104-100 {
2037f03dfbSdrh  SELECT json_patch('{
21633647afSdrh       "a": "b",
22633647afSdrh       "c": {
23633647afSdrh         "d": "e",
24633647afSdrh         "f": "g"
25633647afSdrh       }
26f07b249fSdrh     }','{
27633647afSdrh       "a":"z",
28633647afSdrh       "c": {
29633647afSdrh         "f": null
30633647afSdrh       }
31f07b249fSdrh     }');
32633647afSdrh} {{{"a":"z","c":{"d":"e"}}}}
33633647afSdrh
34633647afSdrh
35633647afSdrh# This is the example from pages 4 and 5 of RFC-7396
36633647afSdrhdo_execsql_test json104-110 {
3737f03dfbSdrh  SELECT json_patch('{
38633647afSdrh       "title": "Goodbye!",
39633647afSdrh       "author" : {
40633647afSdrh         "givenName" : "John",
41633647afSdrh         "familyName" : "Doe"
42633647afSdrh       },
43633647afSdrh       "tags":[ "example", "sample" ],
44633647afSdrh       "content": "This will be unchanged"
45f07b249fSdrh     }','{
46633647afSdrh       "title": "Hello!",
47633647afSdrh       "phoneNumber": "+01-123-456-7890",
48633647afSdrh       "author": {
49633647afSdrh         "familyName": null
50633647afSdrh       },
51633647afSdrh       "tags": [ "example" ]
52f07b249fSdrh     }');
53bb7aa2d8Sdrh} {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}}
54bb7aa2d8Sdrh
55bb7aa2d8Sdrhdo_execsql_test json104-200 {
5637f03dfbSdrh  SELECT json_patch('[1,2,3]','{"x":null}');
57bb7aa2d8Sdrh} {{{}}}
58bb7aa2d8Sdrhdo_execsql_test json104-210 {
5937f03dfbSdrh  SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}');
60bb7aa2d8Sdrh} {{{"y":1}}}
6129c99698Sdrhdo_execsql_test json104-220 {
6229c99698Sdrh  SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
6329c99698Sdrh} {{{"a":{"bb":{}}}}}
6429c99698Sdrhdo_execsql_test json104-221 {
6529c99698Sdrh  SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,null,3]}}}');
6629c99698Sdrh} {{{"a":{"bb":{"ccc":[1,null,3]}}}}}
6729c99698Sdrhdo_execsql_test json104-222 {
6829c99698Sdrh  SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}');
6929c99698Sdrh} {{{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}}}
7029c99698Sdrh
71f9e91972Sdrh# Example test cases at the end of the RFC-7396 document
72f9e91972Sdrhdo_execsql_test json104-300 {
73f9e91972Sdrh  SELECT json_patch('{"a":"b"}','{"a":"c"}');
74f9e91972Sdrh} {{{"a":"c"}}}
75f9e91972Sdrhdo_execsql_test json104-300a {
76f9e91972Sdrh  SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null');
77f9e91972Sdrh} {{real-null}}
78f9e91972Sdrhdo_execsql_test json104-301 {
79f9e91972Sdrh  SELECT json_patch('{"a":"b"}','{"b":"c"}');
80f9e91972Sdrh} {{{"a":"b","b":"c"}}}
81f9e91972Sdrhdo_execsql_test json104-302 {
82f9e91972Sdrh  SELECT json_patch('{"a":"b"}','{"a":null}');
83f9e91972Sdrh} {{{}}}
84f9e91972Sdrhdo_execsql_test json104-303 {
85f9e91972Sdrh  SELECT json_patch('{"a":"b","b":"c"}','{"a":null}');
86f9e91972Sdrh} {{{"b":"c"}}}
87f9e91972Sdrhdo_execsql_test json104-304 {
88f9e91972Sdrh  SELECT json_patch('{"a":["b"]}','{"a":"c"}');
89f9e91972Sdrh} {{{"a":"c"}}}
90f9e91972Sdrhdo_execsql_test json104-305 {
91f9e91972Sdrh  SELECT json_patch('{"a":"c"}','{"a":["b"]}');
92f9e91972Sdrh} {{{"a":["b"]}}}
93f9e91972Sdrhdo_execsql_test json104-306 {
94f9e91972Sdrh  SELECT json_patch('{"a":{"b":"c"}}','{"a":{"b":"d","c":null}}');
95f9e91972Sdrh} {{{"a":{"b":"d"}}}}
96f9e91972Sdrhdo_execsql_test json104-307 {
97f9e91972Sdrh  SELECT json_patch('{"a":[{"b":"c"}]}','{"a":[1]}');
98f9e91972Sdrh} {{{"a":[1]}}}
99f9e91972Sdrhdo_execsql_test json104-308 {
100f9e91972Sdrh  SELECT json_patch('["a","b"]','["c","d"]');
101f9e91972Sdrh} {{["c","d"]}}
102f9e91972Sdrhdo_execsql_test json104-309 {
103f9e91972Sdrh  SELECT json_patch('{"a":"b"}','["c"]');
104f9e91972Sdrh} {{["c"]}}
105f9e91972Sdrhdo_execsql_test json104-310 {
106f9e91972Sdrh  SELECT json_patch('{"a":"foo"}','null');
107f9e91972Sdrh} {{null}}
108f9e91972Sdrhdo_execsql_test json104-310a {
109f9e91972Sdrh  SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null');
110f9e91972Sdrh} {{real-null}}
111f9e91972Sdrhdo_execsql_test json104-311 {
112f9e91972Sdrh  SELECT json_patch('{"a":"foo"}','"bar"');
113f9e91972Sdrh} {{"bar"}}
114f9e91972Sdrhdo_execsql_test json104-312 {
115f9e91972Sdrh  SELECT json_patch('{"e":null}','{"a":1}');
116f9e91972Sdrh} {{{"e":null,"a":1}}}
117f9e91972Sdrhdo_execsql_test json104-313 {
118f9e91972Sdrh  SELECT json_patch('[1,2]','{"a":"b","c":null}');
119f9e91972Sdrh} {{{"a":"b"}}}
120f9e91972Sdrhdo_execsql_test json104-314 {
121f9e91972Sdrh  SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
122f9e91972Sdrh} {{{"a":{"bb":{}}}}}
123*a2852ac4Sdrhdo_execsql_test json104-320 {
124*a2852ac4Sdrh  SELECT json_patch('{"x":{"one":1}}','{"x":{"two":2},"x":"three"}');
125*a2852ac4Sdrh} {{{"x":"three"}}}
126f9e91972Sdrh
127fe9a832eSdan#-------------------------------------------------------------------------
128fe9a832eSdan
129fe9a832eSdando_execsql_test 401 {
130fe9a832eSdan  CREATE TABLE obj(x);
131fe9a832eSdan  INSERT INTO obj VALUES('{"a":1,"b":2}');
132fe9a832eSdan  SELECT * FROM obj;
133fe9a832eSdan} {{{"a":1,"b":2}}}
134fe9a832eSdando_execsql_test 402 {
135fe9a832eSdan  UPDATE obj SET x = json_insert(x, '$.c', 3);
136fe9a832eSdan  SELECT * FROM obj;
137fe9a832eSdan} {{{"a":1,"b":2,"c":3}}}
138fe9a832eSdando_execsql_test 403 {
139fe9a832eSdan  SELECT json_extract(x, '$.b') FROM obj;
140fe9a832eSdan  SELECT json_extract(x, '$."b"') FROM obj;
141fe9a832eSdan} {2 2}
142fe9a832eSdando_execsql_test 404 {
143fe9a832eSdan  UPDATE obj SET x = json_set(x, '$."b"', 555);
144fe9a832eSdan  SELECT json_extract(x, '$.b') FROM obj;
145fe9a832eSdan  SELECT json_extract(x, '$."b"') FROM obj;
146fe9a832eSdan} {555 555}
147fe9a832eSdando_execsql_test 405 {
148fe9a832eSdan  UPDATE obj SET x = json_set(x, '$."d"', 4);
149fe9a832eSdan  SELECT json_extract(x, '$."d"') FROM obj;
150fe9a832eSdan} {4}
151f9e91972Sdrh
152633647afSdrh
153633647afSdrhfinish_test
154