drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 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 | #*********************************************************************** |
drh | 37f03df | 2017-03-23 20:33:49 +0000 | [diff] [blame] | 11 | # This file implements tests for json_patch(A,B) SQL function. |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 12 | # |
| 13 | |
| 14 | set testdir [file dirname $argv0] |
| 15 | source $testdir/tester.tcl |
dan | fe9a832 | 2019-06-17 14:50:33 +0000 | [diff] [blame] | 16 | set testprefix json104 |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 17 | |
| 18 | ifcapable !json1 { |
| 19 | finish_test |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | # This is the example from pages 2 and 3 of RFC-7396 |
| 24 | do_execsql_test json104-100 { |
drh | 37f03df | 2017-03-23 20:33:49 +0000 | [diff] [blame] | 25 | SELECT json_patch('{ |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 26 | "a": "b", |
| 27 | "c": { |
| 28 | "d": "e", |
| 29 | "f": "g" |
| 30 | } |
drh | f07b249 | 2017-03-22 21:45:20 +0000 | [diff] [blame] | 31 | }','{ |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 32 | "a":"z", |
| 33 | "c": { |
| 34 | "f": null |
| 35 | } |
drh | f07b249 | 2017-03-22 21:45:20 +0000 | [diff] [blame] | 36 | }'); |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 37 | } {{{"a":"z","c":{"d":"e"}}}} |
| 38 | |
| 39 | |
| 40 | # This is the example from pages 4 and 5 of RFC-7396 |
| 41 | do_execsql_test json104-110 { |
drh | 37f03df | 2017-03-23 20:33:49 +0000 | [diff] [blame] | 42 | SELECT json_patch('{ |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 43 | "title": "Goodbye!", |
| 44 | "author" : { |
| 45 | "givenName" : "John", |
| 46 | "familyName" : "Doe" |
| 47 | }, |
| 48 | "tags":[ "example", "sample" ], |
| 49 | "content": "This will be unchanged" |
drh | f07b249 | 2017-03-22 21:45:20 +0000 | [diff] [blame] | 50 | }','{ |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 51 | "title": "Hello!", |
| 52 | "phoneNumber": "+01-123-456-7890", |
| 53 | "author": { |
| 54 | "familyName": null |
| 55 | }, |
| 56 | "tags": [ "example" ] |
drh | f07b249 | 2017-03-22 21:45:20 +0000 | [diff] [blame] | 57 | }'); |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 58 | } {{{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}}} |
| 59 | |
| 60 | do_execsql_test json104-200 { |
drh | 37f03df | 2017-03-23 20:33:49 +0000 | [diff] [blame] | 61 | SELECT json_patch('[1,2,3]','{"x":null}'); |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 62 | } {{{}}} |
| 63 | do_execsql_test json104-210 { |
drh | 37f03df | 2017-03-23 20:33:49 +0000 | [diff] [blame] | 64 | SELECT json_patch('[1,2,3]','{"x":null,"y":1,"z":null}'); |
drh | bb7aa2d | 2017-03-23 00:13:52 +0000 | [diff] [blame] | 65 | } {{{"y":1}}} |
drh | 29c9969 | 2017-03-24 12:35:17 +0000 | [diff] [blame] | 66 | do_execsql_test json104-220 { |
| 67 | SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}'); |
| 68 | } {{{"a":{"bb":{}}}}} |
| 69 | do_execsql_test json104-221 { |
| 70 | SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,null,3]}}}'); |
| 71 | } {{{"a":{"bb":{"ccc":[1,null,3]}}}}} |
| 72 | do_execsql_test json104-222 { |
| 73 | SELECT json_patch('{}','{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}'); |
| 74 | } {{{"a":{"bb":{"ccc":[1,{"dddd":null},3]}}}}} |
| 75 | |
drh | f9e9197 | 2017-03-24 13:31:47 +0000 | [diff] [blame] | 76 | # Example test cases at the end of the RFC-7396 document |
| 77 | do_execsql_test json104-300 { |
| 78 | SELECT json_patch('{"a":"b"}','{"a":"c"}'); |
| 79 | } {{{"a":"c"}}} |
| 80 | do_execsql_test json104-300a { |
| 81 | SELECT coalesce(json_patch(null,'{"a":"c"}'), 'real-null'); |
| 82 | } {{real-null}} |
| 83 | do_execsql_test json104-301 { |
| 84 | SELECT json_patch('{"a":"b"}','{"b":"c"}'); |
| 85 | } {{{"a":"b","b":"c"}}} |
| 86 | do_execsql_test json104-302 { |
| 87 | SELECT json_patch('{"a":"b"}','{"a":null}'); |
| 88 | } {{{}}} |
| 89 | do_execsql_test json104-303 { |
| 90 | SELECT json_patch('{"a":"b","b":"c"}','{"a":null}'); |
| 91 | } {{{"b":"c"}}} |
| 92 | do_execsql_test json104-304 { |
| 93 | SELECT json_patch('{"a":["b"]}','{"a":"c"}'); |
| 94 | } {{{"a":"c"}}} |
| 95 | do_execsql_test json104-305 { |
| 96 | SELECT json_patch('{"a":"c"}','{"a":["b"]}'); |
| 97 | } {{{"a":["b"]}}} |
| 98 | do_execsql_test json104-306 { |
| 99 | SELECT json_patch('{"a":{"b":"c"}}','{"a":{"b":"d","c":null}}'); |
| 100 | } {{{"a":{"b":"d"}}}} |
| 101 | do_execsql_test json104-307 { |
| 102 | SELECT json_patch('{"a":[{"b":"c"}]}','{"a":[1]}'); |
| 103 | } {{{"a":[1]}}} |
| 104 | do_execsql_test json104-308 { |
| 105 | SELECT json_patch('["a","b"]','["c","d"]'); |
| 106 | } {{["c","d"]}} |
| 107 | do_execsql_test json104-309 { |
| 108 | SELECT json_patch('{"a":"b"}','["c"]'); |
| 109 | } {{["c"]}} |
| 110 | do_execsql_test json104-310 { |
| 111 | SELECT json_patch('{"a":"foo"}','null'); |
| 112 | } {{null}} |
| 113 | do_execsql_test json104-310a { |
| 114 | SELECT coalesce(json_patch('{"a":"foo"}',null), 'real-null'); |
| 115 | } {{real-null}} |
| 116 | do_execsql_test json104-311 { |
| 117 | SELECT json_patch('{"a":"foo"}','"bar"'); |
| 118 | } {{"bar"}} |
| 119 | do_execsql_test json104-312 { |
| 120 | SELECT json_patch('{"e":null}','{"a":1}'); |
| 121 | } {{{"e":null,"a":1}}} |
| 122 | do_execsql_test json104-313 { |
| 123 | SELECT json_patch('[1,2]','{"a":"b","c":null}'); |
| 124 | } {{{"a":"b"}}} |
| 125 | do_execsql_test json104-314 { |
| 126 | SELECT json_patch('{}','{"a":{"bb":{"ccc":null}}}'); |
| 127 | } {{{"a":{"bb":{}}}}} |
| 128 | |
dan | fe9a832 | 2019-06-17 14:50:33 +0000 | [diff] [blame] | 129 | #------------------------------------------------------------------------- |
| 130 | |
| 131 | do_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}}} |
| 136 | do_execsql_test 402 { |
| 137 | UPDATE obj SET x = json_insert(x, '$.c', 3); |
| 138 | SELECT * FROM obj; |
| 139 | } {{{"a":1,"b":2,"c":3}}} |
| 140 | do_execsql_test 403 { |
| 141 | SELECT json_extract(x, '$.b') FROM obj; |
| 142 | SELECT json_extract(x, '$."b"') FROM obj; |
| 143 | } {2 2} |
| 144 | do_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} |
| 149 | do_execsql_test 405 { |
| 150 | UPDATE obj SET x = json_set(x, '$."d"', 4); |
| 151 | SELECT json_extract(x, '$."d"') FROM obj; |
| 152 | } {4} |
drh | f9e9197 | 2017-03-24 13:31:47 +0000 | [diff] [blame] | 153 | |
drh | 633647a | 2017-03-22 21:24:31 +0000 | [diff] [blame] | 154 | |
| 155 | finish_test |