blob: b5313f01b55e8509486dd3f44b5e9c7fc12e52f5 [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
16
17ifcapable !json1 {
18 finish_test
19 return
20}
21
22# This is the example from pages 2 and 3 of RFC-7396
23do_execsql_test json104-100 {
drh37f03df2017-03-23 20:33:49 +000024 SELECT json_patch('{
drh633647a2017-03-22 21:24:31 +000025 "a": "b",
26 "c": {
27 "d": "e",
28 "f": "g"
29 }
drhf07b2492017-03-22 21:45:20 +000030 }','{
drh633647a2017-03-22 21:24:31 +000031 "a":"z",
32 "c": {
33 "f": null
34 }
drhf07b2492017-03-22 21:45:20 +000035 }');
drh633647a2017-03-22 21:24:31 +000036} {{{"a":"z","c":{"d":"e"}}}}
37
38
39# This is the example from pages 4 and 5 of RFC-7396
40do_execsql_test json104-110 {
drh37f03df2017-03-23 20:33:49 +000041 SELECT json_patch('{
drh633647a2017-03-22 21:24:31 +000042 "title": "Goodbye!",
43 "author" : {
44 "givenName" : "John",
45 "familyName" : "Doe"
46 },
47 "tags":[ "example", "sample" ],
48 "content": "This will be unchanged"
drhf07b2492017-03-22 21:45:20 +000049 }','{
drh633647a2017-03-22 21:24:31 +000050 "title": "Hello!",
51 "phoneNumber": "+01-123-456-7890",
52 "author": {
53 "familyName": null
54 },
55 "tags": [ "example" ]
drhf07b2492017-03-22 21:45:20 +000056 }');
drhbb7aa2d2017-03-23 00:13:52 +000057} {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}}
58
59do_execsql_test json104-200 {
drh37f03df2017-03-23 20:33:49 +000060 SELECT json_patch('[1,2,3]','{"x":null}');
drhbb7aa2d2017-03-23 00:13:52 +000061} {{{}}}
62do_execsql_test json104-210 {
drh37f03df2017-03-23 20:33:49 +000063 SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}');
drhbb7aa2d2017-03-23 00:13:52 +000064} {{{"y":1}}}
drh29c99692017-03-24 12:35:17 +000065do_execsql_test json104-220 {
66 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
67} {{{"a":{"bb":{}}}}}
68do_execsql_test json104-221 {
69 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,null,3]}}}');
70} {{{"a":{"bb":{"ccc":[1,null,3]}}}}}
71do_execsql_test json104-222 {
72 SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}');
73} {{{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}}}
74
drhf9e91972017-03-24 13:31:47 +000075# Example test cases at the end of the RFC-7396 document
76do_execsql_test json104-300 {
77 SELECT json_patch('{"a":"b"}','{"a":"c"}');
78} {{{"a":"c"}}}
79do_execsql_test json104-300a {
80 SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null');
81} {{real-null}}
82do_execsql_test json104-301 {
83 SELECT json_patch('{"a":"b"}','{"b":"c"}');
84} {{{"a":"b","b":"c"}}}
85do_execsql_test json104-302 {
86 SELECT json_patch('{"a":"b"}','{"a":null}');
87} {{{}}}
88do_execsql_test json104-303 {
89 SELECT json_patch('{"a":"b","b":"c"}','{"a":null}');
90} {{{"b":"c"}}}
91do_execsql_test json104-304 {
92 SELECT json_patch('{"a":["b"]}','{"a":"c"}');
93} {{{"a":"c"}}}
94do_execsql_test json104-305 {
95 SELECT json_patch('{"a":"c"}','{"a":["b"]}');
96} {{{"a":["b"]}}}
97do_execsql_test json104-306 {
98 SELECT json_patch('{"a":{"b":"c"}}','{"a":{"b":"d","c":null}}');
99} {{{"a":{"b":"d"}}}}
100do_execsql_test json104-307 {
101 SELECT json_patch('{"a":[{"b":"c"}]}','{"a":[1]}');
102} {{{"a":[1]}}}
103do_execsql_test json104-308 {
104 SELECT json_patch('["a","b"]','["c","d"]');
105} {{["c","d"]}}
106do_execsql_test json104-309 {
107 SELECT json_patch('{"a":"b"}','["c"]');
108} {{["c"]}}
109do_execsql_test json104-310 {
110 SELECT json_patch('{"a":"foo"}','null');
111} {{null}}
112do_execsql_test json104-310a {
113 SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null');
114} {{real-null}}
115do_execsql_test json104-311 {
116 SELECT json_patch('{"a":"foo"}','"bar"');
117} {{"bar"}}
118do_execsql_test json104-312 {
119 SELECT json_patch('{"e":null}','{"a":1}');
120} {{{"e":null,"a":1}}}
121do_execsql_test json104-313 {
122 SELECT json_patch('[1,2]','{"a":"b","c":null}');
123} {{{"a":"b"}}}
124do_execsql_test json104-314 {
125 SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}');
126} {{{"a":{"bb":{}}}}}
127
128
drh633647a2017-03-22 21:24:31 +0000129
130finish_test