示例#1
0
def test_show_all_contacts():
    """Test-func for <show_all_contacts> func"""
    address_book = AddressBook()

    name_one, phone_one = (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
    record_one = Record()
    record_one.add_contact_phonenumb(phone_one)
    address_book.add_record(name_one, record_one.__dict__)

    name_two, phone_two = (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
    record_two = Record()
    record_two.add_contact_phonenumb(phone_two)
    address_book.add_record(name_two, record_two.__dict__)

    # use <n>
    assert show_all_contacts(address_book) == (
        '\n\tContinue your work with commands.'
    )

    # use <y>
    with pytest.raises(StopIteration) as pytest_si:
        show_all_contacts(address_book)
    assert pytest_si.type == StopIteration
示例#2
0
def test_add_phone():
    """Test-func for <add_phone> function"""
    address_book = AddressBook()
    name, phone = (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
    record = Record()
    record.add_contact_phonenumb(phone)
    address_book.add_record(name, record.__dict__)

    datacase_one = ('Zivenko Kos', '380675119812')
    result_one = (
        "\n\tThe contact <Zivenko Kos> was added to address book with phone <380675119812>."
    )

    datacase_two = ('Romanskyy Andrey', '380675119811')
    result_two = (
        "\n\tThe contact's data is already in address book."
    )

    datacase_three = ('Romanskyy Andrey', '380675119822')
    result_three = (
        "\n\tThe phone <380675119822> for contact <Romanskyy Andrey>\n\t\
        was successfully added to address book."
    )

    assert add_phone(address_book, datacase_one) == result_one
    assert add_phone(address_book, datacase_two) == result_two
    assert add_phone(address_book, datacase_three) == result_three
示例#3
0
def change_existing_phone(address_book, data):
    """Changs existiong phone in address book

    Parameters
    ----------
    address_book: dict
    data: tuple

    Returns
    -------
    Info-string about condition anter function has worked
    """
    result = '\n\tEnter correct number.'
    name, phone = inner_parser(data)
    if name in address_book and phone in address_book[name]["phones"]:
        old_phone = phone
        new_phone = input(f"\n\tEnter new phone for contact <{name}>: ")
        raw_phone = re.search(r"38(097|098|068|067|063|068|099)\d{7}",
                              new_phone)
        phone = raw_phone.group().strip() if raw_phone else False
        if phone:
            address_book[name]["phones"].remove(old_phone)
            phone = Phone(phone).phone
            address_book[name]["phones"].append(phone)
            result = (
                f"\n\t<{old_phone}> for contact <{name}>\n\twas changed for <{phone}>."
            )
    return result
示例#4
0
def test_change_existing_phone():
    """Test-func for <change_existing_phone> method"""
    address_book = AddressBook()
    name, phone = (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
    record = Record()
    record.add_contact_phonenumb(phone)
    address_book.add_record(name, record.__dict__)

    data = ('Romanskyy Andrey', '380675119811')
    assert change_existing_phone(address_book, data) == (
        "\n\t<380675119811> for contact <Romanskyy Andrey>\n\twas changed for <380675119812>."
    )
示例#5
0
def test_change_error():
    """Test-func for <change_existing_phone> method"""
    address_book = AddressBook()
    name, phone = (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
    record = Record()
    record.add_contact_phonenumb(phone)
    address_book.add_record(name, record.__dict__)

    data = ('Romanskyy Andrey', '380675119811')

    assert change_existing_phone(address_book, data) == (
        '\n\tEnter correct number.'
    )
示例#6
0
def test_exit():
    """Test-func for <exit> func"""
    address_book = AddressBook()
    name, phone = (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
    record = Record()
    record.add_contact_phonenumb(phone)
    address_book.add_record(name, record.__dict__)

    with pytest.raises(SystemExit) as pytest_exit:
        save_and_exit(address_book)
    assert pytest_exit.type == SystemExit
    assert pytest_exit.value.code == (
        "\n\tThe address book was saved to <data.bin> file.\n\tGood bye!"
    )
示例#7
0
def inner_parser(data):
    """Returns cleared phone and name

    Parameters
    ----------
    data : tuple

    Returns
    -------
    name -> instance of class Name
    phone -> instance of class Phone
    """
    name, phone = data
    name = Name(name).name
    phone = Phone(phone).phone
    return name, phone
示例#8
0
def test_show_one_contact_data():
    """Test-func for <show_one_contact_data> function"""
    address_book = AddressBook()
    name, phone = (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
    record = Record()
    record.add_contact_phonenumb(phone)
    address_book.add_record(name, record.__dict__)

    result = '\n\tThe contact Romanskyy Andrey has\
        \n\tbirthday info - None\
        \n\tphone/s - 380675119811.\n'

    wrong_result = None

    assert show_one_contact_data(address_book) == result
    assert show_one_contact_data(address_book) == wrong_result
示例#9
0
def test_search_contacts():
    """Test-func for <search_contacts> function"""
    address_book = AddressBook()
    name, phone = (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
    record = Record()
    record.add_contact_phonenumb(phone)
    address_book.add_record(name, record.__dict__)

    contact_data = 'romans'
    wrong_data = 'some'

    result = '\n\tThe contact Romanskyy Andrey has\
        \n\tbirthday info - None\
        \n\tphone/s - 380675119811.\n'

    assert address_book.search_contacts(contact_data) == result
    assert address_book.search_contacts(wrong_data) != result
    assert address_book.search_contacts(wrong_data) is None
示例#10
0
def test_handler_func():
    """Test-func for <handler_func> function"""
    address_book = AddressBook()
    name, phone = (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
    record = Record()
    record.add_contact_phonenumb(phone)
    address_book.add_record(name, record.__dict__)

    show_command = 'show all'
    show_result = "\n\tYou have seen all contact's info."

    data = ('Nata Bazan', '380675554433')
    add_command = 'add'
    add_result = '\n\tThe contact <Nata Bazan> was added to address book with phone <380675554433>.'

    assert handler_func(show_command, address_book, data) == (
        '\n\tContinue your work with commands.'
    )
    assert handler_func(show_command, address_book, data) == show_result
    assert handler_func(show_command, address_book, data) == show_result
    assert handler_func(add_command, address_book, data) == add_result
示例#11
0
def test_inner_parser():
    """Inner_parser test func"""
    data = ('Romanskyy Andrey', '380675119811')
    assert inner_parser(data) == (
        Name('Romanskyy Andrey').name, Phone('380675119811').phone)
示例#12
0
def test_get_network():
    """Test-func for <get_network> method"""
    raw_num = '3(8067)511-98-11'
    result = '067'
    assert Phone(raw_num).get_network() == result