Django testing - Multiple queries when externally

2019-08-04 11:26发布

问题:

I have a legacy application which is (currently) using Django to effectively display data. A sample of one of my working tests looks like this.

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)

As you can see it's basic A/B Testing. Now I've found a limitations and I can't seem to get around in that multiple external add - check - loop queries are failing. Modifying the above code fails because it appears query is failing to even run.

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)

I've read about the CACHE_BACKEND but that didn't seem to help. Any other ideas?? After further investigation this appears to not be related to my external db at all. Arghh.. It feels like monday!

  1. Is this a cache problem BTW - Setting CACHE_BACKEND = 'dummy:///' or 'locmem:///' did nothing.
  2. How do I better diagnosis this problem??

Thanks

Update

Here was the final answer - 2 small tweaks.. Based on Daniel and Severio. Much appreciated the pointers!!

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:

That's probably not a transaction issue. The Manager (Project.objects) needs to be informed of a data change, because it's designed to live for a short period of time.

You are querying twice the same query Project.objects.get(name=tree[1].name) and the manager won't execute it again, because it thinks it has already the correct data.

Just before the second query, do

Project.objects.update()

to invalidate the manager cache. The results after the invalidation should be up to date.