go-sqlmock icon indicating copy to clipboard operation
go-sqlmock copied to clipboard

mock update with one more paras

Open qinghai5060 opened this issue 4 years ago • 2 comments
trafficstars

what happened: when I want to test

_, err := o.QueryTable(Quota{}).Filter("project", project).Update(orm.Params{
		"used":      used,
		"update_at": time.Now().UTC(),
	})

expected sql I tested with UPDATE "quota" SET "used" = $1, "update_at" = $2 WHERE "id" ..., It sometimes happen that the actual is "UPDATE "quota" SET "update_at" = $1, "used" = $2 WHERE "id"", and error occures like: [UPDATE "quota" SET "update_at" = $1, "used" = $2 WHERE "id" IN ( SELECT T0."id" FROM"quota" T0 WHERE T0."project" = $3 AND T0."type" = $4 )] - 2021-09-27 02:50:17.460540895 +0000 UTC, 11, project001, key - call to ExecQuery 'UPDATE "quota" SET "update_at" = $1, "used" = $2 WHERE "id" IN ( SELECT T0."id" FROM "quota" T0 WHERE T0."project" = $3 AND T0."type" = $4 )' with args [{Name: Ordinal:1 Value:2021-09-27 02:50:17.460540895 +0000 UTC} {Name: Ordinal:2 Value:11} {Name: Ordinal:3 Value:project001} {Name: Ordinal:4 Value:key}] was not expected I use beego orm lib and orm.Params is a map. For that reason, it is not sorted. Is there any way to solve this?

qinghai5060 avatar Sep 27 '21 04:09 qinghai5060

Same issue for me, hope to provide some function like:

mock.ExpectExec(regexp.QuoteMeta("UPDATE `quota`")).
    WithSets(map[string]driver.Value{
        "used": 1,
        "update_at": time.Now(),
    })

Strang avatar Apr 26 '22 12:04 Strang

I write some code to solve this problem, but I think this code is too tricky to open a MR. Take your own responsibility to use it. If someone has some better idea, plz let me know. :)

https://github.com/Strang/go-sqlmock/commit/f633abcc5e0d0135cf45c939da4b49d44c3e9d3c

Strang avatar Apr 28 '22 06:04 Strang