1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
require_relative "spec_helper"
Sequel.extension :sqlite_json_ops
describe "Sequel::SQLite::JSONOp" do
before do
@db = Sequel.connect('mock://sqlite')
@db.extend_datasets{def quote_identifiers?; false end}
@j = Sequel.sqlite_json_op(:j)
@l = proc{|o| @db.literal(o)}
end
it "#[]/#get should use the ->> operator" do
@l[@j[1]].must_equal "(j ->> 1)"
@l[@j.get('a')].must_equal "(j ->> 'a')"
end
it "#[]/#get should return a JSONOp" do
@l[@j[1][2]].must_equal "((j ->> 1) ->> 2)"
@l[@j.get('a').get('b')].must_equal "((j ->> 'a') ->> 'b')"
end
it "#array_length should use the json_array_length function" do
@l[@j.array_length].must_equal "json_array_length(j)"
@l[@j.array_length("$[1]")].must_equal "json_array_length(j, '$[1]')"
end
it "#array_length should return a numeric expression" do
@l[@j.array_length + 1].must_equal "(json_array_length(j) + 1)"
end
it "#each should use the json_each function" do
@l[@j.each].must_equal "json_each(j)"
@l[@j.each("$[1]")].must_equal "json_each(j, '$[1]')"
end
it "#extract should use the json_extract function" do
@l[@j.extract].must_equal "json_extract(j)"
@l[@j.extract("$[1]")].must_equal "json_extract(j, '$[1]')"
end
it "#get_json should use the -> operator" do
@l[@j.get_json(1)].must_equal "(j -> 1)"
end
it "#get_json should return a JSONOp" do
@l[@j.get_json('a').get_json('b')].must_equal "((j -> 'a') -> 'b')"
end
it "#insert should use the json_insert function" do
@l[@j.insert('$.a', 1)].must_equal "json_insert(j, '$.a', 1)"
@l[@j.insert('$.a', 1, '$.b', 2)].must_equal "json_insert(j, '$.a', 1, '$.b', 2)"
end
it "#insert should return JSONOp" do
@l[@j.insert('$.a', 1).insert('$.b', 2)].must_equal "json_insert(json_insert(j, '$.a', 1), '$.b', 2)"
end
it "#json/#minify should use the json function" do
@l[@j.json].must_equal "json(j)"
@l[@j.minify].must_equal "json(j)"
end
it "#json/#minify should return JSONOp" do
@l[@j.json.extract('$.a')].must_equal "json_extract(json(j), '$.a')"
@l[@j.minify.extract('$.a')].must_equal "json_extract(json(j), '$.a')"
end
it "#jsonb should use the jsonb function" do
@l[@j.jsonb].must_equal "jsonb(j)"
end
it "#jsonb should return JSONBOp" do
@l[@j.jsonb.extract('$.a')].must_equal "jsonb_extract(jsonb(j), '$.a')"
end
it "#patch should use the json_patch function" do
@l[@j.patch('{"a": 1}')].must_equal "json_patch(j, '{\"a\": 1}')"
end
it "#patch should return JSONOp" do
@l[@j.patch('{"a": 1}').patch('{"b": 2}')].must_equal "json_patch(json_patch(j, '{\"a\": 1}'), '{\"b\": 2}')"
end
it "#remove should use the json_remove function" do
@l[@j.remove('$.a')].must_equal "json_remove(j, '$.a')"
@l[@j.remove('$.a', '$[1]')].must_equal "json_remove(j, '$.a', '$[1]')"
end
it "#remove should return JSONOp" do
@l[@j.remove('$.a').remove('$[1]')].must_equal "json_remove(json_remove(j, '$.a'), '$[1]')"
end
it "#replace should use the json_replace function" do
@l[@j.replace('$.a', 1)].must_equal "json_replace(j, '$.a', 1)"
@l[@j.replace('$.a', 1, '$.b', 2)].must_equal "json_replace(j, '$.a', 1, '$.b', 2)"
end
it "#replace should return JSONOp" do
@l[@j.replace('$.a', 1).replace('$.b', 2)].must_equal "json_replace(json_replace(j, '$.a', 1), '$.b', 2)"
end
it "#set should use the json_set function" do
@l[@j.set('$.a', 1)].must_equal "json_set(j, '$.a', 1)"
@l[@j.set('$.a', 1, '$.b', 2)].must_equal "json_set(j, '$.a', 1, '$.b', 2)"
end
it "#set should return JSONOp" do
@l[@j.set('$.a', 1).set('$.b', 2)].must_equal "json_set(json_set(j, '$.a', 1), '$.b', 2)"
end
it "#tree should use the json_tree function" do
@l[@j.tree].must_equal "json_tree(j)"
@l[@j.tree("$[1]")].must_equal "json_tree(j, '$[1]')"
end
it "#type/#typeof should use the json_type function" do
@l[@j.type].must_equal "json_type(j)"
@l[@j.typeof].must_equal "json_type(j)"
@l[@j.type("$[1]")].must_equal "json_type(j, '$[1]')"
@l[@j.typeof("$[1]")].must_equal "json_type(j, '$[1]')"
end
it "#type/#typeof should return a string expression" do
@l[@j.type + '1'].must_equal "(json_type(j) || '1')"
@l[@j.typeof('$.a') + '1'].must_equal "(json_type(j, '$.a') || '1')"
end
it "#valid should use the json_valid function" do
@l[@j.valid].must_equal "json_valid(j)"
end
it "#valid should return a boolean expression" do
@l[@j.valid & 1].must_equal "(json_valid(j) AND 1)"
end
it "Sequel.sqlite_json_op should wrap object in a JSONOp" do
@l[Sequel.sqlite_json_op(:j).valid].must_equal "json_valid(j)"
@l[Sequel.sqlite_json_op(Sequel.join([:j, :k])).valid].must_equal "json_valid((j || k))"
end
it "Sequel.sqlite_json_op return a JSONOp as-is" do
v = Sequel.sqlite_json_op(:j)
Sequel.sqlite_json_op(v).must_be_same_as v
end
it "SQL::GenericExpression#sqlite_json_op should wrap receiver in JSON op" do
@l[Sequel.function(:j, :k).sqlite_json_op.valid].must_equal "json_valid(j(k))"
end
it "SQL::LiteralString#sqlite_json_op should wrap receiver in JSON op" do
@l[Sequel.lit('j || k').sqlite_json_op.valid].must_equal "json_valid(j || k)"
end
end
describe "Sequel::SQLite::JSONBOp" do
before do
@db = Sequel.connect('mock://sqlite')
@db.extend_datasets{def quote_identifiers?; false end}
@j = Sequel.sqlite_jsonb_op(:j)
@l = proc{|o| @db.literal(o)}
end
it "#[]/#get should use the ->> operator" do
@l[@j[1]].must_equal "(j ->> 1)"
@l[@j.get('a')].must_equal "(j ->> 'a')"
end
it "#[]/#get should return a JSONOp" do
@l[@j[1][2]].must_equal "((j ->> 1) ->> 2)"
@l[@j.get('a').get('b')].must_equal "((j ->> 'a') ->> 'b')"
end
it "#array_length should use the json_array_length function" do
@l[@j.array_length].must_equal "json_array_length(j)"
@l[@j.array_length("$[1]")].must_equal "json_array_length(j, '$[1]')"
end
it "#array_length should return a numeric expression" do
@l[@j.array_length + 1].must_equal "(json_array_length(j) + 1)"
end
it "#each should use the json_each function" do
@l[@j.each].must_equal "json_each(j)"
@l[@j.each("$[1]")].must_equal "json_each(j, '$[1]')"
end
it "#extract should use the jsonb_extract function" do
@l[@j.extract].must_equal "jsonb_extract(j)"
@l[@j.extract("$[1]")].must_equal "jsonb_extract(j, '$[1]')"
end
it "#get_json should use the -> operator" do
@l[@j.get_json(1)].must_equal "(j -> 1)"
end
it "#get_json should return a JSONOp" do
@l[@j.get_json('a').get_json('b')].must_equal "((j -> 'a') -> 'b')"
end
it "#insert should use the jsonb_insert function" do
@l[@j.insert('$.a', 1)].must_equal "jsonb_insert(j, '$.a', 1)"
@l[@j.insert('$.a', 1, '$.b', 2)].must_equal "jsonb_insert(j, '$.a', 1, '$.b', 2)"
end
it "#insert should return JSONBOp" do
@l[@j.insert('$.a', 1).insert('$.b', 2)].must_equal "jsonb_insert(jsonb_insert(j, '$.a', 1), '$.b', 2)"
end
it "#json/#minify should use the json function" do
@l[@j.json].must_equal "json(j)"
@l[@j.minify].must_equal "json(j)"
end
it "#json/#minify should return JSONOp" do
@l[@j.json.extract('$.a')].must_equal "json_extract(json(j), '$.a')"
@l[@j.minify.extract('$.a')].must_equal "json_extract(json(j), '$.a')"
end
it "#jsonb should use the jsonb function" do
@l[@j.jsonb].must_equal "jsonb(j)"
end
it "#jsonb should return JSONBOp" do
@l[@j.jsonb.extract('$.a')].must_equal "jsonb_extract(jsonb(j), '$.a')"
end
it "#patch should use the jsonb_patch function" do
@l[@j.patch('{"a": 1}')].must_equal "jsonb_patch(j, '{\"a\": 1}')"
end
it "#patch should return JSONBOp" do
@l[@j.patch('{"a": 1}').patch('{"b": 2}')].must_equal "jsonb_patch(jsonb_patch(j, '{\"a\": 1}'), '{\"b\": 2}')"
end
it "#remove should use the jsonb_remove function" do
@l[@j.remove('$.a')].must_equal "jsonb_remove(j, '$.a')"
@l[@j.remove('$.a', '$[1]')].must_equal "jsonb_remove(j, '$.a', '$[1]')"
end
it "#remove should return JSONBOp" do
@l[@j.remove('$.a').remove('$[1]')].must_equal "jsonb_remove(jsonb_remove(j, '$.a'), '$[1]')"
end
it "#replace should use the jsonb_replace function" do
@l[@j.replace('$.a', 1)].must_equal "jsonb_replace(j, '$.a', 1)"
@l[@j.replace('$.a', 1, '$.b', 2)].must_equal "jsonb_replace(j, '$.a', 1, '$.b', 2)"
end
it "#replace should return JSONBOp" do
@l[@j.replace('$.a', 1).replace('$.b', 2)].must_equal "jsonb_replace(jsonb_replace(j, '$.a', 1), '$.b', 2)"
end
it "#set should use the jsonb_set function" do
@l[@j.set('$.a', 1)].must_equal "jsonb_set(j, '$.a', 1)"
@l[@j.set('$.a', 1, '$.b', 2)].must_equal "jsonb_set(j, '$.a', 1, '$.b', 2)"
end
it "#set should return JSONBOp" do
@l[@j.set('$.a', 1).set('$.b', 2)].must_equal "jsonb_set(jsonb_set(j, '$.a', 1), '$.b', 2)"
end
it "#tree should use the json_tree function" do
@l[@j.tree].must_equal "json_tree(j)"
@l[@j.tree("$[1]")].must_equal "json_tree(j, '$[1]')"
end
it "#type/#typeof should use the json_type function" do
@l[@j.type].must_equal "json_type(j)"
@l[@j.typeof].must_equal "json_type(j)"
@l[@j.type("$[1]")].must_equal "json_type(j, '$[1]')"
@l[@j.typeof("$[1]")].must_equal "json_type(j, '$[1]')"
end
it "#type/#typeof should return a string expression" do
@l[@j.type + '1'].must_equal "(json_type(j) || '1')"
@l[@j.typeof('$.a') + '1'].must_equal "(json_type(j, '$.a') || '1')"
end
it "#valid should use the json_valid function" do
@l[@j.valid].must_equal "json_valid(j)"
end
it "#valid should return a boolean expression" do
@l[@j.valid & 1].must_equal "(json_valid(j) AND 1)"
end
it "Sequel.sqlite_jsonb_op should wrap object in a JSONBOp" do
@l[Sequel.sqlite_jsonb_op(:j).extract('$.a')].must_equal "jsonb_extract(j, '$.a')"
@l[Sequel.sqlite_jsonb_op(Sequel.join([:j, :k])).extract('$.a')].must_equal "jsonb_extract((j || k), '$.a')"
end
it "Sequel.sqlite_jsonb_op return a JSONOp as-is" do
v = Sequel.sqlite_jsonb_op(:j)
Sequel.sqlite_jsonb_op(v).must_be_same_as v
end
it "SQL::GenericExpression#sqlite_jsonb_op should wrap receiver in JSONBOp" do
@l[Sequel.function(:j, :k).sqlite_jsonb_op.extract('$.a')].must_equal "jsonb_extract(j(k), '$.a')"
end
it "SQL::LiteralString#sqlite_jsonb_op should wrap receiver in JSONBOp" do
@l[Sequel.lit('j || k').sqlite_jsonb_op.extract('$.a')].must_equal "jsonb_extract(j || k, '$.a')"
end
end
|