sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2024-01-23 |
| 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 | # |
| 12 | # Legacy JSON bug: If the input is a BLOB that when cast into TEXT looks |
| 13 | # like valid JSON, then treat it as valid JSON. |
| 14 | # |
| 15 | # The original intent of the JSON functions was to raise an error on any |
| 16 | # BLOB input. That intent was clearly documented, but the code failed to |
| 17 | # to implement it. Subsequently, many applications began to depend on the |
| 18 | # incorrect behavior, especially apps that used readfile() to read JSON |
| 19 | # content, since readfile() returns a BLOB. So we need to support the |
| 20 | # bug moving forward. |
| 21 | # |
| 22 | # The tests in this fail verify that the original buggy behavior is |
| 23 | # preserved. |
| 24 | # |
| 25 | |
| 26 | set testdir [file dirname $argv0] |
| 27 | source $testdir/tester.tcl |
| 28 | set testprefix json107 |
| 29 | |
| 30 | if {[db one {PRAGMA encoding}]!="UTF-8"} { |
| 31 | # These tests only work for a UTF-8 encoding. |
| 32 | finish_test |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | do_execsql_test 1.1 { |
| 37 | SELECT json_valid( CAST('{"a":1}' AS BLOB) ); |
| 38 | } 1 |
| 39 | do_execsql_test 1.1.1 { |
| 40 | SELECT json_valid( CAST('{"a":1}' AS BLOB), 1); |
| 41 | } 1 |
| 42 | do_execsql_test 1.1.2 { |
| 43 | SELECT json_valid( CAST('{"a":1}' AS BLOB), 2); |
| 44 | } 1 |
| 45 | do_execsql_test 1.1.4 { |
| 46 | SELECT json_valid( CAST('{"a":1}' AS BLOB), 4); |
| 47 | } 0 |
| 48 | do_execsql_test 1.1.8 { |
| 49 | SELECT json_valid( CAST('{"a":1}' AS BLOB), 8); |
| 50 | } 0 |
| 51 | |
| 52 | do_execsql_test 1.2.1 { |
| 53 | SELECT CAST('{"a":123}' AS blob) -> 'a'; |
| 54 | } 123 |
| 55 | do_execsql_test 1.2.2 { |
| 56 | SELECT CAST('{"a":123}' AS blob) ->> 'a'; |
| 57 | } 123 |
| 58 | do_execsql_test 1.2.3 { |
| 59 | SELECT json_extract(CAST('{"a":123}' AS blob), '$.a'); |
| 60 | } 123 |
| 61 | do_execsql_test 1.3 { |
| 62 | SELECT json_insert(CAST('{"a":123}' AS blob),'$.b',456); |
| 63 | } {{{"a":123,"b":456}}} |
| 64 | do_execsql_test 1.4 { |
| 65 | SELECT json_remove(CAST('{"a":123,"b":456}' AS blob),'$.a'); |
| 66 | } {{{"b":456}}} |
| 67 | do_execsql_test 1.5 { |
| 68 | SELECT json_set(CAST('{"a":123,"b":456}' AS blob),'$.a',789); |
| 69 | } {{{"a":789,"b":456}}} |
| 70 | do_execsql_test 1.6 { |
| 71 | SELECT json_replace(CAST('{"a":123,"b":456}' AS blob),'$.a',789); |
| 72 | } {{{"a":789,"b":456}}} |
| 73 | do_execsql_test 1.7 { |
| 74 | SELECT json_type(CAST('{"a":123,"b":456}' AS blob)); |
| 75 | } object |
| 76 | do_execsql_test 1.8 { |
| 77 | SELECT json(CAST('{"a":123,"b":456}' AS blob)); |
| 78 | } {{{"a":123,"b":456}}} |
| 79 | |
| 80 | ifcapable vtab { |
| 81 | do_execsql_test 2.1 { |
| 82 | SELECT key, value FROM json_tree( CAST('{"a":123,"b":456}' AS blob) ) |
| 83 | WHERE atom; |
| 84 | } {a 123 b 456} |
| 85 | } |
| 86 | finish_test |