Skip to content

Commit

Permalink
fix: move removePartKeyById into internal + add test
Browse files Browse the repository at this point in the history
  • Loading branch information
HashMapsData2Value committed Oct 31, 2024
1 parent fc0f559 commit fcce775
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package internal
import (
"context"
"errors"
"github.com/algorandfoundation/hack-tui/api"
"time"

"github.com/algorandfoundation/hack-tui/api"
)

// GetPartKeys get the participation keys from the node
Expand Down Expand Up @@ -116,3 +117,13 @@ func DeletePartKey(ctx context.Context, client *api.ClientWithResponses, partici
}
return nil
}

// Removes a participation key from the list of keys
func RemovePartKeyByID(slice *[]api.ParticipationKey, id string) {
for i, item := range *slice {
if item.Id == id {
*slice = append((*slice)[:i], (*slice)[i+1:]...)
return
}
}
}
55 changes: 55 additions & 0 deletions internal/participation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,58 @@ func Test_DeleteParticipationKey(t *testing.T) {
t.Fatal(err)
}
}
func Test_RemovePartKeyByID(t *testing.T) {
// Test case: Remove an existing key
t.Run("Remove existing key", func(t *testing.T) {
keys := []api.ParticipationKey{
{Id: "key1"},
{Id: "key2"},
{Id: "key3"},
}
expectedKeys := []api.ParticipationKey{
{Id: "key1"},
{Id: "key3"},
}
RemovePartKeyByID(&keys, "key2")
if len(keys) != len(expectedKeys) {
t.Fatalf("expected %d keys, got %d", len(expectedKeys), len(keys))
}
for i, key := range keys {
if key.Id != expectedKeys[i].Id {
t.Fatalf("expected key ID %s, got %s", expectedKeys[i].Id, key.Id)
}
}
})

// Test case: Remove a non-existing key
t.Run("Remove non-existing key", func(t *testing.T) {
keys := []api.ParticipationKey{
{Id: "key1"},
{Id: "key2"},
{Id: "key3"},
}
expectedKeys := []api.ParticipationKey{
{Id: "key1"},
{Id: "key2"},
{Id: "key3"},
}
RemovePartKeyByID(&keys, "key4")
if len(keys) != len(expectedKeys) {
t.Fatalf("expected %d keys, got %d", len(expectedKeys), len(keys))
}
for i, key := range keys {
if key.Id != expectedKeys[i].Id {
t.Fatalf("expected key ID %s, got %s", expectedKeys[i].Id, key.Id)
}
}
})

// Test case: Remove a key from an empty list
t.Run("Remove key from empty list", func(t *testing.T) {
keys := []api.ParticipationKey{}
RemovePartKeyByID(&keys, "key1")
if len(keys) != 0 {
t.Fatalf("expected 0 keys, got %d", len(keys))
}
})
}

0 comments on commit fcce775

Please sign in to comment.