Skip to content

Commit 6298baa

Browse files
committed
update on checks
1 parent 9943291 commit 6298baa

6 files changed

Lines changed: 76 additions & 78 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ build: clean
5151
publish: build
5252
uv publish
5353

54-
check-all: format lint type-check test
54+
check-all: format type-check test
5555
@echo "All checks passed!"

shconfparser/tree_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ def _convert_to_dict(self, tree: List[Dict[str, Any]], level: int = 0) -> TreeDa
6767
return temp_dict
6868

6969
if next_node["level"] == level:
70-
temp_dict[node["key"]] = None
70+
temp_dict[node["key"]] = ""
7171
elif next_node["level"] > level:
7272
temp_dict[node["key"]] = self._convert_to_dict(
7373
tree[i + 1 :], level=next_node["level"]
7474
)
7575
else:
76-
temp_dict[node["key"]] = None
76+
temp_dict[node["key"]] = ""
7777
return temp_dict
7878
return temp_dict
7979

tests/test_parser.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,73 @@
1+
from os import path
2+
13
import pytest
2-
import collections
34

4-
from os import path
5-
from shconfparser.reader import Reader
6-
from shconfparser.shsplit import ShowSplit
75
from shconfparser.parser import Parser
86

97

108
class TestParser:
119

1210
@pytest.fixture
1311
def setup(self):
14-
file_path = path.abspath('data/shcommands.txt')
12+
file_path = path.abspath("data/shcommands.txt")
1513
p = Parser()
1614
file_data = p.read(file_path)
1715
p.split(file_data)
1816
yield p
1917

2018
def test_data_parser(self, setup):
2119
data = setup.s.shcmd_dict
22-
assert 'version' in data
23-
result = setup.parse_data(data['version'])
20+
assert "version" in data
21+
result = setup.parse_data(data["version"])
2422
assert result != {}
25-
assert 'R1 uptime is 10 minutes' in result
23+
assert "R1 uptime is 10 minutes" in result
2624

2725
def test_tree_parser(self, setup):
2826
data = setup.s.shcmd_dict
29-
assert 'running' in data
30-
result = setup.parse_tree(data['running'])
27+
assert "running" in data
28+
result = setup.parse_tree(data["running"])
3129
assert result != {}
32-
assert 'line vty 0 4' in result
30+
assert "line vty 0 4" in result
3331

3432
def test_table_parser(self, setup):
3533
data = setup.s.shcmd_dict
36-
assert 'cdp_neighbors' in data
37-
header = ['Device ID', 'Local Intrfce', 'Holdtme', 'Capability', 'Platform', 'Port ID']
38-
result = setup.parse_table(data['cdp_neighbors'], header)
34+
assert "cdp_neighbors" in data
35+
header = ["Device ID", "Local Intrfce", "Holdtme", "Capability", "Platform", "Port ID"]
36+
result = setup.parse_table(data["cdp_neighbors"], header)
3937
assert result != []
4038
assert type(result[0]) is dict
41-
assert 'Device ID' in result[0]
42-
assert 'R2' == result[0]['Device ID']
39+
assert "Device ID" in result[0]
40+
assert result[0]["Device ID"] == "R2"
4341

4442
def test_table_parser_multiple_line(self, setup):
45-
data = {'cdp_neighbors': ['R1#show cdp neighbors',
46-
'Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge',
47-
'S - Switch, H - Host, I - IGMP, r - Repeater', '',
48-
'Device ID Local Intrfce Holdtme Capability Platform Port ID',
49-
'ajskdjfajfajlsfjabcdefgh',
50-
' Fas 0/0 164 R S I 3725 Fas 0/0',
51-
'R1#']}
52-
assert 'cdp_neighbors' in data
53-
header = ['Device ID', 'Local Intrfce', 'Holdtme', 'Capability', 'Platform', 'Port ID']
54-
result = setup.parse_table(data['cdp_neighbors'], header)
43+
data = {
44+
"cdp_neighbors": [
45+
"R1#show cdp neighbors",
46+
"Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge",
47+
"S - Switch, H - Host, I - IGMP, r - Repeater",
48+
"",
49+
"Device ID Local Intrfce Holdtme Capability Platform Port ID",
50+
"ajskdjfajfajlsfjabcdefgh",
51+
" Fas 0/0 164 R S I 3725 Fas 0/0",
52+
"R1#",
53+
]
54+
}
55+
assert "cdp_neighbors" in data
56+
header = ["Device ID", "Local Intrfce", "Holdtme", "Capability", "Platform", "Port ID"]
57+
result = setup.parse_table(data["cdp_neighbors"], header)
5558
assert result != []
5659
assert type(result[0]) is dict
57-
assert 'Device ID' in result[0]
58-
assert '3725' == result[0]['Platform']
60+
assert "Device ID" in result[0]
61+
assert result[0]["Platform"] == "3725"
5962

6063
def test_table_parser_header_mismatch(self, setup):
6164
data = setup.s.shcmd_dict
62-
assert 'cdp_neighbors' in data
63-
header = [' Device ID', 'Local Intrfce', 'Holdtme', 'Capability', 'Platform', 'Port ID']
64-
result = setup.parse_table(data['cdp_neighbors'], header)
65-
assert result == None
65+
assert "cdp_neighbors" in data
66+
header = [" Device ID", "Local Intrfce", "Holdtme", "Capability", "Platform", "Port ID"]
67+
result = setup.parse_table(data["cdp_neighbors"], header)
68+
assert result is None
6669
# TODO: need to check log message
6770

6871
def test_dump(self, setup):
6972
data = setup.s.shcmd_dict
7073
assert type(setup.dump(data)) is str
71-

tests/test_reader.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import pytest
2-
31
from os import path
2+
43
from shconfparser.reader import Reader
54

65

76
class TestReader:
87

98
def test_given_file_path(self):
10-
file_path = path.abspath('data/shrun.txt')
9+
file_path = path.abspath("data/shrun.txt")
1110
obj = Reader(file_path)
1211
assert type(obj.data) is list
1312

1413
def test_given_folder_path(self):
15-
folder_path = path.abspath('data')
14+
folder_path = path.abspath("data")
1615
obj = Reader(folder_path)
1716
assert obj.data is None

tests/test_search.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
1+
from os import path
2+
13
import pytest
2-
import collections
34

4-
from os import path
5-
from shconfparser.reader import Reader
6-
from shconfparser.shsplit import ShowSplit
75
from shconfparser.parser import Parser
86

97

108
class TestParser:
119

1210
@pytest.fixture
1311
def setup(self):
14-
file_path = path.abspath('data/shcommands.txt')
12+
file_path = path.abspath("data/shcommands.txt")
1513
p = Parser()
1614
file_data = p.read(file_path)
1715
p.split(file_data)
18-
p.s.shcmd_dict['running'] = p.parse_tree(p.s.shcmd_dict['running'])
19-
p.s.shcmd_dict['version'] = p.parse_data(p.s.shcmd_dict['version'])
20-
header = ['Device ID', 'Local Intrfce', 'Holdtme', 'Capability', 'Platform', 'Port ID']
21-
p.s.shcmd_dict['cdp_neighbors'] = p.parse_table(p.s.shcmd_dict['cdp_neighbors'], header)
22-
header = ['Interface', 'IP-Address', 'OK?', 'Method', 'Status', 'Protocol']
23-
p.s.shcmd_dict['ip_interface_brief'] = p.parse_table(p.s.shcmd_dict['ip_interface_brief'], header)
16+
p.s.shcmd_dict["running"] = p.parse_tree(p.s.shcmd_dict["running"])
17+
p.s.shcmd_dict["version"] = p.parse_data(p.s.shcmd_dict["version"])
18+
header = ["Device ID", "Local Intrfce", "Holdtme", "Capability", "Platform", "Port ID"]
19+
p.s.shcmd_dict["cdp_neighbors"] = p.parse_table(p.s.shcmd_dict["cdp_neighbors"], header)
20+
header = ["Interface", "IP-Address", "OK?", "Method", "Status", "Protocol"]
21+
p.s.shcmd_dict["ip_interface_brief"] = p.parse_table(
22+
p.s.shcmd_dict["ip_interface_brief"], header
23+
)
2424
yield p
2525

2626
def test_search_in_tree_level(self, setup):
2727
data = setup.s.shcmd_dict
28-
pattern = r' privilege level 15'
29-
m = setup.search.search_in_tree_level(pattern, data['running'], level=10)
28+
pattern = r" privilege level 15"
29+
m = setup.search.search_in_tree_level(pattern, data["running"], level=10)
3030
assert pattern.strip() in m
3131

3232
def test_search_all_in_tree(self, setup):
3333
data = setup.s.shcmd_dict
34-
pattern = r'interface\s+FastEthernet.*'
35-
m = setup.search.search_all_in_tree(pattern, data['running'])
36-
assert 'interface FastEthernet0/0' in m.values()
34+
pattern = r"interface\s+FastEthernet.*"
35+
m = setup.search.search_all_in_tree(pattern, data["running"])
36+
assert "interface FastEthernet0/0" in m.values()
3737

3838
def test_search_in_tree(self, setup):
3939
data = setup.s.shcmd_dict
40-
pattern = r'Cisco\s+IOS\s+Software.*'
41-
m = setup.search.search_in_tree(pattern, data['version'])
42-
assert 'Version 12.4(25d)' in m.group(0)
40+
pattern = r"Cisco\s+IOS\s+Software.*"
41+
m = setup.search.search_in_tree(pattern, data["version"])
42+
assert "Version 12.4(25d)" in m.group(0)
4343

4444
def test_search_in_table(self, setup):
4545
data = setup.s.shcmd_dict
46-
pattern = r'R\d+'
47-
header = 'Device ID'
48-
m = setup.search.search_in_table(pattern, data['cdp_neighbors'], header)
49-
assert 'Device ID' in m
50-
assert m['Device ID'] == 'R2'
46+
pattern = r"R\d+"
47+
header = "Device ID"
48+
m = setup.search.search_in_table(pattern, data["cdp_neighbors"], header)
49+
assert "Device ID" in m
50+
assert m["Device ID"] == "R2"
5151

5252
def test_search_all_in_table(self, setup):
5353
data = setup.s.shcmd_dict
54-
pattern = r'FastEthernet.*'
55-
header = 'Interface'
56-
m = setup.search.search_all_in_table(pattern, data['ip_interface_brief'], header)
54+
pattern = r"FastEthernet.*"
55+
header = "Interface"
56+
m = setup.search.search_all_in_table(pattern, data["ip_interface_brief"], header)
5757
assert type(m) is list
58-
assert 'Interface' in m[0]
59-
assert 'FastEthernet0/0' == m[0]['Interface']
60-
58+
assert "Interface" in m[0]
59+
assert m[0]["Interface"] == "FastEthernet0/0"

tests/test_shsplit.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
import pytest
21
import collections
3-
42
from os import path
3+
54
from shconfparser.reader import Reader
65
from shconfparser.shsplit import ShowSplit
76

87

98
class TestShowSplit:
109

1110
def test_split_data(self):
12-
file_path = path.abspath('data/shcommands.txt')
11+
file_path = path.abspath("data/shcommands.txt")
1312
r = Reader(file_path)
1413
obj = ShowSplit()
1514
data = obj.split(r.data)
1615
assert type(data) is collections.OrderedDict
17-
assert 'running' in data
16+
assert "running" in data
1817

1918
def test_split_none_data(self):
20-
folder_path = path.abspath('data')
19+
folder_path = path.abspath("data")
2120
r = Reader(folder_path)
2221
obj = ShowSplit()
2322
data = obj.split(r.data)
2423
assert data is None
2524

2625
def test_command_not_found(self):
27-
lst = ['abcd#sh testing', 'testing']
26+
lst = ["abcd#sh testing", "testing"]
2827
obj = ShowSplit()
2928
data = obj.split(lst)
3029
assert data == {}
3130
# TODO: need assert log messages
32-
# assert 'No key found' in
33-
31+
# assert 'No key found' in

0 commit comments

Comments
 (0)