0

The difficulty lies in the fact that the function has 2 input functions.

name_of_numbers = {1: 'Первый', 2: 'Второй', 3: 'Третий', 4: 'Червертый', 5: 'Пятый', 6: 'Шестой', 7: 'Седьмой', 8: 'Восьмой', 9: 'Девятый', 10: 'Десятый', 11: 'Одиннадцатый', 12: 'Двенадцатый', 13: 'Тринадцатый', 14: 'Четырнадцатый', 15: 'Пятнадцатый', 16: 'Шестнадцатый', 17: 'Семнадцатый', 18: 'Восемнадцатый', 19: 'Девятнадцатый'}
def get_dictionary() -> dict:
    database = dict()
    amount_of_orders = int(input('Количество заказов: '))
    for number in range(1, amount_of_orders + 1):
        customer, pizza_name, count = input(f'{name_of_numbers[number]} заказ: ').split()
        if customer in database:
            if pizza_name in database[customer]:
                database[customer][pizza_name] += int(count)
            else:
                database[customer][pizza_name] = count
        else:
            database[customer] = dict({pizza_name: int(count)})
    return database
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • what unit testing package are you using? – catzilla May 31 '23 at 16:02
  • Unittest unit testing package. Forgot to specify the input data # Иванов Пепперони 1 # Петров Де-Люкс 2 # Иванов Мясная 3 # Иванов Мексиканская 2 # Иванов Пепперони 2 # Петров Интересная 5 – Дмитрий Сатлер May 31 '23 at 16:48

1 Answers1

0

Unit testing this one would be simple. You only need to understand (1) how to mock the input() function, and (2) how to mock it multiple times with different outputs.

For #1: This answer has a clue on how to handle mocking input

so in your code, you might want to wrap the input() into a wrapper function get_input like:

def get_input(text):
     return input(text)

and replace your input() functions with get_input()

amount_of_orders = int(get_input('Количество заказов: '))
...
get_input(f'{name_of_numbers[number]} заказ: ').split()

For #2: You might want to use side_effect. this feature allows you to have multiple return values of a mock depending on the array of values you provided in it.

As for how the unit test would probably look, this would be a good example:

@patch('yourmodule.get_input', side_effect=['input1', 'input2'])
def test_get_dictionary():
    result = get_dictionary()
    # assertions here
catzilla
  • 1,901
  • 18
  • 31