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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
require_relative "spec_helper"
Sequel.extension :pg_array, :pg_array_ops, :pg_json, :pg_json_ops
describe "Sequel::Postgres::JSONOp" do
before do
@db = Sequel.connect('mock://postgres')
@db.extend_datasets{def quote_identifiers?; false end}
@j = Sequel.pg_json_op(:j)
@jb = Sequel.pg_jsonb_op(:j)
@l = proc{|o| @db.literal(o)}
end
it "should have #[] get the element" do
@l[@j[1]].must_equal "(j -> 1)"
@l[@j['a']].must_equal "(j -> 'a')"
end
it "should have #[] use -> operator on for JSONB for identifiers on older PostgreSQL versions" do
def @db.server_version(*); 130000; end
@l[@jb[1]].must_equal "(j -> 1)"
end
it "should have #[] use subscript form on PostgreSQL 14 for JSONB for identifiers" do
@l[@jb[1]].must_equal "j[1]"
@l[@jb['a'][1]].must_equal "j['a'][1]"
@l[Sequel.pg_jsonb_op(Sequel[:j])[1]].must_equal "j[1]"
@l[Sequel.pg_jsonb_op(Sequel[:s][:j])['a'][1]].must_equal "s.j['a'][1]"
@l[@jb[[1, 2]]].must_equal "(j #> ARRAY[1,2])"
@l[Sequel.pg_jsonb_op(Sequel.lit('j'))['a'][1]].must_equal "((j -> 'a') -> 1)"
@db.select(Sequel.pg_jsonb_op(Sequel[:h])['a']).qualify(:t).sql.must_equal "SELECT t.h['a']"
end
it "should have #[] accept an array" do
@l[@j[%w'a b']].must_equal "(j #> ARRAY['a','b'])"
@l[@j[Sequel.pg_array(%w'a b')]].must_equal "(j #> ARRAY['a','b'])"
@l[@j[Sequel.pg_array(:a)]].must_equal "(j #> a)"
end
it "should have #[] return an object of the same class" do
@l[@j[1].to_recordset].must_equal "json_to_recordset((j -> 1))"
@l[@j[%w'a b'][2]].must_equal "((j #> ARRAY['a','b']) -> 2)"
@l[@jb[1].to_recordset].must_equal "jsonb_to_recordset(j[1])"
@l[@jb[%w'a b'][2]].must_equal "((j #> ARRAY['a','b']) -> 2)"
end
it "should have #get be an alias to #[]" do
@l[@j.get(1)].must_equal "(j -> 1)"
@l[@j.get(%w'a b')].must_equal "(j #> ARRAY['a','b'])"
end
it "should have #get_text get the element as text" do
@l[@j.get_text(1)].must_equal "(j ->> 1)"
@l[@j.get_text('a')].must_equal "(j ->> 'a')"
end
it "should have #get_text accept an array" do
@l[@j.get_text(%w'a b')].must_equal "(j #>> ARRAY['a','b'])"
@l[@j.get_text(Sequel.pg_array(%w'a b'))].must_equal "(j #>> ARRAY['a','b'])"
@l[@j.get_text(Sequel.pg_array(:a))].must_equal "(j #>> a)"
end
it "should have #get_text return an SQL::StringExpression" do
@l[@j.get_text(1) + 'a'].must_equal "((j ->> 1) || 'a')"
@l[@j.get_text(%w'a b') + 'a'].must_equal "((j #>> ARRAY['a','b']) || 'a')"
end
it "should have #array_length use the json_array_length function" do
@l[@j.array_length].must_equal "json_array_length(j)"
@l[@jb.array_length].must_equal "jsonb_array_length(j)"
end
it "should have #array_length return a numeric expression" do
@l[@j.array_length & 1].must_equal "(json_array_length(j) & 1)"
@l[@jb.array_length & 1].must_equal "(jsonb_array_length(j) & 1)"
end
it "should have #each use the json_each function" do
@l[@j.each].must_equal "json_each(j)"
@l[@jb.each].must_equal "jsonb_each(j)"
end
it "should have #each_text use the json_each_text function" do
@l[@j.each_text].must_equal "json_each_text(j)"
@l[@jb.each_text].must_equal "jsonb_each_text(j)"
end
it "should have #extract use the json_extract_path function" do
@l[@j.extract('a')].must_equal "json_extract_path(j, 'a')"
@l[@j.extract('a', 'b')].must_equal "json_extract_path(j, 'a', 'b')"
@l[@jb.extract('a')].must_equal "jsonb_extract_path(j, 'a')"
@l[@jb.extract('a', 'b')].must_equal "jsonb_extract_path(j, 'a', 'b')"
end
it "should have #extract return a JSONOp" do
@l[@j.extract('a')[1]].must_equal "(json_extract_path(j, 'a') -> 1)"
@l[@jb.extract('a')[1]].must_equal "(jsonb_extract_path(j, 'a') -> 1)"
end
it "should have #extract_text use the json_extract_path_text function" do
@l[@j.extract_text('a')].must_equal "json_extract_path_text(j, 'a')"
@l[@j.extract_text('a', 'b')].must_equal "json_extract_path_text(j, 'a', 'b')"
@l[@jb.extract_text('a')].must_equal "jsonb_extract_path_text(j, 'a')"
@l[@jb.extract_text('a', 'b')].must_equal "jsonb_extract_path_text(j, 'a', 'b')"
end
it "should have #extract_text return an SQL::StringExpression" do
@l[@j.extract_text('a') + 'a'].must_equal "(json_extract_path_text(j, 'a') || 'a')"
@l[@jb.extract_text('a') + 'a'].must_equal "(jsonb_extract_path_text(j, 'a') || 'a')"
end
it "should have #keys use the json_object_keys function" do
@l[@j.keys].must_equal "json_object_keys(j)"
@l[@jb.keys].must_equal "jsonb_object_keys(j)"
end
it "should have #array_elements use the json_array_elements function" do
@l[@j.array_elements].must_equal "json_array_elements(j)"
@l[@jb.array_elements].must_equal "jsonb_array_elements(j)"
end
it "should have #array_elements use the json_array_elements_text function" do
@l[@j.array_elements_text].must_equal "json_array_elements_text(j)"
@l[@jb.array_elements_text].must_equal "jsonb_array_elements_text(j)"
end
it "should have #strip_nulls use the json_strip_nulls function" do
@l[@j.strip_nulls].must_equal "json_strip_nulls(j)"
@l[@jb.strip_nulls].must_equal "jsonb_strip_nulls(j)"
end
it "should have #typeof use the json_typeof function" do
@l[@j.typeof].must_equal "json_typeof(j)"
@l[@jb.typeof].must_equal "jsonb_typeof(j)"
end
it "should have #to_record use the json_to_record function" do
@l[@j.to_record].must_equal "json_to_record(j)"
@l[@jb.to_record].must_equal "jsonb_to_record(j)"
end
it "should have #to_recordset use the json_to_recordsetfunction" do
@l[@j.to_recordset].must_equal "json_to_recordset(j)"
@l[@jb.to_recordset].must_equal "jsonb_to_recordset(j)"
end
it "should have #populate use the json_populate_record function" do
@l[@j.populate(:a)].must_equal "json_populate_record(a, j)"
@l[@jb.populate(:a)].must_equal "jsonb_populate_record(a, j)"
end
it "should have #populate_set use the json_populate_record function" do
@l[@j.populate_set(:a)].must_equal "json_populate_recordset(a, j)"
@l[@jb.populate_set(:a)].must_equal "jsonb_populate_recordset(a, j)"
end
it "#contain_all should use the ?& operator" do
@l[@jb.contain_all(:h1)].must_equal "(j ?& h1)"
end
it "#contain_all handle arrays" do
@l[@jb.contain_all(%w'h1')].must_equal "(j ?& ARRAY['h1'])"
end
it "#contain_any should use the ?| operator" do
@l[@jb.contain_any(:h1)].must_equal "(j ?| h1)"
end
it "#contain_any should handle arrays" do
@l[@jb.contain_any(%w'h1')].must_equal "(j ?| ARRAY['h1'])"
end
it "#contains should use the @> operator" do
@l[@jb.contains(:h1)].must_equal "(j @> h1)"
end
it "#contains should handle hashes" do
@l[@jb.contains('a'=>'b')].must_equal "(j @> '{\"a\":\"b\"}'::jsonb)"
end
it "#contains should handle arrays" do
@l[@jb.contains([1, 2])].must_equal "(j @> '[1,2]'::jsonb)"
end
it "#contained_by should use the <@ operator" do
@l[@jb.contained_by(:h1)].must_equal "(j <@ h1)"
end
it "#contained_by should handle hashes" do
@l[@jb.contained_by('a'=>'b')].must_equal "(j <@ '{\"a\":\"b\"}'::jsonb)"
end
it "#contained_by should handle arrays" do
@l[@jb.contained_by([1, 2])].must_equal "(j <@ '[1,2]'::jsonb)"
end
it "#concat should use the || operator" do
@l[@jb.concat(:h1)].must_equal "(j || h1)"
end
it "#concat should handle hashes" do
@l[@jb.concat('a'=>'b')].must_equal "(j || '{\"a\":\"b\"}'::jsonb)"
end
it "#concat should handle arrays" do
@l[@jb.concat([1, 2])].must_equal "(j || '[1,2]'::jsonb)"
end
it "#insert should use the jsonb_insert function" do
@l[@jb.insert(:a, :h)].must_equal "jsonb_insert(j, a, h, false)"
@l[@jb.insert(:a, :h, true)].must_equal "jsonb_insert(j, a, h, true)"
end
it "#insert should handle hashes" do
@l[@jb.insert(:a, 'a'=>'b')].must_equal "jsonb_insert(j, a, '{\"a\":\"b\"}'::jsonb, false)"
end
it "#insert should handle arrays" do
@l[@jb.insert(%w'a b', [1, 2])].must_equal "jsonb_insert(j, ARRAY['a','b'], '[1,2]'::jsonb, false)"
end
it "#set should use the jsonb_set function" do
@l[@jb.set(:a, :h)].must_equal "jsonb_set(j, a, h, true)"
@l[@jb.set(:a, :h, false)].must_equal "jsonb_set(j, a, h, false)"
end
it "#set should handle hashes" do
@l[@jb.set(:a, 'a'=>'b')].must_equal "jsonb_set(j, a, '{\"a\":\"b\"}'::jsonb, true)"
end
it "#set should handle arrays" do
@l[@jb.set(%w'a b', [1, 2])].must_equal "jsonb_set(j, ARRAY['a','b'], '[1,2]'::jsonb, true)"
end
it "#set_lax should use the jsonb_set function" do
@l[@jb.set_lax(:a, :h)].must_equal "jsonb_set_lax(j, a, h, true, 'use_json_null')"
@l[@jb.set_lax(:a, :h, false)].must_equal "jsonb_set_lax(j, a, h, false, 'use_json_null')"
@l[@jb.set_lax(:a, :h, false, 'delete_key')].must_equal "jsonb_set_lax(j, a, h, false, 'delete_key')"
end
it "#set should handle hashes" do
@l[@jb.set_lax(:a, 'a'=>'b')].must_equal "jsonb_set_lax(j, a, '{\"a\":\"b\"}'::jsonb, true, 'use_json_null')"
end
it "#set should handle arrays" do
@l[@jb.set_lax(%w'a b', [1, 2])].must_equal "jsonb_set_lax(j, ARRAY['a','b'], '[1,2]'::jsonb, true, 'use_json_null')"
end
it "#pretty should use the jsonb_pretty function" do
@l[@jb.pretty].must_equal "jsonb_pretty(j)"
end
it "#- should use the - operator" do
@l[@jb - 1].must_equal "(j - 1)"
end
it "#delete_path should use the #- operator" do
@l[@jb.delete_path(:a)].must_equal "(j #- a)"
end
it "#delete_path should handle arrays" do
@l[@jb.delete_path(['a'])].must_equal "(j #- ARRAY['a'])"
end
it "#has_key? and aliases should use the ? operator" do
@l[@jb.has_key?('a')].must_equal "(j ? 'a')"
@l[@jb.include?('a')].must_equal "(j ? 'a')"
end
it "#pg_json should return self" do
@j.pg_json.must_be_same_as(@j)
@jb.pg_jsonb.must_be_same_as(@jb)
end
it "Sequel.pg_json_op should return arg for JSONOp" do
Sequel.pg_json_op(@j).must_be_same_as(@j)
Sequel.pg_jsonb_op(@jb).must_be_same_as(@jb)
end
it "should be able to turn expressions into json ops using pg_json" do
@db.literal(Sequel.qualify(:b, :a).pg_json[1]).must_equal "(b.a -> 1)"
@db.literal(Sequel.function(:a, :b).pg_json[1]).must_equal "(a(b) -> 1)"
@db.literal(Sequel.qualify(:b, :a).pg_jsonb[1]).must_equal "b.a[1]"
@db.literal(Sequel.function(:a, :b).pg_jsonb[1]).must_equal "(a(b) -> 1)"
end
it "should be able to turn literal strings into json ops using pg_json" do
@db.literal(Sequel.lit('a').pg_json[1]).must_equal "(a -> 1)"
@db.literal(Sequel.lit('a').pg_jsonb[1]).must_equal "(a -> 1)"
end
it "should be able to turn symbols into json ops using Sequel.pg_json_op" do
@db.literal(Sequel.pg_json_op(:a)[1]).must_equal "(a -> 1)"
@db.literal(Sequel.pg_jsonb_op(:a)[1]).must_equal "a[1]"
end
it "should be able to turn symbols into json ops using Sequel.pg_json" do
@db.literal(Sequel.pg_json(:a)[1]).must_equal "(a -> 1)"
@db.literal(Sequel.pg_jsonb(:a)[1]).must_equal "a[1]"
@db.literal(Sequel.pg_jsonb(:a).contains('a'=>1)).must_equal "(a @> '{\"a\":1}'::jsonb)"
end
it "should allow transforming JSONArray instances into ArrayOp instances" do
@db.literal(Sequel.pg_json([1,2]).op[1]).must_equal "('[1,2]'::json -> 1)"
end
it "should allow transforming JSONHash instances into ArrayOp instances" do
@db.literal(Sequel.pg_json('a'=>1).op['a']).must_equal "('{\"a\":1}'::json -> 'a')"
end
it "should allow transforming JSONBArray instances into ArrayOp instances" do
@db.literal(Sequel.pg_jsonb([1,2]).op[1]).must_equal "('[1,2]'::jsonb -> 1)"
end
it "should allow transforming JSONBHash instances into ArrayOp instances" do
@db.literal(Sequel.pg_jsonb('a'=>1).op['a']).must_equal "('{\"a\":1}'::jsonb -> 'a')"
end
it "#path_exists should use the @? operator" do
@l[@jb.path_exists('$')].must_equal "(j @? '$')"
end
it "#path_exists result should be a boolean expression" do
@jb.path_exists('$').must_be_kind_of Sequel::SQL::BooleanExpression
end
it "#path_match should use the @@ operator" do
@l[@jb.path_match('$')].must_equal "(j @@ '$')"
end
it "#path_match result should be a boolean expression" do
@jb.path_match('$').must_be_kind_of Sequel::SQL::BooleanExpression
end
it "#path_exists! should use the jsonb_path_exists function" do
@l[@jb.path_exists!('$')].must_equal "jsonb_path_exists(j, '$')"
@l[@jb.path_exists!('$', '{"x":2}')].must_equal "jsonb_path_exists(j, '$', '{\"x\":2}')"
@l[@jb.path_exists!('$', x: 2)].must_equal "jsonb_path_exists(j, '$', '{\"x\":2}')"
@l[@jb.path_exists!('$', {x: 2}, true)].must_equal "jsonb_path_exists(j, '$', '{\"x\":2}', true)"
end
it "#path_exists! result should be a boolean expression" do
@jb.path_exists!('$').must_be_kind_of Sequel::SQL::BooleanExpression
end
it "#path_match! should use the jsonb_path_match function" do
@l[@jb.path_match!('$')].must_equal "jsonb_path_match(j, '$')"
@l[@jb.path_match!('$', '{"x":2}')].must_equal "jsonb_path_match(j, '$', '{\"x\":2}')"
@l[@jb.path_match!('$', x: 2)].must_equal "jsonb_path_match(j, '$', '{\"x\":2}')"
@l[@jb.path_match!('$', {x: 2}, true)].must_equal "jsonb_path_match(j, '$', '{\"x\":2}', true)"
end
it "#path_match! result should be a boolean expression" do
@jb.path_match!('$').must_be_kind_of Sequel::SQL::BooleanExpression
end
it "#path_query should use the jsonb_path_query function" do
@l[@jb.path_query('$')].must_equal "jsonb_path_query(j, '$')"
@l[@jb.path_query('$', '{"x":2}')].must_equal "jsonb_path_query(j, '$', '{\"x\":2}')"
@l[@jb.path_query('$', x: 2)].must_equal "jsonb_path_query(j, '$', '{\"x\":2}')"
@l[@jb.path_query('$', {x: 2}, true)].must_equal "jsonb_path_query(j, '$', '{\"x\":2}', true)"
end
it "#path_query_array should use the jsonb_path_query_array function" do
@l[@jb.path_query_array('$')].must_equal "jsonb_path_query_array(j, '$')"
@l[@jb.path_query_array('$', '{"x":2}')].must_equal "jsonb_path_query_array(j, '$', '{\"x\":2}')"
@l[@jb.path_query_array('$', x: 2)].must_equal "jsonb_path_query_array(j, '$', '{\"x\":2}')"
@l[@jb.path_query_array('$', {x: 2}, true)].must_equal "jsonb_path_query_array(j, '$', '{\"x\":2}', true)"
end
it "#path_query_array result should be a JSONBOp" do
@l[@jb.path_query_array('$').path_query_array('$')].must_equal "jsonb_path_query_array(jsonb_path_query_array(j, '$'), '$')"
end
it "#path_query_first should use the jsonb_path_query_first function" do
@l[@jb.path_query_first('$')].must_equal "jsonb_path_query_first(j, '$')"
@l[@jb.path_query_first('$', '{"x":2}')].must_equal "jsonb_path_query_first(j, '$', '{\"x\":2}')"
@l[@jb.path_query_first('$', x: 2)].must_equal "jsonb_path_query_first(j, '$', '{\"x\":2}')"
@l[@jb.path_query_first('$', {x: 2}, true)].must_equal "jsonb_path_query_first(j, '$', '{\"x\":2}', true)"
end
it "#path_query_first result should be a JSONBOp" do
@l[@jb.path_query_first('$').path_query_first('$')].must_equal "jsonb_path_query_first(jsonb_path_query_first(j, '$'), '$')"
end
it "#path_exists_tz! should use the jsonb_path_exists function" do
@l[@jb.path_exists_tz!('$')].must_equal "jsonb_path_exists_tz(j, '$')"
@l[@jb.path_exists_tz!('$', '{"x":2}')].must_equal "jsonb_path_exists_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_exists_tz!('$', x: 2)].must_equal "jsonb_path_exists_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_exists_tz!('$', {x: 2}, true)].must_equal "jsonb_path_exists_tz(j, '$', '{\"x\":2}', true)"
end
it "#path_exists! result should be a boolean expression" do
@jb.path_exists_tz!('$').must_be_kind_of Sequel::SQL::BooleanExpression
end
it "#path_match! should use the jsonb_path_match function" do
@l[@jb.path_match_tz!('$')].must_equal "jsonb_path_match_tz(j, '$')"
@l[@jb.path_match_tz!('$', '{"x":2}')].must_equal "jsonb_path_match_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_match_tz!('$', x: 2)].must_equal "jsonb_path_match_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_match_tz!('$', {x: 2}, true)].must_equal "jsonb_path_match_tz(j, '$', '{\"x\":2}', true)"
end
it "#path_match! result should be a boolean expression" do
@jb.path_match_tz!('$').must_be_kind_of Sequel::SQL::BooleanExpression
end
it "#path_query should use the jsonb_path_query function" do
@l[@jb.path_query_tz('$')].must_equal "jsonb_path_query_tz(j, '$')"
@l[@jb.path_query_tz('$', '{"x":2}')].must_equal "jsonb_path_query_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_query_tz('$', x: 2)].must_equal "jsonb_path_query_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_query_tz('$', {x: 2}, true)].must_equal "jsonb_path_query_tz(j, '$', '{\"x\":2}', true)"
end
it "#path_query_array should use the jsonb_path_query_array function" do
@l[@jb.path_query_array_tz('$')].must_equal "jsonb_path_query_array_tz(j, '$')"
@l[@jb.path_query_array_tz('$', '{"x":2}')].must_equal "jsonb_path_query_array_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_query_array_tz('$', x: 2)].must_equal "jsonb_path_query_array_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_query_array_tz('$', {x: 2}, true)].must_equal "jsonb_path_query_array_tz(j, '$', '{\"x\":2}', true)"
end
it "#path_query_array result should be a JSONBOp" do
@l[@jb.path_query_array_tz('$').path_query_array_tz('$')].must_equal "jsonb_path_query_array_tz(jsonb_path_query_array_tz(j, '$'), '$')"
end
it "#path_query_first should use the jsonb_path_query_first function" do
@l[@jb.path_query_first_tz('$')].must_equal "jsonb_path_query_first_tz(j, '$')"
@l[@jb.path_query_first_tz('$', '{"x":2}')].must_equal "jsonb_path_query_first_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_query_first_tz('$', x: 2)].must_equal "jsonb_path_query_first_tz(j, '$', '{\"x\":2}')"
@l[@jb.path_query_first_tz('$', {x: 2}, true)].must_equal "jsonb_path_query_first_tz(j, '$', '{\"x\":2}', true)"
end
it "#path_query_first result should be a JSONBOp" do
@l[@jb.path_query_first_tz('$').path_query_first_tz('$')].must_equal "jsonb_path_query_first_tz(jsonb_path_query_first_tz(j, '$'), '$')"
end
end
|