Skip to content
Snippets Groups Projects
Select Git revision
  • 7f84f33c1ce0bbbd4e7ed6e80c87d397f371c7ac
  • main default protected
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
22 results

struct_test.go

Blame
  • 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")
    	}