aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/data/put_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/data/put_test.go')
-rw-r--r--pkg/data/put_test.go77
1 files changed, 61 insertions, 16 deletions
diff --git a/pkg/data/put_test.go b/pkg/data/put_test.go
index 7e5ad38..a97187b 100644
--- a/pkg/data/put_test.go
+++ b/pkg/data/put_test.go
@@ -1,7 +1,6 @@
package data_test
import (
- "context"
"database/sql"
"errors"
"testing"
@@ -31,13 +30,14 @@ func TestPut_Insert(t *testing.T) {
FileTime: time.Unix(2, 0),
Authors: []string{"jp"},
Tags: []string{"foo", "bar", "oof", "baz"},
+ Links: []string{"link_1", "link_2", "link_3"},
},
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- ctx := context.Background()
+ ctx := t.Context()
db := tt.newDb(t)
defer db.Close()
@@ -68,31 +68,76 @@ func TestPut_Insert(t *testing.T) {
func TestPutMany_Insert(t *testing.T) {
tests := []struct {
- name string // description of this test case
- // Named input parameters for receiver constructor.
- db *sql.DB
+ name string
+ newDb func(t *testing.T) *sql.DB
documents map[string]*index.Document
- wantErr bool
+ wantErr error
}{
- // TODO: Add test cases.
+ {
+ name: "insert on empty",
+ newDb: func(t *testing.T) *sql.DB {
+ t.Helper()
+ return data.NewMemDB()
+ },
+ documents: map[string]*index.Document{
+ "/file": {
+ Path: "/file",
+ Title: "A file",
+ Date: time.Unix(1, 0),
+ FileTime: time.Unix(2, 0),
+ Authors: []string{"jp"},
+ Tags: []string{"foo", "bar", "oof", "baz"},
+ Links: []string{"link_1", "link_2", "link_3"},
+ },
+ "/file2": {
+ Path: "/file2",
+ Title: "A different file",
+ Date: time.Unix(3, 0),
+ FileTime: time.Unix(4, 0),
+ Authors: []string{"pj"},
+ Tags: []string{"apple", "pear", "peach"},
+ Links: []string{"a very useful link"},
+ },
+ },
+ wantErr: nil,
+ },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- p, err := data.NewPutMany(tt.db, tt.documents)
+ db := tt.newDb(t)
+ p, err := data.NewPutMany(db, tt.documents)
if err != nil {
t.Fatalf("could not construct receiver type: %v", err)
}
- gotErr := p.Insert(context.Background())
- if gotErr != nil {
- if !tt.wantErr {
- t.Errorf("Insert() failed: %v", gotErr)
- }
+
+ gotErr := p.Insert(t.Context())
+ if !errors.Is(gotErr, tt.wantErr) {
+ t.Fatalf("Recieved unexpected error, got %v want %v", gotErr, tt.wantErr)
+ } else if err != nil {
return
}
- if tt.wantErr {
- t.Fatal("Insert() succeeded unexpectedly")
+
+ f := data.FillMany{Db: db}
+ gotDocs, err := f.Get(t.Context())
+ if err != nil {
+ t.Fatal("Error while retrieving documents for comparison:", err)
+ }
+
+ wantLen, gotLen := len(tt.documents), len(gotDocs)
+ if wantLen != gotLen {
+ t.Fatalf("Recieved differnt number of documents than expected: got %d, want %d", gotLen, wantLen)
+ }
+
+ for path, wantDoc := range tt.documents {
+ gotDoc, ok := gotDocs[path]
+ if !ok {
+ t.Errorf("Wanted doc with path %s but did not recieve it", path)
+ }
+
+ if !wantDoc.Equal(*gotDoc) {
+ t.Errorf("Difference betwen docs!\ngot: %+v\nwant: %+v", gotDoc, wantDoc)
+ }
}
})
}
}
-