def test_methods_created(): """ Проверяем, что у объекта есть переменные: _ping, scan """ user = task_11_4.User("testuser") check_attr_or_method(user, attr="username")
def test_methods_created(): """ Проверяем, что у объекта есть нужные методы """ net = task_9_1.IPv4Network("100.7.1.0/26") check_attr_or_method(net, method="allocate_ip") check_attr_or_method(net, method="free_ip")
def test_mixin(): ins = ForTest() check_attr_or_method(ins, method="subclasses") check_attr_or_method(ins, method="superclasses") task_12_5.InheritanceMixin.subclasses() task_12_5.InheritanceMixin.superclasses() ins.subclasses() ins.superclasses()
def test_methods_created(): """ Проверяем, что у объекта есть методы: _ping, scan """ scan_net = task_9_2.PingNetwork(task_9_1.IPv4Network("8.8.4.0/29")) check_attr_or_method(scan_net, method="_ping") check_attr_or_method(scan_net, method="scan")
def test_method_created(): """ Проверяем, что у объекта есть метод: allocate """ net = task_13_4.IPv4Network("100.7.1.0/26") check_attr_or_method(net, method="allocate_ip") check_attr_or_method(net, method="free_ip")
def test_username(first_router_from_devices_yaml, monkeypatch): username = first_router_from_devices_yaml.pop("username") monkeypatch.setattr("builtins.input", lambda x=None: username) check_attr_or_method(task_11_5.CiscoTelnet, method="input_params") r1 = task_11_5.CiscoTelnet.input_params(**first_router_from_devices_yaml) # если в предыдущей строке все правильно с подключением, команда sh clock # должна выполниться без исключений output = r1.send_show_command("sh clock") assert "sh clock" in output
def test_method__add__(): """Проверка наличия метода __add__ и его работы""" ip1 = task_13_2.IPAddress("192.168.1.1", 24) check_attr_or_method(ip1, method="__add__") sum_ip = ip1 + 17 assert isinstance( sum_ip, task_13_2.IPAddress ), "Метод __add__ должен возвращать новый экземпляр класса IPAddress" assert sum_ip.ip == "192.168.1.18"
def test_class(first_router_from_devices_yaml): """Проверяем работу объекта""" r1 = task_12_1a.CiscoTelnet(**first_router_from_devices_yaml) assert isinstance(r1, TelnetBase), "Класс CiscoTelnet должен наследовать TelnetBase" check_attr_or_method(r1, method="send_show_command") check_attr_or_method(r1, method="send_config_commands") with pytest.raises(Exception) as excinfo: return_value = r1.send_show_command("sh clck") with pytest.raises(Exception) as excinfo: return_value = r1.send_config_commands("loggg 7.7.7.7") r1._telnet.close()
def test_sequence_mixin_methods_created(): example = { ("R1", "Eth0/0"): ("SW1", "Eth0/1"), ("R2", "Eth0/0"): ("SW1", "Eth0/2"), ("R3", "Eth0/0"): ("SW1", "Eth0/3"), } top = task_12_3a.Topology(example) check_attr_or_method(top, method="keys") check_attr_or_method(top, method="get") check_attr_or_method(top, method="pop") check_attr_or_method(top, method="clear") check_attr_or_method(top, method="update")
def test_send_show_command(first_router_from_devices_yaml): """Проверяем работу метода send_show_command""" r1 = task_12_1.CiscoTelnet(**first_router_from_devices_yaml) check_attr_or_method(r1, method="send_show_command") show_output = r1.send_show_command("sh ip int br") assert type( show_output) == str, "Метод send_show_command должен возвращать строку" assert ( "Loopback123" in show_output and "123.1.2.3" in show_output ), "В выводе sh ip int br должен быть интерфейс Loopback123 и IP-адрес 123.1.2.3" r1.close()
def test_sequence_special_methods_created(): example = { ("R1", "Eth0/0"): ("SW1", "Eth0/1"), ("R2", "Eth0/0"): ("SW1", "Eth0/2"), ("R3", "Eth0/0"): ("SW1", "Eth0/3"), } top = task_12_3a.Topology(example) check_attr_or_method(top, method="__getitem__") check_attr_or_method(top, method="__setitem__") check_attr_or_method(top, method="__delitem__") check_attr_or_method(top, method="__len__") check_attr_or_method(top, method="__iter__")
def test_method_to_dict(): """Проверка наличия метода to_dict и его работы""" book1 = task_13_3.Book("Fluent Python", 20, 100) check_attr_or_method(book1, method="to_dict") attr_dict = book1.to_dict() assert isinstance(attr_dict, dict), "Метод to_dict должен возвращать словарь" assert attr_dict == { "title": "Fluent Python", "price": 20.0, "quantity": 100, "total": 2000.0, }
def test_send_config_commands(first_router_from_devices_yaml): """Проверяем работу объекта и метода send_config_commands""" r1 = task_12_1.CiscoTelnet(**first_router_from_devices_yaml) assert isinstance( r1, TelnetBase), "Класс CiscoTelnet должен наследовать TelnetBase" # test send_config_commands check_attr_or_method(r1, method="send_config_commands") cfg_output = r1.send_config_commands( ["interface loopback123", "ip address 123.1.2.3 255.255.255.255"]) assert (type(cfg_output) == str ), "Метод send_config_commands должен возвращать строку" assert ( "interface loopback123" in cfg_output and "ip address 123.1.2.3 255.255.255.255" in cfg_output ), "В выводе должны быть строки с командами: 'interface loopback123', 'ip address 123.1.2.3 255.255.255.255'" r1.close()
def test_attributes_created(): """ Проверяем, что у объекта есть атрибуты: address, mask, broadcast, allocated, unassigned """ net = task_13_4.IPv4Network("100.7.1.0/26") check_attr_or_method(net, attr="network") check_attr_or_method(net, attr="broadcast") check_attr_or_method(net, attr="allocated") check_attr_or_method(net, attr="unassigned")
def test_cfg(first_router_from_devices_yaml): r1 = task_11_3.CiscoTelnet(**first_router_from_devices_yaml, config_cache_timeout=3) check_attr_or_method(r1, attr="cfg") # get config to check Loopback interfaces config = r1.send_show_command("sh run") loop_num = get_random_loop_number(config) loop_intf = f"Loopback{loop_num}" # get cfg r1.cfg # create interface r1.send_config(f"interface {loop_intf}") assert (loop_intf not in r1.cfg ), "r1.cfg выполнено сразу после команды, должен отдаваться кеш" time.sleep(5) assert ( loop_intf in r1.cfg ), "r1.cfg выполнено после паузы > config_cache_timeout, конфиг должен быть считан заново"
def test_attributes_created(): """ Проверяем, что у объекта есть атрибуты """ prefix = task_13_1.Route("192.168.1.0/24", "192.168.20.2", "OSPF") check_attr_or_method(prefix, attr="prefix") check_attr_or_method(prefix, attr="nexthop") check_attr_or_method(prefix, attr="protocol")
def test_special_methods_created(): class IntTest(task_12_4.OrderingMixin): def __init__(self, number): self._number = number def __eq__(self, other): return self._number == other._number def __lt__(self, other): return self._number < other._number int1 = IntTest(5) check_attr_or_method(int1, method="__ge__") check_attr_or_method(int1, method="__ne__") check_attr_or_method(int1, method="__le__") check_attr_or_method(int1, method="__gt__")
def test_new_attr_created(): """ Проверяем, что у объекта есть переменные: allocate, unassigned """ net = task_11_1.IPv4Network("100.7.1.0/26") check_attr_or_method(net, method="allocate_ip") check_attr_or_method(net, attr="unassigned") check_attr_or_method(net, attr="hosts")
def test_attributes_created(): """ Проверяем, что у объекта есть атрибуты: address, mask, broadcast, allocated """ net = task_11_1.IPv4Network("100.7.1.0/26") check_attr_or_method(net, attr="network") check_attr_or_method(net, attr="broadcast") check_attr_or_method(net, attr="allocated") assert (net.allocated == set() ), "По умолчанию allocated должен содержать пустое множество"
def test_sequence_special_methods_created(): net = task_10_1.IPv4Network("100.7.1.0/29") check_attr_or_method(net, method="__getitem__") check_attr_or_method(net, method="__len__") check_attr_or_method(net, method="__contains__") check_attr_or_method(net, method="__iter__") check_attr_or_method(net, method="index") check_attr_or_method(net, method="count")
def test_attr_buy(): things = task_7_2a.count_total() check_attr_or_method(things, attr="buy")
def test_attr_total_calls_created(): @task_8_5a.count_calls def do_thing(a, b): return a + b check_attr_or_method(do_thing, attr="total_calls")
def test_attributes_created(): """ Проверяем, что у объекта есть нужные атрибуты """ net = task_9_1.IPv4Network("100.7.1.0/26", gw="100.7.1.1") check_attr_or_method(net, attr="network") check_attr_or_method(net, attr="gw") check_attr_or_method(net, attr="broadcast") check_attr_or_method(net, attr="allocated") check_attr_or_method(net, attr="unassigned") check_attr_or_method(net, attr="hosts")
def test_attr_topology(topology_with_dupl_links): """Проверяем, что в объекте Topology есть атрибут topology""" top_with_data = task_12_3a.Topology(topology_with_dupl_links) check_attr_or_method(top_with_data, attr="topology")