Debian Codegraph @

internal/testutils/testutils_test.go

raw
1 package testutils
2
3 import (
4 "net/url"
5 "testing"
6
7 "github.com/onsi/ginkgo/v2"
8 "github.com/onsi/gomega"
9
10 "github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
11 "github.com/nicholas-fedor/shoutrrr/pkg/types"
12 )
13
14 type dummyConfig struct {
15 standard.EnumlessConfig
16
17 Foo uint64 `default:"-1" key:"foo"`
18 }
19
20 type dummyService struct {
21 standard.Standard
22
23 Config dummyConfig
24 }
25
26 var _ = ginkgo.Describe("the testutils package", func() {
27 ginkgo.When("calling function TestLogger", func() {
28 ginkgo.It("should not return nil", func() {
29 gomega.Expect(TestLogger()).NotTo(gomega.BeNil())
30 })
31 ginkgo.It(`should have the prefix "[Test] "`, func() {
32 gomega.Expect(TestLogger().Prefix()).To(gomega.Equal("[Test] "))
33 })
34 })
35
36 ginkgo.Describe("Must helpers", func() {
37 ginkgo.Describe("URLMust", func() {
38 ginkgo.It("should panic when an invalid URL is passed", func() {
39 failures := gomega.InterceptGomegaFailures(func() { URLMust(":") })
40 gomega.Expect(failures).To(gomega.HaveLen(1))
41 })
42 })
43
44 ginkgo.Describe("JSONRespondMust", func() {
45 ginkgo.It("should panic when an invalid struct is passed", func() {
46 notAValidJSONSource := func() {}
47 failures := gomega.InterceptGomegaFailures(
48 func() { JSONRespondMust(200, notAValidJSONSource) },
49 )
50 gomega.Expect(failures).To(gomega.HaveLen(1))
51 })
52 })
53 })
54
55 ginkgo.Describe("Config test helpers", func() {
56 var config dummyConfig
57
58 ginkgo.BeforeEach(func() {
59 config = dummyConfig{
60 EnumlessConfig: standard.EnumlessConfig{},
61 Foo: 0,
62 }
63 })
64 ginkgo.Describe("TestConfigSetInvalidQueryValue", func() {
65 ginkgo.It("should fail when not correctly implemented", func() {
66 failures := gomega.InterceptGomegaFailures(func() {
67 TestConfigSetInvalidQueryValue(&config, "mock://host?invalid=value")
68 })
69 gomega.Expect(failures).To(gomega.HaveLen(1))
70 })
71 })
72
73 ginkgo.Describe("TestConfigGetInvalidQueryValue", func() {
74 ginkgo.It("should fail when not correctly implemented", func() {
75 failures := gomega.InterceptGomegaFailures(func() {
76 TestConfigGetInvalidQueryValue(&config)
77 })
78 gomega.Expect(failures).To(gomega.HaveLen(1))
79 })
80 })
81
82 ginkgo.Describe("TestConfigSetDefaultValues", func() {
83 ginkgo.It("should fail when not correctly implemented", func() {
84 failures := gomega.InterceptGomegaFailures(func() {
85 TestConfigSetDefaultValues(&config)
86 })
87 gomega.Expect(failures).NotTo(gomega.BeEmpty())
88 })
89 })
90
91 ginkgo.Describe("TestConfigGetEnumsCount", func() {
92 ginkgo.It("should fail when not correctly implemented", func() {
93 failures := gomega.InterceptGomegaFailures(func() {
94 TestConfigGetEnumsCount(&config, 99)
95 })
96 gomega.Expect(failures).NotTo(gomega.BeEmpty())
97 })
98 })
99
100 ginkgo.Describe("TestConfigGetFieldsCount", func() {
101 ginkgo.It("should fail when not correctly implemented", func() {
102 failures := gomega.InterceptGomegaFailures(func() {
103 TestConfigGetFieldsCount(&config, 99)
104 })
105 gomega.Expect(failures).NotTo(gomega.BeEmpty())
106 })
107 })
108 })
109
110 ginkgo.Describe("Service test helpers", func() {
111 var service dummyService
112
113 ginkgo.BeforeEach(func() {
114 service = dummyService{
115 Config: dummyConfig{
116 EnumlessConfig: standard.EnumlessConfig{},
117 Foo: 0,
118 },
119 }
120 })
121 ginkgo.Describe("TestConfigSetInvalidQueryValue", func() {
122 ginkgo.It("should fail when not correctly implemented", func() {
123 failures := gomega.InterceptGomegaFailures(func() {
124 TestServiceSetInvalidParamValue(&service, "invalid", "value")
125 })
126 gomega.Expect(failures).To(gomega.HaveLen(1))
127 })
128 })
129 })
130 })
131
132 func (dc *dummyConfig) Get(string) (string, error) { return "", nil }
133 func (dc *dummyConfig) GetURL() *url.URL {
134 return &url.URL{
135 Scheme: "",
136 Opaque: "",
137 User: nil,
138 Host: "",
139 Path: "",
140 RawPath: "",
141 OmitHost: false,
142 ForceQuery: false,
143 RawQuery: "",
144 Fragment: "",
145 RawFragment: "",
146 }
147 }
148 func (dc *dummyConfig) QueryFields() []string { return []string{} }
149 func (dc *dummyConfig) Set(string, string) error { return nil }
150 func (dc *dummyConfig) SetURL(_ *url.URL) error { return nil }
151
152 func (s *dummyService) GetID() string { return "dummy" }
153 func (s *dummyService) Initialize(_ *url.URL, _ types.StdLogger) error { return nil }
154 func (s *dummyService) Send(_ string, _ *types.Params) error { return nil }
155
156 func TestTestUtils(t *testing.T) {
157 t.Parallel()
158 gomega.RegisterFailHandler(ginkgo.Fail)
159
160 ginkgo.RunSpecs(t, "Shoutrrr TestUtils Suite")
161 }