blob: e56e7edeffcfbb41467eaa6839b73c3d73fb04fc [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
18ifcapable !json1 {
19 finish_test
20 return
21}
22
23# This is the example from pages 2 and 3 of RFC-7396
24do_execsql_test json104-100 {
drh37f03df2017-03-23 20:33:49 +000025 SELECT json_patch('{
drh633647a2017-03-22 21:24:31 +000026 "a": "b",
27 "c": {
28 "d": "e",
29 "f": "g"
30 }
drhf07b2492017-03-22 21:45:20 +000031 }','{
drh633647a2017-03-22 21:24:31 +000032 "a":"z",
33 "c": {
34 "f": null
35 }
drhf07b2492017-03-22 21:45:20 +000036 }');
drh633647a2017-03-22 21:24:31 +000037} {{{"a":"z","c":{"d":"e"}}}}
38
39
40# This is the example from pages 4 and 5 of RFC-7396
41do_execsql_test json104-110 {
drh37f03df2017-03-23 20:33:49 +000042 SELECT json_patch('{
drh633647a2017-03-22 21:24:31 +000043 "title": "Goodbye!",
44 "author" : {
45 "givenName" : "John",
46 "familyName" : "Doe"
47 },
48 "tags":[ "example", "sample" ],
49 "content": "This will be unchanged"
drhf07b2492017-03-22 21:45:20 +000050 }','{
drh633647a2017-03-22 21:24:31 +000051 "title": "Hello!",
52 "phoneNumber": "+01-123-456-7890",
53 "author": {
54 "familyName": null
55 },
56 "tags": [ "example" ]
drhf07b2492017-03-22 21:45:20 +000057 }');
drhbb7aa2d2017-03-23 00:13:52 +000058} {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}}
59
60do_execsql_test json104-200 {
drh37f03df2017-03-23 20:33:49 +000061 SELECT json_patch('[1,2,3]','{"x":null}');
drhbb7aa2d2017-03-23 00:13:52 +000062} {{{}}}
63do_execsql_test json104-210 {
drh37f03df2017-03-23 20:33:49 +000064 SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}');
drhbb7aa2d2017-03-23 00:13:52 +000065} {{{"y":1}}}
drh29c99692017-03-24 12:35:17 +000066do_execsql_test json104-220 {
67 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
68} {{{"a":{"bb":{}}}}}
69do_execsql_test json104-221 {
70 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,null,3]}}}');
71} {{{"a":{"bb":{"ccc":[1,null,3]}}}}}
72do_execsql_test json104-222 {
73 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}');
74} {{{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}}}
75
drhf9e91972017-03-24 13:31:47 +000076# Example test cases at the end of the RFC-7396 document
77do_execsql_test json104-300 {
78 SELECT json_patch('{"a":"b"}','{"a":"c"}');
79} {{{"a":"c"}}}
80do_execsql_test json104-300a {
81 SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null');
82} {{real-null}}
83do_execsql_test json104-301 {
84 SELECT json_patch('{"a":"b"}','{"b":"c"}');
85} {{{"a":"b","b":"c"}}}
86do_execsql_test json104-302 {
87 SELECT json_patch('{"a":"b"}','{"a":null}');
88} {{{}}}
89do_execsql_test json104-303 {
90 SELECT json_patch('{"a":"b","b":"c"}','{"a":null}');
91} {{{"b":"c"}}}
92do_execsql_test json104-304 {
93 SELECT json_patch('{"a":["b"]}','{"a":"c"}');
94} {{{"a":"c"}}}
95do_execsql_test json104-305 {
96 SELECT json_patch('{"a":"c"}','{"a":["b"]}');
97} {{{"a":["b"]}}}
98do_execsql_test json104-306 {
99 SELECT json_patch('{"a":{"b":"c"}}','{"a":{"b":"d","c":null}}');
100} {{{"a":{"b":"d"}}}}
101do_execsql_test json104-307 {
102 SELECT json_patch('{"a":[{"b":"c"}]}','{"a":[1]}');
103} {{{"a":[1]}}}
104do_execsql_test json104-308 {
105 SELECT json_patch('["a","b"]','["c","d"]');
106} {{["c","d"]}}
107do_execsql_test json104-309 {
108 SELECT json_patch('{"a":"b"}','["c"]');
109} {{["c"]}}
110do_execsql_test json104-310 {
111 SELECT json_patch('{"a":"foo"}','null');
112} {{null}}
113do_execsql_test json104-310a {
114 SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null');
115} {{real-null}}
116do_execsql_test json104-311 {
117 SELECT json_patch('{"a":"foo"}','"bar"');
118} {{"bar"}}
119do_execsql_test json104-312 {
120 SELECT json_patch('{"e":null}','{"a":1}');
121} {{{"e":null,"a":1}}}
122do_execsql_test json104-313 {
123 SELECT json_patch('[1,2]','{"a":"b","c":null}');
124} {{{"a":"b"}}}
125do_execsql_test json104-314 {
126 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
127} {{{"a":{"bb":{}}}}}
128
danfe9a8322019-06-17 14:50:33 +0000129#-------------------------------------------------------------------------
130
131do_execsql_test 401 {
132 CREATE TABLE obj(x);
133 INSERT INTO obj VALUES('{"a":1,"b":2}');
134 SELECT * FROM obj;
135} {{{"a":1,"b":2}}}
136do_execsql_test 402 {
137 UPDATE obj SET x = json_insert(x, '$.c', 3);
138 SELECT * FROM obj;
139} {{{"a":1,"b":2,"c":3}}}
140do_execsql_test 403 {
141 SELECT json_extract(x, '$.b') FROM obj;
142 SELECT json_extract(x, '$."b"') FROM obj;
143} {2 2}
144do_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;
148} {555 555}
149do_execsql_test 405 {
150 UPDATE obj SET x = json_set(x, '$."d"', 4);
151 SELECT json_extract(x, '$."d"') FROM obj;
152} {4}
drhf9e91972017-03-24 13:31:47 +0000153
drh633647a2017-03-22 21:24:31 +0000154
155finish_test