def test_empty_mesh_still_returns_valid_data(self): # GIVEN a new Mesh instance m = Mesh() # WHEN we request a dict representation of the mesh data = m.as_dict() # THEN a valid empty representation is returned self.assertEqual({'components': [], 'connections': [], 'resources': []}, data)
def test_added_resource_can_be_retrieved_from_mesh(self): # GIVEN a new Mesh instance m = Mesh() # WHEN I add a resource m.add_resource('Resource X') # THEN that resource is represented in the output self.assertEqual({ 'components': [], 'resources': ['Resource X'], 'connections': [], }, m.as_dict())
def test_resources_are_returned_in_the_order_they_were_added(self): # GIVEN a new Mesh instance m = Mesh() # WHEN I add several resources m.add_resource('Resource X') m.add_resource('Resource Z') m.add_resource('Resource Y') # THEN that resources are presented in the order they were added self.assertEqual({ 'components': [], 'resources': ['Resource X', 'Resource Z', 'Resource Y'], 'connections': [], }, m.as_dict())
def test_added_component_can_be_retrieved_from_mesh(self): # GIVEN a new Mesh instance m = Mesh() # WHEN I add a component m.add_component('Component A') # THEN that component is represented in the output self.assertEqual({ 'components': [ {'name': 'Component A', 'needs_ports': [], 'provides_ports': []}, ], 'resources': [], 'connections': [], }, m.as_dict())
def test_components_are_returned_in_the_order_they_were_added(self): # GIVEN a new Mesh instance m = Mesh() # WHEN I add several component m.add_component('Component A') m.add_component('Component Z') m.add_component('Component M') # THEN that component are presented in the order they were added self.assertEqual({ 'components': [ {'name': 'Component A', 'needs_ports': [], 'provides_ports': []}, {'name': 'Component Z', 'needs_ports': [], 'provides_ports': []}, {'name': 'Component M', 'needs_ports': [], 'provides_ports': []}, ], 'resources': [], 'connections': [], }, m.as_dict())
def test_adding_provides_port_when_components_are_created(self): # GIVEN a new Mesh instance m = Mesh() # WHEN I add a component with provides ports m.add_component('Component A', provides_ports=['provides 1', 'provides 2']) # THEN the provides ports are represented in the output self.assertEqual({ 'components': [ { 'name': 'Component A', 'needs_ports': [], 'provides_ports': ['provides 1', 'provides 2'], }, ], 'resources': [], 'connections': [], }, m.as_dict())
def test_resource_can_be_highlighted(self): # GIVEN the following mesh # # _____________ _____________ # | A | | B | # |-------------| |-------------| # | | n1 |-------->| p1 | n2 |--------->[ Resource X ] # |______|______| |______|______| # # m = Mesh() m.add_component('A', needs_ports=['n1']) m.add_component('B', needs_ports=['n2'], provides_ports=['p1']) m.add_resource('Resource X') m.add_connection('A', 'n1', 'B', 'p1') m.add_connection_to_resource('B', 'n2', 'Resource X') # WHEN Resource X is highlighted m.highlight_resource('Resource X') # THEN this is reflected in the dict representation self.assertEqual({ 'components': [ {'name': 'A', 'needs_ports': ['n1'], 'provides_ports': []}, {'name': 'B', 'needs_ports': ['n2'], 'provides_ports': ['p1']} ], 'resources': ['Resource X'], 'highlighted_resources': ['Resource X'], 'connections': [ { 'consumer_component': 'A', 'consumer_port': 'n1', 'producer_component': 'B', 'producer_port': 'p1', }, { 'consumer_component': 'B', 'consumer_port': 'n2', 'resource': 'Resource X', }, ], }, m.as_dict())
def test_adding_needs_port_to_existing_component(self): # GIVEN a Mesh with existing component m = Mesh() m.add_component('Component A') # WHEN we add a needs port to the component m.add_needs_port('Component A', 'needs 1') # THEN the needs port is represented in the output self.assertEqual({ 'components': [ { 'name': 'Component A', 'needs_ports': ['needs 1'], 'provides_ports': [] }, ], 'resources': [], 'connections': [], }, m.as_dict())
def test_adding_connection_between_two_ports(self): # GIVEN the following components # # _____________ _____________ # | A | | B | # |-------------| |-------------| # | | n1 | | p1 | | # |______|______| |______|______| # m = Mesh() m.add_component('A', needs_ports=['n1']) m.add_component('B', provides_ports=['p1']) # WHEN we add a connection between A:n1 and B:p1 m.add_connection('A', 'n1', 'B', 'p1') # THEN we get the following mesh representation # # _____________ _____________ # | A | | B | # |-------------| |-------------| # | | n1 | ---> | p1 | | # |______|______| |______|______| # self.assertEqual({ 'components': [ {'name': 'A', 'needs_ports': ['n1'], 'provides_ports': []}, {'name': 'B', 'needs_ports': [], 'provides_ports': ['p1']}, ], 'resources': [], 'connections': [ { 'consumer_component': 'A', 'consumer_port': 'n1', 'producer_component': 'B', 'producer_port': 'p1', }, ], }, m.as_dict())
def test_ports_are_returned_in_the_order_they_were_added(self): # GIVEN a new Mesh instance m = Mesh() # WHEN I add a component with ports in a specific order m.add_component('Component A', needs_ports=['needs 8', 'needs 7'], provides_ports=['provides 8', 'provides 7']) m.add_needs_port('Component A', 'needs 1') m.add_provides_port('Component A', 'provides 1') # THEN the ports are represented in the output in the same order they were entered self.assertEqual({ 'components': [ { 'name': 'Component A', 'needs_ports': ['needs 8', 'needs 7', 'needs 1'], 'provides_ports': ['provides 8', 'provides 7', 'provides 1'], }, ], 'resources': [], 'connections': [], }, m.as_dict())
def test_adding_connection_from_port_to_edge_resource(self): # GIVEN a mesh with the following component and resource # # _____________ # | A | # |-------------| # | | n1 | [ Resource X ] # |______|______| # m = Mesh() m.add_component('A', needs_ports=['n1']) m.add_resource('Resource X') # WHEN we connect A:n1 to Resource X m.add_connection_to_resource('A', 'n1', 'Resource X') # THEN we get the following mesh representation # # _____________ # | A | # |-------------| # | | n1 |--------->[ Resource X ] # |______|______| # self.assertEqual({ 'components': [ {'name': 'A', 'needs_ports': ['n1'], 'provides_ports': []}, ], 'resources': ['Resource X'], 'connections': [ { 'consumer_component': 'A', 'consumer_port': 'n1', 'resource': 'Resource X', }, ], }, m.as_dict())
def test_multiple_connections_across_components_with_multiple_ports(self): # GIVEN the following components # # _____________ _____________ # | A | | B | # |-------------| |-------------| # | | n1 | | p1 | | # | |______| |______| | # | | n2 | | p2 | | # |______|______| |______|______| # # # _____________ _____________ # | C | | D | # |-------------| |-------------| # | | nX | | pX | | # |______|______| |______|______| # m = Mesh() m.add_component('A', needs_ports=['n1', 'n2']) m.add_component('B', provides_ports=['p1', 'p2']) m.add_component('C', needs_ports=['nX']) m.add_component('D', provides_ports=['pX']) # WHEN we add a connection between A:n1 and B:p1 # WHEN we add a connection between C:nX and B:p1 m.add_connection('A', 'n1', 'B', 'p1') m.add_connection('A', 'n2', 'D', 'pX') m.add_connection('C', 'nX', 'B', 'p2') # THEN we get the following mesh representation # # _____________ _____________ # | A | | B | # |-------------| |-------------| # | | n1 |----------->| p1 | | # | |______| |______| | # | | n2 |-----+ +-->| p2 | | # |______|______| | | |______|______| # +- | -+ # | | # _____________ | | _____________ # | C | | | | D | # |-------------| | | |-------------| # | | nX |--+ +---->| pX | | # |______|______| |______|______| # self.assertEqual({ 'components': [ {'name': 'A', 'needs_ports': ['n1', 'n2'], 'provides_ports': []}, {'name': 'B', 'needs_ports': [], 'provides_ports': ['p1', 'p2']}, {'name': 'C', 'needs_ports': ['nX'], 'provides_ports': []}, {'name': 'D', 'needs_ports': [], 'provides_ports': ['pX']}, ], 'resources': [], 'connections': [ { 'consumer_component': 'A', 'consumer_port': 'n1', 'producer_component': 'B', 'producer_port': 'p1', }, { 'consumer_component': 'A', 'consumer_port': 'n2', 'producer_component': 'D', 'producer_port': 'pX', }, { 'consumer_component': 'C', 'consumer_port': 'nX', 'producer_component': 'B', 'producer_port': 'p2', }, ], }, m.as_dict())