github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/notify_times_test.go (about) 1 package model 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/evergreen-ci/evergreen/db" 8 "github.com/evergreen-ci/evergreen/testutil" 9 . "github.com/smartystreets/goconvey/convey" 10 ) 11 12 var notifyTimesTestConfig = testutil.TestConfig() 13 14 func init() { 15 db.SetGlobalSessionProvider( 16 db.SessionFactoryFromConfig(notifyTimesTestConfig)) 17 } 18 19 func TestSetLastNotificationsEventTime(t *testing.T) { 20 21 Convey("When setting the last notification event time", t, func() { 22 23 testutil.HandleTestingErr(db.Clear(NotifyTimesCollection), 24 t, "Error clearing '%v' collection", NotifyTimesCollection) 25 26 Convey("the last notification time for only the specified project"+ 27 " should be set to the specified time", func() { 28 29 projectOne := "projectOne" 30 projectTwo := "projectTwo" 31 32 timeOne := time.Now() 33 timeTwo := timeOne.Add(time.Second) 34 35 So(SetLastNotificationsEventTime(projectOne, timeOne), ShouldBeNil) 36 lastEventTime, err := LastNotificationsEventTime(projectOne) 37 So(err, ShouldBeNil) 38 So(lastEventTime.Round(time.Second).Equal( 39 timeOne.Round(time.Second)), ShouldBeTrue) 40 41 So(SetLastNotificationsEventTime(projectTwo, timeTwo), ShouldBeNil) 42 lastEventTime, err = LastNotificationsEventTime(projectTwo) 43 So(err, ShouldBeNil) 44 So(lastEventTime.Round(time.Second).Equal( 45 timeTwo.Round(time.Second)), ShouldBeTrue) 46 47 // make sure the time for project one wasn't changed 48 lastEventTime, err = LastNotificationsEventTime(projectOne) 49 So(err, ShouldBeNil) 50 So(lastEventTime.Round(time.Second).Equal( 51 timeOne.Round(time.Second)), ShouldBeTrue) 52 53 // now change it 54 So(SetLastNotificationsEventTime(projectOne, timeTwo), ShouldBeNil) 55 lastEventTime, err = LastNotificationsEventTime(projectOne) 56 So(err, ShouldBeNil) 57 So(lastEventTime.Round(time.Second).Equal( 58 timeTwo.Round(time.Second)), ShouldBeTrue) 59 }) 60 61 }) 62 63 } 64 65 func TestLastNotificationsEventTime(t *testing.T) { 66 67 Convey("When checking the last notifications event time for a"+ 68 " project", t, func() { 69 70 testutil.HandleTestingErr(db.Clear(NotifyTimesCollection), 71 t, "Error clearing '%v' collection", NotifyTimesCollection) 72 73 Convey("if there are no times stored, the earliest date to consider"+ 74 " should be returned", func() { 75 76 projectName := "project" 77 78 lastEventTime, err := LastNotificationsEventTime(projectName) 79 So(err, ShouldBeNil) 80 So(lastEventTime.Round(time.Second).Equal( 81 EarliestDateToConsider.Round(time.Second)), ShouldBeTrue) 82 83 }) 84 85 Convey("if a time has previously been recorded, it should be"+ 86 " returned", func() { 87 88 projectName := "project" 89 timeToUse := time.Now() 90 91 So(SetLastNotificationsEventTime(projectName, timeToUse), ShouldBeNil) 92 93 lastEventTime, err := LastNotificationsEventTime(projectName) 94 So(err, ShouldBeNil) 95 So(lastEventTime.Round(time.Second).Equal( 96 timeToUse.Round(time.Second)), ShouldBeTrue) 97 }) 98 99 }) 100 101 }