1*5281864fSdrh# 2019-11-22 2*5281864fSdrh# 3*5281864fSdrh# The author disclaims copyright to this source code. In place of 4*5281864fSdrh# a legal notice, here is a blessing: 5*5281864fSdrh# 6*5281864fSdrh# May you do good and not evil. 7*5281864fSdrh# May you find forgiveness for yourself and forgive others. 8*5281864fSdrh# May you share freely, never taking more than you give. 9*5281864fSdrh# 10*5281864fSdrh#*********************************************************************** 11*5281864fSdrh# This file implements tests for "[#]" extension to json-path 12*5281864fSdrh# 13*5281864fSdrh 14*5281864fSdrhset testdir [file dirname $argv0] 15*5281864fSdrhsource $testdir/tester.tcl 16*5281864fSdrhset testprefix json104 17*5281864fSdrh 18*5281864fSdrh# This is the example from pages 2 and 3 of RFC-7396 19*5281864fSdrhdb eval { 20*5281864fSdrh CREATE TABLE t1(j); 21*5281864fSdrh INSERT INTO t1(j) VALUES('{"a":1,"b":[1,[2,3],4],"c":99}'); 22*5281864fSdrh} 23*5281864fSdrhproc json_extract_test {testnum path result} { 24*5281864fSdrh do_execsql_test json105-1.$testnum "SELECT quote(json_extract(j,$path)) FROM t1" $result 25*5281864fSdrh} 26*5281864fSdrhjson_extract_test 10 {'$.b[#]'} NULL 27*5281864fSdrhjson_extract_test 20 {'$.b[#-1]'} 4 28*5281864fSdrhjson_extract_test 30 {'$.b[#-2]'} {'[2,3]'} 29*5281864fSdrhjson_extract_test 31 {'$.b[#-02]'} {'[2,3]'} 30*5281864fSdrhjson_extract_test 40 {'$.b[#-3]'} 1 31*5281864fSdrhjson_extract_test 50 {'$.b[#-4]'} NULL 32*5281864fSdrhjson_extract_test 60 {'$.b[#-2][#-1]'} 3 33*5281864fSdrhjson_extract_test 70 {'$.b[0]','$.b[#-1]'} {'[1,4]'} 34*5281864fSdrh 35*5281864fSdrhjson_extract_test 100 {'$.a[#-1]'} NULL 36*5281864fSdrhjson_extract_test 110 {'$.b[#-000001]'} 4 37*5281864fSdrh 38*5281864fSdrhproc json_remove_test {testnum path result} { 39*5281864fSdrh do_execsql_test json105-2.$testnum "SELECT quote(json_remove(j,$path)) FROM t1" $result 40*5281864fSdrh} 41*5281864fSdrhjson_remove_test 10 {'$.b[#]'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} 42*5281864fSdrhjson_remove_test 20 {'$.b[#-0]'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} 43*5281864fSdrhjson_remove_test 30 {'$.b[#-1]'} {'{"a":1,"b":[1,[2,3]],"c":99}'} 44*5281864fSdrhjson_remove_test 40 {'$.b[#-2]'} {'{"a":1,"b":[1,4],"c":99}'} 45*5281864fSdrhjson_remove_test 50 {'$.b[#-3]'} {'{"a":1,"b":[[2,3],4],"c":99}'} 46*5281864fSdrhjson_remove_test 60 {'$.b[#-4]'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} 47*5281864fSdrhjson_remove_test 70 {'$.b[#-2][#-1]'} {'{"a":1,"b":[1,[2],4],"c":99}'} 48*5281864fSdrh 49*5281864fSdrhjson_remove_test 100 {'$.b[0]','$.b[#-1]'} {'{"a":1,"b":[[2,3]],"c":99}'} 50*5281864fSdrhjson_remove_test 110 {'$.b[#-1]','$.b[0]'} {'{"a":1,"b":[[2,3]],"c":99}'} 51*5281864fSdrhjson_remove_test 120 {'$.b[#-1]','$.b[#-2]'} {'{"a":1,"b":[[2,3]],"c":99}'} 52*5281864fSdrhjson_remove_test 130 {'$.b[#-1]','$.b[#-1]'} {'{"a":1,"b":[1],"c":99}'} 53*5281864fSdrhjson_remove_test 140 {'$.b[#-2]','$.b[#-1]'} {'{"a":1,"b":[1],"c":99}'} 54*5281864fSdrh 55*5281864fSdrhproc json_insert_test {testnum x result} { 56*5281864fSdrh do_execsql_test json105-3.$testnum "SELECT quote(json_insert(j,$x)) FROM t1" $result 57*5281864fSdrh} 58*5281864fSdrhjson_insert_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4,"AAA"],"c":99}'} 59*5281864fSdrhjson_insert_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3,"AAA"],4],"c":99}'} 60*5281864fSdrhjson_insert_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \ 61*5281864fSdrh {'{"a":1,"b":[1,[2,3,"AAA"],4,"BBB"],"c":99}'} 62*5281864fSdrhjson_insert_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \ 63*5281864fSdrh {'{"a":1,"b":[1,[2,3],4,"AAA","BBB"],"c":99}'} 64*5281864fSdrh 65*5281864fSdrhproc json_set_test {testnum x result} { 66*5281864fSdrh do_execsql_test json105-4.$testnum "SELECT quote(json_set(j,$x)) FROM t1" $result 67*5281864fSdrh} 68*5281864fSdrhjson_set_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4,"AAA"],"c":99}'} 69*5281864fSdrhjson_set_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3,"AAA"],4],"c":99}'} 70*5281864fSdrhjson_set_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \ 71*5281864fSdrh {'{"a":1,"b":[1,[2,3,"AAA"],4,"BBB"],"c":99}'} 72*5281864fSdrhjson_set_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \ 73*5281864fSdrh {'{"a":1,"b":[1,[2,3],4,"AAA","BBB"],"c":99}'} 74*5281864fSdrhjson_set_test 50 {'$.b[#-1]','AAA'} {'{"a":1,"b":[1,[2,3],"AAA"],"c":99}'} 75*5281864fSdrhjson_set_test 60 {'$.b[1][#-1]','AAA'} {'{"a":1,"b":[1,[2,"AAA"],4],"c":99}'} 76*5281864fSdrhjson_set_test 70 {'$.b[1][#-1]','AAA','$.b[#-1]','BBB'} \ 77*5281864fSdrh {'{"a":1,"b":[1,[2,"AAA"],"BBB"],"c":99}'} 78*5281864fSdrhjson_set_test 80 {'$.b[#-1]','AAA','$.b[#-1]','BBB'} \ 79*5281864fSdrh {'{"a":1,"b":[1,[2,3],"BBB"],"c":99}'} 80*5281864fSdrh 81*5281864fSdrhproc json_replace_test {testnum x result} { 82*5281864fSdrh do_execsql_test json105-5.$testnum "SELECT quote(json_replace(j,$x)) FROM t1" $result 83*5281864fSdrh} 84*5281864fSdrhjson_replace_test 10 {'$.b[#]','AAA'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} 85*5281864fSdrhjson_replace_test 20 {'$.b[1][#]','AAA'} {'{"a":1,"b":[1,[2,3],4],"c":99}'} 86*5281864fSdrhjson_replace_test 30 {'$.b[1][#]','AAA','$.b[#]','BBB'} \ 87*5281864fSdrh {'{"a":1,"b":[1,[2,3],4],"c":99}'} 88*5281864fSdrhjson_replace_test 40 {'$.b[#]','AAA','$.b[#]','BBB'} \ 89*5281864fSdrh {'{"a":1,"b":[1,[2,3],4],"c":99}'} 90*5281864fSdrhjson_replace_test 50 {'$.b[#-1]','AAA'} {'{"a":1,"b":[1,[2,3],"AAA"],"c":99}'} 91*5281864fSdrhjson_replace_test 60 {'$.b[1][#-1]','AAA'} {'{"a":1,"b":[1,[2,"AAA"],4],"c":99}'} 92*5281864fSdrhjson_replace_test 70 {'$.b[1][#-1]','AAA','$.b[#-1]','BBB'} \ 93*5281864fSdrh {'{"a":1,"b":[1,[2,"AAA"],"BBB"],"c":99}'} 94*5281864fSdrhjson_replace_test 80 {'$.b[#-1]','AAA','$.b[#-1]','BBB'} \ 95*5281864fSdrh {'{"a":1,"b":[1,[2,3],"BBB"],"c":99}'} 96*5281864fSdrh 97*5281864fSdrhdo_catchsql_test json105-6.10 { 98*5281864fSdrh SELECT json_extract(j, '$.b[#-]') FROM t1; 99*5281864fSdrh} {1 {JSON path error near '[#-]'}} 100*5281864fSdrhdo_catchsql_test json105-6.20 { 101*5281864fSdrh SELECT json_extract(j, '$.b[#9]') FROM t1; 102*5281864fSdrh} {1 {JSON path error near '[#9]'}} 103*5281864fSdrhdo_catchsql_test json105-6.30 { 104*5281864fSdrh SELECT json_extract(j, '$.b[#+2]') FROM t1; 105*5281864fSdrh} {1 {JSON path error near '[#+2]'}} 106*5281864fSdrhdo_catchsql_test json105-6.40 { 107*5281864fSdrh SELECT json_extract(j, '$.b[#-1') FROM t1; 108*5281864fSdrh} {1 {JSON path error near '[#-1'}} 109*5281864fSdrhdo_catchsql_test json105-6.50 { 110*5281864fSdrh SELECT json_extract(j, '$.b[#-1x]') FROM t1; 111*5281864fSdrh} {1 {JSON path error near '[#-1x]'}} 112*5281864fSdrh 113*5281864fSdrhfinish_test 114