Skip to content

Commit

Permalink
refactor: adding init to envreader package (#8)
Browse files Browse the repository at this point in the history
* test: adding utils to envReader testing

* refactor: adding init to envreader package
  • Loading branch information
higfonseca authored May 23, 2021
1 parent d588668 commit aef4854
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 81 deletions.
23 changes: 14 additions & 9 deletions envreader/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package envreader

import "strings"
import (
"os"
"strings"
)

var Env func(key string) string

func init() {
Env = os.Getenv
}

// Create a new envReader
func NewEnvReader(env func(key string) string) *EnvReader {
func New() *EnvReader {
return &EnvReader{
Env: env,
Env: Env,
}
}

// Check if envReader Errs slice is not empty
// Check if envReader has errors
func (r *EnvReader) HasErrors() bool {
if len(r.Errs) > 0 {
return true
}

return false
return len(r.Errs) > 0
}

// Returns envReader Errs slice items as a string
Expand Down
33 changes: 16 additions & 17 deletions test/envreader/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,48 @@ package envreader_test

import (
"testing"

"github.com/escaletech/go-escale/envreader"
"github.com/escaletech/go-escale/messages"
testUtils "github.com/escaletech/go-escale/test/utils"
"github.com/escaletech/go-escale/test/testutils"

"github.com/stretchr/testify/assert"
)

func init() {
envreader.Env = testutils.GetFakeEnv
}

func TestBool(t *testing.T) {
var er *envreader.EnvReader = envreader.New()

t.Run("boolOrDefault", func(t *testing.T) {
t.Run("Env has the required value", func(t *testing.T){
t.Run("Env has the required value", func(t *testing.T) {
t.Run("It should return the variable value and error's slice should be empty", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)

response := r.BoolOrDefault(testUtils.FakeEnvVarNameBool, false)
response := er.BoolOrDefault(testutils.FakeEnvVarNameBool, false)

assert.Equal(t, true, response)
assert.Equal(t, false, r.HasErrors())
assert.Equal(t, false, er.HasErrors())
})

t.Run("Env doesn't have the required key", func(t *testing.T) {
t.Run("It should return a default value and error's slice should be empty", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)

response := r.BoolOrDefault("invalidEnvVarName", false)
response := er.BoolOrDefault("invalidEnvVarName", false)

assert.Equal(t, false, response)
assert.Equal(t, false, r.HasErrors())
assert.Equal(t, false, er.HasErrors())
})
})

t.Run("Env has the required key but its value isn't a boolean", func(t *testing.T) {
t.Run("It should return false and an error message to the slice", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)

response := r.BoolOrDefault(testUtils.FakeEnvVarNameString, false)
response := er.BoolOrDefault(testutils.FakeEnvVarNameString, false)

assert.Equal(t, false, response)
assert.Equal(t, true, r.HasErrors())
assert.Equal(t, messages.UnableToConvertToBool(testUtils.FakeEnvVarNameString), r.Errs[0])
assert.Equal(t, true, er.HasErrors())
assert.Equal(t, messages.UnableToConvertToBool(testutils.FakeEnvVarNameString), er.Errs[0])
})
})
})
})
}
}
31 changes: 15 additions & 16 deletions test/envreader/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,48 @@ package envreader_test

import (
"testing"

"github.com/escaletech/go-escale/envreader"
"github.com/escaletech/go-escale/messages"
testUtils "github.com/escaletech/go-escale/test/utils"
"github.com/escaletech/go-escale/test/testutils"

"github.com/stretchr/testify/assert"
)

func TestInt(t *testing.T) {

t.Run("intOrDefault", func(t *testing.T) {
t.Run("Env has the required value", func(t *testing.T){
t.Run("Env has the required value", func(t *testing.T) {
t.Run("It should return the variable value and error's slice should be empty", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

response := r.IntOrDefault(testUtils.FakeEnvVarNameInt, 21)
response := er.IntOrDefault(testutils.FakeEnvVarNameInt, 21)

assert.Equal(t, 13, response)
assert.Equal(t, false, r.HasErrors())
assert.Equal(t, false, er.HasErrors())
})

t.Run("Env doesn't have the required key", func(t *testing.T) {
t.Run("It should return a default value and error's slice should be empty", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

response := r.IntOrDefault("invalidEnvVarName", 21)
response := er.IntOrDefault("invalidEnvVarName", 21)

assert.Equal(t, 21, response)
assert.Equal(t, false, r.HasErrors())
assert.Equal(t, false, er.HasErrors())
})
})

t.Run("Env has the required key but its value isn't a integer", func(t *testing.T) {
t.Run("It should return 0 and an error message to the slice", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
response := r.IntOrDefault(testUtils.FakeEnvVarNameString, 21)
er := envreader.New()

response := er.IntOrDefault(testutils.FakeEnvVarNameString, 21)

assert.Equal(t, 0, response)
assert.Equal(t, true, r.HasErrors())
assert.Equal(t, messages.UnableToConvertToInt(testUtils.FakeEnvVarNameString), r.Errs[0])
assert.Equal(t, true, er.HasErrors())
assert.Equal(t, messages.UnableToConvertToInt(testutils.FakeEnvVarNameString), er.Errs[0])
})
})
})
})
}
}
23 changes: 11 additions & 12 deletions test/envreader/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/escaletech/go-escale/envreader"
"github.com/escaletech/go-escale/messages"
testUtils "github.com/escaletech/go-escale/test/utils"

"github.com/stretchr/testify/assert"
)
Expand All @@ -14,42 +13,42 @@ func TestMain(t *testing.T) {
t.Run("HasErrors", func(t *testing.T) {
t.Run("envReader errs slice is empty", func(t *testing.T) {
t.Run("returns false", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

assert.Equal(t, false, r.HasErrors())
assert.Equal(t, false, er.HasErrors())
})
})

t.Run("envReader errs slice has items", func(t *testing.T) {
t.Run("returns false", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

r.StringRequired("WHATEVER")
er.StringRequired("WHATEVER")

assert.Equal(t, true, r.HasErrors())
assert.Equal(t, true, er.HasErrors())
})
})
})

t.Run("GetErrors", func(t *testing.T) {
t.Run("envReader errs slice has items", func(t *testing.T) {
t.Run("returns items as a string", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

r.StringRequired("WHATEVER")
r.StringRequired("WHATEVER2")
er.StringRequired("WHATEVER")
er.StringRequired("WHATEVER2")

expectedErrs := messages.MissingRequiredEnv("WHATEVER") + ", " + messages.MissingRequiredEnv("WHATEVER2")

assert.Equal(t, expectedErrs, r.GetErrors())
assert.Equal(t, expectedErrs, er.GetErrors())
})
})

t.Run("envReader errs slice is empty", func(t *testing.T) {
t.Run("returns an empty string", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

assert.Equal(t, "", r.GetErrors())
assert.Equal(t, "", er.GetErrors())
})
})
})
Expand Down
34 changes: 19 additions & 15 deletions test/envreader/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,60 @@ import (

"github.com/escaletech/go-escale/envreader"
"github.com/escaletech/go-escale/messages"
testUtils "github.com/escaletech/go-escale/test/utils"
"github.com/escaletech/go-escale/test/testutils"

"github.com/stretchr/testify/assert"
)

func init() {
envreader.Env = testutils.GetFakeEnv
}

func TestString(t *testing.T) {
t.Run("StringRequired", func(t *testing.T) {
t.Run("Env has the required value", func(t *testing.T) {
t.Run("It should return the variable value and error's slice should be empty", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

response := r.StringRequired(testUtils.FakeEnvVarNameString)
response := er.StringRequired(testutils.FakeEnvVarNameString)

assert.Equal(t, testUtils.FakeEnvVarValueString, response)
assert.Equal(t, false, r.HasErrors())
assert.Equal(t, testutils.FakeEnvVarValueString, response)
assert.Equal(t, false, er.HasErrors())
})
})

t.Run("Env doesn't have the required value", func(t *testing.T) {
t.Run("It should return an empty string and add an error message to the slice", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

response := r.StringRequired("invalidEnvVarName")
response := er.StringRequired("invalidEnvVarName")

assert.Equal(t, "", response)
assert.Equal(t, messages.MissingRequiredEnv("invalidEnvVarName"), r.Errs[0])
assert.Equal(t, messages.MissingRequiredEnv("invalidEnvVarName"), er.Errs[0])
})
})
})

t.Run("StringOrDefault", func(t *testing.T) {
t.Run("Env has the required value", func(t *testing.T) {
t.Run("It should return the variable value and error's slice should be empty", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

response := r.StringOrDefault(testUtils.FakeEnvVarNameString, "defaultValue")
response := er.StringOrDefault(testutils.FakeEnvVarNameString, "defaultValue")

assert.Equal(t, testUtils.FakeEnvVarValueString, response)
assert.Equal(t, false, r.HasErrors())
assert.Equal(t, testutils.FakeEnvVarValueString, response)
assert.Equal(t, false, er.HasErrors())
})
})

t.Run("Env doesn't have the required value", func(t *testing.T) {
t.Run("It should return a default value and error's slice should be empty", func(t *testing.T) {
r := envreader.NewEnvReader(testUtils.GetFakeEnv)
er := envreader.New()

response := r.StringOrDefault("invalidEnvVarName", "defaultValue")
response := er.StringOrDefault("invalidEnvVarName", "defaultValue")

assert.Equal(t, "defaultValue", response)
assert.Equal(t, false, r.HasErrors())
assert.Equal(t, false, er.HasErrors())
})
})
})
Expand Down
23 changes: 11 additions & 12 deletions test/utils/envReaderUtils.go → test/testutils/envReaderUtils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test_utils
package testutils

var FakeEnvVarNameString = "fakeEnvString"
var FakeEnvVarValueString = "fakeEnvValue"
Expand All @@ -7,15 +7,14 @@ var FakeEnvVarValueBool = "true"
var FakeEnvVarNameInt = "fakeEnvInt"
var FakeEnvVarValueInt = "13"

func GetFakeEnv(key string) string {
switch {
case key == FakeEnvVarNameString:
return FakeEnvVarValueString
case key == FakeEnvVarNameBool:
return FakeEnvVarValueBool
case key == FakeEnvVarNameInt:
return FakeEnvVarValueInt
default:
return ""
func setFakeValues() map[string]string {
return map[string]string{
FakeEnvVarNameString: FakeEnvVarValueString,
FakeEnvVarNameBool: FakeEnvVarValueBool,
FakeEnvVarNameInt: FakeEnvVarValueInt,
}
}
}

func GetFakeEnv(key string) string {
return setFakeValues()[key]
}

0 comments on commit aef4854

Please sign in to comment.