-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_reader_test.go
More file actions
153 lines (137 loc) · 4.88 KB
/
Copy pathtree_reader_test.go
File metadata and controls
153 lines (137 loc) · 4.88 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
package dalgo2ghingitdb
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/dal-go/record"
"github.com/google/go-github/v88/github"
)
// TestTreeReader_ReadsBlobFromHeadTree verifies that the tree-based reader
// resolves branch head → recursive tree → blob and returns the blob content,
// and reports found=false for a path absent from the head tree.
func TestTreeReader_ReadsBlobFromHeadTree(t *testing.T) {
t.Parallel()
fixtures := map[string]string{
"data/tags/$records/active.yaml": "title: Active\n",
}
srv := makeBatchServer(t, fixtures)
defer srv.Close()
cfg := Config{Owner: "ingitdb", Repo: "ingitdb-cli", Ref: "main", APIBaseURL: srv.URL + "/"}
tr, err := newTreeReader(cfg)
if err != nil {
t.Fatalf("newTreeReader: %v", err)
}
ctx := context.Background()
content, found, err := tr.readFile(ctx, "data/tags/$records/active.yaml")
if err != nil {
t.Fatalf("readFile: %v", err)
}
if !found {
t.Fatal("expected blob to be found in head tree")
}
if string(content) != "title: Active\n" {
t.Errorf("content = %q, want %q", content, "title: Active\n")
}
_, found, err = tr.readFile(ctx, "data/tags/$records/missing.yaml")
if err != nil {
t.Fatalf("readFile(missing): %v", err)
}
if found {
t.Error("expected missing path to report found=false")
}
}
// TestTreeReader_TruncatedTreeIsError verifies that a truncated tree is a hard
// error (consistent reads require a complete view of the head tree).
func TestTreeReader_TruncatedTreeIsError(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := r.URL.Path
switch {
case strings.Contains(p, "/git/ref/heads/main"):
_ = json.NewEncoder(w).Encode(map[string]any{
"ref": "refs/heads/main",
"object": map[string]any{"sha": "c", "type": "commit"},
})
case strings.Contains(p, "/git/commits/c"):
_ = json.NewEncoder(w).Encode(map[string]any{"sha": "c", "tree": map[string]any{"sha": "t"}})
case strings.Contains(p, "/git/trees/"):
_ = json.NewEncoder(w).Encode(map[string]any{"sha": "t", "tree": []any{}, "truncated": true})
default:
http.Error(w, "unexpected", http.StatusInternalServerError)
}
}))
defer srv.Close()
cfg := Config{Owner: "o", Repo: "r", Ref: "main", APIBaseURL: srv.URL + "/"}
tr, err := newTreeReader(cfg)
if err != nil {
t.Fatalf("newTreeReader: %v", err)
}
if _, _, err := tr.readFile(context.Background(), "x.yaml"); err == nil || !strings.Contains(err.Error(), "truncated") {
t.Fatalf("expected truncated-tree error, got %v", err)
}
}
// TestBatchingGitHubDB_Get_ReadAfterWrite verifies that the batching DB's Get
// reads through the Git Data API and returns a record whose blob is present in
// the head tree — the strongly-consistent read path.
func TestBatchingGitHubDB_Get_ReadAfterWrite(t *testing.T) {
t.Parallel()
def := singleRecordDef("tags", "data/tags", "{key}.yaml")
fixtures := map[string]string{
resolveRecordPath(def.Collections["tags"], "active"): "title: Active\n",
}
srv := makeBatchServer(t, fixtures)
defer srv.Close()
cfg := Config{Owner: "ingitdb", Repo: "ingitdb-cli", Ref: "main", APIBaseURL: srv.URL + "/"}
bdb, err := NewBatchingGitHubDB(cfg, def, "test: read")
if err != nil {
t.Fatalf("NewBatchingGitHubDB: %v", err)
}
rec := record.NewRecordWithData(record.NewKeyWithID("tags", "active"), map[string]any{})
if getErr := bdb.Get(context.Background(), rec); getErr != nil {
t.Fatalf("Get: %v", getErr)
}
if !rec.Exists() {
t.Fatal("expected record present in head tree to exist")
}
data := rec.Data().(map[string]any)
if data["title"] != "Active" {
t.Errorf("title = %v, want Active", data["title"])
}
}
// TestDecodeBlobContent_Encodings covers base64 (with embedded newlines),
// utf-8, an unsupported encoding, and a nil blob.
func TestDecodeBlobContent_Encodings(t *testing.T) {
t.Parallel()
// base64 payload split across lines, as the GitHub blob API returns it.
b64 := base64.StdEncoding.EncodeToString([]byte("hello world"))
withNewlines := b64[:4] + "\n" + b64[4:]
enc64 := "base64"
got, err := decodeBlobContent(&github.Blob{Content: &withNewlines, Encoding: &enc64})
if err != nil {
t.Fatalf("base64 decode: %v", err)
}
if string(got) != "hello world" {
t.Errorf("base64 decode = %q, want %q", got, "hello world")
}
utf := "utf-8"
raw := "plain text"
got, err = decodeBlobContent(&github.Blob{Content: &raw, Encoding: &utf})
if err != nil {
t.Fatalf("utf-8 decode: %v", err)
}
if string(got) != "plain text" {
t.Errorf("utf-8 decode = %q, want %q", got, "plain text")
}
bad := "rot13"
if _, err := decodeBlobContent(&github.Blob{Content: &raw, Encoding: &bad}); err == nil {
t.Error("expected error for unsupported encoding")
}
if _, err := decodeBlobContent(nil); err == nil {
t.Error("expected error for nil blob")
}
}