I have a function to generate sms_token
. It must not duplicate with the existing one in the database. However, the space of token might not so big enough then the collision of the newer one might happen.
Python 3.7.0
from random import randint
from multy_herr.user_profiles.models import UserProfile
def rand_six():
"""
Must find the `sms_token` which no `UserProfile`
:return:
"""
tmp = ""
for i in range(6):
tmp += str(randint(0, 9))
if 0 == UserProfile.objects.filter(sms_token=tmp).count():
return tmp
else:
return rand_six()
Therefore I wants to make side_effect
of randint
to return me the deterministic values by this order 123456, 123456, 111222
With given values. I will be able to test else
logic in my rand_six
I had tried this answer, but does not work. rand_six()
still return me the original function not the fake one I made.
from unittest.mock import patch
from multy_herr.users.utils import rand_six
@patch('random.randint')
def test_rand_six(self, some_func):
"""
Suppose it generates the duplicated `sms_token`
:return:
"""
some_func.return_value = '123456'
assert '123456' == rand_six()
Problem:
It does not patch the behavior of random.randint
Question:
How can I put my fake generated list to my randint
?