如何解决SQLMock 和 Gorm:模拟 Postgres 插入
当我尝试使用 sqlMock 和 Gorm.io 模拟 Postgres 插入时,我收到一个错误,表明查询不是预期的。我尝试使用 regexp.QuoteMeta()
来包装和转义我的字符串,但它不起作用。我添加并删除了args和result,但错误继续出现
我给你原始的 PostgresQuery 和 usermodel
//RAW QUERY
INSERT INTO "users" ("id","name","surname","birthdate","company","custom_claims","deleted") VALUES ($1,$2,$3,$4,$5,$6,$7)' with args [{Name: Ordinal:1 Value:my_user_id} {Name: Ordinal:2 Value:<nil>} {Name: Ordinal:3 Value:<nil>} {Name: Ordinal:4 Value:<nil>} {Name: Ordinal:5 Value:<nil>} {Name: Ordinal:6 Value:<nil>} {Name: Ordinal:7 Value:<nil>}]
//Gorm model
type User struct {
ID string `gorm:"primaryKey"`
Name *string
Surname *string
Birthdate *time.Time
Company *string
CustomClaims *json.RawMessage
Deleted gorm.DeletedAt
}
func (repository Repository) createuser(user users.User) (*users.User,error) {
newUser := toRepositoryModel(user)
err := repository.db.Create(newUser).Error //db -> *gorm.DB
//....
}
//TEST
const expectedQuery = `INSERT INTO "users" ("id",$7)' with args [{Name: Ordinal:1 Value:my_user_id} {Name: Ordinal:2 Value:<nil>} {Name: Ordinal:3 Value:<nil>} {Name: Ordinal:4 Value:<nil>} {Name: Ordinal:5 Value:<nil>} {Name: Ordinal:6 Value:<nil>} {Name: Ordinal:7 Value:<nil>}]`
suite.mock.ExpectQuery(regexp.QuoteMeta(experctedQuery)) //HOW SHOULD BE MODIFIED?
user,err2 := postgres.createuser(users.User{
ID: ID,})
//ERROR
"call to ExecQuery 'INSERT INTO \"users\" (\"id\",\"name\",\"surname\",\"birthdate\",\"company\",\"custom_claims\",\"deleted\") VALUES ($1,$7)' with args [{Name: Ordinal:1 Value:my_user_id} {Name: Ordinal:2 Value:<nil>} {Name: Ordinal:3 Value:<nil>} {Name: Ordinal:4 Value:<nil>} {Name: Ordinal:5 Value:<nil>} {Name: Ordinal:6 Value:<nil>} {Name: Ordinal:7 Value:<nil>}],was not expected,next expectation is: ExpectedQuery => expecting Query,QueryContext or QueryRow which:\n - matches sql: 'INSERT INTO \"users\" \\(\"id\",\"deleted\"\\) VALUES \\(\\$1,\\$2,\\$3,\\$4,\\$5,\\$6,\\$7\\)' with args \\[\\{Name: Ordinal:1 Value:my_user_id\\} \\{Name: Ordinal:2 Value:<nil>\\} \\{Name: Ordinal:3 Value:<nil>\\} \\{Name: Ordinal:4 Value:<nil>\\} \\{Name: Ordinal:5 Value:<nil>\\} \\{Name: Ordinal:6 Value:<nil>\\} \\{Name: Ordinal:7 Value:<nil>\\}\\]'\n - is without arguments",}
call to ExecQuery 'INSERT INTO "users" ("id",QueryContext or QueryRow which:
- matches sql: 'INSERT INTO "users" \("id","deleted"\) VALUES \(\$1,\$2,\$3,\$4,\$5,\$6,\$7\)' with args \[\{Name: Ordinal:1 Value:my_user_id\} \{Name: Ordinal:2 Value:<nil>\} \{Name: Ordinal:3 Value:<nil>\} \{Name: Ordinal:4 Value:<nil>\} \{Name: Ordinal:5 Value:<nil>\} \{Name: Ordinal:6 Value:<nil>\} \{Name: Ordinal:7 Value:<nil>\}\]'
- is without arguments
occurred
解决方法
我已经成功了。
suite.mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO "users" ("id","name","surname","birthdate","company","custom_claims","deleted") VALUES ($1,$2,$3,$4,$5,$6,$7)`)).WithArgs(ID,nil,nil).WillReturnResult(sqlmock.NewResult(0,1))
更新
Postgres 选择:
//Simulate returned row(s)
userMockRows := sqlmock.NewRows([]string{"id","deleted"}).AddRow(ID,nil)
//Expect SELECT
suite.mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "users" WHERE id = $1`)).WithArgs(ID).WillReturnRows(userMockRows)
Postgres 更新:
suite.mock.ExpectExec(regexp.QuoteMeta(`UPDATE "users" SET "name"=$1,"surname"=$2,"birthdate"=$3,"company"=$4,"custom_claims"=$5,"deleted"=$6 WHERE "id" = $7`)).WithArgs(newName,ID).WillReturnResult(sqlmock.NewResult(0,1))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。