xref: /sqlite-3.40.0/test/json104.test (revision f71a243a)
1# 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#***********************************************************************
11# This file implements tests for json_patch(A,B) SQL function.
12#
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16set testprefix json104
17
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 {
25  SELECT json_patch('{
26       "a": "b",
27       "c": {
28         "d": "e",
29         "f": "g"
30       }
31     }','{
32       "a":"z",
33       "c": {
34         "f": null
35       }
36     }');
37} {{{"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 {
42  SELECT json_patch('{
43       "title": "Goodbye!",
44       "author" : {
45         "givenName" : "John",
46         "familyName" : "Doe"
47       },
48       "tags":[ "example", "sample" ],
49       "content": "This will be unchanged"
50     }','{
51       "title": "Hello!",
52       "phoneNumber": "+01-123-456-7890",
53       "author": {
54         "familyName": null
55       },
56       "tags": [ "example" ]
57     }');
58} {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}}
59
60do_execsql_test json104-200 {
61  SELECT json_patch('[1,2,3]','{"x":null}');
62} {{{}}}
63do_execsql_test json104-210 {
64  SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}');
65} {{{"y":1}}}
66do_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
76# 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
129#-------------------------------------------------------------------------
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}
153
154
155finish_test
156
157
158