channeldb: reformat TestFilterChannelRange

This commit is contained in:
Elle Mouton committed 2023-12-11 09:12:04 +02:00
1 parent a439cc970f
commit 50e17dab59
1 file changed
+31 -33
+31 -33
View File
@@ -2031,28 +2031,23 @@ func TestFilterChannelRange(t *testing.T) {
t.Parallel() t.Parallel()
graph, err := MakeTestGraph(t) graph, err := MakeTestGraph(t)
require.NoError(t, err, "unable to make test database") require.NoError(t, err)
// We'll first populate our graph with two nodes. All channels created // We'll first populate our graph with two nodes. All channels created
// below will be made between these two nodes. // below will be made between these two nodes.
node1, err := createTestVertex(graph.db) node1, err := createTestVertex(graph.db)
require.NoError(t, err, "unable to create test node") require.NoError(t, err)
if err := graph.AddLightningNode(node1); err != nil { require.NoError(t, graph.AddLightningNode(node1))
t.Fatalf("unable to add node: %v", err)
}
node2, err := createTestVertex(graph.db) node2, err := createTestVertex(graph.db)
require.NoError(t, err, "unable to create test node") require.NoError(t, err)
if err := graph.AddLightningNode(node2); err != nil { require.NoError(t, graph.AddLightningNode(node2))
t.Fatalf("unable to add node: %v", err)
}
// If we try to filter a channel range before we have any channels // If we try to filter a channel range before we have any channels
// inserted, we should get an empty slice of results. // inserted, we should get an empty slice of results.
resp, err := graph.FilterChannelRange(10, 100) resp, err := graph.FilterChannelRange(10, 100)
require.NoError(t, err, "unable to filter channels") require.NoError(t, err)
if len(resp) != 0 { require.Empty(t, resp)
t.Fatalf("expected zero chans, instead got %v", len(resp))
}
// To start, we'll create a set of channels, two mined in a block 10 // To start, we'll create a set of channels, two mined in a block 10
// blocks after the prior one. // blocks after the prior one.
@@ -2065,16 +2060,12 @@ func TestFilterChannelRange(t *testing.T) {
channel1, chanID1 := createEdge( channel1, chanID1 := createEdge(
chanHeight, uint32(i+1), 0, 0, node1, node2, chanHeight, uint32(i+1), 0, 0, node1, node2,
) )
if err := graph.AddChannelEdge(&channel1); err != nil { require.NoError(t, graph.AddChannelEdge(&channel1))
t.Fatalf("unable to create channel edge: %v", err)
}
channel2, chanID2 := createEdge( channel2, chanID2 := createEdge(
chanHeight, uint32(i+2), 0, 0, node1, node2, chanHeight, uint32(i+2), 0, 0, node1, node2,
) )
if err := graph.AddChannelEdge(&channel2); err != nil { require.NoError(t, graph.AddChannelEdge(&channel2))
t.Fatalf("unable to create channel edge: %v", err)
}
channelRanges = append(channelRanges, BlockChannelRange{ channelRanges = append(channelRanges, BlockChannelRange{
Height: chanHeight, Height: chanHeight,
@@ -2086,7 +2077,9 @@ func TestFilterChannelRange(t *testing.T) {
// With our channels inserted, we'll construct a series of queries that // With our channels inserted, we'll construct a series of queries that
// we'll execute below in order to exercise the features of the // we'll execute below in order to exercise the features of the
// FilterKnownChanIDs method. // FilterKnownChanIDs method.
queryCases := []struct { tests := []struct {
name string
startHeight uint32 startHeight uint32
endHeight uint32 endHeight uint32
@@ -2095,15 +2088,17 @@ func TestFilterChannelRange(t *testing.T) {
// If we query for the entire range, then we should get the same // If we query for the entire range, then we should get the same
// set of short channel IDs back. // set of short channel IDs back.
{ {
name: "entire range",
startHeight: startHeight, startHeight: startHeight,
endHeight: endHeight, endHeight: endHeight,
resp: channelRanges, resp: channelRanges,
}, },
// If we query for a range of channels right before our range, we // If we query for a range of channels right before our range,
// shouldn't get any results back. // we shouldn't get any results back.
{ {
name: "range before",
startHeight: 0, startHeight: 0,
endHeight: 10, endHeight: 10,
}, },
@@ -2111,6 +2106,7 @@ func TestFilterChannelRange(t *testing.T) {
// If we only query for the last height (range wise), we should // If we only query for the last height (range wise), we should
// only get that last channel. // only get that last channel.
{ {
name: "last height",
startHeight: endHeight - 10, startHeight: endHeight - 10,
endHeight: endHeight - 10, endHeight: endHeight - 10,
@@ -2120,6 +2116,7 @@ func TestFilterChannelRange(t *testing.T) {
// If we query for just the first height, we should only get a // If we query for just the first height, we should only get a
// single channel back (the first one). // single channel back (the first one).
{ {
name: "first height",
startHeight: startHeight, startHeight: startHeight,
endHeight: startHeight, endHeight: startHeight,
@@ -2127,24 +2124,25 @@ func TestFilterChannelRange(t *testing.T) {
}, },
{ {
name: "subset",
startHeight: startHeight + 10, startHeight: startHeight + 10,
endHeight: endHeight - 10, endHeight: endHeight - 10,
resp: channelRanges[1:5], resp: channelRanges[1:5],
}, },
} }
for i, queryCase := range queryCases { for _, test := range tests {
resp, err := graph.FilterChannelRange( test := test
queryCase.startHeight, queryCase.endHeight,
)
if err != nil {
t.Fatalf("unable to issue range query: %v", err)
}
if !reflect.DeepEqual(resp, queryCase.resp) { t.Run(test.name, func(t *testing.T) {
t.Fatalf("case #%v: expected %v, got %v", i, t.Parallel()
queryCase.resp, resp)
} resp, err := graph.FilterChannelRange(
test.startHeight, test.endHeight,
)
require.NoError(t, err)
require.Equal(t, test.resp, resp)
})
} }
} }