TT#156754 Add the new function NGCPDlgCounters:is_in_set_regex

The new function behaves like NGCPDlgCounters:is_in_set but
it uses a regex match to see if the key is contained in the list

Additionally added the helper function 'contains_regex' in the
utils.lua

* ignore vscode files

Change-Id: Iee89f5298ced10c2a9cdf7ebc19a4c0945b8cc9d
(cherry picked from commit 51454fd2b5)
mr9.5.5
Marco Capetta 3 years ago
parent 0a766dcf4a
commit 20804f1e96

1
.gitignore vendored

@ -2,3 +2,4 @@
kamailio
reports
*.pyc
.vscode/*

@ -113,6 +113,14 @@ end
return utable.contains(res, key);
end
function NGCPDlgCounters:is_in_set_regex(callid, key)
if not self._test_connection(self.pair) then
self.pair = self._connect(self.config.pair);
end
local res = self.pair:lrange(callid, 0, -1);
return utable.contains_regex(res, key);
end
function NGCPDlgCounters:set(callid, key)
if not self._test_connection(self.central) then
self.central = self._connect(self.config.central);

@ -112,6 +112,17 @@ function ut.contains(t, element)
return false
end
function ut.contains_regex(t, element)
if t then
for _, value in pairs(t) do
if string.match(value, element) then
return true
end
end --for
end --if
return false
end
-- add if element is not in table
function ut.add(t, element)
if not ut.contains(t, element) then

@ -230,6 +230,32 @@ TestNGCPDlgCnt = {} --class
lu.assertTrue(res)
end
function TestNGCPDlgCnt:test_is_in_set_regex_ok()
self.pair:ping() ;mc :returns(true)
self.pair:lrange("callid0", 0, -1) ;mc :returns({"user:whatever", "fake", "jojo"})
mc:replay()
local res = self.dlg:is_in_set_regex("callid0", "^user:")
mc:verify()
lu.assertIs(self.dlg.central, self.central)
lu.assertIs(self.dlg.pair, self.pair)
lu.assertTrue(res)
end
function TestNGCPDlgCnt:test_is_in_set_regex_fail()
self.pair:ping() ;mc :returns(true)
self.pair:lrange("callid0", 0, -1) ;mc :returns({"user:whatever", "fake", "jojo"})
mc:replay()
local res = self.dlg:is_in_set_regex("callid0", "^ser:")
mc:verify()
lu.assertIs(self.dlg.central, self.central)
lu.assertIs(self.dlg.pair, self.pair)
lu.assertFalse(res)
end
function TestNGCPDlgCnt:test_del_key()
self.pair:ping() ;mc :returns(true)
self.pair:lrem("callid0", 1, "key1") ;mc :returns(1)

@ -34,6 +34,9 @@ TestUtils = {}
cone = self.simple_list,
ctwo = self.simple_hash
}
self.simple_list_str = {
"uno", "dos", "tres", "user:1", "user:2",
}
end
function TestUtils:test_table_deepcopy()
@ -49,6 +52,14 @@ TestUtils = {}
lu.assertError(utils.table.contains, "hola",1)
end
function TestUtils:test_table_contains_regex()
lu.assertTrue(utils.table.contains_regex(self.simple_list_str, '^u.+$'))
lu.assertFalse(utils.table.contains_regex(self.simple_list_str, '.*q.*'))
lu.assertFalse(utils.table.contains_regex(nil))
lu.assertTrue(utils.table.contains_regex(self.simple_list_str, '^user:.+$'))
lu.assertError(utils.table.contains_regex, "hola",1)
end
function TestUtils:test_table_add()
lu.assertEquals(self.simple_list, {1,2,3})
utils.table.add(self.simple_list, 1)

Loading…
Cancel
Save