From 2e421b766e4d23b6e9ae0e17caad94d7b4e651c7 Mon Sep 17 00:00:00 2001 From: Wil Simpson Date: Wed, 4 Dec 2024 18:51:11 -0500 Subject: [PATCH] feat: add code coverage for pkg/config --- pkg/config/binding_test.go | 97 +++++++++++++++++++++++++++++ pkg/config/db_test.go | 18 ++++++ pkg/config/global_test.go | 40 ++++++++++++ pkg/config/invalid_test_config.yaml | 1 + pkg/config/test_config.yaml | 4 ++ 5 files changed, 160 insertions(+) create mode 100644 pkg/config/binding_test.go create mode 100644 pkg/config/global_test.go create mode 100644 pkg/config/invalid_test_config.yaml create mode 100644 pkg/config/test_config.yaml diff --git a/pkg/config/binding_test.go b/pkg/config/binding_test.go new file mode 100644 index 0000000..90335e9 --- /dev/null +++ b/pkg/config/binding_test.go @@ -0,0 +1,97 @@ +package config_test + +import ( + "os" + + "github.com/ShatteredRealms/go-common-service/pkg/config" + "github.com/ShatteredRealms/go-common-service/pkg/log" + "github.com/go-faker/faker/v4" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/sirupsen/logrus/hooks/test" +) + +const ( + envKey = "SRO_FOO" + nestedEnvKey = "SRO_BAR_BAZ" + embeddedEnvKey = "SRO_BUZ" +) + +type TestStruct struct { + EmbeddedStruct `yaml:",inline" mapstructure:",squash"` + + Foo string + Bar TestInnerStruct +} + +type EmbeddedStruct struct { + Buz string +} + +type TestInnerStruct struct { + Baz string +} + +var _ = Describe("Config bind", func() { + log.Logger, _ = test.NewNullLogger() + var ( + testStruct *TestStruct + originalVal string + envVal string + ) + + BeforeEach(func() { + Expect(faker.FakeData(&testStruct)).Should(Succeed()) + }) + + Context("config file", func() { + Context("valid config", func() { + BeforeEach(func(ctx SpecContext) { + Expect(config.BindConfigEnvs(ctx, "test_config", testStruct)).To(Succeed()) + }) + It("should bind to nested structs", func() { + Expect(testStruct.Bar.Baz).To(Equal("testbaz")) + }) + It("should bind to non-nested structs", func() { + Expect(testStruct.Foo).To(Equal("testfoo")) + }) + It("should bind to embedded structs", func() { + Expect(testStruct.Buz).To(Equal("testbuz")) + }) + }) + Context("invalid config", func() { + It("should error if not valid yaml", func(ctx SpecContext) { + Expect(config.BindConfigEnvs(ctx, "invalid_test_config", testStruct)).NotTo(Succeed()) + }) + }) + }) + + Context("environment variables", func() { + It("should bind to nested structs", func(ctx SpecContext) { + originalVal = testStruct.Bar.Baz + envVal = originalVal + faker.Username() + GinkgoT().Setenv(nestedEnvKey, envVal) + Expect(os.Getenv(nestedEnvKey)).To(Equal(envVal)) + Expect(config.BindConfigEnvs(ctx, faker.Username(), testStruct)).To(Succeed()) + Expect(testStruct.Bar.Baz).To(Equal(envVal)) + }) + + It("should bind to non-nested structs", func(ctx SpecContext) { + originalVal = testStruct.Foo + envVal = faker.Name() + GinkgoT().Setenv(envKey, envVal) + Expect(os.Getenv(envKey)).To(Equal(envVal)) + Expect(config.BindConfigEnvs(ctx, faker.Username(), testStruct)).To(Succeed()) + Expect(testStruct.Foo).To(Equal(envVal)) + }) + + It("should bind to embedded structs", func(ctx SpecContext) { + originalVal = testStruct.Buz + envVal = faker.Name() + GinkgoT().Setenv(embeddedEnvKey, envVal) + Expect(os.Getenv(embeddedEnvKey)).To(Equal(envVal)) + Expect(config.BindConfigEnvs(ctx, faker.Username(), testStruct)).To(Succeed()) + Expect(testStruct.Buz).To(Equal(envVal)) + }) + }) +}) diff --git a/pkg/config/db_test.go b/pkg/config/db_test.go index 553769c..e58ee56 100644 --- a/pkg/config/db_test.go +++ b/pkg/config/db_test.go @@ -33,6 +33,13 @@ var _ = Describe("Db config", func() { }) }) + Describe("PostgresDSNWithoutName", func() { + It("should work", func() { + dsn = config.PostgresDSNWithoutName() + Expect(dsn).NotTo(ContainSubstring(config.Name)) + }) + }) + Describe("MongoDSN", func() { It("should work", func() { dsn = config.MongoDSN() @@ -47,3 +54,14 @@ var _ = Describe("Db config", func() { }) }) +var _ = Describe("Db pool config", func() { + var ( + config *config.DBPoolConfig + ) + + It("should be able to list addresses and ports", func() { + Expect(faker.FakeData(&config)).To(Succeed()) + addrs := config.Addresses() + Expect(addrs).To(HaveLen(len(config.Slaves) + 1)) + }) +}) diff --git a/pkg/config/global_test.go b/pkg/config/global_test.go new file mode 100644 index 0000000..115b767 --- /dev/null +++ b/pkg/config/global_test.go @@ -0,0 +1,40 @@ +package config_test + +import ( + "github.com/go-faker/faker/v4" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/ShatteredRealms/go-common-service/pkg/config" +) + +var _ = Describe("ServerAddress(s)", func() { + var ( + serverAddress *config.ServerAddress + ) + + BeforeEach(func() { + serverAddress = &config.ServerAddress{ + Host: "localhost", + Port: "8080", + } + }) + + Describe("Address", func() { + It("should work", func() { + Expect(serverAddress.Address()).To(Equal("localhost:8080")) + }) + }) + + Describe("Addresses", func() { + It("should work", func() { + serverAddresses := config.ServerAddresses{ + {Host: faker.Username(), Port: faker.Username()}, + {Host: faker.Username(), Port: faker.Username()}, + } + Expect(serverAddresses.Addresses()).To(HaveLen(2)) + Expect(serverAddresses.Addresses()[0]).To(Equal(serverAddresses[0].Address())) + Expect(serverAddresses.Addresses()[1]).To(Equal(serverAddresses[1].Address())) + }) + }) +}) diff --git a/pkg/config/invalid_test_config.yaml b/pkg/config/invalid_test_config.yaml new file mode 100644 index 0000000..98232c6 --- /dev/null +++ b/pkg/config/invalid_test_config.yaml @@ -0,0 +1 @@ +{ diff --git a/pkg/config/test_config.yaml b/pkg/config/test_config.yaml new file mode 100644 index 0000000..d3057fa --- /dev/null +++ b/pkg/config/test_config.yaml @@ -0,0 +1,4 @@ +foo: testfoo +bar: + baz: testbaz +buz: testbuz