我有问题比较查询预期与真正的查询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
在这个seccion中存在一个答案,它被替换为“SELECT(.*)”,但是根据我所读到内容,这不是一个真实的的解决方案
1条答案
按热度按时间oknwwptz1#
让我试着帮助解决这个问题。我下载了你所有的文件,
domain.go
和repository.go
看起来很好。但是,我在
repository_test.go
文件中发现了一些小问题:1.您编写的SQL查询中缺少反引号
1.在查询的末尾添加额外的
;
1.缺少对方法
WithArgs(2)
的调用如果您调整了这些小问题,您应该有一个代码,看起来像这样:
然后,如果您尝试运行测试,它应该工作。
如果这解决了你的问题,请告诉我,谢谢!