Django 模型缓存?如何在测试中禁用

标签 django unit-testing django-models

首先,我的tests.py的代码

def test_get_current(self):
    m = Member.objects.create(...)
    q = Question.objects.create(name="q1", text="q1", start_datetime=self.day_before, close_datetime=self.day_after, type=self.type)
    r = Response.objects.create(question=q, text='response')
    expected = q, None 
    #self.assertEquals(expected, Question.objects.get_current(m.id))

    q2 = Question.objects.create(name="q2", text="q2", start_datetime=self.day_before, close_datetime=self.day_after, type=self.type)
    #print Question.objects.all()
    #self.assertEquals(expected, Question.objects.get_current(m.id))
    MemberResponse.objects.create(member=m, response=r)
    print Question.objects.all().exclude(response__memberresponse__member=m)
    print Question.objects.all().exclude(response__memberresponse__member=m)

我在 get_current 函数中得到了意想不到的结果,因此,我对其进行了评论并尝试通过打印函数内部使用的主查询集进行调试,并且还得到了奇怪的结果:

...
Installing index for ... model
[<Question: q1>, <Question: q2>]
[<Question: q2>]
.....
----------------------------------------------------------------------
Ran 5 tests in 3.125s

我想知道,为什么具有相同参数的 QuerySet 首先返回错误的数据,但在下一次调用时返回正确的数据,如何避免它?

顺便问一下,django world 有没有类似于 Rail 的 Factory Girl 的东西来创建测试数据?

最佳答案

factory_boy是“基于thoughtbot的factory_girl的固定装置替换。”

如果您来自 Rails,您会发现它的工作方式与factory_girl 非常相似。推荐。

关于Django 模型缓存?如何在测试中禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2251434/

相关文章:

python - Django formset UpdateView 不允许我上传文件

mysql - 无法在 Windows 上安装 python mysqlclient

python - Python构造函数中异常的单元测试

django - Django迁移中如何创建GIN索引

django - modelformset_factory 不支持额外参数

asp.net-mvc - asp.net MVC 中的单元测试,如何模拟页面请求?

ios - 使用示例核心数据进行单元测试

django - 如何从 Django 查询集中提取数据

Django - 反向查询名称冲突

python - 调用objects.update_or_create后,Django模型得到 "ValueError: invalid literal for int() with base 10: "