pgm/main_test.go
2020-10-30 20:12:51 -05:00

133 lines
2.3 KiB
Go

package main_test
import (
"encoding/json"
"github.com/spf13/viper"
"pgm/ini"
"pgm/data"
"pgm/db"
"pgm/logger"
"testing"
)
func TestLogs(t *testing.T) {
t.Parallel()
var s string
var want bool = true
s = "test"
got := logger.Logger(s)
if want != got {
t.Errorf("want %t, got %t", want, got)
}
}
func TestLoadConfig(t *testing.T) {
t.Parallel()
var v *viper.Viper
var want error = nil
v, got := ini.ReadConfig()
_ = v
if want != got {
t.Error("load ini reaturned a non nil error")
}
}
func TestViperPgmConfig(t *testing.T) {
t.Parallel()
testCases := []viperReturnKey{
{a: "junk", b: "r", want: false, need: ""},
{a: "test_user", b: "r", want: true, need: "test"},
}
for _, tc := range testCases{
found, got := data.ViperPgmConfig(tc.a, tc.b)
if tc.want != found{
t.Errorf("want %t, got %t", tc.want, found)
}
if tc.need != got {
t.Errorf("want %s, got %s", tc.need, got)
}
}
}
func TestViperScnConfig(t *testing.T) {
t.Parallel()
testCases := []viperScnReturn {
{t: "test", want: "test"},
{t: "userSessions", want: "active sessions"},
}
for _, tc := range testCases {
var v *viper.Viper
v, err := data.ViperScrConfig("scn", map[string]interface{}{})
if err != nil {
logger.Logger("[ERROR] scn cfg test error: " + err.Error())
}
got := v.GetString(tc.t + ".title")
if tc.want != got {
t.Errorf("want %s, got %s", tc.want, got)
}
}
}
func TestDbConn(t *testing.T) {
t.Parallel()
found, c := data.ViperPgmConfig("test_db", "r")
testCases := []connTest{
{connstring: c, want: nil},
}
if found != true {
logger.Logger("[LOG] failed to find test_db in .pgm.yaml")
}
for _, tc := range testCases {
var h data.HostDetails
err := json.Unmarshal([]byte(tc.connstring), &h)
if err != nil {
logger.Logger("[ERROR] db conn test failure: " + err.Error())
}
got, err := db.DbConn(h)
if tc.want != err {
t.Error("db conn returned an invalid error for test scenario, see logs")
}
err = got.Close()
if err != nil {
logger.Logger("[ERROR] Unable to close connection during test")
}
}
}
type connTest struct {
connstring string
want error
}
type viperScnReturn struct {
t string
want string
}
type viperReturnKey struct {
a, b string
want bool
need string
}
type initTest struct {
a, b, c string
want bool
}