From 7904449ee034610fc29cee2ac7363581f654f581 Mon Sep 17 00:00:00 2001 From: pablomendezroyo Date: Tue, 7 May 2024 17:00:12 +0200 Subject: [PATCH] add test --- .../api/validation/GetActiveValidators.go | 8 ++--- .../validation/GetActiveValidators_test.go | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/listener/internal/api/validation/GetActiveValidators.go b/listener/internal/api/validation/GetActiveValidators.go index ecfa7db..c6831c9 100644 --- a/listener/internal/api/validation/GetActiveValidators.go +++ b/listener/internal/api/validation/GetActiveValidators.go @@ -22,14 +22,14 @@ func GetActiveValidators(requestsDecoded []types.SignatureRequestDecoded, beacon // iterate over the networks available in the beaconNodeUrls map for network, url := range beaconNodeUrls { // prepare request body, get the list of ids from the requestsDecoded for the current network - idss := make([]string, 0, len(requestsDecoded)) + ids := make([]string, 0, len(requestsDecoded)) for _, req := range requestsDecoded { if req.Network == network { - idss = append(idss, req.DecodedPayload.Pubkey) + ids = append(ids, req.DecodedPayload.Pubkey) } } // if there are no ids for the current network, log and skip it - if len(idss) == 0 { + if len(ids) == 0 { fmt.Printf("no ids for network %s\n", network) continue } @@ -39,7 +39,7 @@ func GetActiveValidators(requestsDecoded []types.SignatureRequestDecoded, beacon Ids []string `json:"ids"` Statuses []string `json:"statuses"` }{ - Ids: idss, + Ids: ids, Statuses: []string{"active_ongoing"}, }) if err != nil { diff --git a/listener/internal/api/validation/GetActiveValidators_test.go b/listener/internal/api/validation/GetActiveValidators_test.go index 9c6c565..8486a14 100644 --- a/listener/internal/api/validation/GetActiveValidators_test.go +++ b/listener/internal/api/validation/GetActiveValidators_test.go @@ -2,8 +2,43 @@ package validation import ( "testing" + + "github.com/dappnode/validator-monitoring/listener/internal/api/types" ) func TestGetActiveValidators(t *testing.T) { + // Setup the input data + beaconNodeUrls := map[string]string{ + "holesky": "https://holesky.infura.io/v3/e6c920580178424bbdf6dde266bfb5bd", + } + + requestsDecoded := []types.SignatureRequestDecoded{ + { + Network: "holesky", + DecodedPayload: types.DecodedPayload{ + Pubkey: "0xa685beb5a1f317f5a01ecd6dade42113aad945b2ab53fb1b356334ab441323e538feadd2889894b17f8fa2babe1989ca", + }, + }, + { + Network: "holesky", + DecodedPayload: types.DecodedPayload{ + Pubkey: "0xab31efdd97f32087e96d3262f6fb84a4480411d391689be0dfc931fd8a5c16c3f51f10b127040b1cb65eb955f2b78a63", + }, + }, + { + Network: "holesky", + DecodedPayload: types.DecodedPayload{ + Pubkey: "0xa24a030d7d8ca3c5e1f5824760d0f4157a7a89bcca6414377cca97e6e63445bef0e1b63761ee35a0fc46bb317e31b34b", + }, + }, + } + + // Call the function + result := GetActiveValidators(requestsDecoded, beaconNodeUrls) + // You may need to mock the server's response or adjust the expected values here according to your actual setup + expectedNumValidators := 3 // This should match the number of mock validators that are "active" + if len(result) != expectedNumValidators { + t.Errorf("Expected %d active validators, got %d", expectedNumValidators, len(result)) + } }