MongoEngine的唯一性不适用于多个测试用例

时间:2014-11-27 15:23:09

标签: python mongodb mongoengine python-unittest

我有一个简单的测试,试图验证文档某个字段的唯一性。但是,除了在测试之间删除数据库之外,唯一性设置似乎无法在不删除整个集合的情况下工作。我不确定我是否误解了这里的某些内容,或者它是某种类型的错误。任何建议将不胜感激。谢谢!

import unittest
from mongoengine import *

class User(Document):
    email = EmailField(max_length=200, required=True, unique=True)

class TestBase(unittest.TestCase):
    def setUp(self):
        self.db = connect('test_database')

    def tearDown(self):
        # User.drop_collection() # Uncomment this line to get the expected results.
        self.db.drop_database('test_database')
        self.db.close()

    def test_duplicate(self):
        User(email='test@test.com').save()
        User(email='test@test.com').save()

    def test_duplicate_again(self):
        User(email='test@test.com').save()
        User(email='test@test.com').save()

    def test_duplicatw_once_again(self):
        User(email='test@test.com').save()
        User(email='test@test.com').save()

if __name__ == '__main__':
    unittest.main()

这里的预期结果是所有三个测试用例都会因NotUniqueError错误而失败。但是,只有第一个测试用例失败,其余的测试确实通过了。

(venv)$ python -m unittest -v tests.users
test_duplicate (tests.users.TestBase) ... ERROR
test_duplicate_again (tests.users.TestBase) ... ok
test_duplicatw_once_again (tests.users.TestBase) ... ok

======================================================================
ERROR: test_duplicate (tests.users.TestBase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/users.py", line 18, in test_duplicate
    User(email='test@test.com').save()
  File "/Users/starsirius/Code/santa/venv/src/mongoengine/mongoengine/document.py", line 312, in save
    raise NotUniqueError(message % unicode(err))
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error index:     test_database.user.$email_1  dup key: { : "test@test.com" })

----------------------------------------------------------------------
Ran 3 tests in 1.138s

FAILED (errors=1)

如果我取消注释User.drop_collection()行(因此在测试之间删除集合),结果是正确的,并且所有三个测试都失败了。

(venv)$ python -m unittest -v tests.users
test_duplicate (tests.users.TestBase) ... ERROR
test_duplicate_again (tests.users.TestBase) ... ERROR
test_duplicatw_once_again (tests.users.TestBase) ... ERROR

======================================================================
ERROR: test_duplicate (tests.users.TestBase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/users.py", line 18, in test_duplicate
    User(email='test@test.com').save()
  File "/Users/starsirius/Code/santa/venv/src/mongoengine/mongoengine/document.py", line 312, in save
    raise NotUniqueError(message % unicode(err))
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error index: test_database.user.$email_1  dup key: { : "test@test.com" })

======================================================================
ERROR: test_duplicate_again (tests.users.TestBase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/users.py", line 22, in test_duplicate_again
    User(email='test@test.com').save()
  File "/Users/starsirius/Code/santa/venv/src/mongoengine/mongoengine/document.py", line 312, in save
    raise NotUniqueError(message % unicode(err))
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error index: test_database.user.$email_1  dup key: { : "test@test.com" })

======================================================================
ERROR: test_duplicatw_once_again (tests.users.TestBase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/users.py", line 26, in test_duplicatw_once_again
    User(email='test@test.com').save()
  File "/Users/starsirius/Code/santa/venv/src/mongoengine/mongoengine/document.py", line 312, in save
    raise NotUniqueError(message % unicode(err))
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error index: test_database.user.$email_1  dup key: { : "test@test.com" })

----------------------------------------------------------------------
Ran 3 tests in 1.200s

FAILED (errors=3)

所以,我在这里不了解的两件事。首先,除了删除数据库之外,仅删除数据库和删除集合之间的区别是什么。我认为删除数据库应该足以重置状态。其次,无论如何,这里的独特性表现得很奇怪,也许我做错了什么?

有什么想法吗?非常感谢!

1 个答案:

答案 0 :(得分:1)

因此,在数据库被删除后,MongoEngine无法确保索引。

问题已在GitHub issue中报告,修复程序已合并到pull request