gorm changes - tests

pull/78/head
Hunter Long 2018-09-16 03:29:52 -07:00
parent 5128b153a9
commit 82ac8bfc27
5 changed files with 57 additions and 3 deletions

View File

@ -30,7 +30,6 @@ type Failure struct {
// CreateFailure will create a new failure record for a service
func (s *Service) CreateFailure(f *types.Failure) (int64, error) {
f.CreatedAt = time.Now().UTC()
f.Service = s.Id
s.Failures = append(s.Failures, f)
row := failuresDB().Create(f)

View File

@ -27,7 +27,6 @@ type Hit struct {
// CreateHit will create a new 'hit' record in the database for a successful/online service
func (s *Service) CreateHit(h *types.Hit) (int64, error) {
h.CreatedAt = time.Now().UTC()
db := hitsDB().Create(h)
if db.Error != nil {
utils.Log(2, db.Error)

View File

@ -232,7 +232,7 @@ func TestRunAllQueueAndStop(t *testing.T) {
assert.Equal(t, 16, len(example.Queue))
go Queue(example)
assert.Equal(t, 16, len(example.Queue))
time.Sleep(15 * time.Second)
time.Sleep(13 * time.Second)
assert.Equal(t, 6, len(example.Queue))
time.Sleep(1 * time.Second)
assert.Equal(t, 6, len(example.Queue))

View File

@ -166,6 +166,56 @@ func GroupDataBy(column string, id int64, tm time.Time, increment string) string
return sql
}
func (s *Service) AfterFind() (err error) {
s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone)
return
}
func (s *Hit) AfterFind() (err error) {
s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone)
return
}
func (s *Failure) AfterFind() (err error) {
s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone)
return
}
func (s *User) AfterFind() (err error) {
s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone)
return
}
func (u *Hit) BeforeCreate() (err error) {
u.CreatedAt = time.Now().UTC()
return
}
func (u *Failure) BeforeCreate() (err error) {
u.CreatedAt = time.Now().UTC()
return
}
func (u *User) BeforeCreate() (err error) {
u.CreatedAt = time.Now().UTC()
return
}
func (u *Service) BeforeCreate() (err error) {
u.CreatedAt = time.Now().UTC()
return
}
func (s *Service) Downtime() time.Duration {
hits, _ := s.Hits()
fails := s.LimitedFailures()
if len(fails) == 0 {
return time.Duration(0)
}
since := fails[0].CreatedAt.Sub(hits[0].CreatedAt)
return since
}
func (s *Service) GraphDataRaw() []*DateScan {
var d []*DateScan
since := time.Now().Add(time.Hour*-24 + time.Minute*0 + time.Second*0)

View File

@ -45,6 +45,12 @@ func TestSelectAllServices(t *testing.T) {
assert.Equal(t, 15, len(services))
}
func TestServiceDowntime(t *testing.T) {
service := SelectService(15)
downtime := service.Downtime()
assert.True(t, downtime.Minutes() > 9)
}
func TestSelectTCPService(t *testing.T) {
services := CoreApp.Services
assert.Equal(t, 15, len(services))