Skip to content

Commit

Permalink
test: add unit tests for strIndex and strToASN
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed Nov 2, 2024
1 parent 4aa2f63 commit eca078e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/database/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package database

var (
StrToASN = strToASN
StrIndex = strIndex
)
54 changes: 54 additions & 0 deletions pkg/database/resolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package database_test

import (
"testing"

"github.com/danroc/geoblock/pkg/database"
)

func TestStrIndex(t *testing.T) {
tests := []struct {
data []string
index int
expected string
}{
{[]string{"a", "b", "c"}, 0, "a"},
{[]string{"a", "b", "c"}, 1, "b"},
{[]string{"a", "b", "c"}, 2, "c"},
{[]string{"a", "b", "c"}, 3, ""},
{[]string{"a", "b", "c"}, -1, ""},
{[]string{}, 0, ""},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
result := database.StrIndex(tt.data, tt.index)
if result != tt.expected {
t.Errorf("got %q, want %q", result, tt.expected)
}
})
}
}

func TestStrToASN(t *testing.T) {
tests := []struct {
input string
expected uint32
}{
{"12345", 12345},
{"0", 0},
{"4294967295", 4294967295},
{"invalid", database.ReservedAS0},
{"", database.ReservedAS0},
{"-1", database.ReservedAS0},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := database.StrToASN(tt.input)
if result != tt.expected {
t.Errorf("got %d, want %d", result, tt.expected)
}
})
}
}

0 comments on commit eca078e

Please sign in to comment.