Skip to content

Commit a54597b

Browse files
committed
Refactor tests
Add helpers.insert_objects and helpers.get_by_idxs functions
1 parent 5d090de commit a54597b

File tree

4 files changed

+93
-103
lines changed

4 files changed

+93
-103
lines changed

test/entrypoint/srv_select.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ package.preload['customers-storage'] = function()
2424
if_not_exists = true,
2525
engine = engine,
2626
})
27-
--primary index
27+
-- primary index
2828
customers_space:create_index('id_index', {
2929
parts = { {field = 'id'} },
3030
if_not_exists = true,
@@ -39,7 +39,7 @@ package.preload['customers-storage'] = function()
3939
unique = false,
4040
if_not_exists = true,
4141
})
42-
--indexes with same names as fields
42+
-- indexes with same names as fields
4343
customers_space:create_index('age', {
4444
parts = { {field = 'age'} },
4545
unique = false,

test/helper.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
require('strict').on()
22

3+
local t = require('luatest')
4+
35
local log = require('log')
46
local checks = require('checks')
57
local digest = require('digest')
68
local fio = require('fio')
79

10+
local crud = require('crud')
11+
812
if os.getenv('DEV') == nil then
913
os.setenv('DEV', 'ON')
1014
end
@@ -76,4 +80,29 @@ function helpers.box_cfg()
7680
fio.rmtree(tempdir)
7781
end
7882

83+
function helpers.insert_objects(g, space_name, objects)
84+
local inserted_objects = {}
85+
86+
for _, obj in ipairs(objects) do
87+
local result, err = g.cluster.main_server.net_box:call('crud.insert_object', {space_name, obj})
88+
89+
t.assert_equals(err, nil)
90+
91+
local objects, err = crud.unflatten_rows(result.rows, result.metadata)
92+
t.assert_equals(err, nil)
93+
t.assert_equals(#objects, 1)
94+
table.insert(inserted_objects, objects[1])
95+
end
96+
97+
return inserted_objects
98+
end
99+
100+
function helpers.get_objects_by_idxs(objects, idxs)
101+
local results = {}
102+
for _, idx in ipairs(idxs) do
103+
table.insert(results, objects[idx])
104+
end
105+
return results
106+
end
107+
79108
return helpers

test/integration/pairs_test.lua

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ local t = require('luatest')
44
local g_memtx = t.group('pairs_memtx')
55
local g_vinyl = t.group('pairs_vinyl')
66

7-
local crud = require('crud')
87
local crud_utils = require('crud.common.utils')
98

109
local helpers = require('test.helper')
@@ -84,32 +83,8 @@ local function add(name, fn)
8483
g_vinyl[name] = fn
8584
end
8685

87-
local function insert_customers(g, customers)
88-
local inserted_objects = {}
89-
90-
for _, customer in ipairs(customers) do
91-
local result, err = g.cluster.main_server.net_box:call('crud.insert_object', {'customers', customer})
92-
t.assert_equals(err, nil)
93-
94-
local objects, err = crud.unflatten_rows(result.rows, result.metadata)
95-
t.assert_equals(err, nil)
96-
t.assert_equals(#objects, 1)
97-
table.insert(inserted_objects, objects[1])
98-
end
99-
100-
return inserted_objects
101-
end
102-
103-
local function get_by_ids(customers, ids)
104-
local results = {}
105-
for _, id in ipairs(ids) do
106-
table.insert(results, customers[id])
107-
end
108-
return results
109-
end
110-
11186
add('test_pairs_no_conditions', function(g)
112-
local customers = insert_customers(g, {
87+
local customers = helpers.insert_objects(g, 'customers', {
11388
{
11489
id = 1, name = "Elizabeth", last_name = "Jackson",
11590
age = 12, city = "New York",
@@ -190,7 +165,7 @@ add('test_pairs_no_conditions', function(g)
190165
]], {after})
191166

192167
t.assert_equals(err, nil)
193-
t.assert_equals(objects, get_by_ids(customers, {3, 4}))
168+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {3, 4}))
194169

195170
-- after obj 4 (last)
196171
local after = crud_utils.flatten(customers[4], g.space_format)
@@ -212,7 +187,7 @@ add('test_pairs_no_conditions', function(g)
212187
end)
213188

214189
add('test_ge_condition_with_index', function(g)
215-
local customers = insert_customers(g, {
190+
local customers = helpers.insert_objects(g, 'customers', {
216191
{
217192
id = 1, name = "Elizabeth", last_name = "Jackson",
218193
age = 12, city = "New York",
@@ -249,7 +224,7 @@ add('test_ge_condition_with_index', function(g)
249224
]], {conditions})
250225

251226
t.assert_equals(err, nil)
252-
t.assert_equals(objects, get_by_ids(customers, {3, 2, 4})) -- in age order
227+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {3, 2, 4})) -- in age order
253228

254229
-- after obj 3
255230
local after = crud_utils.flatten(customers[3], g.space_format)
@@ -267,11 +242,11 @@ add('test_ge_condition_with_index', function(g)
267242
]], {conditions, after})
268243

269244
t.assert_equals(err, nil)
270-
t.assert_equals(objects, get_by_ids(customers, {2, 4})) -- in age order
245+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {2, 4})) -- in age order
271246
end)
272247

273248
add('test_le_condition_with_index', function(g)
274-
local customers = insert_customers(g, {
249+
local customers = helpers.insert_objects(g, 'customers', {
275250
{
276251
id = 1, name = "Elizabeth", last_name = "Jackson",
277252
age = 12, city = "New York",
@@ -308,7 +283,7 @@ add('test_le_condition_with_index', function(g)
308283
]], {conditions})
309284

310285
t.assert_equals(err, nil)
311-
t.assert_equals(objects, get_by_ids(customers, {3, 1})) -- in age order
286+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {3, 1})) -- in age order
312287

313288
-- after obj 3
314289
local after = crud_utils.flatten(customers[3], g.space_format)
@@ -326,11 +301,11 @@ add('test_le_condition_with_index', function(g)
326301
]], {conditions, after})
327302

328303
t.assert_equals(err, nil)
329-
t.assert_equals(objects, get_by_ids(customers, {1})) -- in age order
304+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {1})) -- in age order
330305
end)
331306

332307
add('test_negative_first', function(g)
333-
local customers = insert_customers(g,{
308+
local customers = helpers.insert_objects(g, 'customers',{
334309
{
335310
id = 1, name = "Elizabeth", last_name = "Jackson",
336311
age = 12, city = "New York",

0 commit comments

Comments
 (0)