diff mbox series

[ovs-dev,v3,15/18] python: add unit tests for ListParser

Message ID 20220311152128.3988946-16-amorenoz@redhat.com
State Changes Requested
Headers show
Series python: add flow parsing library | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/intel-ovs-compilation success test: success
ovsrobot/github-robot-_Build_and_Test success github build: passed

Commit Message

Adrian Moreno March 11, 2022, 3:21 p.m. UTC
Add unit tests for ListParser class.

Acked-by: Eelco Chaudron <echaudro@redhat.com>
Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 python/automake.mk            |  3 +-
 python/ovs/tests/test_list.py | 66 +++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 python/ovs/tests/test_list.py
diff mbox series

Patch

diff --git a/python/automake.mk b/python/automake.mk
index 43a27d3cb..559ec3019 100644
--- a/python/automake.mk
+++ b/python/automake.mk
@@ -54,7 +54,8 @@  ovs_pyfiles = \
 	python/ovs/winutils.py
 
 ovs_pytests = \
-	python/ovs/tests/test_kv.py
+	python/ovs/tests/test_kv.py \
+	python/ovs/tests/test_list.py
 
 # These python files are used at build time but not runtime,
 # so they are not installed.
diff --git a/python/ovs/tests/test_list.py b/python/ovs/tests/test_list.py
new file mode 100644
index 000000000..e5139869f
--- /dev/null
+++ b/python/ovs/tests/test_list.py
@@ -0,0 +1,66 @@ 
+import pytest
+
+from ovs.flows.list import ListParser, ListDecoders
+from ovs.flows.kv import KeyValue
+
+
+@pytest.mark.parametrize(
+    "input_data,expected",
+    [
+        (
+            ("field1,field2,3,nested:value", None, [","]),
+            [
+                KeyValue("elem_0", "field1"),
+                KeyValue("elem_1", "field2"),
+                KeyValue("elem_2", 3),
+                KeyValue("elem_3", "nested:value"),
+            ],
+        ),
+        (
+            (
+                "field1,field2,3,nested:value",
+                ListDecoders(
+                    [
+                        ("key1", str),
+                        ("key2", str),
+                        ("key3", int),
+                        ("key4", lambda x: x.split(":"), [","]),
+                    ]
+                ),
+                [","],
+            ),
+            [
+                KeyValue("key1", "field1"),
+                KeyValue("key2", "field2"),
+                KeyValue("key3", 3),
+                KeyValue("key4", ["nested", "value"]),
+            ],
+        ),
+        (
+            ("field1:field2:3", None, [":"]),
+            [
+                KeyValue("elem_0", "field1"),
+                KeyValue("elem_1", "field2"),
+                KeyValue("elem_2", 3),
+            ],
+        ),
+    ],
+)
+def test_kv_parser(input_data, expected):
+    input_string = input_data[0]
+    decoders = input_data[1]
+    delims = input_data[2]
+    tparser = ListParser(input_string, decoders, delims)
+    tparser.parse()
+    result = tparser.kv()
+    assert len(expected) == len(result)
+    for i in range(0, len(result)):
+        assert result[i].key == expected[i].key
+        assert result[i].value == expected[i].value
+        kpos = result[i].meta.kpos
+        kstr = result[i].meta.kstring
+        vpos = result[i].meta.vpos
+        vstr = result[i].meta.vstring
+        assert input_string[kpos : kpos + len(kstr)] == kstr
+        if vpos != -1:
+            assert input_string[vpos : vpos + len(vstr)] == vstr