Debian Codegraph @

pkg/format/format_query_test.go

raw
1 package format
2
3 import (
4 "net/url"
5 "time"
6
7 "github.com/onsi/ginkgo/v2"
8 "github.com/onsi/gomega"
9 )
10
11 var _ = ginkgo.Describe("Query Formatter", func() {
12 var pkr PropKeyResolver
13
14 ginkgo.BeforeEach(func() {
15 ts = &testStruct{}
16 pkr = NewPropKeyResolver(ts)
17 _ = pkr.SetDefaultProps(ts)
18 })
19 ginkgo.Describe("Creating a service URL query from a config", func() {
20 ginkgo.When("a config property has been changed from default", func() {
21 ginkgo.It("should be included in the query string", func() {
22 ts.Str = "test"
23 query := BuildQuery(&pkr)
24 // (pkr, )
25 gomega.Expect(query).To(gomega.Equal("str=test"))
26 })
27 })
28 ginkgo.When("a duration property is at its default", func() {
29 ginkgo.It("should be omitted from the query string", func() {
30 query := BuildQuery(&pkr)
31 gomega.Expect(query).NotTo(gomega.ContainSubstring("duration="))
32 })
33 })
34 ginkgo.When("a duration property has been changed from default", func() {
35 ginkgo.It("should be included in the query string", func() {
36 ts.Duration = 30 * time.Second
37 query := BuildQuery(&pkr)
38 gomega.Expect(query).To(gomega.ContainSubstring("duration=30s"))
39 })
40 })
41 ginkgo.When("a custom query key conflicts with a config property key", func() {
42 ginkgo.It("should include both values, with the custom escaped", func() {
43 ts.Str = "service"
44 customQuery := url.Values{"str": {"custom"}}
45 query := BuildQueryWithCustomFields(&pkr, customQuery)
46 gomega.Expect(query.Encode()).To(gomega.Equal("__str=custom&str=service"))
47 })
48 })
49 })
50 ginkgo.Describe("Setting prop values from query", func() {
51 ginkgo.When("a custom query key conflicts with a config property key", func() {
52 ginkgo.It(
53 "should set the config prop from the regular and return the custom one unescaped",
54 func() {
55 ts.Str = "service"
56 serviceQuery := url.Values{"__str": {"custom"}, "str": {"service"}}
57 query, err := SetConfigPropsFromQuery(&pkr, serviceQuery)
58 gomega.Expect(err).NotTo(gomega.HaveOccurred())
59 gomega.Expect(ts.Str).To(gomega.Equal("service"))
60 gomega.Expect(query.Get("str")).To(gomega.Equal("custom"))
61 },
62 )
63 })
64 })
65 })