def test_switch_table_implements_str(): table = SwitchTable() table.add(10, connection='eth0', next_hop=4) table.add(22, connection='eth1', next_hop=3) assert str(table) in { 'SwitchTable{10: (eth0, 4), 22: (eth1, 3)}', 'SwitchTable{22: (eth1, 3), 10: (eth0, 4)}' }
def test_switch_table_record_can_be_updated(): table = SwitchTable() table.add(13, connection='wifi', next_hop=5) link = table[13] link.connection = 'ge' link.next_hop = 24 assert table.as_dict() == {13: ('ge', 24)}
def test_switch_table_getitem_method(): table = SwitchTable() table.add(10, connection='eth0', next_hop=4) link1 = table[10] assert link1.connection == 'eth0' assert link1.next_hop == 4 with pytest.raises(KeyError): print(table[22])
def test_network_switch_provides_table_read_only_property(): sim = Mock() switch = NetworkSwitch(sim) assert isinstance(switch.table, SwitchTable) with pytest.raises(AttributeError): # noinspection PyPropertyAccess switch.table = SwitchTable()
def test_switch_table_add_replaces_existing_record(): table = SwitchTable() table.add(10, connection='eth0', next_hop=4) assert table.as_dict() == {10: ('eth0', 4)} table.add(10, connection='wifi', next_hop=9) assert table.as_dict() == {10: ('wifi', 9)}
def test_switch_table_implements_contains_magic_method(): table = SwitchTable() table.add(13, connection='wifi', next_hop=5) assert 13 in table assert 14 not in table
def test_switch_table_provides_get_method(): table = SwitchTable() table.add(13, connection='wifi', next_hop=5) assert table.get(13).connection == 'wifi' assert table.get(22) is None
def test_switch_table_as_dict_returns_read_only_dict(): table = SwitchTable() with pytest.raises(TypeError): table.as_dict()[13] = ('illegal', 66)
def test_switch_table_add_and_as_tuple_methods(): table = SwitchTable() table.add(10, connection='eth0', next_hop=4) table.add(22, connection='eth1', next_hop=3) assert table.as_dict() == {10: ('eth0', 4), 22: ('eth1', 3)}