我有一个函数来生成sms_token
。它不能与数据库中现有的重复。但是,令牌的空间可能不够大,可能会发生新令牌的碰撞。你知道吗
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()
因此,我想使side_effect
的randint
按这个顺序返回确定性值123456, 123456, 111222
使用给定的值。我将能够在我的rand_six
中测试else
逻辑
我试过这个answer,但不起作用。rand_six()
仍然返回原始函数,而不是我创建的伪函数。你知道吗
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()
问题:
它不会修补random.randint
的行为
问题:
如何将生成的假列表放入我的randint
?你知道吗
感谢
Klaus D.
的评论。我必须坚持module
。你知道吗import random
和random.randint(0, 9)
global
以获得给定条件下定义的值。还有我自己的问题。因为我想要两个相同的答案,但不是最后一个。你知道吗相关问题 更多 >
编程相关推荐