【发布时间】:2021-02-22 19:26:58
【问题描述】:
我正在为使用事务的函数编写测试。
Transactions only work on replica sets or mongos:
pymongo.errors.OperationFailure: Transaction numbers are only allowed on a replica set member or mongos, full error: {'ok': 0.0, 'errmsg': 'Transaction numbers are only allowed on a replica set member or mongos', 'code': 20, 'codeName': 'IllegalOperation'}
如果数据库是独立的 mongod 实例,我希望跳过这些测试。
如何从 Python 代码(例如,使用 pymongo 函数)检查我正在使用适当的环境?
我可以将 CI 管道配置为使用副本集。我可以并且确实在我的机器上进行了配置。但我不希望这几个测试对潜在贡献者造成麻烦,所以我宁愿跳过它们。
或者,我可以捕获OperationFailure 并尝试通过匹配字符串来确保它正是上面的错误,而不是另一个OperationFailure,但这将是一个抱歉的解决方法。
也许我可以使用类似的东西
MongoClient('localhost', 27017).admin.command("replSetGetStatus")
在经历艰难的反复试验之前,我很乐意获得明智的建议。
【问题讨论】: