From 79ea1e90e83618c097fdef173a73b81e4cce910d Mon Sep 17 00:00:00 2001 From: mpenning Date: Sun, 22 Oct 2023 02:43:33 -0500 Subject: [PATCH] Fix CiscoRange() tests --- tests/test_Ccp_Util.py | 53 ++++++++++++++++++++++++-------------- tests/test_Models_Cisco.py | 20 +++++++------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/tests/test_Ccp_Util.py b/tests/test_Ccp_Util.py index e4cbbc8f..d1608fcc 100755 --- a/tests/test_Ccp_Util.py +++ b/tests/test_Ccp_Util.py @@ -856,23 +856,35 @@ def test_CiscoRange_18(): assert CiscoRange(uut_str).as_set(result_type=str) == result_correct -if False: - def test_CiscoRange_19(): - """Parse a string with a common prefix on all of the CiscoRange() inputs""" - result_correct = { - "interface Eth1/1", "interface Eth1/10", "interface Eth1/12", - "interface Eth1/13", "interface Eth1/14", "interface Eth1/15", - "interface Eth1/16", "interface Eth1/17", "interface Eth1/18", - "interface Eth1/19", "interface Eth1/20" - } - uut_str = "interface Eth1/1,interface Eth1/12-20,interface Eth1/16,interface Eth1/10" - assert isinstance(uut_str, str) - assert CiscoRange(uut_str).as_set(result_type=str) == result_correct +def test_CiscoRange_19(): + """Check that the exact results are correct for CiscoRange().as_set() with a redundant input ('Eth1/1')""" + result_correct = { + CiscoInterface("Eth1/1"), + CiscoInterface("Eth1/2"), + CiscoInterface("Eth1/3"), + CiscoInterface("Eth1/4"), + CiscoInterface("Eth1/5"), + CiscoInterface("Eth1/16"), + } + uut_str = "Eth1/1,Eth1/1-5,Eth1/16" + assert CiscoRange(uut_str).as_set() == result_correct + +def test_CiscoRange_20(): + """Check that the exact results are correct for CiscoRange().as_list() with a redundant input ('Eth1/1')""" + result_correct = [ + CiscoInterface("Eth1/1"), + CiscoInterface("Eth1/2"), + CiscoInterface("Eth1/3"), + CiscoInterface("Eth1/4"), + CiscoInterface("Eth1/5"), + CiscoInterface("Eth1/16"), + ] + uut_str = "Eth1/1,Eth1/1-5,Eth1/16" + assert CiscoRange(uut_str).as_list() == result_correct def test_CiscoRange_compressed_str_01(): """compressed_str test with a very basic set of vlan numbers""" uut_str = "1,2,911" - assert isinstance(uut_str, str) assert CiscoRange(uut_str, result_type=str).as_compressed_str() == "1,2,911" assert "2" in CiscoRange(uut_str).as_compressed_str() assert "911" in CiscoRange(uut_str).as_compressed_str() @@ -880,17 +892,20 @@ def test_CiscoRange_compressed_str_01(): def test_CiscoRange_compressed_str_02(): """compressed_str test with vlan number ranges""" uut_str = "1,2, 3, 6, 7, 8 , 9, 911" - assert isinstance(uut_str, str) assert CiscoRange(uut_str, result_type=str).as_compressed_str() == "1-3,6-9,911" -def test_CiscoRange_contains(): +def test_CiscoRange_contains_01(): + """Check that the exact results are correct that a CiscoRange() contains an input""" + uut_str = "Ethernet1/1-20" + # Ethernet1/5 is in CiscoRange("Ethernet1/1-20")... + assert CiscoInterface("Ethernet1/5") in CiscoRange(uut_str) + +def test_CiscoRange_contains_02(): + """Check that the exact results are correct that a CiscoRange() does not contain an input""" uut_str = "Ethernet1/1-20" - assert isinstance(uut_str, str) # Ethernet1/5 is in CiscoRange("Ethernet1/1-20")... - assert "1/1" in CiscoRange(uut_str).as_compressed_str() - assert "20" in CiscoRange(uut_str).as_compressed_str() - assert "Ethernet1/5" in CiscoRange(uut_str).as_list() + assert (CiscoInterface("Ethernet1/48") in CiscoRange(uut_str)) is False #pragma warning restore S1192 #pragma warning restore S1313 diff --git a/tests/test_Models_Cisco.py b/tests/test_Models_Cisco.py index c0c9f036..988792e0 100755 --- a/tests/test_Models_Cisco.py +++ b/tests/test_Models_Cisco.py @@ -5,9 +5,9 @@ sys.path.insert(0, "..") +from ciscoconfparse.ccp_util import IPv4Obj, CiscoRange, CiscoInterface from ciscoconfparse.errors import DynamicAddressException from ciscoconfparse.ciscoconfparse import CiscoConfParse -from ciscoconfparse.ccp_util import IPv4Obj, CiscoRange import pytest from loguru import logger @@ -281,8 +281,8 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_01(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert len(intf_obj.trunk_vlans_allowed.as_set(result_type=str)) == len(range(1, 4095)) - assert intf_obj.trunk_vlans_allowed.as_set(result_type=str) == {str(ii) for ii in range(1, 4095)} + assert len(intf_obj.trunk_vlans_allowed.as_set(result_type=int)) == len(range(1, 4095)) + assert intf_obj.trunk_vlans_allowed.as_list(result_type=int) == list(range(1, 4095)) def testVal_IOSIntfLine_trunk_vlan_allowed_02(): @@ -296,7 +296,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_02(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed.as_set(result_type=str) == {"2", "4", "6", "911"} + assert intf_obj.trunk_vlans_allowed.as_set(result_type=int) == {2, 4, 6, 911} def testVal_IOSIntfLine_trunk_vlan_allowed_03(): @@ -311,7 +311,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_03(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed.as_set(result_type=str) == {"4", "6", "911"} + assert intf_obj.trunk_vlans_allowed.as_set(result_type=int) == {4, 6, 911} def testVal_IOSIntfLine_trunk_vlan_allowed_04(): @@ -326,7 +326,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_04(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed.as_set(result_type=str) == {"1"} + assert intf_obj.trunk_vlans_allowed.as_set(result_type=int) == {1} def testVal_IOSIntfLine_trunk_vlan_allowed_05(): @@ -341,7 +341,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_05(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed.as_set(result_type=str) == {"1",} + assert intf_obj.trunk_vlans_allowed.as_set(result_type=int) == {1,} def testVal_IOSIntfLine_trunk_vlan_allowed_06(): @@ -357,7 +357,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_06(): ] cfg = CiscoConfParse(lines, factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed == CiscoRange(text="2-5,17-19", result_type=str) + assert intf_obj.trunk_vlans_allowed.as_set(result_type=int) == {2,3,4,5,17,18,19} def testVal_IOSIntfLine_trunk_vlan_allowed_07(): @@ -374,7 +374,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_07(): """ cfg = CiscoConfParse(config.splitlines(), factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed == CiscoRange("2-19,21-4094", result_type=str) + assert intf_obj.trunk_vlans_allowed == CiscoRange("2-19,21-4094", result_type=int) def testVal_IOSIntfLine_trunk_vlan_allowed_08(): @@ -387,7 +387,7 @@ def testVal_IOSIntfLine_trunk_vlan_allowed_08(): """ cfg = CiscoConfParse(config.splitlines(), factory=True) intf_obj = cfg.find_objects("^interface")[0] - assert intf_obj.trunk_vlans_allowed == CiscoRange(text="", result_type=str) + assert intf_obj.trunk_vlans_allowed == CiscoRange(text="", result_type=int) def testVal_IOSIntfLine_abbvs(parse_c03_factory):