Add unit test structure for common

drew/sql-it
Drew Bednar 7 months ago
parent 16e5df26d6
commit f3200419ba

@ -2,4 +2,7 @@ build:
go build -o ./bin/gowiki ./cmd/gowiki/main.go
clean:
rm -rf ./bin/gowiki
rm -rf ./bin/gowiki
test:
go test ./...

@ -0,0 +1,40 @@
package common
import (
"log"
"os"
"testing"
)
func assertString(t *testing.T, got, want string) {
t.Helper()
if got != want {
log.Fatalf("Assertion failed. Got: %s want: %s", got, want)
}
}
func TestGetDefaultEnv(t *testing.T) {
t.Run("test key exists", func(t *testing.T) {
//Setup
want := "test value"
os.Setenv("TEST_KEY", want)
got := GetDefaultEnv("TEST_KEY", "")
assertString(t, got, want)
os.Unsetenv("TEST_KEY")
})
t.Run("key does not exists", func(t *testing.T) {
//Setup
want := "another test value"
got := GetDefaultEnv("TEST_KEY", want)
assertString(t, got, want)
})
t.Run("key maps to empty value", func(t *testing.T) {
//Setup
want := ""
got := GetDefaultEnv("TEST_EMPTY_KEY", want)
assertString(t, got, want)
})
}
Loading…
Cancel
Save