graph/db tests: make compareEdgePolicies assert internally

Convert compareEdgePolicies into a test helper that accepts a
testing handle and performs assertions directly.

Update call sites to invoke the helper instead of threading errors
into immediate require.NoError checks.
This commit is contained in:
Elle Mouton committed 2026-02-25 11:34:31 +02:00
1 parent ceb939ebb1
commit 160c2a9f68
1 file changed
+19 -32
+19 -32
View File
@@ -11,7 +11,6 @@ import (
"math" "math"
prand "math/rand" prand "math/rand"
"net" "net"
"reflect"
"sync" "sync"
"testing" "testing"
"time" "time"
@@ -1196,8 +1195,8 @@ func testEdgeInfoUpdates(t *testing.T, v lnwire.GossipVersion) {
// been inserted properly. // been inserted properly.
dbEdgeInfo, dbEdge1, dbEdge2, err := graph.FetchChannelEdgesByID(chanID) dbEdgeInfo, dbEdge1, dbEdge2, err := graph.FetchChannelEdgesByID(chanID)
require.NoError(t, err, "unable to fetch channel by ID") require.NoError(t, err, "unable to fetch channel by ID")
require.NoError(t, compareEdgePolicies(dbEdge1, edge1)) compareEdgePolicies(t, dbEdge1, edge1)
require.NoError(t, compareEdgePolicies(dbEdge2, edge2)) compareEdgePolicies(t, dbEdge2, edge2)
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo) assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
// Next, attempt to query the channel edges according to the outpoint // Next, attempt to query the channel edges according to the outpoint
@@ -1206,8 +1205,8 @@ func testEdgeInfoUpdates(t *testing.T, v lnwire.GossipVersion) {
&outpoint, &outpoint,
) )
require.NoError(t, err, "unable to fetch channel by ID") require.NoError(t, err, "unable to fetch channel by ID")
require.NoError(t, compareEdgePolicies(dbEdge1, edge1)) compareEdgePolicies(t, dbEdge1, edge1)
require.NoError(t, compareEdgePolicies(dbEdge2, edge2)) compareEdgePolicies(t, dbEdge2, edge2)
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo) assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
} }
@@ -1269,12 +1268,8 @@ func testEdgePolicyCRUD(t *testing.T, v lnwire.GossipVersion) {
policy1 *models.ChannelEdgePolicy, policy1 *models.ChannelEdgePolicy,
policy2 *models.ChannelEdgePolicy) error { policy2 *models.ChannelEdgePolicy) error {
require.NoError( compareEdgePolicies(t, edge1, policy1)
t, compareEdgePolicies(edge1, policy1), compareEdgePolicies(t, edge2, policy2)
)
require.NoError(
t, compareEdgePolicies(edge2, policy2),
)
return nil return nil
}, func() {}, }, func() {},
@@ -2521,15 +2516,12 @@ func TestChanUpdatesInHorizon(t *testing.T) {
assertEdgeInfoEqual(t, chanExp.Info, chanRet.Info) assertEdgeInfoEqual(t, chanExp.Info, chanRet.Info)
err = compareEdgePolicies( compareEdgePolicies(
chanExp.Policy1, chanRet.Policy1, t, chanExp.Policy1, chanRet.Policy1,
) )
require.NoError(t, err) compareEdgePolicies(
t, chanExp.Policy2, chanRet.Policy2,
err = compareEdgePolicies(
chanExp.Policy2, chanRet.Policy2,
) )
require.NoError(t, err)
} }
} }
} }
@@ -3773,10 +3765,8 @@ func testFetchChanInfos(t *testing.T, v lnwire.GossipVersion) {
require.Len(t, resp, len(edges)) require.Len(t, resp, len(edges))
for i := 0; i < len(resp); i++ { for i := 0; i < len(resp); i++ {
err := compareEdgePolicies(resp[i].Policy1, edges[i].Policy1) compareEdgePolicies(t, resp[i].Policy1, edges[i].Policy1)
require.NoError(t, err) compareEdgePolicies(t, resp[i].Policy2, edges[i].Policy2)
err = compareEdgePolicies(resp[i].Policy2, edges[i].Policy2)
require.NoError(t, err)
assertEdgeInfoEqual(t, resp[i].Info, edges[i].Info) assertEdgeInfoEqual(t, resp[i].Info, edges[i].Info)
} }
} }
@@ -4482,7 +4472,7 @@ func TestEdgePolicyMissingMaxHTLC(t *testing.T) {
// The first edge should have a nil-policy returned // The first edge should have a nil-policy returned
require.Nil(t, dbEdge1) require.Nil(t, dbEdge1)
require.NoError(t, compareEdgePolicies(dbEdge2, edge2)) compareEdgePolicies(t, dbEdge2, edge2)
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo) assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
// Now add the original, unmodified edge policy, and make sure the edge // Now add the original, unmodified edge policy, and make sure the edge
@@ -4491,8 +4481,8 @@ func TestEdgePolicyMissingMaxHTLC(t *testing.T) {
dbEdgeInfo, dbEdge1, dbEdge2, err = graph.FetchChannelEdgesByID(chanID) dbEdgeInfo, dbEdge1, dbEdge2, err = graph.FetchChannelEdgesByID(chanID)
require.NoError(t, err, "unable to fetch channel by ID") require.NoError(t, err, "unable to fetch channel by ID")
require.NoError(t, compareEdgePolicies(dbEdge1, edge1)) compareEdgePolicies(t, dbEdge1, edge1)
require.NoError(t, compareEdgePolicies(dbEdge2, edge2)) compareEdgePolicies(t, dbEdge2, edge2)
assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo) assertEdgeInfoEqual(t, dbEdgeInfo, edgeInfo)
} }
@@ -4624,7 +4614,9 @@ func compareNodes(t *testing.T, a, b *models.Node) {
// compareEdgePolicies compares two ChannelEdgePolicy values for semantic // compareEdgePolicies compares two ChannelEdgePolicy values for semantic
// equality after normalizing version-specific/backend-specific differences. // equality after normalizing version-specific/backend-specific differences.
func compareEdgePolicies(a, b *models.ChannelEdgePolicy) error { func compareEdgePolicies(t testing.TB, a, b *models.ChannelEdgePolicy) {
t.Helper()
//nolint:ll //nolint:ll
normalize := func(p *models.ChannelEdgePolicy) *models.ChannelEdgePolicy { normalize := func(p *models.ChannelEdgePolicy) *models.ChannelEdgePolicy {
if p == nil { if p == nil {
@@ -4660,12 +4652,7 @@ func compareEdgePolicies(a, b *models.ChannelEdgePolicy) error {
normalizedA := normalize(a) normalizedA := normalize(a)
normalizedB := normalize(b) normalizedB := normalize(b)
if !reflect.DeepEqual(normalizedA, normalizedB) { require.Equal(t, normalizedA, normalizedB)
return fmt.Errorf("expected %v, got %v", normalizedA,
normalizedB)
}
return nil
} }
// TestLightningNodeSigVerification checks that we can use the Node's // TestLightningNodeSigVerification checks that we can use the Node's