同一文件中有多个Postgresql查询选择如何对两者进行单元测试?

问题描述

在这个单元测试/代码覆盖“哲学”中,我太新了。我有一个.go文件,对PostGres有两个不同的查询(选择),第一个的结果集在第二个打开了用于过滤的值。我正在为该代码编写代码覆盖率的单元测试,但遇到了问题。在完成第一个查询的案例后,我尝试从第二个查询开始,如果我将第一个查询错误模拟为nil,它将继续第二个查询

var mock sqlmock.sqlmock
db.Postgres,mock,_ = sqlmock.New()
defer db.Postgres.Close()
mock.ExpectBegin()
query1Rows := sqlmock.
    NewRows([]string{"ID","DESCRIPTION","ALLOW_COMBINE_COUPON","START_DATE","END_DATE","CHANNELS","RELEASE_STATUS","PROMOTION_TYPE","COUPON_LIMIT_TIMES_USED","BUSInesS_OWNER_CODE","ACTIVE_FLAG","BUY_BASKET_ID","GET_BASKET_ID","BUY_BASKET_COUNT","GET_BASKET_COUNT","CREATION_DATE_TIME"}).
    AddRow(311151,"$45 Off $200 Minimum Purchase","N",time.Date(2019,5,7,00,time.UTC),time.Date(2022,12,31,"BSD,JMC,JML,POS,WEB","RLS","3","MRKT","Y",120223,81434,1,"07-MAY-2019 10:50:28xFF")

query2Rows := sqlmock.
    NewRows([]string{"ID","BUY_GET","SKU_ID","SKU_QTY","required","DEPT","CLASS","SUBCLASS","AMOUNT","PORCENTAGE","MINIMUM_AMOUNT","PROPORTIONAL_ALLOCATION_FLAG","vendOR"}).
    AddRow(81434,"G","",45.0,"")

mock.
    ExpectQuery(`SELECT A\.ID,A\.DESCRIPTION,A\.ALLOW_COMBINE_COUPON,A\.START_DATE,A\.END_DATE,A\.CHANNELS,A\.RELEASE_STATUS,`).
    WillReturnRows(query1Rows).
    WillReturnError(nil)

mock.
    ExpectQuery(`SELECT A\.ID,A\.BUY_GET,A\.SKU_ID,`).
    WillReturnRows(query2Rows).
    WillReturnError(nil) 

好吧,由于它在第一个查询的模拟中给我查询“ SELECT A.ID,A.DESCRIPTION,A.ALLOW_COMBINE_COUPO ... + 506 more”的错误,因此无法正常工作。

关于如何为第二个查询编写测试用例的任何建议,而又不将它们分开放在不同的函数文件中?预先感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)