Помогите исправить тест в lua
Я новичок в lua. Извините за много текста, но мне очень нужна помощь в доработке теста.
Есть спейс "company". Внутри "information" map. Внутри этой map находится объект "job" и массив объектов "users". Массив "users" состоит из 2 объектов. Каждый объект имеет 4 поля.
"company": {
"company_id" : 1,
"type" : "01",
"number" : "YES",
"information":
{
"job":
{
"job_name": "N",
"job_address": 1670392687114
},
"users":
[
{
"id": 1,
"name": "Alex",
"rate": 4,
"address": "bla bla bla"
},
{
"id": 2,
"name": "Jenifer",
"rate": 5,
"address": "bla bla bla"
}
]
}
}
Есть уже написанная миграция для переименования полей в спейсе и добавления нового поля при условии:
local space_name = 'company'
local space = box.space[space_name]
local key_parts = space.index.primary.parts
local updated = 0
local counter = 0
local isUpdateType = {
["01"] = true,
["02"] = true,
["03"] = true,
["04"] = true,
}
for _, tuple in space:pairs() do
if tuple.information ~= nil and tuple.information.users ~= nil then
local information = tuple.information
local users = tuple.information.users
-- rename fields and assign nil to the old fields
for _, attr in pairs(users) do
attr.user_rate = attr.rate
attr.rate = nil
attr.user_address = attr.address
attr.address = nil
end
-- update data according to the condition
if isUpdateType[tuple.type] and tuple.number == "YES" then
for _, attr in pairs(users) do
attr.status = "UPDATED"
end
end
local key = {}
for _, part in ipairs(key_parts) do table.insert(key, tuple[part.fieldno]) end
space:update(key, { {'=', 'information', information} })
updated = updated + 1
end
counter = counter + 1
if counter % 1000 == 0 then
fiber.yield()
end
if counter % 10000 == 0 then
log.info('%s: %s tuples have been checked in space %s', migration, counter, space_name)
end
end log.info('%s: %s tuples have been updated from space %s', migration, updated, space_name)
Что мы здесь делаем:
- переименовываем поля и присвоить nil старым полям
- обновляем данные по условию: если type = "01" или "02" или "03" или "04" и number = "YES", то добавить новое поле status = "UPDATED" в каждый элемент массива "users".
Я написал часть теста, которая проверяет, есть ли спейс "company" и содержит ли оно map "information" и содержит ли он массив "users", в котором поля из "rate" и "address" были переименованы в "user_rate" и "user_address".
Я не уверен, что правильно написал тест. Также я не совсем понимаю, как реализовать вторую часть теста для второго условия миграции: добавление нового поля status = "UPDATED" в каждый элемент массива "users" при определенном условии.
local space_name = 'company'
g.before_all(function()
utils.init_config_loader(helper)
utils.apply_migrations_before(helper, '005')
end)
g.after_all(function()
utils.cleanup(helper)
end)
g.check_if_exist_space = function()
local operation = 'upsert'
utils.data_into_space(helper, space_name, 4, operation)
utils.apply_migrations(helper, { '005_update_company.lua' },
{ './migrations/005_update_company.lua' })
for index = 1, 4 do
local res, err = helper.cluster.main_server.net_box:eval([[
local router_helper = require('app.utils.router_helper')
local space_name, key, shard_value = ...
return crud.get(space_name, key, { fields = {'information'}})
]], { space_name, 'company_id' .. index })
t.assert_equals(err, nil)
t.assert_equals(#res.rows, 1)
t.assert_equals(#res.rows[1], 1)
local information = res.rows[1][1]
t.assert_equals(#information, 1)
t.assert_equals(information.users[1].rate, 'user_rate' .. index)
t.assert_equals(information.users[1].address, 'user_address' .. index)
end
end
Может кто-нибудь помочь/подсказать?