【发布时间】:2014-01-08 03:34:41
【问题描述】:
我正在尝试在我的测试运行中从 json 文件加载测试夹具(以匹配 Django 中使用的类似夹具加载样式)。这是我目前得到的代码;
from django.utils import simplejson as json
from mongoengine import base as mongobase
from mongoengine import connect
from pymongo import json_util
db = connect("DATABASENAME")
# Clear out the current collections.
for collection in db.collection_names():
if collection == 'system.indexes':
continue
db.drop_collection(collection)
# Open json fixtures
f = open('path/to/test_fixtures.json', 'r')
datas = json.loads(f.read(), object_hook=json_util.object_hook)
# For each serialised model instance, loop through and save to the database.
for data in datas:
print data
if data['_cls'] not in mongobase._document_registry:
print "Skipping %s" % data['_cls2']
continue
model = mongobase._document_registry[data['_cls']]
model_instance = model._from_son(data)
model_instance.save(force_insert=True)
这似乎很有效,但是,当其中一个模型具有 ReferenceField 时,它会失败。抱怨重复的密钥。您会注意到其中有一个打印语句来显示 dict 内容,在示例运行中,我将得到以下输出,在错误之前看起来一切正常
{u'_types': [u'Account'], u'status': u'ok', u'name': u'Alice', u'local_id': u'3', u'_cls': u'Account', u'members': [], u'_id': ObjectId('4f17f0855585d32457000001'), u'email': u'alice@example.com', u'permissions': []}
{u'_types': [u'Account'], u'status': u'ok', u'name': u'Bob', u'local_id': u'2', u'_cls': u'Account', u'members': [], u'_id': ObjectId('4f17f0855585d32457000000'), u'email': u'bob@example.com', u'permissions': []}
{u'_types': [u'Account'], u'status': u'ok', u'name': u'company', u'_cls': u'Account', u'members': [], u'_id': ObjectId('4f17f0855585d32457000002'), u'email': u'org@example.com', u'permissions': []}
{u'_types': [u'Membership'], u'parent_account': DBRef(u'account', ObjectId('4f17f0855585d32457000002')), u'member': DBRef(u'account', ObjectId('4f17f0855585d32457000001')), u'role': u'member', u'_cls': u'Membership', u'_id': ObjectId('4f17f0855585d32457000003')}
最后报错是:
Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/engineclub/lib/python2.6/site-packages/django/test/testcases.py", line 292, in __call__
self._pre_setup()
File "/home/vagrant/.virtualenvs/engineclub/lib/python2.6/site-packages/django/test/testcases.py", line 259, in _pre_setup
self._fixture_setup()
File "/vagrant/engineclub/engineclub/apps/notifications/tests.py", line 67, in _fixture_setup
model_instance.save(force_insert=True)
File "/home/vagrant/.virtualenvs/engineclub/lib/python2.6/site-packages/mongoengine/document.py", line 177, in save
_refs=_refs)
File "/home/vagrant/.virtualenvs/engineclub/lib/python2.6/site-packages/mongoengine/document.py", line 183, in save
raise OperationError(message % unicode(err))
OperationError: Tried to save duplicate unique keys (E11000 duplicate key error index: test_aliss.account.$_id_ dup key: { : ObjectId('4f17f0855585d32457000001') })
在插入仅引用帐户集合的 membership 实例时,它似乎在抱怨帐户上的重复键。$id。
如果我可以包含任何其他信息,请告诉我,我会添加 JSON 文件,但它非常类似于打印的 dicts(并且只有 3 个帐户和一个成员资格)。我确实注意到了一件事,当我从保存中删除 force_insert 时,它似乎根本没有保存任何东西(因此看起来很好)。
【问题讨论】:
-
我认为您正在手动提供对象 ID,因此正在创建带有硬编码 ID 的 DBRef 对象。帐户是否保存正确?我想知道是否正在发生一些取消引用问题,并且当您保存成员资格时,它会尝试重新保存 Account 对象。
-
是的,正在手动提供对象 ID。我怀疑问题可能与此有关,mongoengine 似乎对序列化没有帮助。帐户正在保存,我在循环中添加了一条快速行:
print data, Account.objects.count(), Membership.objects.count()并且帐户的计数正在增加(第一次失败时,会员资格停留在 0) -
直接处理pymongo并将数据保存在正确的集合中可能更容易。给我一个测试用例,我会研究它 - 在 github 上添加一张票
-
你是如何创建夹具文件的?
-
@JosephMisiti 使用此处描述的方法:groups.google.com/forum/?fromgroups#!topic/mongoengine-users/…
标签: python mongodb mongoengine