问题描述
我正在研究Robot Framework,我的基本方法之一是用python编写的,用于构建具有n列和多个where条件的SQL查询。该功能看起来像
from pypika import Query,Table,Field
def get_query_with_filter_conditions(table_name,*column,**where):
table_name_with_no_lock = table_name + ' with (nolock)'
table = Table(table_name_with_no_lock)
where_condition = get_where_condition(**where)
sql_query = Query.from_(table).select(
*column
).where(
Field(where_condition)
)
return str(sql_query).replace('"','')
我在我的机器人关键字中将此方法称为:
Get Query With Filter Conditions ${tableName} ${column} &{tableFilter}
此函数在另外两个关键字中调用。对于一个它工作正常。对于另一个,它总是抛出错误
效果很好的关键字如下:
Verify the ${element} in ${grid} is fetched from ${column} column in ${tableName} table from DB
[Documentation] Verifies Monetary values in the View Sale Grid
${feature}= Get Variable Value ${FEATURE_NAME}
${filterValue}= Get Variable value ${FILTER_VALUE}
${queryFilter}= Get the Test Data valid ${filterValue} ${feature}
&{tableFilter}= Create Dictionary
Set To Dictionary ${tableFilter} ${filterValue}=${queryFilter}
Set To Dictionary ${tableFilter} form_of_payment_type=${element}
${tableName}= Catenate SEParaTOR=. SmartPRASales ${tableName}
${query}= Get query with Filter Conditions ${tableName} ${column} &{tableFilter}
Log ${query}
@{queryResult}= CommonPage.Get a Column values from DB ${query}
Verify ${element} drop down contains all values from ${column} column in ${tableName} table
[Documentation] To verify the drop down has all values from DB
${feature}= Get Variable Value ${FEATURE_NAME}
${filterElement}= Run Keyword If '${element}'=='batch_type' Set Variable transaction_type
... ELSE IF '${element}'=='channel' Set Variable agency_type
... ELSE Set Variable ${element}
&{tableFilter}= Create Dictionary
Set To Dictionary ${tableFilter} table_name=GENERAL
Set To Dictionary ${tableFilter} column_name=${filterElement}
Set To Dictionary ${tableFilter} client_id=QR
Log ${tableFilter}
Log ${tableName}
Log ${column}
${tableName}= Catenate SEParaTOR=. SmartPRAMaster ${tableName}
${query}= Get Query With Filter Conditions ${tableName} ${column} &{tableFilter}
Log ${query}
@{expectedvalues}= CommonPage.Get a Column values from DB ${query}
解决方法
该问题归因于字典中的键值对。字典中的键之一
&{tableFilter}= Create Dictionary
Set To Dictionary ${tableFilter} table_name=GENERAL
与
中的一个参数相同def get_query_with_filter_conditions(table_name,*column,**where):
将get_query_with_filter_conditions函数中的参数从table_name更改为p_table_name并起作用。由于该函数采用可以指定为命名参数的位置参数,因此python与我已经传递的table_name参数与字典中键table_name的参数混淆了。