Skip to content

Commit

Permalink
improving test readability in response to pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zackverham committed Jun 6, 2022
1 parent e695a3c commit 19318e8
Showing 1 changed file with 33 additions and 48 deletions.
81 changes: 33 additions & 48 deletions pkg/rslog/rslogtest/logger_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/rstudio/platform-lib/pkg/rslog"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -356,34 +357,35 @@ func (s *LoggerImplTestSuite) TestNewDiscardingLogger() {
}

func (s *LoggerImplTestSuite) TestUTCJSONFormatter() {
// create a test location 1 hr east of UTC
loc := time.FixedZone("test/zone", 3600)
utc := time.Now().UTC()

// shift UTC time to the test location
nonUtc := utc.In(loc)
utc, nonUtc := getTestTimes(s.Assertions)

// verify shifted time is 1 hr ahead
expected := utc.Add(time.Hour)
formatter := rslog.NewUTCJSONFormatter()

expYear, expMonth, expDay := expected.Date()
nonUtcYear, nonUtcMonth, nonUtcDay := nonUtc.Date()
// create a log entry with non-UTC time
entry := &logrus.Entry{
Level: logrus.DebugLevel,
Time: nonUtc,
Message: "test",
}

s.Equal(expYear, nonUtcYear)
s.Equal(expMonth, nonUtcMonth)
s.Equal(expDay, nonUtcDay)
serialized, err := formatter.Format(entry)

expHour, expMin, expSec := expected.Clock()
nonUtcHour, nonUtcMin, nonUtcSec := nonUtc.Clock()
s.Nil(err)

s.Equal(expHour, nonUtcHour)
s.Equal(expMin, nonUtcMin)
s.Equal(expSec, nonUtcSec)
result := string(serialized)

// verify shifted time is not in UTC location
s.NotEqual(utc.Location(), nonUtc.Location())
// assert that UTC time appears in the formatted entry
utcTimestamp := utc.Format(formatter.TimestampFormat)
s.True(strings.Contains(result, utcTimestamp))

formatter := rslog.NewUTCJSONFormatter()
}

func (s *LoggerImplTestSuite) TestUTCTextFormatter() {

utc, nonUtc := getTestTimes(s.Assertions)

formatter := rslog.NewUTCTextFormatter()

// create a log entry with non-UTC time
entry := &logrus.Entry{
Expand All @@ -401,11 +403,11 @@ func (s *LoggerImplTestSuite) TestUTCJSONFormatter() {
// assert that UTC time appears in the formatted entry
utcTimestamp := utc.Format(formatter.TimestampFormat)
s.True(strings.Contains(result, utcTimestamp))

}

func (s *LoggerImplTestSuite) TestUTCTextFormatter() {

// returns a tuple containing a UTC time and its conversion to a test timezone 1 hr east of UTC.
// assertions are included to prove that we have created the intended times.
func getTestTimes(a *assert.Assertions) (time.Time, time.Time) {
// create a test location 1 hr east of UTC
loc := time.FixedZone("test/zone", 3600)
utc := time.Now().UTC()
Expand All @@ -419,36 +421,19 @@ func (s *LoggerImplTestSuite) TestUTCTextFormatter() {
expYear, expMonth, expDay := expected.Date()
nonUtcYear, nonUtcMonth, nonUtcDay := nonUtc.Date()

s.Equal(expYear, nonUtcYear)
s.Equal(expMonth, nonUtcMonth)
s.Equal(expDay, nonUtcDay)
a.Equal(expYear, nonUtcYear)
a.Equal(expMonth, nonUtcMonth)
a.Equal(expDay, nonUtcDay)

expHour, expMin, expSec := expected.Clock()
nonUtcHour, nonUtcMin, nonUtcSec := nonUtc.Clock()

s.Equal(expHour, nonUtcHour)
s.Equal(expMin, nonUtcMin)
s.Equal(expSec, nonUtcSec)
a.Equal(expHour, nonUtcHour)
a.Equal(expMin, nonUtcMin)
a.Equal(expSec, nonUtcSec)

// verify shifted time is not in UTC location
s.NotEqual(utc.Location(), nonUtc.Location())

formatter := rslog.NewUTCTextFormatter()

// create a log entry with non-UTC time
entry := &logrus.Entry{
Level: logrus.DebugLevel,
Time: nonUtc,
Message: "test",
}
a.NotEqual(utc.Location(), nonUtc.Location())

serialized, err := formatter.Format(entry)

s.Nil(err)

result := string(serialized)

// assert that UTC time appears in the formatted entry
utcTimestamp := utc.Format(formatter.TimestampFormat)
s.True(strings.Contains(result, utcTimestamp))
return utc, nonUtc
}

0 comments on commit 19318e8

Please sign in to comment.