From a648635d604c8f2b1e776c94efd98d71a3fc704c Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Fri, 17 May 2024 14:37:19 +0100 Subject: [PATCH] test_: Added much better coverage for testing for filterNetworkComplianceV2 --- services/wallet/router/filter_test.go | 174 +++++++++++++++++++++++--- 1 file changed, 156 insertions(+), 18 deletions(-) diff --git a/services/wallet/router/filter_test.go b/services/wallet/router/filter_test.go index 15db47a71..8f2ce6cf4 100644 --- a/services/wallet/router/filter_test.go +++ b/services/wallet/router/filter_test.go @@ -203,31 +203,169 @@ func TestHasSufficientCapacityV2(t *testing.T) { } func TestFilterNetworkComplianceV2(t *testing.T) { - fromLockedAmount := map[uint64]*hexutil.Big{ - 1: (*hexutil.Big)(big.NewInt(100)), - 2: (*hexutil.Big)(big.NewInt(0)), - } - - routes := [][]*PathV2{ + tests := []struct { + name string + routes [][]*PathV2 + fromLockedAmount map[uint64]*hexutil.Big + expectedRoutes [][]*PathV2 + }{ { - {From: ¶ms.Network{ChainID: 1}}, - {From: ¶ms.Network{ChainID: 3}}, + name: "Mixed routes with valid and invalid paths", + routes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + { + {From: ¶ms.Network{ChainID: 2}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 2}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + }, + fromLockedAmount: map[uint64]*hexutil.Big{ + 1: (*hexutil.Big)(big.NewInt(100)), + 2: (*hexutil.Big)(big.NewInt(0)), + }, + expectedRoutes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 2}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + }, }, { - {From: ¶ms.Network{ChainID: 2}}, - {From: ¶ms.Network{ChainID: 3}}, + name: "All valid routes", + routes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 4}}, + }, + }, + fromLockedAmount: map[uint64]*hexutil.Big{ + 1: (*hexutil.Big)(big.NewInt(100)), + }, + expectedRoutes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 4}}, + }, + }, + }, + { + name: "All invalid routes", + routes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 2}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + { + {From: ¶ms.Network{ChainID: 4}}, + {From: ¶ms.Network{ChainID: 5}}, + }, + }, + fromLockedAmount: map[uint64]*hexutil.Big{ + 1: (*hexutil.Big)(big.NewInt(100)), + 2: (*hexutil.Big)(big.NewInt(0)), + }, + expectedRoutes: [][]*PathV2{}, + }, + { + name: "Empty routes", + routes: [][]*PathV2{}, + fromLockedAmount: map[uint64]*hexutil.Big{ + 1: (*hexutil.Big)(big.NewInt(100)), + }, + expectedRoutes: [][]*PathV2{}, + }, + { + name: "No locked amounts", + routes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 2}}, + }, + { + {From: ¶ms.Network{ChainID: 3}}, + {From: ¶ms.Network{ChainID: 4}}, + }, + }, + fromLockedAmount: map[uint64]*hexutil.Big{}, + expectedRoutes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 2}}, + }, + { + {From: ¶ms.Network{ChainID: 3}}, + {From: ¶ms.Network{ChainID: 4}}, + }, + }, + }, + { + name: "Single route with mixed valid and invalid paths", + routes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 2}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + }, + fromLockedAmount: map[uint64]*hexutil.Big{ + 1: (*hexutil.Big)(big.NewInt(100)), + 2: (*hexutil.Big)(big.NewInt(0)), + }, + expectedRoutes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 3}}, + }, + }, + }, + { + name: "Routes with duplicate chain IDs", + routes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 2}}, + }, + }, + fromLockedAmount: map[uint64]*hexutil.Big{ + 1: (*hexutil.Big)(big.NewInt(100)), + }, + expectedRoutes: [][]*PathV2{ + { + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 1}}, + {From: ¶ms.Network{ChainID: 2}}, + }, + }, }, } - expectedRoutes := [][]*PathV2{ - { - {From: ¶ms.Network{ChainID: 1}}, - {From: ¶ms.Network{ChainID: 3}}, - }, + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + filteredRoutes := filterNetworkComplianceV2(tt.routes, tt.fromLockedAmount) + assert.Equal(t, tt.expectedRoutes, filteredRoutes) + }) } - - filteredRoutes := filterNetworkComplianceV2(routes, fromLockedAmount) - assert.Equal(t, expectedRoutes, filteredRoutes) } func TestFilterCapacityValidationV2(t *testing.T) {