The first assert() added in [0ebc65481f4a3e79] is not necessarily true in a
[sqlite.git] / test / json104.test
blobc3c43d1e9861f8116ebc7d4647b33c6dc4e83b30
1 # 2017-03-22
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 {
20   SELECT json_patch('{
21        "a": "b",
22        "c": {
23          "d": "e",
24          "f": "g"
25        }
26      }','{
27        "a":"z",
28        "c": {
29          "f": null
30        }
31      }');
32 } {{{"a":"z","c":{"d":"e"}}}}
33 do_execsql_test json104-101 {
34   SELECT json_patch('{
35        "a": "b",
36        "c": {
37          "d": "e",
38          "f": "g"
39        }
40      }','{
41        a:"z",
42        c: {
43          f: null
44        }
45      }');
46 } {{{"a":"z","c":{"d":"e"}}}}
47 do_execsql_test json104-102 {
48   SELECT json_patch('{
49        a: "b",
50        c: {
51          d: "e",
52          f: "g"
53        }
54      }','{
55        "a":"z",
56        "c": {
57          "f": null
58        }
59      }');
60 } {{{"a":"z","c":{"d":"e"}}}}
61 do_execsql_test json104-103 {
62   SELECT json_patch('{
63        a: "b",
64        c: {
65          d: "e",
66          f: "g"
67        }
68      }','{
69        a:"z",
70        c: {
71          f: null
72        }
73      }');
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 {
79   SELECT json_patch('{
80        "title": "Goodbye!",
81        "author" : {
82          "givenName" : "John",
83          "familyName" : "Doe"
84        },
85        "tags":[ "example", "sample" ],
86        "content": "This will be unchanged"
87      }','{
88        "title": "Hello!",
89        "phoneNumber": "+01-123-456-7890",
90        "author": {
91          "familyName": null
92        },
93        "tags": [ "example" ]
94      }');
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}');
99 } {{{}}}
100 do_execsql_test json104-210 {
101   SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}');
102 } {{{"y":1}}}
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"}');
116 } {{{"a":"c"}}}
117 do_execsql_test json104-300a {
118   SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null');
119 } {{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}');
125 } {{{}}}
126 do_execsql_test json104-303 {
127   SELECT json_patch('{"a":"b","b":"c"}','{"a":null}');
128 } {{{"b":"c"}}}
129 do_execsql_test json104-304 {
130   SELECT json_patch('{"a":["b"]}','{"a":"c"}');
131 } {{{"a":"c"}}}
132 do_execsql_test json104-305 {
133   SELECT json_patch('{"a":"c"}','{"a":["b"]}');
134 } {{{"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]}');
140 } {{{"a":[1]}}}
141 do_execsql_test json104-308 {
142   SELECT json_patch('["a","b"]','["c","d"]');
143 } {{["c","d"]}}
144 do_execsql_test json104-309 {
145   SELECT json_patch('{"a":"b"}','["c"]');
146 } {{["c"]}}
147 do_execsql_test json104-310 {
148   SELECT json_patch('{"a":"foo"}','null');
149 } {{null}}
150 do_execsql_test json104-310a {
151   SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null');
152 } {{real-null}}
153 do_execsql_test json104-311 {
154   SELECT json_patch('{"a":"foo"}','"bar"');
155 } {{"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}');
161 } {{{"a":"b"}}}
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"}');
167 } {{{"x":"three"}}}
169 #-------------------------------------------------------------------------
171 do_execsql_test 401 {
172   CREATE TABLE obj(x);
173   INSERT INTO obj VALUES('{"a":1,"b":2}');
174   SELECT * FROM obj;
175 } {{{"a":1,"b":2}}}
176 do_execsql_test 402 {
177   UPDATE obj SET x = json_insert(x, '$.c', 3);
178   SELECT * FROM obj;
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;
183 } {2 2}
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;
188 } {555 555}
189 do_execsql_test 405 {
190   UPDATE obj SET x = json_set(x, '$."d"', 4);
191   SELECT json_extract(x, '$."d"') FROM obj;
192 } {4}
195 finish_test