Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test a transaction (MULTI) command with redigomock

Tags:

mocking

go

redigo

In a unittest, how can I setup redigomock to test a MULTI call with several commands included?

like image 965
Charles L. Avatar asked May 16 '26 22:05

Charles L.


1 Answers

There is no real trick to this. I found an example in the redigomock tests & then found I had a typo, which caused an error that was never returned (probably a bug). For reference,

https://github.com/rafaeljusto/redigomock/blob/master/redigomock_test.go#L501 (TestDoFlushesQueue)

shows a test that uses MULTI. If you're using go-check, it becomes something like

connection := redigomock.NewConn()
cmd1 := connection.Command("MULTI")
cmd2 := connection.Command("SET", "person-123", 123456)
cmd3 := connection.Command("EXPIRE", "person-123", 1000)
cmd4 := connection.Command("EXEC").Expect([]interface{}{"OK", "OK"})
c.Check(connection.Stats(cmd1), Equals, 1)
c.Check(connection.Stats(cmd2), Equals, 1)
c.Check(connection.Stats(cmd3), Equals, 1)
c.Check(connection.Stats(cmd4), Equals, 1)

(and if anyone is curious, here is the PR so that typos result in detectable errors https://github.com/rafaeljusto/redigomock/pull/21)

like image 101
Charles L. Avatar answered May 21 '26 16:05

Charles L.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!