[Python] How to mock a function that returns some value

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I need to test saving the confirmation code to the Redis cache.

    For example, there is a view that processes a user registration request. The view accepts a phone number and sends an SMS with a code to it. The code is generated by an additional function. Here's how to mocking this function to send the code I need?

    This is my view

    def post(self, request):
    serializer = ReviewerSignUpSerializer(data=request.data)
    serializer.is_valid(raise_exception=True)

    phone = serializer.validated_data.get("phone")
    confirmation_code = get_confirmation_code()
    cache.set(
    key=str(phone),
    value=confirmation_code,
    timeout=settings.CONFIRMATION_CODE_LIFETIME
    )

    send_sms_task.apply_async([confirmation_code, str(phone)])


    This is a function that generates a confirmation code

    def get_confirmation_code() -> str:
    seq = list(map(lambda x: str(x), range(10)))
    shuffle(seq)
    code = choices(seq, k=settings.INVITE_CODE_LENGTH)
    return "".join(code)


    Test function

    def test_saving_confirmation_code_in_cache(self, client, cache):
    phone = "+79389999999"
    generated_code = "9999"
    with patch("api.v1.authentication.utils.get_confirmation_code", return_value=generated_code):
    client.post(reverse('api:v1:signup'), data={'phone': phone})

    confirmation_code = cache.get(str(phone))
    assert confirmation_code is not None
    assert confirmation_code == generated_code


    Perhaps, I need to test the entire view function?

    Continue reading...

Compartilhe esta Página