Django Unitestept中的模拟Queryset

发布于 2025-01-22 23:27:00 字数 935 浏览 0 评论 0原文

我有示例代码和测试:

def outer():
   inner_response = inner(param1)

def inner(something):
    queryset_response = something.object.filter(foo="bar", 
    foo1="bar1") #this should get a reponse when testing.
    response_list = []
    for count, queryset_res in enumerate(queryset_response):
        response_list.append(queryset_response[count].data)
        #get response for data in this line.
    return response_list    

我想使用模拟测试这种情况,并可能会使用模拟列表返回QuerySet的列表。

def setup():
    something = mock.Mock()

def test_outer():
    # what should be done to the below line work so that 
    # response_list.append gets some value.
    something.objects.filter()[0].data = "some string"
    # Also is it possible to return queryset as like shown below.
    something.objects.filter().return_value = <queryset>  # list of objects in queryset.


I have sample code and test:

def outer():
   inner_response = inner(param1)

def inner(something):
    queryset_response = something.object.filter(foo="bar", 
    foo1="bar1") #this should get a reponse when testing.
    response_list = []
    for count, queryset_res in enumerate(queryset_response):
        response_list.append(queryset_response[count].data)
        #get response for data in this line.
    return response_list    

I wanna test this situation using mock and probably return list of queryset using mock if possible.

def setup():
    something = mock.Mock()

def test_outer():
    # what should be done to the below line work so that 
    # response_list.append gets some value.
    something.objects.filter()[0].data = "some string"
    # Also is it possible to return queryset as like shown below.
    something.objects.filter().return_value = <queryset>  # list of objects in queryset.


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱着落日 2025-01-29 23:27:00

我使用模拟并返回的名称tuple使其表现得像QuerySet,并使用(。)点访问数据集。我可以上课并使用相同的方式。

def test_func():
    something = mock.Mock()
    key = collections.namedtuple('key', 'data')
    response = key('some_string')
    something.objects.filter.return_value = [response]

正如Gloo所说,这有点嘲笑Django,我的前工程师决定以这种方式选择。

i used a mock and returned namedtuple to make it behave like queryset and use (.) dot to access the data set. I could make a class and used it same way.

def test_func():
    something = mock.Mock()
    key = collections.namedtuple('key', 'data')
    response = key('some_string')
    something.objects.filter.return_value = [response]

this is kinda mocking django as gloo said, my ex-engineers decided to opt in that way.

白衬杉格子梦 2025-01-29 23:27:00

您不应该嘲笑过滤器的结果,因为这就像单位测试Django本身一样。相反,您应该测试自己的功能,该功能将调用model.object.filter。您可以在单元测试的设置中创建要使用的对象,并断言,当您调用功能时,预期结果与这些对象相同。例如:

def my_own_function(foo, foo1):
    queryset_response = Something.objects.filter(foo=foo, foo1=foo1)
    store = queryset_response[0].data
    return store

在您的单位测试中:

def test_my_own_function():
    data = "mydata"
    sample_obj = Something.objects.create(foo="bar", foo1="bar1", data=data)
    result = my_own_function("bar", "bar1")
    self.assertEqual(result, data)

You shouldn't be mocking the result of a filter as that would be like unit testing django itself. Instead, you should be testing your own functions that will call Model.object.filter. You can create the objects you will be working with in the setup of your unit test, and assert that when you call your function, the expected result is the same as those objects. For example:

def my_own_function(foo, foo1):
    queryset_response = Something.objects.filter(foo=foo, foo1=foo1)
    store = queryset_response[0].data
    return store

and in your unit test:

def test_my_own_function():
    data = "mydata"
    sample_obj = Something.objects.create(foo="bar", foo1="bar1", data=data)
    result = my_own_function("bar", "bar1")
    self.assertEqual(result, data)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文