github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/metax/meta_test.go (about) 1 package metax_test 2 3 import ( 4 "context" 5 "testing" 6 7 . "github.com/onsi/gomega" 8 9 . "github.com/machinefi/w3bstream/pkg/depends/kit/metax" 10 ) 11 12 func TestParseMeta(t *testing.T) { 13 t.Run("parse id", func(t *testing.T) { 14 meta := ParseMeta("xxxxxx") 15 NewWithT(t).Expect(meta.String()).To(Equal("_id=xxxxxx")) 16 }) 17 18 t.Run("parse meta", func(t *testing.T) { 19 meta := ParseMeta("operator=1&operator=2&_id=xxx") 20 NewWithT(t).Expect(meta.Get("operator")).To(Equal("1")) 21 NewWithT(t).Expect(meta.String()).To(Equal("_id=xxx&operator=1&operator=2")) 22 }) 23 } 24 25 func TestMeta(t *testing.T) { 26 27 t.Run("ContextConcat", func(t *testing.T) { 28 ctx := ContextWith(context.Background(), "key", "1") 29 ctx = ContextWithMeta(ctx, (Meta{}).With("key", "2", "3")) 30 31 NewWithT(t).Expect(GetMetaFrom(ctx)["key"]).To(Equal([]string{"1", "2", "3"})) 32 }) 33 34 t.Run("ContextOverwrite", func(t *testing.T) { 35 ctx := ContextWith(context.Background(), "_key", "1") 36 ctx = ContextWithMeta(ctx, (Meta{}).With("_key", "2", "3")) 37 38 NewWithT(t).Expect(GetMetaFrom(ctx)["_key"]).To(Equal([]string{"2", "3"})) 39 }) 40 41 t.Run("EmptyKeyIgnore", func(t *testing.T) { 42 ctx := ContextWith(context.Background(), "", "1") 43 NewWithT(t).Expect(GetMetaFrom(ctx)).To(HaveLen(0)) 44 }) 45 } 46 47 type Some struct{ Ctx } 48 49 func (s *Some) WithContext(ctx context.Context) *Some { 50 return &Some{Ctx: s.Ctx.WithContext(ctx)} 51 } 52 53 func TestCtx(t *testing.T) { 54 s := &Some{} 55 s2 := s.WithContext(ContextWith(context.Background(), "k", "1")) 56 57 NewWithT(t).Expect(GetMetaFrom(s.Context()).Get("k")).To(Equal("")) 58 NewWithT(t).Expect(GetMetaFrom(s2.Context()).Get("k")).To(Equal("1")) 59 }