我扩展了MongoDbLibrary for Robot框架,以便可以使用.skip(),.take()和.order()
def _retrieve_mongodb_records_ex(self, dbName, dbCollName, recordJSON, sorting, skip=0, take=None, fields=[], returnDocuments=False):
db = None
try:
dbName = str(dbName)
dbCollName = str(dbCollName)
criteria = dict(json.loads(recordJSON))
db = self._dbconnection['%s' % (dbName,)]
coll = db['%s' % (dbCollName)]
if fields:
results = coll.find(criteria, fields)
else:
results = coll.find(criteria)
if sorting:
results = results.sort(sorting)
if (skip):
skip = int(skip)
if skip > 0:
results = results.skip(skip)
if take:
results = results.limit(int(take))
if returnDocuments:
return list(results)
else:
response = ''
for d in results:
response = '%s%s' % (response, d.items())
return response
finally :
if db :
self._dbconnection.end_request()
我在python上做了一个测试:
^{pr2}$此测试返回正确的数据
在robot framework中,我在关键字中使用此方法:
Get Some Mongo Records With Order And Limit
[Arguments] ${MongoHostProp} ${MongoPortProp} ${MongoDbNameProp} ${MongoCollProp} ${QueryProp} ${Sorting}
... ${Skip} ${Take}
Connect To MongoDB ${MongoHostProp} ${MongoPortProp}
${records}= Retrieve Some Records ${VAR_ImportMongoDbName} ${VAR_ImportJobMongoCollName} ${QueryProp} ${Sorting} ${Skip}
... ${Take} true
Disconnect From Mongodb
[Return] ${records}
但此关键字返回第一个与${QueryProp}匹配的项,忽略${Sorting}。在
我比较了发送给这个python方法的参数-它们是相等的。有人知道问题出在哪里吗?在
我相信pymongo之所以不把排序参数当作键值对,是因为RobotFramework倾向于将每个值转换成字符串。在
Python代码应该检查值是否为string,并对其求值,以将其返回到键值对的列表中。在
if isinstance(sorting, string_types): sorting = eval(sorting)
;Pymongo将字符串解释为单个排序键,并通过添加'pymongo.升序'这是默认方向。在