|
| 1 | +local fio = require('fio') |
| 2 | +local server = require('luatest.server') |
| 3 | +local t = require('luatest') |
| 4 | +local g = t.group() |
| 5 | +g.before_each(function(cg) |
| 6 | + cg.server = server:new { |
| 7 | + box_cfg = {}, |
| 8 | + workdir = fio.cwd() .. '/tmp' |
| 9 | + } |
| 10 | + cg.server:start() |
| 11 | +end) |
| 12 | + |
| 13 | +g.after_each(function(cg) |
| 14 | + cg.server:stop() |
| 15 | + cg.server:drop() |
| 16 | +end) |
| 17 | + |
| 18 | +g.test_constraints = function(cg) |
| 19 | + cg.server:exec(function() |
| 20 | + -- create_constraint_start |
| 21 | + box.schema.func.create('check_person', { |
| 22 | + language = 'SQL_EXPR', |
| 23 | + is_deterministic = true, |
| 24 | + body = [["age" > 21 AND "name" != 'Admin']] |
| 25 | + }) |
| 26 | + -- create_constraint_end |
| 27 | + |
| 28 | + -- configure_space_start |
| 29 | + local customers = box.schema.space.create('customers', { constraint = 'check_person' }) |
| 30 | + customers:format({ |
| 31 | + { name = 'id', type = 'number' }, |
| 32 | + { name = 'name', type = 'string' }, |
| 33 | + { name = 'age', type = 'number' }, |
| 34 | + }) |
| 35 | + customers:create_index('primary', { parts = { 1 } }) |
| 36 | + -- configure_space_end |
| 37 | + |
| 38 | + -- insert_ok_start |
| 39 | + customers:insert { 1, "Alice", 30 } |
| 40 | + -- insert_ok_end |
| 41 | + |
| 42 | + local _, age_err = pcall(function() |
| 43 | + -- insert_age_error_start |
| 44 | + customers:insert { 2, "Bob", 18 } |
| 45 | + -- error: Check constraint 'check_person' failed for tuple |
| 46 | + -- insert_age_error_end |
| 47 | + end) |
| 48 | + |
| 49 | + local _, name_err = pcall(function() |
| 50 | + -- insert_name_error_start |
| 51 | + customers:insert { 3, "Admin", 25 } |
| 52 | + -- error: Check constraint 'check_person' failed for tuple |
| 53 | + -- insert_name_error_end |
| 54 | + end) |
| 55 | + |
| 56 | + -- Tests -- |
| 57 | + t.assert_equals(customers:count(), 1) |
| 58 | + t.assert_equals(customers:get(1), { 1, "Alice", 30 }) |
| 59 | + t.assert_equals(age_err:unpack().message, 'Check constraint \'check_person\' failed for tuple') |
| 60 | + t.assert_equals(name_err:unpack().message, 'Check constraint \'check_person\' failed for tuple') |
| 61 | + end) |
| 62 | +end |
0 commit comments