Django模式缓存?如何在测试中禁用 [英] Django models caching? How to disable in tests
问题描述
首先,我的test.py的代码
def test_get_current(self):
pre>
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,无
#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)
打印Question.objects.all()。exclude(response__memberresponse__member = m)
打印Question.objects.all()。exclude(response__memberresponse__member = m)
我在get_cur中有意想不到的结果租金功能,所以,我评论它,并尝试通过打印功能内部使用的主要查询器进行调试,并得到了奇怪的结果:
。
安装...的模型
[<问题:q1>,<问题:q2>]
[<问题:q2>]
.. ...
------------------------------------------- ---------------------------
在3.125s中进行5次测试
我想知道,为什么使用相同参数的QuerySet会返回第一个错误的数据,但是通过下一个调用 - 是否正确,如何避免?
Btw,django世界有没有类似于铁路工厂的女孩创建测试数据?
解决方案factory_boy 是基于thinkbot的factory_girl的灯具更换。
如果你来自Rails,你会发现它与factory_girl非常相似。推荐。
First, the code of my 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)
I've got unexpected results in my get_current function, so, I commented it and tried to debug by printing main queryset used inside function and got also strange results:
... Installing index for ... model [<Question: q1>, <Question: q2>] [<Question: q2>] ..... ---------------------------------------------------------------------- Ran 5 tests in 3.125s
I'm wondering, why QuerySet with the same arguments returns first wrong data, but by the next call - correct and how can I avoid it?
Btw, does django world have something similar to rail's factory girl for creating test data?
解决方案factory_boy is "a fixtures replacement based on thoughtbot's factory_girl."
If you're coming from Rails, you'll find it works quite similarly to factory_girl. Recommended.
这篇关于Django模式缓存?如何在测试中禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!