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-03 13:45:02 +03:00
|
|
|
var d data
|
2021-02-02 10:34:52 +03:00
|
|
|
|
2021-02-03 13:45:02 +03:00
|
|
|
Expect(Scan(&d, i{}, i{})).NotTo(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
Expect(d).To(Equal(data{}))
|
|
|
|
|
2021-02-03 13:45:02 +03:00
|
|
|
Expect(Scan(&d, i{"key"}, i{})).To(HaveOccurred())
|
|
|
|
Expect(Scan(&d, i{"key"}, i{"1", "2"})).To(HaveOccurred())
|
|
|
|
Expect(Scan(nil, i{"key", "1"}, i{})).To(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
|
2021-02-02 13:58:10 +03:00
|
|
|
var m map[string]interface{}
|
2021-02-03 13:45:02 +03:00
|
|
|
Expect(Scan(&m, i{"key"}, i{"1"})).To(HaveOccurred())
|
|
|
|
Expect(Scan(data{}, i{"key"}, i{"1"})).To(HaveOccurred())
|
|
|
|
Expect(Scan(data{}, i{"key", "string"}, i{nil, nil})).To(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("scans good values", func() {
|
|
|
|
var d data
|
|
|
|
|
|
|
|
// non-tagged fields.
|
2021-02-03 13:45:02 +03:00
|
|
|
Expect(Scan(&d, i{"key"}, i{"value"})).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"}
|
2021-02-03 13:45:02 +03:00
|
|
|
Expect(Scan(&d, keys, vals)).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-03 13:45:02 +03:00
|
|
|
Expect(Scan(&d2, keys, vals)).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-03 13:45:02 +03:00
|
|
|
Expect(Scan(&d, i{"string", "float", "bool"}, i{"", "1", "t"})).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-03 13:45:02 +03:00
|
|
|
Expect(Scan(&d, i{"empty", "omit", "string"}, i{"value", "value", "str!"})).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-03 13:45:02 +03:00
|
|
|
Expect(Scan(&d, i{"int"}, i{"a"})).To(HaveOccurred())
|
|
|
|
Expect(Scan(&d, i{"uint"}, i{"a"})).To(HaveOccurred())
|
|
|
|
Expect(Scan(&d, i{"uint"}, i{""})).To(HaveOccurred())
|
|
|
|
Expect(Scan(&d, i{"float"}, i{"b"})).To(HaveOccurred())
|
|
|
|
Expect(Scan(&d, i{"bool"}, i{"-1"})).To(HaveOccurred())
|
|
|
|
Expect(Scan(&d, i{"bool"}, i{""})).To(HaveOccurred())
|
|
|
|
Expect(Scan(&d, i{"bool"}, i{"123"})).To(HaveOccurred())
|
2021-02-02 10:34:52 +03:00
|
|
|
})
|
|
|
|
})
|