您如何在单元测试中模拟 ORM 模型查询?

时间:2021-05-15 14:32:07

标签: python unit-testing orm

就我而言,我有一个如下所示的函数:

# the ModelA is an ORM model here (peewee)

def my_func():
    result1 = ModelA.select().where( 
        # some condition here
    )
    result2 = ModelA.select().where(
        # some other condition here 
    )
    
    # use the combination of result1 and 2 to do some thing and return result 

我不知道如何在这里模拟查询。有两个问题:

  1. 我不知道如何模拟查询。有文章说:

    mock.patch(some_path.ModelA) 作为 mock_model mock_model.select.where.return_value = 我期望的值

哪个不起作用

  1. 即使我知道如何编写模拟。有两个针对同一模型的查询。 所以我想我只能模拟一个结果

一种解决方法是将两个查询转换为两个函数。但我看不出有什么好处,除了容易 ut

0 个答案:

没有答案
相关问题