-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_github_getters_test.go
More file actions
252 lines (237 loc) · 6.87 KB
/
Copy pathdb_github_getters_test.go
File metadata and controls
252 lines (237 loc) · 6.87 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package dalgo2ghingitdb
import (
"context"
"fmt"
"testing"
"github.com/dal-go/dalgo/dal"
"github.com/ingitdb/ingitdb-go/ingitdb"
)
func TestGitHubDB_ID(t *testing.T) {
t.Parallel()
cfg := Config{Owner: "test", Repo: "test"}
def := &ingitdb.Definition{Collections: map[string]*ingitdb.CollectionDef{}}
db, err := NewGitHubDBWithDef(cfg, def)
if err != nil {
t.Fatalf("NewGitHubDBWithDef: %v", err)
}
id := db.ID()
if id != DatabaseID {
t.Errorf("ID() = %q, want %q", id, DatabaseID)
}
}
func TestGitHubDB_Adapter(t *testing.T) {
t.Parallel()
cfg := Config{Owner: "test", Repo: "test"}
def := &ingitdb.Definition{Collections: map[string]*ingitdb.CollectionDef{}}
db, err := NewGitHubDBWithDef(cfg, def)
if err != nil {
t.Fatalf("NewGitHubDBWithDef: %v", err)
}
adapter := db.Adapter()
if adapter == nil {
t.Fatal("Adapter() returned nil")
}
if adapter.Name() != DatabaseID {
t.Errorf("Adapter().Name() = %q, want %q", adapter.Name(), DatabaseID)
}
if adapter.Version() != "v0.0.1" {
t.Errorf("Adapter().Version() = %q, want %q", adapter.Version(), "v0.0.1")
}
}
func TestGitHubDB_Schema(t *testing.T) {
t.Parallel()
cfg := Config{Owner: "test", Repo: "test"}
def := &ingitdb.Definition{Collections: map[string]*ingitdb.CollectionDef{}}
db, err := NewGitHubDBWithDef(cfg, def)
if err != nil {
t.Fatalf("NewGitHubDBWithDef: %v", err)
}
schema := db.Schema()
if schema != nil {
t.Errorf("Schema() = %v, want nil", schema)
}
}
func TestNewGitHubDB(t *testing.T) {
t.Parallel()
cfg := Config{Owner: "test", Repo: "test"}
db, err := NewGitHubDB(cfg)
if err != nil {
t.Fatalf("NewGitHubDB: %v", err)
}
if db == nil {
t.Fatal("NewGitHubDB returned nil DB")
}
if db.ID() != DatabaseID {
t.Errorf("DB.ID() = %q, want %q", db.ID(), DatabaseID)
}
}
func TestNewGitHubDB_InvalidConfig(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cfg Config
wantError string
}{
{
name: "missing owner",
cfg: Config{Repo: "test"},
wantError: "owner is required",
},
{
name: "missing repo",
cfg: Config{Owner: "test"},
wantError: "repo is required",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
_, err := NewGitHubDB(tc.cfg)
if err == nil {
t.Fatal("NewGitHubDB() expected error, got nil")
}
if tc.wantError != "" && err.Error() != tc.wantError {
t.Errorf("NewGitHubDB() error = %q, want %q", err.Error(), tc.wantError)
}
})
}
}
func TestNewGitHubDBWithDef_NilDefinition(t *testing.T) {
t.Parallel()
cfg := Config{Owner: "test", Repo: "test"}
_, err := NewGitHubDBWithDef(cfg, nil)
if err == nil {
t.Fatal("NewGitHubDBWithDef() expected error for nil definition, got nil")
}
expectedMsg := "definition is required"
if err.Error() != expectedMsg {
t.Errorf("NewGitHubDBWithDef() error = %q, want %q", err.Error(), expectedMsg)
}
}
func TestGitHubDB_Get(t *testing.T) {
t.Parallel()
fixtures := []githubFileFixture{{
path: "demo-dbs/todo/tags/$records/active.yaml",
content: "title: Active\n",
}}
server := newGitHubContentsServer(t, fixtures)
defer server.Close()
def := &ingitdb.Definition{
Collections: map[string]*ingitdb.CollectionDef{
"todo.tags": {
ID: "todo.tags",
DirPath: "demo-dbs/todo/tags",
RecordFile: &ingitdb.RecordFileDef{
Name: "{key}.yaml",
Format: "yaml",
RecordType: ingitdb.SingleRecord,
},
Columns: map[string]*ingitdb.ColumnDef{
"title": {Type: ingitdb.ColumnTypeString},
},
},
},
}
cfg := Config{Owner: "ingitdb", Repo: "ingitdb-cli", Ref: "main", APIBaseURL: server.URL + "/"}
db, err := NewGitHubDBWithDef(cfg, def)
if err != nil {
t.Fatalf("NewGitHubDBWithDef: %v", err)
}
key := dal.NewKeyWithID("todo.tags", "active")
data := map[string]any{}
record := dal.NewRecordWithData(key, data)
ctx := context.Background()
err = db.Get(ctx, record)
if err != nil {
t.Fatalf("Get: %v", err)
}
if !record.Exists() {
t.Fatal("expected record to exist")
}
if data["title"] != "Active" {
t.Errorf("expected title=Active, got %v", data["title"])
}
}
func TestGitHubDB_Exists(t *testing.T) {
t.Parallel()
cfg := Config{Owner: "test", Repo: "test"}
def := &ingitdb.Definition{Collections: map[string]*ingitdb.CollectionDef{}}
db, err := NewGitHubDBWithDef(cfg, def)
if err != nil {
t.Fatalf("NewGitHubDBWithDef: %v", err)
}
ctx := context.Background()
key := dal.NewKeyWithID("test", "test")
exists, err := db.Exists(ctx, key)
if err == nil {
t.Fatal("Exists() expected error, got nil")
}
expectedMsg := fmt.Sprintf("exists is not implemented by %s", DatabaseID)
if err.Error() != expectedMsg {
t.Errorf("Exists() error = %q, want %q", err.Error(), expectedMsg)
}
if exists {
t.Error("Exists() = true, want false")
}
}
func TestGitHubDB_GetMulti(t *testing.T) {
t.Parallel()
cfg := Config{Owner: "test", Repo: "test"}
def := &ingitdb.Definition{Collections: map[string]*ingitdb.CollectionDef{}}
db, err := NewGitHubDBWithDef(cfg, def)
if err != nil {
t.Fatalf("NewGitHubDBWithDef: %v", err)
}
ctx := context.Background()
records := []dal.Record{}
err = db.GetMulti(ctx, records)
if err == nil {
t.Fatal("GetMulti() expected error, got nil")
}
expectedMsg := fmt.Sprintf("getmulti is not implemented by %s", DatabaseID)
if err.Error() != expectedMsg {
t.Errorf("GetMulti() error = %q, want %q", err.Error(), expectedMsg)
}
}
func TestGitHubDB_ExecuteQueryToRecordsReader(t *testing.T) {
t.Parallel()
cfg := Config{Owner: "test", Repo: "test"}
def := &ingitdb.Definition{Collections: map[string]*ingitdb.CollectionDef{}}
db, err := NewGitHubDBWithDef(cfg, def)
if err != nil {
t.Fatalf("NewGitHubDBWithDef: %v", err)
}
ctx := context.Background()
reader, err := db.ExecuteQueryToRecordsReader(ctx, nil)
if err == nil {
t.Fatal("ExecuteQueryToRecordsReader() expected error, got nil")
}
expectedMsg := fmt.Sprintf("query is not implemented by %s", DatabaseID)
if err.Error() != expectedMsg {
t.Errorf("ExecuteQueryToRecordsReader() error = %q, want %q", err.Error(), expectedMsg)
}
if reader != nil {
t.Error("ExecuteQueryToRecordsReader() reader should be nil")
}
}
func TestGitHubDB_ExecuteQueryToRecordsetReader(t *testing.T) {
t.Parallel()
cfg := Config{Owner: "test", Repo: "test"}
def := &ingitdb.Definition{Collections: map[string]*ingitdb.CollectionDef{}}
db, err := NewGitHubDBWithDef(cfg, def)
if err != nil {
t.Fatalf("NewGitHubDBWithDef: %v", err)
}
ctx := context.Background()
reader, err := db.ExecuteQueryToRecordsetReader(ctx, nil)
if err == nil {
t.Fatal("ExecuteQueryToRecordsetReader() expected error, got nil")
}
expectedMsg := fmt.Sprintf("query is not implemented by %s", DatabaseID)
if err.Error() != expectedMsg {
t.Errorf("ExecuteQueryToRecordsetReader() error = %q, want %q", err.Error(), expectedMsg)
}
if reader != nil {
t.Error("ExecuteQueryToRecordsetReader() reader should be nil")
}
}