Skip to content

Commit

Permalink
test: add more database tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed Nov 5, 2024
1 parent 054762a commit 764084f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/database/database_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database_test

import (
"errors"
"net"
"strings"
"testing"
Expand All @@ -23,6 +24,12 @@ const (

// No CSV data
csvData5 = "\n"

// Invalid start IP
csvData6 = "invalid-ip,192.168.1.2,data1,data2\n"

// Invalid end IP
csvData7 = "192.168.1.1,invalid-ip,data1,data2\n"
)

func TestNewDatabase(t *testing.T) {
Expand All @@ -36,6 +43,8 @@ func TestNewDatabase(t *testing.T) {
{"Missing end IP", csvData3, true},
{"Missing data", csvData4, false},
{"No CSV data", csvData5, false},
{"Invalid start IP", csvData6, true},
{"Invalid end IP", csvData7, true},
}

for _, test := range tests {
Expand All @@ -50,6 +59,20 @@ func TestNewDatabase(t *testing.T) {
}
}

type errorReader struct{}

func (r *errorReader) Read(p []byte) (n int, err error) {
return 0, errors.New("read error")
}

func TestNewDatabaseReadErr(t *testing.T) {
reader := &errorReader{}
_, err := database.NewDatabase(reader)
if err == nil {
t.Fatalf("Expected an error but got nil")
}
}

func TestFind(t *testing.T) {
reader := strings.NewReader(csvData1)
db, err := database.NewDatabase(reader)
Expand Down

0 comments on commit 764084f

Please sign in to comment.