interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / json105.test

118 lines · 5211 bytes Raw Blame History
1# 2019-11-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 "[#]" extension to json-path
12#
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16set testprefix json105
17
18# This is the example from pages 2 and 3 of RFC-7396
19db eval {
20 CREATE TABLE t1(j);
21 INSERT INTO t1(j) VALUES('{"a":1,"b":[1,[2,3],4],"c":99}');
22}
23proc json_extract_test {testnum path result} {
24 do_execsql_test json105-1.$testnum "SELECT quote(json_extract(j,$path)) FROM t1" $result
25}
26json_extract_test 10 {'$.b[#]'} NULL
27json_extract_test 20 {'$.b[#-1]'} 4
28json_extract_test 30 {'$.b[#-2]'} {'[2,3]'}
29json_extract_test 31 {'$.b[#-02]'} {'[2,3]'}
30json_extract_test 40 {'$.b[#-3]'} 1
31json_extract_test 50 {'$.b[#-4]'} NULL
32json_extract_test 51 {'$.b[#-4296967295]'} NULL
33json_extract_test 52 {'$.b[#-4296967296]'} NULL
34json_extract_test 53 {'$.b[#-4296967297]'} NULL
35json_extract_test 54 {'$.b[#-42969672950]'} NULL
36json_extract_test 55 {'$.b[#-42969672960]'} NULL
37json_extract_test 60 {'$.b[#-2][#-1]'} 3
38json_extract_test 70 {'$.b[0]','$.b[#-1]'} {'[1,4]'}
39
40json_extract_test 100 {'$.a[#-1]'} NULL
41json_extract_test 110 {'$.b[#-000001]'} 4
42
43proc json_remove_test {testnum path result} {
44 do_execsql_test json105-2.$testnum "SELECT quote(json_remove(j,$path)) FROM t1" $result
45}
46json_remove_test 10 {'$.b[#]'} {'{"a":1,"b":[1,[2,3],4],"c":99}'}
47json_remove_test 20 {'$.b[#-0]'} {'{"a":1,"b":[1,[2,3],4],"c":99}'}
48json_remove_test 30 {'$.b[#-1]'} {'{"a":1,"b":[1,[2,3]],"c":99}'}
49json_remove_test 40 {'$.b[#-2]'} {'{"a":1,"b":[1,4],"c":99}'}
50json_remove_test 50 {'$.b[#-3]'} {'{"a":1,"b":[[2,3],4],"c":99}'}
51json_remove_test 60 {'$.b[#-4]'} {'{"a":1,"b":[1,[2,3],4],"c":99}'}
52json_remove_test 70 {'$.b[#-2][#-1]'} {'{"a":1,"b":[1,[2],4],"c":99}'}
53
54json_remove_test 100 {'$.b[0]','$.b[#-1]'} {'{"a":1,"b":[[2,3]],"c":99}'}
55json_remove_test 110 {'$.b[#-1]','$.b[0]'} {'{"a":1,"b":[[2,3]],"c":99}'}
56json_remove_test 120 {'$.b[#-1]','$.b[#-2]'} {'{"a":1,"b":[[2,3]],"c":99}'}
57json_remove_test 130 {'$.b[#-1]','$.b[#-1]'} {'{"a":1,"b":[1],"c":99}'}
58json_remove_test 140 {'$.b[#-2]','$.b[#-1]'} {'{"a":1,"b":[1],"c":99}'}
59
60proc json_insert_test {testnum x result} {
61 do_execsql_test json105-3.$testnum "SELECT quote(json_insert(j,$x)) FROM t1" $result
62}
63json_insert_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4,"AAA"],"c":99}'}
64json_insert_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3,"AAA"],4],"c":99}'}
65json_insert_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \
66 {'{"a":1,"b":[1,[2,3,"AAA"],4,"BBB"],"c":99}'}
67json_insert_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \
68 {'{"a":1,"b":[1,[2,3],4,"AAA","BBB"],"c":99}'}
69
70proc json_set_test {testnum x result} {
71 do_execsql_test json105-4.$testnum "SELECT quote(json_set(j,$x)) FROM t1" $result
72}
73json_set_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4,"AAA"],"c":99}'}
74json_set_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3,"AAA"],4],"c":99}'}
75json_set_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \
76 {'{"a":1,"b":[1,[2,3,"AAA"],4,"BBB"],"c":99}'}
77json_set_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \
78 {'{"a":1,"b":[1,[2,3],4,"AAA","BBB"],"c":99}'}
79json_set_test 50 {'$.b[#-1]','AAA'} {'{"a":1,"b":[1,[2,3],"AAA"],"c":99}'}
80json_set_test 60 {'$.b[1][#-1]','AAA'} {'{"a":1,"b":[1,[2,"AAA"],4],"c":99}'}
81json_set_test 70 {'$.b[1][#-1]','AAA','$.b[#-1]','BBB'} \
82 {'{"a":1,"b":[1,[2,"AAA"],"BBB"],"c":99}'}
83json_set_test 80 {'$.b[#-1]','AAA','$.b[#-1]','BBB'} \
84 {'{"a":1,"b":[1,[2,3],"BBB"],"c":99}'}
85
86proc json_replace_test {testnum x result} {
87 do_execsql_test json105-5.$testnum "SELECT quote(json_replace(j,$x)) FROM t1" $result
88}
89json_replace_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4],"c":99}'}
90json_replace_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3],4],"c":99}'}
91json_replace_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \
92 {'{"a":1,"b":[1,[2,3],4],"c":99}'}
93json_replace_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \
94 {'{"a":1,"b":[1,[2,3],4],"c":99}'}
95json_replace_test 50 {'$.b[#-1]','AAA'} {'{"a":1,"b":[1,[2,3],"AAA"],"c":99}'}
96json_replace_test 60 {'$.b[1][#-1]','AAA'} {'{"a":1,"b":[1,[2,"AAA"],4],"c":99}'}
97json_replace_test 70 {'$.b[1][#-1]','AAA','$.b[#-1]','BBB'} \
98 {'{"a":1,"b":[1,[2,"AAA"],"BBB"],"c":99}'}
99json_replace_test 80 {'$.b[#-1]','AAA','$.b[#-1]','BBB'} \
100 {'{"a":1,"b":[1,[2,3],"BBB"],"c":99}'}
101
102do_catchsql_test json105-6.10 {
103 SELECT json_extract(j, '$.b[#-]') FROM t1;
104} {1 {bad JSON path: '$.b[#-]'}}
105do_catchsql_test json105-6.20 {
106 SELECT json_extract(j, '$.b[#9]') FROM t1;
107} {1 {bad JSON path: '$.b[#9]'}}
108do_catchsql_test json105-6.30 {
109 SELECT json_extract(j, '$.b[#+2]') FROM t1;
110} {1 {bad JSON path: '$.b[#+2]'}}
111do_catchsql_test json105-6.40 {
112 SELECT json_extract(j, '$.b[#-1') FROM t1;
113} {1 {bad JSON path: '$.b[#-1'}}
114do_catchsql_test json105-6.50 {
115 SELECT json_extract(j, '$.b[#-1x]') FROM t1;
116} {1 {bad JSON path: '$.b[#-1x]'}}
117
118finish_test