statping/utils/utils_test.go

135 lines
3.5 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// 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/>.
2018-07-27 04:45:42 +00:00
package utils
import (
2018-10-06 22:46:26 +00:00
"errors"
2018-07-27 04:45:42 +00:00
"github.com/stretchr/testify/assert"
2018-07-28 01:50:13 +00:00
"net/http"
2018-07-27 04:45:42 +00:00
"testing"
"time"
)
2018-08-16 02:22:10 +00:00
func TestCreateLog(t *testing.T) {
err := createLog(Directory)
assert.Nil(t, err)
}
2018-07-27 04:45:42 +00:00
func TestInitLogs(t *testing.T) {
assert.Nil(t, InitLogs())
2018-08-16 02:22:10 +00:00
assert.FileExists(t, "../logs/statup.log")
2018-07-27 04:45:42 +00:00
}
func TestFileExists(t *testing.T) {
assert.True(t, FileExists("../logs/statup.log"))
}
2018-07-28 01:50:13 +00:00
func TestDir(t *testing.T) {
2018-07-28 03:24:54 +00:00
assert.Contains(t, Directory, "github.com/hunterlong/statup")
2018-07-28 01:50:13 +00:00
}
func TestCommand(t *testing.T) {
2018-10-06 05:56:08 +00:00
t.SkipNow()
in, out, err := Command("pwd")
assert.Nil(t, err)
assert.Contains(t, in, "statup")
assert.Empty(t, out)
}
func TestDurationReadable(t *testing.T) {
dur, _ := time.ParseDuration("1505s")
readable := DurationReadable(dur)
assert.Equal(t, "25 minutes", readable)
}
2018-07-28 01:50:13 +00:00
func TestLog(t *testing.T) {
assert.Nil(t, Log(0, errors.New("this is a 0 level error")))
assert.Nil(t, Log(1, errors.New("this is a 1 level error")))
assert.Nil(t, Log(2, errors.New("this is a 2 level error")))
assert.Nil(t, Log(3, errors.New("this is a 3 level error")))
assert.Nil(t, Log(4, errors.New("this is a 4 level error")))
assert.Nil(t, Log(5, errors.New("this is a 5 level error")))
}
func TestFormatDuration(t *testing.T) {
dur, _ := time.ParseDuration("158s")
formatted := FormatDuration(dur)
assert.Equal(t, "3 minutes", formatted)
dur, _ = time.ParseDuration("-65s")
formatted = FormatDuration(dur)
assert.Equal(t, "1 minute", formatted)
}
2018-08-16 02:22:10 +00:00
func TestDeleteFile(t *testing.T) {
assert.Nil(t, DeleteFile(Directory+"/logs/statup.log"))
}
2018-08-16 02:22:10 +00:00
func TestFailedDeleteFile(t *testing.T) {
assert.Error(t, DeleteFile(Directory+"/missingfilehere.txt"))
2018-07-28 03:24:54 +00:00
}
2018-07-28 01:50:13 +00:00
func TestLogHTTP(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
assert.Nil(t, err)
assert.NotEmpty(t, Http(req))
}
2018-07-27 04:45:42 +00:00
func TestIntString(t *testing.T) {
assert.Equal(t, "1", ToString(1))
2018-07-27 04:45:42 +00:00
}
func TestStringInt(t *testing.T) {
assert.Equal(t, int64(1), StringInt("1"))
}
func TestDbTime(t *testing.T) {
}
func TestTimezone(t *testing.T) {
2018-09-17 22:13:42 +00:00
zone := float32(-4.0)
loc, _ := time.LoadLocation("America/Los_Angeles")
2018-09-17 22:13:42 +00:00
timestamp := time.Date(2018, 1, 1, 10, 0, 0, 0, loc)
timezone := Timezoner(timestamp, zone)
2018-09-17 22:13:42 +00:00
assert.Equal(t, "2018-01-01 10:00:00 -0800 PST", timestamp.String())
assert.Equal(t, "2018-01-01 15:00:00 -0300 -0300", timezone.String())
}
2018-07-27 04:45:42 +00:00
func TestTimestamp_Ago(t *testing.T) {
now := Timestamp(time.Now())
assert.Equal(t, "Just now", now.Ago())
}
func TestUnderScoreString(t *testing.T) {
assert.Equal(t, "this_is_a_test", UnderScoreString("this is a test"))
}
func TestHashPassword(t *testing.T) {
assert.Equal(t, 60, len(HashPassword("password123")))
}
func TestNewSHA1Hash(t *testing.T) {
assert.NotEmpty(t, NewSHA1Hash(5))
}
func TestRandomString(t *testing.T) {
assert.NotEmpty(t, RandomString(5))
}
func TestDeleteDirectory(t *testing.T) {
2018-08-16 02:22:10 +00:00
assert.Nil(t, DeleteDirectory(Directory+"/logs"))
}