1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package data_test
import (
"database/sql"
"errors"
"testing"
"time"
"github.com/jpappel/atlas/pkg/data"
"github.com/jpappel/atlas/pkg/index"
)
func TestPut_Insert(t *testing.T) {
tests := []struct {
name string
newDb func(t *testing.T) *sql.DB
doc index.Document
wantErr error
}{
{
"insert on empty",
func(t *testing.T) *sql.DB {
t.Helper()
return data.NewMemDB()
},
index.Document{
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"},
},
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := t.Context()
db := tt.newDb(t)
defer db.Close()
p, err := data.NewPut(ctx, db, tt.doc)
if err != nil {
t.Fatalf("could not construct receiver type: %v", err)
}
gotErr := p.Insert()
if !errors.Is(gotErr, tt.wantErr) {
t.Fatalf("Unexpected error on Insert():, want %v got %v", tt.wantErr, gotErr)
} else if gotErr != nil {
return
}
f := data.Fill{Path: tt.doc.Path, Db: db}
gotDoc, err := f.Get(ctx)
if err != nil {
t.Fatal("Error while retrieving document for comparison:", err)
}
if !gotDoc.Equal(tt.doc) {
t.Errorf("Retrieved doc is not stored doc!\nrecv: %+v\nsent: %+v", gotDoc, tt.doc)
}
})
}
}
func TestPutMany_Insert(t *testing.T) {
tests := []struct {
name string
newDb func(t *testing.T) *sql.DB
documents map[string]*index.Document
wantErr error
}{
{
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) {
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(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
}
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)
}
}
})
}
}
|