Skip to content

Commit

Permalink
tests: debug_mode verification added
Browse files Browse the repository at this point in the history
  • Loading branch information
tsypuk committed Jul 27, 2023
1 parent 8101482 commit 5cdbd93
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
11 changes: 7 additions & 4 deletions multicloud_diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class OnPrem(Enum):


class MultiCloudDiagrams:
def __init__(self):
def __init__(self, debug_mode=False):
self.mxfile = et.Element('mxfile', host="multicloud-diagrams",
agent="PIP package multicloud-diagrams. Generate resources in draw.io compatible format for Cloud infrastructure. Copyrights @ Roman Tsypuk 2023. MIT license.",
type="MultiCloud")
Expand All @@ -46,6 +46,8 @@ def __init__(self):
self.mx_cell_id_0 = et.SubElement(self.root, 'mxCell', id="0")
self.mx_cell_id_1 = et.SubElement(self.root, 'mxCell', id="1", parent="0")

self.debug_mode = debug_mode

prev_coords = {}

supported_vertex = {}
Expand Down Expand Up @@ -145,8 +147,8 @@ def add_vertex(self, id: str, node_name: str, arn: str, metadata: dict = {}, nod
style=(f"{shape_parameters['style']}"),
parent="1",
vertex="1")

mx_cell.insert(0, et.Comment(f'vertex:{node_name}'))
if self.debug_mode:
mx_cell.insert(0, et.Comment(f'vertex:{node_name}'))
mx_geometry = et.SubElement(mx_cell, 'mxGeometry', width=shape_parameters['width'],
height=shape_parameters['height'])
mx_geometry.set('as', 'geometry')
Expand Down Expand Up @@ -224,7 +226,8 @@ def add_connection(self, src_node_id, dest_node_id, start, end, labels=[]):
target=f'vertex:{dest_node_id}',
edge="2")

mx_cell.insert(0, et.Comment(f'edge:{src_node_id}:to:{dest_node_id}'))
if self.debug_mode:
mx_cell.insert(0, et.Comment(f'edge:{src_node_id}:to:{dest_node_id}'))
mx_geometry = et.SubElement(mx_cell, 'mxGeometry')
mx_geometry.set('as', 'geometry')

Expand Down
19 changes: 19 additions & 0 deletions tests/test_mcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,22 @@ def test_mx_cell(self):
self.assertEqual(2, len(mx_cells))
self.verify_mx_cell(mx_cells[0], expected={'id': '0'})
self.verify_mx_cell(mx_cells[1], expected={'id': '1', 'parent': '0'})

def test_comment(self):
# given
mcd = MultiCloudDiagrams(debug_mode=True)
resource_type = "test"
resource_name = "test_name"

# when
mcd.add_vertex(id="123", node_name=resource_name, arn="test_arn", metadata={}, node_type=resource_type)

# then
expected = {
'id': 'vertex:test:123',
'value': '<b>Name</b>: test_name<BR><b>ARN</b>: test_arn ',
'style': 'sketch=0;aspect=fixed;html=1;points=[];align=center;image;fontSize=12;image=img/lib/mscae/Info.svg;',
'parent': '1',
'vertex': '1'
}
self.verify_aws_resource(expected, mcd.mxfile, resource_name, 'fallback_vertex', debug_mode=True)
33 changes: 18 additions & 15 deletions utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class TestUtilities(unittest.TestCase):

def setUp(self) -> None:
project_folder = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(project_folder, '../multicloud_diagrams/providers/aws_services.json')
with open(path, 'r') as file:
json_data = json.load(file)

self.supported_vertex.update(json_data)
for provides_file in ['aws_services', 'fallback']:
path = os.path.join(project_folder, f'../multicloud_diagrams/providers/{provides_file}.json')
with open(path, 'r') as file:
json_data = json.load(file)
self.supported_vertex.update(json_data)

def verify_mx_cell(self, mx_cell, expected):
# then
Expand Down Expand Up @@ -70,7 +70,7 @@ def verify_vertex_in_isolation(self, mx_cells):
self.verify_mx_cell(mx_cells[0], expected={'id': '0'})
self.verify_mx_cell(mx_cells[1], expected={'id': '1', 'parent': '0'})

def verify_aws_resource(self, expected: dict, mx_file: et.Element, resource_name, resource_type):
def verify_aws_resource(self, expected: dict, mx_file: et.Element, resource_name, resource_type, debug_mode=False):
tree = et.ElementTree(mx_file)
self.verify_mxfile_default(et.ElementTree(tree))

Expand All @@ -82,17 +82,20 @@ def verify_aws_resource(self, expected: dict, mx_file: et.Element, resource_name
del expected['style']
expected['style'] = self.supported_vertex[resource_type]['style']

self.verify_mx_cell(mx_cells[2], expected)
# <!--vertex:node-name-->
# <mxGeometry width="69" height="72" as="geometry"/>
children = mx_cells[2].findall("./*")
self.assertEqual(2, len(children))

comment = children[0]
self.assertEqual(f'vertex:{resource_name}', comment.text)
self.assertEqual(0, len(comment.attrib))

mx_geometry = children[1]
self.verify_mx_cell(mx_cells[2], expected)
children_count = 1
if debug_mode:
comment = children[0]
self.assertEqual(f'vertex:{resource_name}', comment.text)
self.assertEqual(0, len(comment.attrib))
children_count = 2
mx_geometry = children[1]
else:
mx_geometry = children[0]

self.assertEqual(children_count, len(children))
self.assertEqual('mxGeometry', mx_geometry.tag)
self.assertEqual('geometry', mx_geometry.attrib['as'])
# 'height', 'width' are verified based on providers file content
Expand Down

0 comments on commit 5cdbd93

Please sign in to comment.