DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.16k stars 407 forks source link

mock update with one more paras #270

Closed qinghai5060 closed 5 months ago

qinghai5060 commented 3 years ago

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 "updateat" = $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 "updateat" = $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?

Strang commented 2 years ago

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 commented 2 years ago

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

diegommm commented 5 months ago

Hi @qinghai5060! Closing as there is not enough information to help you, unfortunately. If the problem persists, reopen and provide the following information:

Thank you!