Skip to content

Commit

Permalink
feat: add code coverage for pkg/config
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Simpson committed Dec 4, 2024
1 parent 1bccdb0 commit 2e421b7
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
97 changes: 97 additions & 0 deletions pkg/config/binding_test.go
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))
})
})
})
18 changes: 18 additions & 0 deletions pkg/config/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
})
})
40 changes: 40 additions & 0 deletions pkg/config/global_test.go
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()))
})
})
})
1 change: 1 addition & 0 deletions pkg/config/invalid_test_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{
4 changes: 4 additions & 0 deletions pkg/config/test_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo: testfoo
bar:
baz: testbaz
buz: testbuz

0 comments on commit 2e421b7

Please sign in to comment.