Select Git revision
struct_test.go
struct_test.go 4.07 KiB
package config_test
import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"time"
"gitlab.com/uafrica/go-utils/config"
"gitlab.com/uafrica/go-utils/errors"
"gitlab.com/uafrica/go-utils/logger"
)
func TestLoad(t *testing.T) {
logger.SetGlobalFormat(logger.NewConsole())
logger.SetGlobalLevel(logger.LevelDebug)
//booleans
os.Setenv("TEST_VALUE_ENABLE_CACHE", "true")
os.Setenv("TEST_VALUE_DISABLE_LOG", "true")
os.Setenv("TEST_VALUE_ADMIN", "false")
//integers
os.Setenv("TEST_VALUE_MAX_SIZE", "12")
os.Setenv("TEST_VALUE_SEQ1", "[4,5,6]") //list in one value
os.Setenv("TEST_VALUE_SEQ2_10", "10") //numbered list elements
os.Setenv("TEST_VALUE_SEQ2_20", "20")
os.Setenv("TEST_VALUE_SEQ2_4", "4")
os.Setenv("TEST_VALUE_SEQ2_15", "15")
os.Setenv("TEST_VALUE_SEQ2", "100")
os.Setenv("TEST_VALUE_CUTOFF", "2021-11-20T12:00:00+02:00")
os.Setenv("TEST_VALUE_HOLIDAYS", "[2021-03-21,2021-04-27,2021-05-01,2021-06-16,2021-08-09,2021-12-16,2021-12-25]")
c := Config{}
if err := config.LoadEnv("TEST_VALUE", &c); err != nil {
t.Fatalf("Cannot load config: %+v", err)
}
t.Logf("Loaded config: %+v", c)
if !c.EnableCache || !c.DisableLog || c.Admin {
t.Fatalf("wrong bool values: %+v", c)
}
if c.MaxSize != 12 {
t.Fatalf("wrong nr values: %+v", c)
}
if len(c.Seq1) != 3 || c.Seq1[0] != 4 || c.Seq1[1] != 5 || c.Seq1[2] != 6 {
t.Fatalf("wrong seq1: %+v", c)
}
if len(c.Seq2) != 5 || c.Seq2[0] != 100 || c.Seq2[1] != 4 || c.Seq2[2] != 10 || c.Seq2[3] != 15 || c.Seq2[4] != 20 {
t.Fatalf("wrong seq2: %+v", c)
}
if c.Cutoff.UTC().Format("2006-01-02 15:04:05") != "2021-11-20 10:00:00" {
t.Fatalf("wrong cutoff")
}
if len(c.Holidays) != 7 ||
c.Holidays[0].String() != "2021-03-21" ||
c.Holidays[1].String() != "2021-04-27" ||
c.Holidays[2].String() != "2021-05-01" ||
c.Holidays[3].String() != "2021-06-16" ||
c.Holidays[4].String() != "2021-08-09" ||
c.Holidays[5].String() != "2021-12-16" ||
c.Holidays[6].String() != "2021-12-25" {
t.Fatalf("wrong holidays")
}