chanfitness/test: switchover to testify

This commit is contained in:
carla 2020-09-08 13:47:15 +02:00
parent 94accfb69d
commit c33d0aad46
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/clock"
"github.com/stretchr/testify/require"
) )
// TestAdd tests adding events to an event log. It tests the case where the // 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. // closed and the event should not be added.
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
eventLog *chanEventLog eventLog *chanEventLog
event eventType event eventType
expected []eventType expectedEvents []*channelEvent
}{ }{
{ {
name: "Channel open", name: "Channel open",
eventLog: &chanEventLog{ eventLog: &chanEventLog{
clock: clock.NewTestClock(testNow), clock: clock.NewTestClock(testNow),
}, },
event: peerOnlineEvent, event: peerOnlineEvent,
expected: []eventType{peerOnlineEvent}, expectedEvents: []*channelEvent{
{
eventType: peerOnlineEvent,
timestamp: testNow,
},
},
}, },
{ {
name: "Channel closed, event not added", name: "Channel closed, event not added",
eventLog: &chanEventLog{ eventLog: &chanEventLog{
clock: clock.NewTestClock(testNow), clock: clock.NewTestClock(testNow),
closedAt: testNow,
}, },
event: peerOnlineEvent, event: peerOnlineEvent,
expected: []eventType{}, expectedEvents: nil,
}, },
} }
@ -41,14 +48,9 @@ func TestAdd(t *testing.T) {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
test.eventLog.add(test.event) test.eventLog.add(test.event)
for i, e := range test.expected { require.Equal(
eventType := test.eventLog.events[i].eventType t, test.expectedEvents, test.eventLog.events,
if eventType != e { )
t.Fatalf("Expected event type: %v, "+
"got: %v", e, eventType,
)
}
}
}) })
} }
} }
@ -146,22 +148,7 @@ func TestGetOnlinePeriod(t *testing.T) {
online := score.getOnlinePeriods() online := score.getOnlinePeriods()
if len(online) != len(test.expectedOnline) { require.Equal(t, test.expectedOnline, online)
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)
}
}
}) })
} }
@ -378,17 +365,8 @@ func TestUptime(t *testing.T) {
uptime, err := score.uptime( uptime, err := score.uptime(
test.startTime, test.endTime, test.startTime, test.endTime,
) )
if test.expectErr && err == nil { require.Equal(t, test.expectErr, err != nil)
t.Fatal("Expected an error, got nil") require.Equal(t, test.expectedUptime, uptime)
}
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)
}
}) })
} }
} }