fixed unit testing

pull/50/head
Hunter Long 2018-08-19 20:48:28 -07:00
parent df98c36853
commit 625b5d4b18
7 changed files with 15 additions and 18 deletions

View File

@ -132,7 +132,7 @@ func RunOnce() {
if err != nil { if err != nil {
utils.Log(4, "config.yml file not found") utils.Log(4, "config.yml file not found")
} }
err = core.DbConnection(core.Configs.Connection, false, ".") err = core.DbConnection(core.Configs.Connection, false, utils.Directory)
if err != nil { if err != nil {
utils.Log(4, err) utils.Log(4, err)
} }

View File

@ -95,7 +95,7 @@ func LoadDotEnvs() error {
func mainProcess() { func mainProcess() {
var err error var err error
err = core.DbConnection(core.Configs.Connection, false, ".") err = core.DbConnection(core.Configs.Connection, false, utils.Directory)
if err != nil { if err != nil {
utils.Log(4, fmt.Sprintf("could not connect to database: %v", err)) utils.Log(4, fmt.Sprintf("could not connect to database: %v", err))
} }

View File

@ -32,7 +32,7 @@ func LoadConfig() (*types.Config, error) {
return LoadUsingEnv() return LoadUsingEnv()
} }
Configs = new(types.Config) Configs = new(types.Config)
file, err := ioutil.ReadFile("config.yml") file, err := ioutil.ReadFile(utils.Directory + "/config.yml")
if err != nil { if err != nil {
return nil, errors.New("config.yml file not found - starting in setup mode") return nil, errors.New("config.yml file not found - starting in setup mode")
} }

View File

@ -72,7 +72,7 @@ func InitApp() {
func InsertNotifierDB() error { func InsertNotifierDB() error {
if DbSession == nil { if DbSession == nil {
err := DbConnection(CoreApp.DbConnection, false, ".") err := DbConnection(CoreApp.DbConnection, false, utils.Directory)
if err != nil { if err != nil {
return errors.New("database connection has not been created") return errors.New("database connection has not been created")
} }

View File

@ -104,7 +104,7 @@ func DbConnection(dbType string, retry bool, location string) error {
func waitForDb(dbType string) error { func waitForDb(dbType string) error {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
return DbConnection(dbType, true, ".") return DbConnection(dbType, true, utils.Directory)
} }
func DatabaseMaintence() { func DatabaseMaintence() {
@ -126,7 +126,7 @@ func DeleteAllSince(table string, date time.Time) {
func (c *DbConfig) Save() error { func (c *DbConfig) Save() error {
var err error var err error
config, err := os.Create("config.yml") config, err := os.Create(utils.Directory + "/config.yml")
if err != nil { if err != nil {
utils.Log(4, err) utils.Log(4, err)
return err return err

View File

@ -114,7 +114,7 @@ func ProcessSetupHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
err = core.DbConnection(core.Configs.Connection, false, ".") err = core.DbConnection(core.Configs.Connection, false, utils.Directory)
if err != nil { if err != nil {
utils.Log(3, err) utils.Log(3, err)
core.DeleteConfig() core.DeleteConfig()

View File

@ -20,14 +20,13 @@ import (
"github.com/hunterlong/statup/utils" "github.com/hunterlong/statup/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"os"
"testing" "testing"
"upper.io/db.v3/sqlite" "upper.io/db.v3/sqlite"
) )
var ( var (
testNotifier *Tester testNotifier *Tester
testDatabase string dir string
) )
// //
@ -63,15 +62,13 @@ func (n *Tester) Test() error {
} }
func init() { func init() {
testDatabase = os.Getenv("GOPATH") dir = utils.Directory
testDatabase += "/src/github.com/hunterlong/statup"
utils.InitLogs() utils.InitLogs()
} }
func injectDatabase() { func injectDatabase() {
sqliteDb := sqlite.ConnectionURL{ sqliteDb := sqlite.ConnectionURL{
Database: testDatabase + "/statup.db", Database: dir + "/statup.db",
} }
dbSession, _ := sqlite.Open(sqliteDb) dbSession, _ := sqlite.Open(sqliteDb)
Collections = dbSession.Collection("communication") Collections = dbSession.Collection("communication")
@ -87,11 +84,11 @@ func TestInit(t *testing.T) {
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
testNotifier = &Tester{&Notification{ testNotifier = &Tester{&Notification{
Id: 1, Id: 999999,
Method: "tester", Method: "tester",
Host: "0.0.0.0", Host: "0.0.0.0",
Form: []NotificationForm{{ Form: []NotificationForm{{
Id: 1, Id: 999999,
Type: "text", Type: "text",
Title: "Incoming Webhook Url", Title: "Incoming Webhook Url",
Placeholder: "Insert your Slack webhook URL here.", Placeholder: "Insert your Slack webhook URL here.",
@ -119,14 +116,14 @@ func TestInsertDatabase(t *testing.T) {
} }
func TestSelectNotification(t *testing.T) { func TestSelectNotification(t *testing.T) {
notifier, err := SelectNotification(1) notifier, err := SelectNotification(999999)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "tester", notifier.Method) assert.Equal(t, "tester", notifier.Method)
assert.False(t, notifier.Enabled) assert.False(t, notifier.Enabled)
} }
func TestNotification_Update(t *testing.T) { func TestNotification_Update(t *testing.T) {
notifier, err := SelectNotification(1) notifier, err := SelectNotification(999999)
assert.Nil(t, err) assert.Nil(t, err)
notifier.Method = "updatedName" notifier.Method = "updatedName"
notifier.Enabled = true notifier.Enabled = true
@ -139,7 +136,7 @@ func TestNotification_Update(t *testing.T) {
} }
func TestNotification_GetValue(t *testing.T) { func TestNotification_GetValue(t *testing.T) {
notifier, err := SelectNotification(1) notifier, err := SelectNotification(999999)
assert.Nil(t, err) assert.Nil(t, err)
val := notifier.GetValue("Host") val := notifier.GetValue("Host")
assert.Equal(t, "0.0.0.0", val) assert.Equal(t, "0.0.0.0", val)