From c33d0aad465ef2454f44f0c3ca563a38869e7192 Mon Sep 17 00:00:00 2001 From: carla Date: Tue, 8 Sep 2020 13:47:15 +0200 Subject: [PATCH] chanfitness/test: switchover to testify --- chanfitness/chanevent_test.go | 66 ++++++++++++----------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/chanfitness/chanevent_test.go b/chanfitness/chanevent_test.go index 457ae80b..4086a40f 100644 --- a/chanfitness/chanevent_test.go +++ b/chanfitness/chanevent_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/lightningnetwork/lnd/clock" + "github.com/stretchr/testify/require" ) // TestAdd tests adding events to an event log. It tests the case where the @@ -12,26 +13,32 @@ import ( // closed and the event should not be added. func TestAdd(t *testing.T) { tests := []struct { - name string - eventLog *chanEventLog - event eventType - expected []eventType + name string + eventLog *chanEventLog + event eventType + expectedEvents []*channelEvent }{ { name: "Channel open", eventLog: &chanEventLog{ clock: clock.NewTestClock(testNow), }, - event: peerOnlineEvent, - expected: []eventType{peerOnlineEvent}, + event: peerOnlineEvent, + expectedEvents: []*channelEvent{ + { + eventType: peerOnlineEvent, + timestamp: testNow, + }, + }, }, { name: "Channel closed, event not added", eventLog: &chanEventLog{ - clock: clock.NewTestClock(testNow), + clock: clock.NewTestClock(testNow), + closedAt: testNow, }, - event: peerOnlineEvent, - expected: []eventType{}, + event: peerOnlineEvent, + expectedEvents: nil, }, } @@ -41,14 +48,9 @@ func TestAdd(t *testing.T) { t.Run(test.name, func(t *testing.T) { test.eventLog.add(test.event) - for i, e := range test.expected { - eventType := test.eventLog.events[i].eventType - if eventType != e { - t.Fatalf("Expected event type: %v, "+ - "got: %v", e, eventType, - ) - } - } + require.Equal( + t, test.expectedEvents, test.eventLog.events, + ) }) } } @@ -146,22 +148,7 @@ func TestGetOnlinePeriod(t *testing.T) { online := score.getOnlinePeriods() - if len(online) != len(test.expectedOnline) { - t.Fatalf("Expectd: %v online periods, got: %v", - len(test.expectedOnline), len(online)) - } - - for i, o := range test.expectedOnline { - if online[i].start != o.start { - t.Errorf("Expected start: %v, got "+ - "%v", o.start, online[i].start) - } - - if online[i].end != o.end { - t.Errorf("Expected end: %v, got %v", - o.end, online[i].end) - } - } + require.Equal(t, test.expectedOnline, online) }) } @@ -378,17 +365,8 @@ func TestUptime(t *testing.T) { uptime, err := score.uptime( test.startTime, test.endTime, ) - if test.expectErr && err == nil { - t.Fatal("Expected an error, got nil") - } - if !test.expectErr && err != nil { - t.Fatalf("Expcted no error, got: %v", err) - } - - if uptime != test.expectedUptime { - t.Errorf("Expected uptime: %v, got: %v", - test.expectedUptime, uptime) - } + require.Equal(t, test.expectErr, err != nil) + require.Equal(t, test.expectedUptime, uptime) }) } }