statping/core/core_test.go

150 lines
3.4 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-16 06:22:20 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package core
import (
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/utils"
"github.com/stretchr/testify/assert"
2019-12-30 00:40:20 +00:00
"github.com/stretchr/testify/require"
2019-02-11 21:10:17 +00:00
"os"
"testing"
)
var (
2018-10-07 08:16:38 +00:00
dir string
skipNewDb bool
)
2018-07-27 04:45:42 +00:00
func init() {
2018-08-10 04:38:54 +00:00
dir = utils.Directory
2018-07-27 04:45:42 +00:00
utils.InitLogs()
2018-08-10 04:38:54 +00:00
source.Assets()
2018-10-07 08:16:38 +00:00
skipNewDb = false
2019-03-05 20:13:25 +00:00
SampleHits = 480
2018-07-27 04:45:42 +00:00
}
func TestNewCore(t *testing.T) {
2019-12-30 01:40:21 +00:00
err := TmpRecords("core.db")
2019-12-30 00:40:20 +00:00
require.Nil(t, err)
require.NotNil(t, CoreApp)
2018-07-27 04:45:42 +00:00
}
func TestDbConfig_Save(t *testing.T) {
2019-12-30 00:40:20 +00:00
t.SkipNow()
//if skipNewDb {
// t.SkipNow()
//}
//var err error
//Configs = &DbConfig{
// DbConn: "sqlite",
// Project: "Tester",
// Location: dir,
//}
//Configs, err = Configs.Save()
//assert.Nil(t, err)
//assert.Equal(t, "sqlite", Configs.DbConn)
//assert.NotEmpty(t, Configs.ApiKey)
//assert.NotEmpty(t, Configs.ApiSecret)
}
func TestLoadDbConfig(t *testing.T) {
2018-10-11 16:53:13 +00:00
Configs, err := LoadConfigFile(dir)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
assert.Equal(t, "sqlite", Configs.DbConn)
2018-07-27 04:45:42 +00:00
}
func TestDbConnection(t *testing.T) {
err := Configs.Connect(false, dir)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
}
func TestDropDatabase(t *testing.T) {
2019-12-30 00:40:20 +00:00
t.SkipNow()
2018-10-07 08:16:38 +00:00
if skipNewDb {
t.SkipNow()
}
err := Configs.DropDatabase()
assert.Nil(t, err)
}
func TestSeedSchemaDatabase(t *testing.T) {
2019-12-30 00:40:20 +00:00
t.SkipNow()
2018-10-07 08:16:38 +00:00
if skipNewDb {
t.SkipNow()
}
err := Configs.CreateDatabase()
assert.Nil(t, err)
}
func TestMigrateDatabase(t *testing.T) {
2019-12-30 00:40:20 +00:00
t.SkipNow()
err := Configs.MigrateDatabase()
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
}
func TestSeedDatabase(t *testing.T) {
2019-12-30 00:40:20 +00:00
t.SkipNow()
err := InsertLargeSampleData()
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
}
func TestReLoadDbConfig(t *testing.T) {
err := Configs.Connect(false, dir)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
assert.Equal(t, "sqlite", Configs.DbConn)
2018-07-27 04:45:42 +00:00
}
func TestSelectCore(t *testing.T) {
core, err := SelectCore()
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
2018-12-04 05:57:11 +00:00
assert.Equal(t, "Statping Sample Data", core.Name)
2018-07-27 04:45:42 +00:00
}
func TestInsertNotifierDB(t *testing.T) {
2019-12-30 00:40:20 +00:00
t.SkipNow()
2018-10-07 08:16:38 +00:00
if skipNewDb {
t.SkipNow()
}
2018-10-11 16:53:13 +00:00
err := InsertNotifierDB()
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
}
2019-02-11 21:10:17 +00:00
func TestEnvToConfig(t *testing.T) {
os.Setenv("DB_CONN", "sqlite")
os.Setenv("DB_USER", "")
os.Setenv("DB_PASS", "")
os.Setenv("DB_DATABASE", "")
os.Setenv("NAME", "Testing")
os.Setenv("DOMAIN", "http://localhost:8080")
os.Setenv("DESCRIPTION", "Testing Statping")
os.Setenv("ADMIN_USER", "admin")
os.Setenv("ADMIN_PASS", "admin123")
2019-12-30 00:40:20 +00:00
os.Setenv("VERBOSE", "1")
2019-02-11 21:10:17 +00:00
config, err := EnvToConfig()
assert.Nil(t, err)
assert.Equal(t, config.DbConn, "sqlite")
assert.Equal(t, config.Domain, "http://localhost:8080")
assert.Equal(t, config.Description, "Testing Statping")
assert.Equal(t, config.Username, "admin")
assert.Equal(t, config.Password, "admin123")
}
func TestGetLocalIP(t *testing.T) {
ip := GetLocalIP()
assert.Contains(t, ip, "http://")
}