这两个都在“生产”中工作。后者仅适用于测试。现在我已经在生产和测试中得到了一些工作,我想了解为什么我必须走整条游标而不是Django路线。我认为这个问题与交易有关,但我并不积极,因为我在晚上8:30坐在这里,这让我烦恼。
这与this question有关,我认为我得到了答案(和理解),但我没有。我的测试是一个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)
谢谢!
答案 0 :(得分: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
"""
您同时获得pmPropDef.id
和pmProp.propid
,即使它们相同且前者不会映射到Property
字段。
一般情况下,使用.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
"""
如果确实已经需要强制值,请尝试在同一查询中强制它。