-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #3520 #3617 Co-authored-by: Andrey Aksenov <38073144+andreyaksenov@users.noreply.github.com>
- Loading branch information
1 parent
9e72c46
commit 163b69c
Showing
6 changed files
with
228 additions
and
12 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
doc/code_snippets/test/default_values/default_functions_test.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
local fio = require('fio') | ||
local server = require('luatest.server') | ||
local t = require('luatest') | ||
local g = t.group() | ||
g.before_each(function(cg) | ||
cg.server = server:new { | ||
box_cfg = {}, | ||
workdir = fio.cwd() .. '/tmp' | ||
} | ||
cg.server:start() | ||
end) | ||
|
||
g.after_each(function(cg) | ||
cg.server:drop() | ||
fio.rmtree(cg.server.workdir) | ||
end) | ||
|
||
g.test_default_func = function(cg) | ||
cg.server:exec(function() | ||
|
||
-- create_no_arg_function_start | ||
box.schema.func.create('current_year', { | ||
language = 'Lua', | ||
body = "function() return require('datetime').now().year end" | ||
}) | ||
-- create_no_arg_function_end | ||
|
||
-- format_space_default_func_start | ||
local books = box.schema.space.create('books') | ||
books:format({ | ||
{ name = 'id', type = 'unsigned' }, | ||
{ name = 'isbn', type = 'string' }, | ||
{ name = 'title', type = 'string' }, | ||
{ name = 'year', type = 'unsigned', default_func = 'current_year' } | ||
}) | ||
books:create_index('primary', { parts = { 1 } }) | ||
-- format_space_default_func_end | ||
|
||
-- insert_ok_start | ||
books:insert { 1, '978000', 'Thinking in Java' } | ||
-- insert_ok_end | ||
|
||
-- create_arg_function_start | ||
box.schema.func.create('randomize', { | ||
language = 'Lua', | ||
body = "function(limit) return math.random(limit.min, limit.max) end" | ||
}) | ||
-- create_arg_function_end | ||
|
||
-- reformat_space_start | ||
books:format({ | ||
{ name = 'id', type = 'unsigned', default_func= 'randomize', default = {min = 0, max = 1000} }, | ||
{ name = 'isbn', type = 'string' }, | ||
{ name = 'title', type = 'string' }, | ||
{ name = 'year', type = 'unsigned', default_func = 'current_year' } | ||
}) | ||
-- reformat_space_end | ||
|
||
books:insert { nil, '978001', 'How to code in Go' } | ||
|
||
-- Tests -- | ||
t.assert_equals(books:count(), 2) | ||
t.assert_equals(books:get(1), { 1, '978000','Thinking in Java', 2024 }) | ||
end) | ||
end |
39 changes: 39 additions & 0 deletions
39
doc/code_snippets/test/default_values/explicit_default_test.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
local fio = require('fio') | ||
local server = require('luatest.server') | ||
local t = require('luatest') | ||
local g = t.group() | ||
g.before_each(function(cg) | ||
cg.server = server:new { | ||
box_cfg = {}, | ||
workdir = fio.cwd() .. '/tmp' | ||
} | ||
cg.server:start() | ||
end) | ||
|
||
g.after_each(function(cg) | ||
cg.server:drop() | ||
fio.rmtree(cg.server.workdir) | ||
end) | ||
|
||
g.test_default_values = function(cg) | ||
cg.server:exec(function() | ||
-- configure_space_start | ||
local books = box.schema.space.create('books') | ||
books:format({ | ||
{ name = 'id', type = 'number' }, | ||
{ name = 'name', type = 'string' }, | ||
{ name = 'year', type = 'number', default = 2024 }, | ||
}) | ||
books:create_index('primary', { parts = { 1 } }) | ||
-- configure_space_end | ||
|
||
-- insert_ok_start | ||
books:insert { 1, 'Thinking in Java' } | ||
books:insert { 2, 'How to code in Go', nil } | ||
-- insert_ok_end | ||
|
||
-- Tests -- | ||
t.assert_equals(books:count(), 2) | ||
t.assert_equals(books:get(1), { 1, 'Thinking in Java', 2024 }) | ||
end) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters