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