Add a test for the fixes on this branch.
[sqlite.git] / test / json106.test
blob06859a10b44643876a2b564d9bb7dc9b82f49adf
1 # 2023-12-18
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # Invariant tests for JSON built around the randomjson extension
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix json106
18 # These tests require virtual table "json_tree" to run.
19 ifcapable !vtab { finish_test ; return }
21 load_static_extension db randomjson
22 db eval {
23   CREATE TEMP TABLE t1(j0,j5,p);
24   CREATE TEMP TABLE kv(n,key,val);
26 unset -nocomplain ii
27 for {set ii 1} {$ii<=5000} {incr ii} {
28   do_execsql_test $ii.1 {
29     DELETE FROM t1;
30     INSERT INTO t1(j0,j5) VALUES(random_json($ii),random_json5($ii));
31     SELECT json_valid(j0), json_valid(j5,2) FROM t1;
32   } {1 1}
33   do_execsql_test $ii.2 {
34     SELECT count(*)
35       FROM t1, json_tree(j0) AS rt
36      WHERE rt.type NOT IN ('object','array')
37        AND rt.atom IS NOT (j0 ->> rt.fullkey);
38   } 0
39   do_execsql_test $ii.3 {
40     SELECT count(*)
41       FROM t1, json_tree(j5) AS rt
42      WHERE rt.type NOT IN ('object','array')
43        AND rt.atom IS NOT (j0 ->> rt.fullkey);
44   } 0
45   do_execsql_test $ii.4 {
46     DELETE FROM kv;
47     INSERT INTO kv
48       SELECT rt.rowid, rt.fullkey, rt.atom
49         FROM t1, json_tree(j0) AS rt
50        WHERE rt.type NOT IN ('object','array');
51   }
52   do_execsql_test $ii.5 {
53     SELECT count(*)
54       FROM t1, kv
55      WHERE key NOT LIKE '%]'
56        AND json_remove(j5,key)->>key IS NOT NULL
57   } 0
58   do_execsql_test $ii.6 {
59     SELECT count(*)
60       FROM t1, kv
61      WHERE key NOT LIKE '%]'
62        AND json_insert(json_remove(j5,key),key,val)->>key IS NOT val
63   } 0
64   do_execsql_test $ii.7 {
65     UPDATE t1 SET p=json_patch(j0,j5);
66     SELECT count(*)
67       FROM t1, kv
68      WHERE p->>key IS NOT val
69   } 0
70   do_execsql_test $ii.8 {
71     SELECT j0 FROM t1 WHERE json(j0)!=json(json_pretty(j0));
72   } {}
73   do_execsql_test $ii.9 {
74     SELECT j5 FROM t1 WHERE json(j5)!=json(json_pretty(j5));
75   } {}
79 finish_test