我有一个我正在为其编写单元测试的 UserRepository 类
class UserRepository(object):
"""
Repository that handles storage and retrieval of models.User objects
in and from the datastore.
"""
def create(self, user):
"""
Create the given user in the datastore if it doesn't exist yet.
Args:
user: The user to create.
Returns:
The created user.
Raises:
exc.DuplicateEntity: If the desired phonenumber is
already taken.
"""
duplicate_user = models.User.query(models.User.phonenumber == user.phonenumber).fetch()
if duplicate_user:
raise exc.DuplicateEntity()
user.put()
return user
我有这些测试
class UserServiceTest(unittest.TestCase):
"""Tests for the UserService."""
def setUp(self):
"""
Called before tests are run.
"""
self.user_repo = repositories.UserRepository()
#self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)
def test_create(self):
"""
Test if the create method creates a user.
"""
ndb.delete_multi(models.User.query().fetch(keys_only=True))
user = models.User(phonenumber='+31612345678#',
email='tim@castelijns.nl',
password='1234abcd')
ret_user = self.user_repo.create(user)
self.assertEqual(ret_user, user)
def test_create_duplicate_fails(self):
"""
Test if attempting to create a user with an existing phonenumber
fails.
"""
ndb.delete_multi(models.User.query().fetch(keys_only=True))
user = models.User(phonenumber='+31612345678#',
email='tim@castelijns.nl',
password='1234abcd')
self.user_repo.create(user)
with self.assertRaises(exc.DuplicateEntity):
self.user_repo.create(user)
ndb.delete_multi(models.User.query().fetch(keys_only=True)) 是为了从测试环境中清除现有用户,这样测试用例就不会相互影响.
这是自定义异常
class DuplicateEntity(Exception):
"""Exception to raise when trying to create a duplicate entity."""
我用
运行测试$ nosetests --with-gae
输出
======================================================================
FAIL: Test if attempting to create a user with an existing phonenumber
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tests/dal/test_repositories.py", line 53, in test_create_duplicate_fails
self.user_repo.create(user)
AssertionError: DuplicateEntity not raised
----------------------------------------------------------------------
Ran 2 tests in 0.080s
FAILED (failures=1)
这是出乎意料的,因为此处对 .create 的第二次调用应该引发异常,因为已经有一个用户使用该电话号码。
我确信代码有效,因为我已经对其进行了实时测试。
同样奇怪的是,如果我在 with 语句上方添加对 .create 的调用,它确实会引发异常:
self.user_repo.create(user)
self.user_repo.create(user)
with self.assertRaises(exc.DuplicateEntity):
self.user_repo.create(user)
所以它在第 3 次调用时被引发,但不是第 2 次。
我感觉它与数据存储一致性策略有关,如 here 所述:
The PseudoRandomHRConsistencyPolicy class lets you control the likelihood of a write applying before each global (non-ancestor) query. By setting the probability to 0%, we are instructing the datastore stub to operate with the maximum amount of eventual consistency. Maximum eventual consistency means writes will commit but always fail to apply, so global (non-ancestor) queries will consistently fail to see changes.
但是我不知道 nosegae 如何处理这个问题。它甚至是可配置的吗? nosegae doesn't have alot of documentation .
我该如何解决(或解决)这个问题?
请您参考如下方法:
您的问题是您正在使用查询来测试重复项,但由于最终一致性,不能保证该查询有效。您会注意到,您引用的文档中的测试都使用确保一致性的祖先查询。
我的看法是,这是预期的正确行为(AssertionError: DuplicateEntity not raised error)并强调了你的模型/方法的问题。
