Skip to content

Commit dbffd84

Browse files
author
Guo Hui
committed
feat: Improve HSet unit tests
1 parent 46f878b commit dbffd84

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

commands_test.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -2585,20 +2585,45 @@ var _ = Describe("Commands", func() {
25852585
Set3 time.Duration `redis:"set3,omitempty"`
25862586
Set4 string `redis:"set4,omitempty"`
25872587
Set5 time.Time `redis:"set5,omitempty"`
2588+
Set6 childStruct `redis:"set6,omitempty"`
25882589
}
2590+
25892591
hSet = client.HSet(ctx, "hash3", &setOmitEmpty{
25902592
Set1: "val",
25912593
})
25922594
Expect(hSet.Err()).NotTo(HaveOccurred())
25932595
Expect(hSet.Val()).To(Equal(int64(1)))
25942596

2595-
var dest setOmitEmpty
25962597
hGetAll := client.HGetAll(ctx, "hash3")
25972598
Expect(hGetAll.Err()).NotTo(HaveOccurred())
25982599
Expect(hGetAll.Val()).To(Equal(map[string]string{
25992600
"set1": "val",
26002601
}))
2601-
Expect(hGetAll.Scan(&dest)).NotTo(HaveOccurred())
2602+
var hash3 setOmitEmpty
2603+
Expect(hGetAll.Scan(&hash3)).NotTo(HaveOccurred())
2604+
Expect(hash3.Set1).To(Equal("val"))
2605+
Expect(hash3.Set2).To(Equal(0))
2606+
Expect(hash3.Set3).To(Equal(time.Duration(0)))
2607+
Expect(hash3.Set4).To(Equal(""))
2608+
Expect(hash3.Set5).To(Equal(time.Time{}))
2609+
Expect(hash3.Set6).To(Equal(childStruct{}))
2610+
2611+
now := time.Now()
2612+
hSet = client.HSet(ctx, "hash4", setOmitEmpty{
2613+
Set1: "val",
2614+
Set6: childStruct{
2615+
Date: now,
2616+
},
2617+
})
2618+
Expect(hSet.Err()).NotTo(HaveOccurred())
2619+
Expect(hSet.Val()).To(Equal(int64(2)))
2620+
2621+
hGetAll = client.HGetAll(ctx, "hash4")
2622+
Expect(hGetAll.Err()).NotTo(HaveOccurred())
2623+
Expect(hGetAll.Val()).To(Equal(map[string]string{
2624+
"set1": "val",
2625+
"set6": fmt.Sprintf("{\"Date\":\"%s\"}", now.Format(time.RFC3339Nano)),
2626+
}))
26022627
})
26032628

26042629
It("should HSetNX", func() {
@@ -7633,3 +7658,15 @@ func deref(viface interface{}) interface{} {
76337658
}
76347659
return v.Interface()
76357660
}
7661+
7662+
type childStruct struct {
7663+
Date time.Time `redis:"date,omitempty"`
7664+
}
7665+
7666+
func (c childStruct) MarshalBinary() ([]byte, error) {
7667+
return json.Marshal(&c)
7668+
}
7669+
7670+
func (c childStruct) UnmarshalBinary(data []byte) error {
7671+
return json.Unmarshal(data, &c)
7672+
}

0 commit comments

Comments
 (0)