statping/core/services_checkin_test.go

101 lines
3.0 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-10-07 08:16:38 +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-10-07 08:16:38 +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/types"
"github.com/hunterlong/statping/utils"
2018-10-07 08:16:38 +00:00
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var (
2018-10-07 22:38:56 +00:00
testCheckin *Checkin
2018-10-07 08:16:38 +00:00
)
func TestCreateCheckin(t *testing.T) {
service := SelectService(1)
testCheckin = ReturnCheckin(&types.Checkin{
2018-10-08 04:27:36 +00:00
ServiceId: service.Id,
2018-10-07 08:16:38 +00:00
Interval: 10,
GracePeriod: 5,
ApiKey: utils.RandomString(7),
})
id, err := testCheckin.Create()
assert.Nil(t, err)
assert.NotZero(t, id)
assert.NotEmpty(t, testCheckin.ApiKey)
assert.Equal(t, int64(10), testCheckin.Interval)
assert.Equal(t, int64(5), testCheckin.GracePeriod)
assert.True(t, testCheckin.Expected().Minutes() < 0)
}
func TestSelectCheckin(t *testing.T) {
service := SelectService(1)
2018-12-06 19:03:55 +00:00
checkins := service.AllCheckins()
2018-10-07 08:16:38 +00:00
assert.NotNil(t, checkins)
assert.Equal(t, 1, len(checkins))
testCheckin = checkins[0]
assert.Equal(t, int64(10), testCheckin.Interval)
assert.Equal(t, int64(5), testCheckin.GracePeriod)
assert.Equal(t, 7, len(testCheckin.ApiKey))
}
func TestUpdateCheckin(t *testing.T) {
testCheckin.Interval = 60
testCheckin.GracePeriod = 15
id, err := testCheckin.Update()
assert.Nil(t, err)
assert.NotZero(t, id)
assert.NotEmpty(t, testCheckin.ApiKey)
service := SelectService(1)
2018-12-06 19:03:55 +00:00
checkin := service.AllCheckins()[0]
2018-10-07 08:16:38 +00:00
assert.Equal(t, int64(60), checkin.Interval)
assert.Equal(t, int64(15), checkin.GracePeriod)
t.Log(testCheckin.Expected())
assert.True(t, testCheckin.Expected().Minutes() < 0)
}
func TestCreateCheckinHits(t *testing.T) {
service := SelectService(1)
2018-12-06 19:03:55 +00:00
checkins := service.AllCheckins()
2018-10-07 08:16:38 +00:00
assert.Equal(t, 1, len(checkins))
created := time.Now().UTC().Add(-60 * time.Second)
hit := ReturnCheckinHit(&types.CheckinHit{
Checkin: testCheckin.Id,
From: "192.168.1.1",
CreatedAt: created,
})
hit.Create()
hits := testCheckin.AllHits()
2018-10-07 08:16:38 +00:00
assert.Equal(t, 1, len(hits))
}
func TestSelectCheckinMethods(t *testing.T) {
time.Sleep(5 * time.Second)
service := SelectService(1)
2018-12-06 19:03:55 +00:00
checkins := service.AllCheckins()
2018-10-07 08:16:38 +00:00
assert.NotNil(t, checkins)
lastHit := testCheckin.Last()
assert.Equal(t, float64(60), testCheckin.Period().Seconds())
assert.Equal(t, float64(15), testCheckin.Grace().Seconds())
t.Log(testCheckin.Expected())
assert.True(t, testCheckin.Expected().Seconds() < -5)
2018-10-11 00:43:23 +00:00
assert.False(t, lastHit.CreatedAt.IsZero())
2018-10-07 08:16:38 +00:00
assert.Equal(t, "A minute ago", lastHit.Ago())
}