Django测试 - 外部修改数据时的多个查询。缓存问题?

时间:2011-08-11 15:59:17

标签: django django-testing

我有一个遗留应用程序(目前)使用Django来有效地显示数据。我的一个工作测试的样本看起来像这样。

def test_add_property_value(self):
    """Test set / get a value"""
    # This will do some external process which occcurs to the db.
    pm = Pm(mysql_db='test_bugs')
    tree =  pm.add_release_tree()
    prop_type, pmvalue = ("string", "Funny Business")
    pmproperty = "%s_%s_basic" % (tree[0].name, prop_type)
    pm.add_property_definition(pmproperty, prop_type=prop_type)
    pm.add_propval(pmproperty, value=pmvalue, project=tree[0].name)

    # Now use Django to pull the value back out..
    project = Project.objects.get(name=tree[0].name)
    property = project.get_property(pmproperty) # Custom query using sql.raw
    self.assertEqual(pmvalue, property.value)

您可以看到它是基本的A / B测试。现在我发现了一个限制,我似乎无法解决多个外部添加 - 检查 - 循环查询失败。修改上述代码失败,因为出现查询无法甚至运行

def test_add_property_value(self):
    """Test set / get a value"""

    # This will do some external process which occcurs to the db.
    pm = Pm(mysql_db='test_bugs', p4_port = settings.ICMSERVER_TEST_PORT)
    tree = pm.add_release_tree()
    prop_type, pmvalue = ("string", "Funny Business")
    pmproperty = "%s_%s_basic" % (tree[1].name, prop_type)
    pm.add_property_definition(pmproperty, prop_type=prop_type)
    pm.add_propval(pmproperty, value=pmvalue, project=tree[1].name)

    # Now use Django to pull the value back out..
    project = Project.objects.get(name=tree[1].name)
    property = project.get_property(pmproperty)
    self.assertEqual(pmvalue, property.value)

    # ONLY CHANGE WAS TO ADD THIS..

    # This will do some external process which occcurs to the db.
    pmproperty = "%s_%s_basic_two" % (tree[1].name, prop_type)
    pm.add_property_definition(pmproperty, prop_type=prop_type)
    pm.add_propval(pmproperty, value=pmvalue, project=tree[1].name)

    # Now use Django to pull the value back out..
    project = Project.objects.get(name=tree[1].name)
    property = project.get_property(pmproperty)
    self.assertEqual(pmvalue, property.value)

我已经阅读了CACHE_BACKEND,但似乎没有帮助。还有其他想法吗?经过进一步调查后,这似乎与我的外部数据库无关。 Arghh ..感觉就像星期一!

  1. 这是一个缓存问题BTW - 设置CACHE_BACKEND ='dummy:///'或'locmem:///'什么也没做。
  2. 如何更好地诊断此问题?
  3. 由于

    更新

    以下是最终答案 - 2个小调整......基于DanielSeverio。很赞赏指针!

    class PropertyTests(TransactionTestCase):  #CHANGE1
        def test_add_property_value(self):
            """Test set / get a value"""
    
            import logging
            l = logging.getLogger('django.db.backends')
            l.setLevel(logging.DEBUG)
            l.addHandler(logging.StreamHandler())
    
            # This will do some external process which occcurs to the db.
            pm = Pm(mysql_db='test_bugs', p4_port = settings.ICMSERVER_TEST_PORT)
            tree = pm.add_release_tree()
            prop_type, pmvalue = ("string", "Funny Business")
            pmproperty = "%s_%s_basic" % (tree[1].name, prop_type)
            pm.add_property_definition(pmproperty, prop_type=prop_type)
            pm.add_propval(pmproperty, value=pmvalue, project=tree[1].name)
    
            # Now use Django to pull the value back out..
            project = Project.objects.get(name=tree[1].name)
            property = project.get_property(pmproperty)
            self.assertEqual(pmvalue, property.value)
    
            # This will do some external process which occcurs to the db.
            pmproperty = "%s_%s_basic_two" % (tree[1].name, prop_type)
            pm.add_property_definition(pmproperty, prop_type=prop_type)
            pm.add_propval(pmproperty, value=pmvalue, project=tree[1].name)
    
            # Now use Django to pull the value back out..
            Project.objects.update() #CHANGE2
            project = Project.objects.get(name=tree[1].name)
            property = project.get_property(pmproperty)
            self.assertEqual(pmvalue, property.value)
    

1 个答案:

答案 0 :(得分:1)

这可能不是交易问题。管理员(Project.objects)需要被告知数据更改,因为它的设计可以在短时间内生效。

您正在查询同一个查询Project.objects.get(name=tree[1].name)两次,并且管理员不再执行它,因为它认为它已经有正确的数据。

在第二次查询之前,执行

Project.objects.update()

使管理器缓存无效。失效后的结果应该是最新的。