0

这两个都在“生产”中工作,后者只在测试中工作。现在我已经在生产和测试中得到了一些工作,我想了解为什么我必须走整个游标路线而不是 Django 路线。我相信问题与交易有关,但我并不积极,因为我晚上 8:30 坐在这里,这让我很烦。

这与这个问题有关,我认为我有我的答案(和理解),但可惜我没有。我的测试是一个 A/B,其中 A 在 Django 外部注入,B 与已知的 A 进行比较。提供的答案解决了我的部分问题,但是当我添加更多测试时,问题不断浮出水面。

我深入研究并假设它是 RawQuery 没有提交交易,但没有金额transaction.commit似乎可以解决它。我还从中删除了django.testing.TestCase它并直接进行了单元测试。我想我已经尝试了每一种组合,但我对 SQL 或事务支持不是很精通,现在我想知道为什么一个有效而一个无效......

如果有人在这里有任何见解,我将不胜感激!

更新 2已修改和清理但仍然失败..

    # BUG: https://code.djangoproject.com/ticket/12768
    # - Requirement for pmProp.* - This (in-part) forced me to shift to raw.
    sqlraw = """ SELECT
                    pmProp.propid as propid_id,
                    pmProp.owner as owner,
                    pmProp.ownertype as ownertype,
                    pmProp.behavior as behavior,
                    pmProp.value as value_id,
                    pmPropDef.id as propdef_id,
                    pmPropDef.name as name,
                    pmPropDef.datatype as datatype,
                    pmPropDef.choicetype as choicetype,
                    pmPropDef.definition as definition_id,
                    pmPropDef.ptrig as prop_trigger,
                    pmPropDef.units as units,
                    IFNULL(pmPropShort.str, pmPropLong.str) as str_value FROM pmProp
                INNER JOIN pmPropDef ON pmProp.propid=pmPropDef.id AND pmPropDef.name=%s
                LEFT JOIN pmPropShort ON sid=pmProp.value
                LEFT JOIN pmPropLong ON lid=-pmProp.value
                WHERE pmProp.ownertype=%s AND pmProp.owner=%s AND pmPropDef.id=pmProp.propid
                """
    if explicit:
            sqlraw += " AND pmProp.behavior='explicit'"

    # TRY ONE - DOES NOT WORK FOR TESTING..
    # This will NOT work for testing - It simply doesn't get the value
    # when repeatedly inserting from pm and checking the value.
    #
    # Note if you use this you must update the sqlraw to include pmProp.* bug..
    #
    #try:
    #    result = list(Property.objects.raw(sqlraw, [property, owner, self.id]))[0]
    #    result.value = self.coerce_datatype(result.str_val, result.datatype)
    #except IndexError:
    #    result = None
    # END TRY ONE

    # Try TWO:  THIS WORKS for both
    cursor = connections['catalog'].cursor()
    cursor.execute(sqlraw, [property, owner, self.id])
    row = cursor.fetchone()
    transaction.commit_unless_managed(using='catalog')

    if row:
        field_map =  "propid_id owner ownertype behavior value_id propdef_id "
        field_map += "name datatype choicetype definition_id prop_trigger "
        field_map +=  "units str_value"
        field_map = field_map.split()
        class PropVal(object): pass
        result = PropVal()
        result.__dict__=dict(zip(field_map, row))
        result.value = self.coerce_datatype(result.str_value, result.datatype)
        try:
            log.info("%s %s=%s %s" % (property.capitalize(), result.name,
                                               result.value, result.units))
        except UnicodeDecodeError: pass
    else:
        result = None
    # END TRY Two

更新

这是一个示例 A/B 测试。

from django.db import connection, transaction
from unittest import TestCase
#from django.test import TestCase, TransactionTestCase
from apps.pmCatalog.utility.ICMPM.pm import Pm
from apps.pmCatalog.models import Property, Site, Project, Variant, Library, LibraryType, Release
import settings
import datetime

import logging
log = logging.getLogger(__name__)

class PropertyTests(TestCase):

    def test_add_property_value(self):
        """Test the ability to add a property and retrieve a property"""

        prop_types = [("string", "Funny Business"), ("integer", 1234), ("real", 12.34) ]
        pm = Pm(mysql_db='test_bugs')
        tree = pm.add_release_tree()

        for prop_type, pmvalue in prop_types:
            # Add a property definition for a branch (like a project)
            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)
            #Project.objects.update()
            project = Project.objects.get(name=pmproject.name)
            property = project.get_property(pmproperty)
            #When using the first one this ALWAYS returned None!
            self.assertEqual(str(pmvalue), property.str_value)
            self.assertEqual(pmvalue, property.value)

谢谢!

4

1 回答 1

1

我看到两个问题

sqlraw = """SELECT pmProp.*, pmPropDef.id, pmPropDef.name, pmPropDef.units,
            IFNULL(pmPropShort.str, pmPropLong.str) as value FROM pmProp
            INNER JOIN pmPropDef ON pmProp.propid=pmPropDef.id AND pmPropDef.name=%s
            LEFT JOIN pmPropShort ON sid=pmProp.value
            LEFT JOIN pmPropLong ON lid=-pmProp.value
            WHERE pmProp.ownertype=%s AND pmProp.owner=%s AND pmPropDef.id = pmProp.propid
            """
  1. 你得到两个pmPropDef.idand pmProp.propid,即使它们相等并且前者不会映射到一个Property字段。

  2. 通常,使用.raw()查询,您必须返回正确的名称(SELECT pmPropDef.name AS name对于每个字段使用等等,或者使用可选的转换映射到 raw() 方法,它将列映射到属性。直接返回很容易真实姓名

尝试以下操作(调整以匹配表中的实际列名和模型上的字段名):

sqlraw = """SELECT 
                pmProp.id as id,
                pmProp.owner as owner,
                pmProp.ownertype as ownertype,
                pmProp.behavior as behavior,
                pmProp.propdef_id as propdef_id,
                pmPropDef.name as name, 
                pmPropDef.units as units,
                IFNULL(pmPropShort.str, pmPropLong.str) as str_value
            FROM pmProp
            INNER JOIN pmPropDef ON pmProp.propid=pmPropDef.id AND pmPropDef.name=%s
            LEFT JOIN pmPropShort ON sid=pmProp.value
            LEFT JOIN pmPropLong ON lid=-pmProp.value
            WHERE pmProp.ownertype=%s AND pmProp.owner=%s AND pmPropDef.id = pmProp.propid
            """

如果您确实已经需要强制值,请尝试在同一个查询中强制它。

于 2011-08-12T04:16:57.733 回答