|
| 1 | +#!/usr/bin/env tarantool |
| 2 | + |
| 3 | +local t = require('luatest') |
| 4 | +local db = require('test.db') |
| 5 | +local ddl = require('ddl') |
| 6 | +local cache = require('ddl.cache') |
| 7 | + |
| 8 | +local SPACE_NAME_IDX = 1 |
| 9 | +local SHARD_FUNC_NAME_IDX = 2 |
| 10 | +local SHARD_FUNC_BODY_IDX = 3 |
| 11 | + |
| 12 | +local test_space = { |
| 13 | + engine = 'memtx', |
| 14 | + is_local = true, |
| 15 | + temporary = false, |
| 16 | + format = { |
| 17 | + {name = 'unsigned_nonnull', type = 'unsigned', is_nullable = false}, |
| 18 | + {name = 'unsigned_nullable', type = 'unsigned', is_nullable = true}, |
| 19 | + {name = 'integer_nonnull', type = 'integer', is_nullable = false}, |
| 20 | + {name = 'integer_nullable', type = 'integer', is_nullable = true}, |
| 21 | + {name = 'number_nonnull', type = 'number', is_nullable = false}, |
| 22 | + {name = 'number_nullable', type = 'number', is_nullable = true}, |
| 23 | + {name = 'boolean_nonnull', type = 'boolean', is_nullable = false}, |
| 24 | + {name = 'boolean_nullable', type = 'boolean', is_nullable = true}, |
| 25 | + {name = 'string_nonnull', type = 'string', is_nullable = false}, |
| 26 | + {name = 'string_nullable', type = 'string', is_nullable = true}, |
| 27 | + {name = 'scalar_nonnull', type = 'scalar', is_nullable = false}, |
| 28 | + {name = 'scalar_nullable', type = 'scalar', is_nullable = true}, |
| 29 | + {name = 'array_nonnull', type = 'array', is_nullable = false}, |
| 30 | + {name = 'array_nullable', type = 'array', is_nullable = true}, |
| 31 | + {name = 'map_nonnull', type = 'map', is_nullable = false}, |
| 32 | + {name = 'map_nullable', type = 'map', is_nullable = true}, |
| 33 | + {name = 'any_nonnull', type = 'any', is_nullable = false}, |
| 34 | + {name = 'any_nullable', type = 'any', is_nullable = true}, |
| 35 | + }, |
| 36 | +} |
| 37 | + |
| 38 | +local primary_index = { |
| 39 | + type = 'HASH', |
| 40 | + unique = true, |
| 41 | + parts = { |
| 42 | + {path = 'string_nonnull', is_nullable = false, type = 'string'}, |
| 43 | + {path = 'unsigned_nonnull', is_nullable = false, type = 'unsigned'}, |
| 44 | + }, |
| 45 | + name = 'primary' |
| 46 | +} |
| 47 | + |
| 48 | +local bucket_id_idx = { |
| 49 | + type = 'TREE', |
| 50 | + unique = false, |
| 51 | + parts = {{path = 'bucket_id', type = 'unsigned', is_nullable = false}}, |
| 52 | + name = 'bucket_id' |
| 53 | +} |
| 54 | + |
| 55 | +local func_body_first = 'function() return 42 end' |
| 56 | +local func_body_second = 'function() return 24 end' |
| 57 | + |
| 58 | +local function rebuild_db(g) |
| 59 | + db.drop_all() |
| 60 | + |
| 61 | + g.space = table.deepcopy(test_space) |
| 62 | + table.insert(g.space.format, 1, { |
| 63 | + name = 'bucket_id', type = 'unsigned', is_nullable = false |
| 64 | + }) |
| 65 | + |
| 66 | + g.space.indexes = { |
| 67 | + table.deepcopy(primary_index), |
| 68 | + table.deepcopy(bucket_id_idx) |
| 69 | + } |
| 70 | + g.space.sharding_key = {'unsigned_nonnull', 'integer_nonnull'} |
| 71 | + g.schema = { |
| 72 | + spaces = { |
| 73 | + space = g.space, |
| 74 | + } |
| 75 | + } |
| 76 | +end |
| 77 | + |
| 78 | +local g = t.group() |
| 79 | +g.before_all(db.init) |
| 80 | +g.before_each(function() |
| 81 | + rebuild_db(g) |
| 82 | +end) |
| 83 | + |
| 84 | +function g.test_cache_processed_func_body() |
| 85 | + g.schema.spaces.space.sharding_func = { |
| 86 | + body = func_body_first |
| 87 | + } |
| 88 | + local ok, err = ddl.set_schema(g.schema) |
| 89 | + t.assert_equals(err, nil) |
| 90 | + t.assert_equals(ok, true) |
| 91 | + |
| 92 | + local res = cache.internal.get('space') |
| 93 | + t.assert(res) |
| 94 | + t.assert(res.processed) |
| 95 | + res = res.processed |
| 96 | + t.assert(type(res) == 'function') |
| 97 | + t.assert_equals(res(), 42) |
| 98 | +end |
| 99 | + |
| 100 | +function g.test_cache_processed_func_name() |
| 101 | + local sharding_func_name = 'sharding_func' |
| 102 | + rawset(_G, sharding_func_name, function(key) return key end) |
| 103 | + g.schema.spaces.space.sharding_func = sharding_func_name |
| 104 | + |
| 105 | + local ok, err = ddl.set_schema(g.schema) |
| 106 | + t.assert_equals(err, nil) |
| 107 | + t.assert_equals(ok, true) |
| 108 | + |
| 109 | + local res = cache.internal.get('space') |
| 110 | + t.assert(res) |
| 111 | + t.assert(res.processed) |
| 112 | + res = res.processed |
| 113 | + t.assert(type(res) == 'table') |
| 114 | + t.assert_equals(res[1], 'sharding_func') |
| 115 | + |
| 116 | + rawset(_G, sharding_func_name, nil) |
| 117 | +end |
| 118 | + |
| 119 | +function g.test_cache_schema_changed() |
| 120 | + g.schema.spaces.space.sharding_func = { |
| 121 | + body = func_body_first |
| 122 | + } |
| 123 | + local ok, err = ddl.set_schema(g.schema) |
| 124 | + t.assert_equals(err, nil) |
| 125 | + t.assert_equals(ok, true) |
| 126 | + |
| 127 | + local res = cache.internal.get('space') |
| 128 | + t.assert(res) |
| 129 | + t.assert(res.raw) |
| 130 | + res = res.raw |
| 131 | + t.assert_equals(res[SPACE_NAME_IDX], 'space') |
| 132 | + t.assert_equals(res[SHARD_FUNC_NAME_IDX], nil) |
| 133 | + t.assert_equals(res[SHARD_FUNC_BODY_IDX], func_body_first) |
| 134 | + |
| 135 | + rebuild_db(g) |
| 136 | + |
| 137 | + g.schema.spaces.space.sharding_func = { |
| 138 | + body = func_body_second |
| 139 | + } |
| 140 | + local ok, err = ddl.set_schema(g.schema) |
| 141 | + t.assert_equals(err, nil) |
| 142 | + t.assert_equals(ok, true) |
| 143 | + |
| 144 | + local res = cache.internal.get('space') |
| 145 | + t.assert(res) |
| 146 | + t.assert(res.raw) |
| 147 | + res = res.raw |
| 148 | + t.assert_equals(res[SPACE_NAME_IDX], 'space') |
| 149 | + t.assert_equals(res[SHARD_FUNC_NAME_IDX], nil) |
| 150 | + t.assert_equals(res[SHARD_FUNC_BODY_IDX], func_body_second) |
| 151 | +end |
| 152 | + |
| 153 | +function g.test_cache_space_updated() |
| 154 | + g.schema.spaces.space.sharding_func = { |
| 155 | + body = func_body_first |
| 156 | + } |
| 157 | + local ok, err = ddl.set_schema(g.schema) |
| 158 | + t.assert_equals(err, nil) |
| 159 | + t.assert_equals(ok, true) |
| 160 | + |
| 161 | + local res = cache.internal.get('space') |
| 162 | + t.assert(res) |
| 163 | + t.assert(res.raw) |
| 164 | + res = res.raw |
| 165 | + t.assert_equals(res[SPACE_NAME_IDX], 'space') |
| 166 | + t.assert_equals(res[SHARD_FUNC_NAME_IDX], nil) |
| 167 | + t.assert_equals(res[SHARD_FUNC_BODY_IDX], func_body_first) |
| 168 | + |
| 169 | + box.space._ddl_sharding_func |
| 170 | + :update({'space'}, {{'=', SHARD_FUNC_BODY_IDX, func_body_second}}) |
| 171 | + |
| 172 | + local res = cache.internal.get('space') |
| 173 | + t.assert(res) |
| 174 | + t.assert(res.raw) |
| 175 | + res = res.raw |
| 176 | + t.assert_equals(res[SPACE_NAME_IDX], 'space') |
| 177 | + t.assert_equals(res[SHARD_FUNC_NAME_IDX], nil) |
| 178 | + t.assert_equals(res[SHARD_FUNC_BODY_IDX], func_body_second) |
| 179 | +end |
0 commit comments