我正在使用peewee进行项目,在测试时我遇到了一个问题。我想检查是否已使用特定参数调用函数,该参数是peewee Expression。这里的问题是peewee.Expression会覆盖__eq__运算符:https://github.com/coleifer/peewee/blob/master/peewee.py#L379
因此,现在放在==
表达式右侧的任何内容都将返回一个新表达式并通过pytest计算为True。
pytest库中有什么东西可以通过属性测试对象相等而不是使用==
运算符吗?
答案 0 :(得分:0)
对于那些感兴趣的人,我找到了一个轻松执行的解决方案
def _extract_call_dict(name, args, kwargs=None):
"""Checks if the current element has an overriden __eq__ method"""
if kwargs is None:
# mock._Call is a superclass of Tuple and can have
# 2 or 3 elements. So, I unpack them and ajust my args accordingly
args, kwargs = name, args
# This check can be improved, my use case is pretty simple,
# so I only check types
has_eq_overriden = lambda obj: (isinstance(obj, peewee.Model) or
isinstance(obj, peewee.Expression))
# Replace the object by its dict representation if boolean is True
get_dict = lambda obj: obj.__dict__ if has_eq_overriden(obj) else obj
# apply this treatment for every elements from _Call
args = [get_dict(arg) for arg in args]
kwargs = {key: get_dict(value) for key, value in kwargs.items()}
# return a new instance
return mock.call(args, kwargs)
class _Call(mock._Call):
"""Override the _Call class from mock"""
def __eq__(self, other):
"""This will be use every time you try to compare
mock.call statements
"""
self_replacement = _extract_call_dict(*self)
if isinstance(other, mock._Call):
other = _extract_call_dict(*other)
return super(_Call, self_replacement).__eq__(other)
# Override mock.call to be compliant with peewee __eq__ override
unittest.mock._Call = test.utils._Call