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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
#!/usr/bin/env tarantool
local tap = require('tap')
local ffi = require('ffi')
local json = require('json')
local fun = require('fun')
-- XXX fix for gh-4252: to prevent invalid trace assembling (see
-- https://github.com/LuaJIT/LuaJIT/issues/584) disable JIT for
-- <fun.chain> iterator (i.e. <chain_gen_r1>). Since the function
-- is local, the dummy chain generator is created to obtain the
-- function GC object.
jit.off(fun.chain({}).gen)
local key_def_lib = require('key_def')
local usage_error = 'Bad params, use: key_def.new({' ..
'{fieldno = fieldno, type = type' ..
'[, is_nullable = <boolean>]' ..
'[, path = <string>]' ..
'[, collation_id = <number>]' ..
'[, collation = <string>]}, ...}'
local function coll_not_found(fieldno, collation)
if type(collation) == 'number' then
return ('Wrong index options (field %d): ' ..
'collation was not found by ID'):format(fieldno)
end
return ('Unknown collation: "%s"'):format(collation)
end
local function set_key_part_defaults(parts)
local res = {}
for i, part in ipairs(parts) do
res[i] = table.copy(part)
if res[i].is_nullable == nil then
res[i].is_nullable = false
end
end
return res
end
local key_def_new_cases = {
-- Cases to call before box.cfg{}.
{
'Pass no key parts',
parts = {},
exp_err = 'Key definition can only be constructed by using at least 1 key_part',
},
{
"Pass a garbage instead of key parts",
parts = {fieldno = 1, type = 'unsigned'},
exp_err = 'Key definition can only be constructed by using at least 1 key_part',
},
{
'Pass a field on an unknown type',
parts = {{
fieldno = 2,
type = 'unknown',
}},
exp_err = 'Unknown field type: unknown',
},
{
'Try to use collation_id before box.cfg{}',
parts = {{
fieldno = 1,
type = 'string',
collation_id = 2,
}},
exp_err = coll_not_found(1, 2),
},
{
'Try to use collation before box.cfg{}',
parts = {{
fieldno = 1,
type = 'string',
collation = 'unicode_ci',
}},
exp_err = coll_not_found(1, 'unicode_ci'),
},
function()
-- For collations.
box.cfg{}
end,
-- Cases to call after box.cfg{}.
{
'Try to use both collation_id and collation',
parts = {{
fieldno = 1,
type = 'string',
collation_id = 2,
collation = 'unicode_ci',
}},
exp_err = 'Conflicting options: collation_id and collation',
},
{
'Unknown collation_id',
parts = {{
fieldno = 1,
type = 'string',
collation_id = 999,
}},
exp_err = coll_not_found(1, 999),
},
{
'Unknown collation name',
parts = {{
fieldno = 1,
type = 'string',
collation = 'unknown',
}},
exp_err = 'Unknown collation: "unknown"',
},
{
'Bad parts parameter type',
parts = 1,
exp_err = usage_error,
},
{
'No parameters',
params = {},
exp_err = usage_error,
},
{
'Two parameters',
params = {{}, {}},
exp_err = usage_error,
},
{
'Invalid JSON path',
parts = {{
fieldno = 1,
type = 'string',
path = '[3[',
}},
exp_err = 'invalid path',
},
{
'Multikey JSON path',
parts = {{
fieldno = 1,
type = 'string',
path = '[*]',
}},
exp_err = 'multikey path is unsupported',
},
{
'Success case; one part',
parts = {{
fieldno = 1,
type = 'string',
}},
exp_err = nil,
},
{
'Success case; one part with a JSON path',
parts = {{
fieldno = 1,
type = 'string',
path = '[3]',
}},
exp_err = nil,
},
--
-- gh-4519: key_def should allow the same options as
-- <space_object>.create_index(). That is, a field number
-- should be allowed to be specified as `field`, not only
-- `fieldno`.
--
{
'Success case; `field` is alias to `fieldno`',
parts = {{
field = 1,
type = 'unsigned'
}},
exp_err = nil,
},
{
'Field and fieldno can not be set both',
parts = {{
field = 1,
fieldno = 1,
type = 'unsigned'
}},
exp_err = 'Conflicting options: fieldno and field',
}
}
local test = tap.test('key_def')
test:plan(#key_def_new_cases - 1 + 7)
for _, case in ipairs(key_def_new_cases) do
if type(case) == 'function' then
case()
else
local ok, res
if case.params then
ok, res = pcall(key_def_lib.new, unpack(case.params))
else
ok, res = pcall(key_def_lib.new, case.parts)
end
if case.exp_err == nil then
ok = ok and type(res) == 'cdata' and
ffi.istype('struct key_def', res)
test:ok(ok, case[1])
else
local err = tostring(res) -- cdata -> string
test:is_deeply({ok, err}, {false, case.exp_err}, case[1])
end
end
end
-- Prepare source data for test cases.
-- Case: extract_key().
test:test('extract_key()', function(test)
test:plan(13)
local key_def_a = key_def_lib.new({
{type = 'unsigned', fieldno = 1},
})
local key_def_b = key_def_lib.new({
{type = 'number', fieldno = 2},
{type = 'number', fieldno = 3},
})
local key_def_c = key_def_lib.new({
{type = 'scalar', fieldno = 2},
{type = 'scalar', fieldno = 1},
{type = 'string', fieldno = 4, is_nullable = true},
})
local tuple_a = box.tuple.new({1, 1, 22})
test:is_deeply(key_def_a:extract_key(tuple_a):totable(), {1}, 'case 1')
test:is_deeply(key_def_b:extract_key(tuple_a):totable(), {1, 22}, 'case 2')
-- JSON path.
local res = key_def_lib.new({
{type = 'string', fieldno = 1, path = 'a.b'},
}):extract_key(box.tuple.new({{a = {b = 'foo'}}})):totable()
test:is_deeply(res, {'foo'}, 'JSON path (tuple argument)')
local res = key_def_lib.new({
{type = 'string', fieldno = 1, path = 'a.b'},
}):extract_key({{a = {b = 'foo'}}}):totable()
test:is_deeply(res, {'foo'}, 'JSON path (table argument)')
-- A key def has a **nullable** part with a field that is over
-- a tuple size.
--
-- The key def options are:
--
-- * is_nullable = true;
-- * has_optional_parts = true.
test:is_deeply(key_def_c:extract_key(tuple_a):totable(), {1, 1, box.NULL},
'short tuple with a nullable part')
-- A key def has a **non-nullable** part with a field that is
-- over a tuple size.
--
-- The key def options are:
--
-- * is_nullable = false;
-- * has_optional_parts = false.
local exp_err = 'Tuple field [2] required by space format is missing'
local key_def = key_def_lib.new({
{type = 'string', fieldno = 1},
{type = 'string', fieldno = 2},
})
local ok, err = pcall(key_def.extract_key, key_def,
box.tuple.new({'foo'}))
test:is_deeply({ok, tostring(err)}, {false, exp_err},
'short tuple with a non-nullable part (case 1)')
-- Same as before, but a max fieldno is over tuple:len() + 1.
local exp_err = 'Tuple field [2] required by space format is missing'
local key_def = key_def_lib.new({
{type = 'string', fieldno = 1},
{type = 'string', fieldno = 2},
{type = 'string', fieldno = 3},
})
local ok, err = pcall(key_def.extract_key, key_def,
box.tuple.new({'foo'}))
test:is_deeply({ok, tostring(err)}, {false, exp_err},
'short tuple with a non-nullable part (case 2)')
-- Same as before, but with another key def options:
--
-- * is_nullable = true;
-- * has_optional_parts = false.
local exp_err = 'Tuple field [2] required by space format is missing'
local key_def = key_def_lib.new({
{type = 'string', fieldno = 1, is_nullable = true},
{type = 'string', fieldno = 2},
})
local ok, err = pcall(key_def.extract_key, key_def,
box.tuple.new({'foo'}))
test:is_deeply({ok, tostring(err)}, {false, exp_err},
'short tuple with a non-nullable part (case 3)')
-- A tuple has a field that does not match corresponding key
-- part type.
local exp_err = 'Supplied key type of part 2 does not match index ' ..
'part type: expected string'
local key_def = key_def_lib.new({
{type = 'string', fieldno = 1},
{type = 'string', fieldno = 2},
{type = 'string', fieldno = 3},
})
local ok, err = pcall(key_def.extract_key, key_def, {'one', 'two', 3})
test:is_deeply({ok, tostring(err)}, {false, exp_err},
'wrong field type')
local key_def = key_def_lib.new({
{type = 'number', fieldno = 1, path='a'},
{type = 'number', fieldno = 1, path='b'},
{type = 'number', fieldno = 1, path='c', is_nullable=true},
{type = 'number', fieldno = 3, is_nullable=true},
})
local ok, err = pcall(key_def.extract_key, key_def,
box.tuple.new({1, 1, 22}))
test:is_deeply({ok, tostring(err)},
{false, 'Tuple field [1]a required by space format is missing'},
'invalid JSON structure')
test:is_deeply(key_def:extract_key({{a=1, b=2}, 1}):totable(),
{1, 2, box.NULL, box.NULL},
'tuple with optional parts - case 1')
test:is_deeply(key_def:extract_key({{a=1, b=2, c=3}, 1}):totable(),
{1, 2, 3, box.NULL},
'tuple with optional parts - case 2')
test:is_deeply(key_def:extract_key({{a=1, b=2}, 1, 3}):totable(),
{1, 2, box.NULL, 3},
'tuple with optional parts - case 3')
end)
-- Case: compare().
test:test('compare()', function(test)
test:plan(8)
local key_def_a = key_def_lib.new({
{type = 'unsigned', fieldno = 1},
})
local key_def_b = key_def_lib.new({
{type = 'number', fieldno = 2},
{type = 'number', fieldno = 3},
})
local tuple_a = box.tuple.new({1, 1, 22})
local tuple_b = box.tuple.new({2, 1, 11})
local tuple_c = box.tuple.new({3, 1, 22})
test:is(key_def_a:compare(tuple_b, tuple_a), 1,
'case 1: great (tuple argument)')
test:is(key_def_a:compare(tuple_b, tuple_c), -1,
'case 2: less (tuple argument)')
test:is(key_def_b:compare(tuple_b, tuple_a), -1,
'case 3: less (tuple argument)')
test:is(key_def_b:compare(tuple_a, tuple_c), 0,
'case 4: equal (tuple argument)')
test:is(key_def_a:compare(tuple_b:totable(), tuple_a:totable()), 1,
'case 1: great (table argument)')
test:is(key_def_a:compare(tuple_b:totable(), tuple_c:totable()), -1,
'case 2: less (table argument)')
test:is(key_def_b:compare(tuple_b:totable(), tuple_a:totable()), -1,
'case 3: less (table argument)')
test:is(key_def_b:compare(tuple_a:totable(), tuple_c:totable()), 0,
'case 4: equal (table argument)')
end)
-- Case: compare_with_key().
test:test('compare_with_key()', function(test)
test:plan(2)
local key_def_b = key_def_lib.new({
{type = 'number', fieldno = 2},
{type = 'number', fieldno = 3},
})
local tuple_a = box.tuple.new({1, 1, 22})
local key = {1, 22}
test:is(key_def_b:compare_with_key(tuple_a:totable(), key), 0, 'table')
local key = box.tuple.new({1, 22})
test:is(key_def_b:compare_with_key(tuple_a, key), 0, 'tuple')
end)
-- Case: totable().
test:test('totable()', function(test)
test:plan(2)
local parts_a = {
{type = 'unsigned', fieldno = 1}
}
local parts_b = {
{type = 'number', fieldno = 2},
{type = 'number', fieldno = 3},
}
local key_def_a = key_def_lib.new(parts_a)
local key_def_b = key_def_lib.new(parts_b)
local exp = set_key_part_defaults(parts_a)
test:is_deeply(key_def_a:totable(), exp, 'case 1')
local exp = set_key_part_defaults(parts_b)
test:is_deeply(key_def_b:totable(), exp, 'case 2')
end)
-- Case: __serialize().
test:test('__serialize()', function(test)
test:plan(2)
local parts_a = {
{type = 'unsigned', fieldno = 1}
}
local parts_b = {
{type = 'number', fieldno = 2},
{type = 'number', fieldno = 3},
}
local key_def_a = key_def_lib.new(parts_a)
local key_def_b = key_def_lib.new(parts_b)
local exp = set_key_part_defaults(parts_a)
test:is(json.encode(key_def_a), json.encode(exp), 'case 1')
local exp = set_key_part_defaults(parts_b)
test:is(json.encode(key_def_b), json.encode(exp), 'case 2')
end)
-- Case: tostring().
test:test('tostring()', function(test)
test:plan(2)
local parts_a = {
{type = 'unsigned', fieldno = 1}
}
local parts_b = {
{type = 'number', fieldno = 2},
{type = 'number', fieldno = 3},
}
local key_def_a = key_def_lib.new(parts_a)
local key_def_b = key_def_lib.new(parts_b)
local exp = '<struct key_def &>'
test:is(tostring(key_def_a), exp, 'case 1')
test:is(tostring(key_def_b), exp, 'case 2')
end)
-- Case: merge().
test:test('merge()', function(test)
test:plan(6)
local key_def_a = key_def_lib.new({
{type = 'unsigned', fieldno = 1},
})
local key_def_b = key_def_lib.new({
{type = 'number', fieldno = 2},
{type = 'number', fieldno = 3},
})
local key_def_c = key_def_lib.new({
{type = 'scalar', fieldno = 2},
{type = 'scalar', fieldno = 1},
{type = 'string', fieldno = 4, is_nullable = true},
})
local tuple_a = box.tuple.new({1, 1, 22})
local key_def_ab = key_def_a:merge(key_def_b)
local exp_parts = fun.iter(key_def_a:totable())
:chain(fun.iter(key_def_b:totable())):totable()
test:is_deeply(key_def_ab:totable(), exp_parts,
'case 1: verify with :totable()')
test:is_deeply(key_def_ab:extract_key(tuple_a):totable(), {1, 1, 22},
'case 1: verify with :extract_key()')
local key_def_ba = key_def_b:merge(key_def_a)
local exp_parts = fun.iter(key_def_b:totable())
:chain(fun.iter(key_def_a:totable())):totable()
test:is_deeply(key_def_ba:totable(), exp_parts,
'case 2: verify with :totable()')
test:is_deeply(key_def_ba:extract_key(tuple_a):totable(), {1, 22, 1},
'case 2: verify with :extract_key()')
-- Intersecting parts + NULL parts.
local key_def_cb = key_def_c:merge(key_def_b)
local exp_parts = key_def_c:totable()
exp_parts[#exp_parts + 1] = {type = 'number', fieldno = 3,
is_nullable = false}
test:is_deeply(key_def_cb:totable(), exp_parts,
'case 3: verify with :totable()')
test:is_deeply(key_def_cb:extract_key(tuple_a):totable(),
{1, 1, box.NULL, 22}, 'case 3: verify with :extract_key()')
end)
os.exit(test:check() and 0 or 1)
|