首頁  >  文章  >  後端開發  >  使用 go-sqlmock 測試 gorm 問題,將查詢與mock.ExpectQuery 和 regexp.QuoteMeta 進行比較

使用 go-sqlmock 測試 gorm 問題,將查詢與mock.ExpectQuery 和 regexp.QuoteMeta 進行比較

WBOY
WBOY轉載
2024-02-11 12:48:08405瀏覽

使用 go-sqlmock 测试 gorm 问题,将查询与mock.ExpectQuery 和 regexp.QuoteMeta 进行比较

在開發過程中,使用 go-sqlmock 來測試 gorm 的問題是一種常見的需求。 go-sqlmock 是一個用來模擬資料庫操作的工具,而 gorm 則是一個流行的 Go 語言 ORM 函式庫。在測試過程中,我們經常需要比較查詢語句是否符合預期。為了做到這一點,我們可以使用 mock.ExpectQuery 和 regexp.QuoteMeta 來進行比較。這種方法能夠幫助我們更好地測試和調試程式碼,確保程式的正確性和穩定性。接下來,我們將詳細介紹如何使用 go-sqlmock 進行 gorm 測試,並展示如何使用 mock.ExpectQuery 和 regexp.QuoteMeta 進行查詢語句的比較。

問題內容

我在比較預期查詢與 gorm 的真實查詢時遇到問題,這是我的程式碼:

package repository

import (
    "regexp"
    "testing"

    "github.com/data-dog/go-sqlmock"
    "your_go_root/pkg/domain"
    "github.com/stretchr/testify/assert"
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
)


var successgettransaction domain.transaction = domain.transaction{
    id:          2,
    buyerid:     2,
    sellerid:    5,
    itemid:      2,
    messageid:   2,
    expireddate: "2022-09-010 01:01:00",
    createdat:   "2022-09-08 01:01:00",
}

func testsuccessgettransactionbyid(t *testing.t) {

    db, mock, err := sqlmock.new()
    assert.noerror(t, err)
    gdb, err := gorm.open(mysql.new(mysql.config{
        conn:                      db,
        skipinitializewithversion: true,
    }), &gorm.config{})
    assert.noerror(t, err)

    rows := sqlmock.newrows([]string{"id", "buyer_id", "seller_id", "item_id", "message_id", "expired_date", "created_at"}).
        addrow(2, 2, 5, 2, 2, "2022-09-010 01:01:00", "2022-09-08 01:01:00")
    mock.expectquery(regexp.quotemeta("select * from transaction where id = ?;")).willreturnrows(rows)

    repo := defaultclient(gdb)
    actualsectionlist, _ := repo.gettransactionbyid(2)
    
    assert.equal(t, successgettransaction, actualsectionlist, "ambas listas deberian ser iguales")
    assert.noerror(t, mock.expectationsweremet())
}

這是模組域:

package domain

type transaction struct {
    id          int64  `gorm:"primarykey;column:id"`
    buyerid     int64  `gorm:"column:buyer_id"`
    sellerid    int64  `gorm:"column:seller_id"`
    itemid      int    `gorm:"column:item_id"`
    messageid   int    `gorm:"column:message_id"`
    expireddate string `gorm:"column:expired_date"`
    createdat   string `gorm:"column:created_at"`
}

func (transaction) tablename() string {
    return "transaction"
}

type transactionstatus struct {
    id             int64  `gorm:"primarykey;column:id"`
    transactionid  int64  `gorm:"column:transaction_id"`
    status         int    `gorm:"column:status"`
    notificationid int    `gorm:"column:notification_id"`
    createdat      string `gorm:"column:created_at"`
}

func (transactionstatus) tablename() string {
    return "transaction_status"
}

這是我正在測試的功能:

package repository

import (
    "fmt"

    "your_go_root/pkg/domain"
    "gorm.io/gorm"
)

type repositoryclient interface {
    gettransactionbyid(id int) (domain.transaction, error)
}

type repositoryclient struct {
    db *gorm.db
}

func defaultclient(db *gorm.db) repositoryclient {
    return &repositoryclient{
        db: db,
    }
}

func (rc repositoryclient) gettransactionbyid(id int) (domain.transaction, error) {
    trans := domain.transaction{}
    status := rc.db.where("id = ?", id).find(&trans)

    if status.error != nil {
        return domain.transaction{}, status.error
    }
    if trans == (domain.transaction{}) {
        return domain.transaction{}, fmt.errorf("error finding transaction id %v", id)
    }
    return trans, nil
}

這是我從控制台收到的錯誤:

Query: could not match actual sql: "SELECT * FROM `transaction` WHERE id = ?" with expected regexp "SELECT \* FROM transaction WHERE id = \?;"[0m[33m[0.218ms] [34;1m[rows:0][0m SELECT * FROM `transaction` WHERE id = 2

在本節中存在一個用「select(.*)」取代的答案,但根據我讀到的內容,這不是真正的解決方案

#解決方法

#讓我嘗試幫助解決這個問題。我下載了您的所有文件,並且 domain.gorepository.go 對我來說看起來不錯。
但是,我在 repository_test.go 檔案中發現了一些小問題:

  1. 您所寫的 sql 查詢中缺少反引號
  2. 查詢最後額外的 ;
  3. #缺少對 withargs(2) 方法的呼叫

如果您調整了這些小問題,您應該得到如下所示的程式碼:

// ... omitted for brevity
func TestSuccessGetTransactionByID(t *testing.T) {
    db, mock, err := sqlmock.New()
    assert.NoError(t, err)
    gdb, err := gorm.Open(mysql.New(mysql.Config{
        Conn:                      db,
        SkipInitializeWithVersion: true,
    }), &gorm.Config{})
    assert.NoError(t, err)

    rows := sqlmock.NewRows([]string{"id", "buyer_id", "seller_id", "item_id", "message_id", "expired_date", "created_at"}).AddRow(2, 2, 5, 2, 2, "2022-09-010 01:01:00", "2022-09-08 01:01:00")
    mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `transaction` WHERE id = ?")).WithArgs(2).WillReturnRows(rows)

    repo := DefaultClient(gdb)
    actualSectionList, _ := repo.GetTransactionByID(2)

    assert.Equal(t, successGetTransaction, actualSectionList, "ambas listas deberian ser iguales")
    assert.NoError(t, mock.ExpectationsWereMet())
}

然後,如果您嘗試執行測試,它應該可以工作。
如果這解決了您的問題,請告訴我,謝謝!

以上是使用 go-sqlmock 測試 gorm 問題,將查詢與mock.ExpectQuery 和 regexp.QuoteMeta 進行比較的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除