-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add code coverage for pkg/config
- Loading branch information
Wil Simpson
committed
Dec 4, 2024
1 parent
1bccdb0
commit 2e421b7
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
foo: testfoo | ||
bar: | ||
baz: testbaz | ||
buz: testbuz |