Search code examples
gomockingredigo

Test a transaction (MULTI) command with redigomock


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


Solution

  • 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)