TT#5034 fix avp:clean()

* fix pv mock to align kamailio behavior with pv_unset() for avps.
  It removes only the first value not all, only when using '[*]'
  pv_unset("$avp(name)") != pv_unset("$(avp(name)[*])")
  pv_unset("$avp(name)") == pv_unset("$(avp(name)[0])")
  pv_unset("$(avp(name)[1])") == pv_unset("$(avp(name)[0])")

Change-Id: I1aab2150d288da546b144388adb7b2f87ecfb069
changes/63/9163/2
Victor Seva 9 years ago
parent fd7da8a36e
commit b09db92994

@ -95,12 +95,16 @@ local pvMock = {
local patterns = {
'%$avp%(([%w_]+)%)$',
'%$%(avp%(([%w_]+)%)%)$',
'%$%(avp%(([%w_]+)%)%[%*%]%)$'
'%$%(avp%(([%w_]+)%)%[%*%]%)$',
'%$%(avp%(([%w_]+)%)%[(%d+)%]%)$',
}
_id = t._clean_id(id)
for _,v in pairs(patterns) do
for i in string.gmatch(_id, v) do
return { id=i, clean=(v==patterns[3]), type='avp' }
for i, indx in string.gmatch(_id, v) do
if _ == 4 then
indx = tonumber(indx)
end
return { id=i, indx=indx, clean=(v==patterns[3]), type='avp' }
end
end
end
@ -218,11 +222,15 @@ local pvMock = {
end
elseif result.type == 'avp' then
if t.vars[result.private_id] then
local l = t.vars[result.private_id]:list()
if not result.indx then
result.indx = 0
end
if result.clean then
return l
return t.vars[result.private_id]:list()
else
return l[1]
if t.vars[result.private_id][result.indx] then
return t.vars[result.private_id][result.indx]
end
end
end
elseif result.type == 'hdr' then
@ -348,7 +356,13 @@ local pvMock = {
t.vars[result.private_id][result.indx][result.key] = nil
end
elseif result.type == 'avp' then
t.vars[result.private_id] = nil
if result.clean then
t.vars[result.private_id] = nil
return
end
if t.vars[result.private_id] then
t.vars[result.private_id]:pop()
end
elseif result.type == 'var' or result.type == 'dlg_var' then
t.vars[result.private_id] = nil
end

@ -27,7 +27,10 @@ local NGCPAvp_MT = {
}
function NGCPAvp:new(id)
local t = { id = "$avp(s:" .. id .. ")" }
local t = {
id = "$avp(s:" .. id .. ")",
id_all = "$(avp(s:" .. id .. ")[*])",
}
NGCPAvp_MT.__call = function(s, value)
if not value then
return sr.pv.get(s.id)
@ -44,7 +47,7 @@ local NGCPAvp_MT = {
end
end
function t.all()
return sr.pv.get("$(avp(" .. id .. ")[*])")
return sr.pv.get(t.id_all)
end
NGCPAvp_MT.__tostring = function(s)
local value = sr.pv.get(s.id)
@ -63,7 +66,7 @@ local NGCPAvp_MT = {
function NGCPAvp:del(value)
local values = self.all()
if not values or not value then return end
sr.pv.unset(self.id)
sr.pv.unset(self.id_all)
for i = #values, 1, -1 do
if values[i] ~= value then
self(values[i])
@ -72,7 +75,7 @@ local NGCPAvp_MT = {
end
function NGCPAvp:clean()
sr.pv.unset(self.id)
sr.pv.unset(self.id_all)
end
-- class
return NGCPAvp

@ -166,8 +166,8 @@ TestPVMock = {}
local result = self.pv._is_avp("$avp(id2_f)")
assertTrue(result)
assertEquals(result.type, 'avp')
--print(table.tostring(result))
assertEquals(result.id, 'id2_f')
assertIsNil(result.indx)
assertFalse(result.clean)
end
@ -175,8 +175,8 @@ TestPVMock = {}
local result = self.pv._is_avp("$(avp(s:id))")
assertTrue(result)
assertEquals(result.type, 'avp')
--print(table.tostring(result))
assertEquals(result.id, 'id')
assertIsNil(result.indx)
assertFalse(result.clean)
end
@ -184,8 +184,8 @@ TestPVMock = {}
local result = self.pv._is_avp("$(avp(id))")
assertTrue(result)
assertEquals(result.type, 'avp')
--print(table.tostring(result))
assertEquals(result.id, 'id')
assertIsNil(result.indx)
assertFalse(result.clean)
end
@ -194,10 +194,19 @@ TestPVMock = {}
assertTrue(result)
assertEquals(result.type, 'avp')
assertEquals(result.id, 'id')
--print(table.tostring(result))
assertIsNil(result.indx)
assertTrue(result.clean)
end
function TestPVMock:test_is_avp_simple4()
local result = self.pv._is_avp("$(avp(s:id)[1])")
assertTrue(result)
assertEquals(result.type, 'avp')
assertEquals(result.id, 'id')
assertEquals(result.indx, 1)
assertFalse(result.clean)
end
function TestPVMock:test_is_var_simple()
local result = self.pv._is_var("$var(id)")
assertTrue(result)
@ -326,6 +335,11 @@ TestPVMock = {}
assertEquals(self.pv.get("$avp(s:hithere)"), 1)
end
function TestPVMock:test_avp_get_simple3()
self.pv.seti("$avp(s:hithere)", 1)
assertEquals(self.pv.get("$(avp(s:hithere)[0])"), 1)
end
function TestPVMock:test_avp_get()
local vals = {1,2,3}
for i=1,#vals do
@ -342,6 +356,19 @@ TestPVMock = {}
end
end
function TestPVMock:test_avp_get_2()
local vals = {1,2,3}
for i=1,#vals do
self.pv.seti("$avp(s:hithere)", vals[i])
end
local l = "$(avp(s:hithere)[%d])"
local v = 1
for i=#vals,1,-1 do
assertEquals(self.pv.get(string.format(l, i-1)), vals[v])
v = v + 1
end
end
function TestPVMock:test_avp_get_all()
self.pv.sets("$avp(s:hithere)", "value")
assertEquals(self.pv.get("$avp(hithere)"), "value")
@ -385,6 +412,26 @@ TestPVMock = {}
assertEquals(self.pv.get("$avp(s:hithere)"), nil)
end
function TestPVMock:test_unset_avp_2()
self.pv.sets("$avp(s:hithere)", "value")
self.pv.sets("$avp(s:hithere)", "other")
assertEquals(self.pv.get("$avp(hithere)"), "other")
self.pv.unset("$avp(s:hithere)")
assertEquals(self.pv.get("$avp(hithere)"), "value")
self.pv.unset("$(avp(s:hithere)[*])")
assertEquals(self.pv.get("$avp(s:hithere)"), nil)
end
function TestPVMock:test_unset_avp_3()
self.pv.sets("$avp(s:hithere)", "value")
self.pv.sets("$avp(s:hithere)", "other")
assertEquals(self.pv.get("$(avp(hithere)[0])"), "other")
assertEquals(self.pv.get("$(avp(hithere)[1])"), "value")
-- same behavior than kamailio!!
self.pv.unset("$(avp(s:hithere)[1])")
assertEquals(self.pv.get("$(avp(hithere)[*])"), {"value"})
end
function TestPVMock:test_unset_xavp()
self.pv.sets("$xavp(g=>t)", "value")
assertEquals(self.pv.get("$xavp(g[0]=>t)"), "value")

Loading…
Cancel
Save