
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
github.com/rafaeljusto/redigomock
Advanced tools
Easy way to unit test projects using redigo library (Redis client in go). You can find the latest release here.
go get -u github.com/rafaeljusto/redigomock
Here is an example of using redigomock, for more information please check the API documentation.
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/rafaeljusto/redigomock"
)
type Person struct {
Name string `redis:"name"`
Age int `redis:"age"`
}
func RetrievePerson(conn redis.Conn, id string) (Person, error) {
var person Person
values, err := redis.Values(conn.Do("HGETALL", fmt.Sprintf("person:%s", id)))
if err != nil {
return person, err
}
err = redis.ScanStruct(values, &person)
return person, err
}
func main() {
// Simulate command result
conn := redigomock.NewConn()
cmd := conn.Command("HGETALL", "person:1").ExpectMap(map[string]string{
"name": "Mr. Johson",
"age": "42",
})
person, err := RetrievePerson(conn, "1")
if err != nil {
fmt.Println(err)
return
}
if conn.Stats(cmd) != 1 {
fmt.Println("Command was not used")
return
}
if person.Name != "Mr. Johson" {
fmt.Printf("Invalid name. Expected 'Mr. Johson' and got '%s'\n", person.Name)
return
}
if person.Age != 42 {
fmt.Printf("Invalid age. Expected '42' and got '%d'\n", person.Age)
return
}
// Simulate command error
conn.Clear()
cmd = conn.Command("HGETALL", "person:1").ExpectError(fmt.Errorf("Simulate error!"))
person, err = RetrievePerson(conn, "1")
if err == nil {
fmt.Println("Should return an error!")
return
}
if conn.Stats(cmd) != 1 {
fmt.Println("Command was not used")
return
}
fmt.Println("Success!")
}
package main
import "github.com/rafaeljusto/redigomock"
func CreateSubscriptionMessage(data []byte) []interface{} {
values := []interface{}{}
values = append(values, interface{}([]byte("message")))
values = append(values, interface{}([]byte("chanName")))
values = append(values, interface{}(data))
return values
}
func main() {
conn := redigomock.NewConn()
// Setup the initial subscription message
values := []interface{}{}
values = append(values, interface{}([]byte("subscribe")))
values = append(values, interface{}([]byte("chanName")))
values = append(values, interface{}([]byte("1")))
conn.Command("SUBSCRIBE", subKey).Expect(values)
conn.ReceiveWait = true
// Add a response that will come back as a subscription message
conn.AddSubscriptionMessage(CreateSubscriptionMessage([]byte("hello")))
// You need to send messages to conn.ReceiveNow in order to get a response.
// Sending to this channel will block until receive, so do it in a goroutine
go func() {
conn.ReceiveNow <- true // This unlocks the subscribe message
conn.ReceiveNow <- true // This sends the "hello" message
}()
}
// Note you cannot get access to the connection via the pool,
// the only way is to use this conn variable.
conn := redigomock.NewConn()
pool := &redis.Pool{
// Return the same connection mock for each Get() call.
Dial: func() (redis.Conn, error) { return conn, nil },
MaxIdle: 10,
}
Sometimes you need to check the executed arguments in your Redis command. For that you can use the command handler.
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/rafaeljusto/redigomock"
)
func Publish(conn redis.Conn, x, y int) error {
if x < 0 {
x = 0
}
if y < 0 {
y = 0
}
_, err := conn.Do("PUBLISH", "sumCh", int64(x+y))
return err
}
func main() {
conn := redigomock.NewConn()
conn.GenericCommand("PUBLISH").Handle(redigomock.ResponseHandler(func(args []interface{}) (interface{}, error) {{
if len(args) != 2 {
return nil, fmt.Errorf("unexpected number of arguments: %d", len(args))
}
v, ok := args[1].(int64)
if !ok {
return nil, fmt.Errorf("unexpected type %T", args[1])
}
if v < 0 {
return nil, fmt.Errorf("unexpected value '%d'", v)
}
return int64(1), nil
})
if err := Publish(conn, -1, 10); err != nil {
fmt.Println(err)
return
}
fmt.Println("Success!")
}
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.