2021-02-02 10:34:52 +03:00
|
|
|
package hscan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
type data struct {
|
|
|
|
Omit string `redis:"-"`
|
|
|
|
Empty string
|
|
|
|
|
|
|
|
String string `redis:"string"`
|
|
|
|
Bytes []byte `redis:"byte"`
|
|
|
|
Int int `redis:"int"`
|
|
|
|
Uint uint `redis:"uint"`
|
|
|
|
Float float32 `redis:"float"`
|
|
|
|
Bool bool `redis:"bool"`
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
type i []interface{}
|
|
|
|
|
2021-02-02 10:34:52 +03:00
|
|
|
func TestGinkgoSuite(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RunSpecs(t, "hscan")
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = Describe("Scan", func() {
|
|
|
|
It("catches bad args", func() {
|
2021-02-02 13:58:10 +03:00
|
|
|
var (
|
|
|
|
d data
|
|
|
|
)
|
2021-02-02 10:34:52 +03:00
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
Expect(Scan(i{}, i{}, &d)).NotTo(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
Expect(d).To(Equal(data{}))
|
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
Expect(Scan(i{"key"}, i{}, &d)).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"key"}, i{"1", "2"}, &d)).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"key", "1"}, i{}, nil)).To(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
var m map[string]interface{}
|
|
|
|
Expect(Scan(i{"key"}, i{"1"}, &m)).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"key"}, i{"1"}, data{})).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"key", "string"}, i{nil, nil}, data{})).To(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("scans good values", func() {
|
|
|
|
var d data
|
|
|
|
|
|
|
|
// non-tagged fields.
|
2021-02-02 13:58:10 +03:00
|
|
|
Expect(Scan(i{"key"}, i{"value"}, &d)).NotTo(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
Expect(d).To(Equal(data{}))
|
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
keys := i{"string", "byte", "int", "uint", "float", "bool"}
|
|
|
|
vals := i{"str!", "bytes!", "123", "456", "123.456", "1"}
|
|
|
|
Expect(Scan(keys, vals, &d)).NotTo(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
Expect(d).To(Equal(data{
|
|
|
|
String: "str!",
|
|
|
|
Bytes: []byte("bytes!"),
|
|
|
|
Int: 123,
|
|
|
|
Uint: 456,
|
|
|
|
Float: 123.456,
|
|
|
|
Bool: true,
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Scan a different type with the same values to test that
|
|
|
|
// the struct spec maps don't conflict.
|
|
|
|
type data2 struct {
|
|
|
|
String string `redis:"string"`
|
|
|
|
Bytes []byte `redis:"byte"`
|
|
|
|
Int int `redis:"int"`
|
|
|
|
Uint uint `redis:"uint"`
|
|
|
|
Float float32 `redis:"float"`
|
|
|
|
Bool bool `redis:"bool"`
|
|
|
|
}
|
|
|
|
var d2 data2
|
2021-02-02 13:58:10 +03:00
|
|
|
Expect(Scan(keys, vals, &d2)).NotTo(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
Expect(d2).To(Equal(data2{
|
|
|
|
String: "str!",
|
|
|
|
Bytes: []byte("bytes!"),
|
|
|
|
Int: 123,
|
|
|
|
Uint: 456,
|
|
|
|
Float: 123.456,
|
|
|
|
Bool: true,
|
|
|
|
}))
|
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
Expect(Scan(i{"string", "float", "bool"}, i{"", "1", "t"}, &d)).NotTo(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
Expect(d).To(Equal(data{
|
|
|
|
String: "",
|
|
|
|
Bytes: []byte("bytes!"),
|
|
|
|
Int: 123,
|
|
|
|
Uint: 456,
|
|
|
|
Float: 1.0,
|
|
|
|
Bool: true,
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("omits untagged fields", func() {
|
|
|
|
var d data
|
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
Expect(Scan(i{"empty", "omit", "string"}, i{"value", "value", "str!"}, &d)).NotTo(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
Expect(d).To(Equal(data{
|
|
|
|
String: "str!",
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("catches bad values", func() {
|
|
|
|
var d data
|
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
Expect(Scan(i{"int"}, i{"a"}, &d)).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"uint"}, i{"a"}, &d)).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"uint"}, i{""}, &d)).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"float"}, i{"b"}, &d)).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"bool"}, i{"-1"}, &d)).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"bool"}, i{""}, &d)).To(HaveOccurred())
|
|
|
|
Expect(Scan(i{"bool"}, i{"123"}, &d)).To(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
})
|
|
|
|
})
|