-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_message_test.go
More file actions
120 lines (106 loc) · 4.08 KB
/
Copy pathcommit_message_test.go
File metadata and controls
120 lines (106 loc) · 4.08 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
package dalgo2ghingitdb
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/dal-go/dalgo/dal"
"github.com/ingitdb/ingitdb-go/ingitdb"
)
// newCommitMessageCapturingServer returns an httptest server implementing the
// minimal Git Data API surface used by a batching commit, plus a pointer that
// receives the commit message sent to POST /git/commits.
func newCommitMessageCapturingServer(t *testing.T, captured *string) *httptest.Server {
t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
path := r.URL.Path
switch {
case r.Method == "GET" && strings.Contains(path, "/git/ref/heads/main"):
_ = json.NewEncoder(w).Encode(map[string]any{
"ref": "refs/heads/main",
"object": map[string]any{"sha": "head-commit-sha", "type": "commit"},
})
case r.Method == "GET" && strings.Contains(path, "/git/commits/head-commit-sha"):
_ = json.NewEncoder(w).Encode(map[string]any{
"sha": "head-commit-sha",
"tree": map[string]any{"sha": "base-tree-sha"},
})
case r.Method == "POST" && strings.HasSuffix(path, "/git/trees"):
_ = json.NewEncoder(w).Encode(map[string]any{"sha": "new-tree-sha"})
case r.Method == "POST" && strings.HasSuffix(path, "/git/commits"):
var body struct {
Message string `json:"message"`
}
_ = json.NewDecoder(r.Body).Decode(&body)
*captured = body.Message
_ = json.NewEncoder(w).Encode(map[string]any{"sha": "new-commit-sha"})
case r.Method == "PATCH" && strings.Contains(path, "/git/refs/heads/main"):
_ = json.NewEncoder(w).Encode(map[string]any{
"ref": "refs/heads/main",
"object": map[string]any{"sha": "new-commit-sha"},
})
default:
http.Error(w, "unexpected request: "+r.Method+" "+path, http.StatusNotImplemented)
}
}))
}
func commitMessageTestDef() *ingitdb.Definition {
return &ingitdb.Definition{
Collections: map[string]*ingitdb.CollectionDef{
"cities": {
ID: "cities",
DirPath: "data/cities",
RecordFile: &ingitdb.RecordFileDef{
RecordType: ingitdb.SingleRecord,
Format: ingitdb.RecordFormatYAML,
Name: "{key}.yaml",
},
},
},
}
}
func runOneSetTx(t *testing.T, bdb *BatchingGitHubDB, options ...dal.TransactionOption) {
t.Helper()
err := bdb.RunReadwriteTransaction(context.Background(), func(ctx context.Context, tx dal.ReadwriteTransaction) error {
return tx.Set(ctx, dal.NewRecordWithData(dal.NewKeyWithID("cities", "ie"), map[string]any{"region": "eu"}))
}, options...)
if err != nil {
t.Fatalf("RunReadwriteTransaction: %v", err)
}
}
// TestBatchingGitHubDB_TxMessageOverridesDefault verifies that a transaction
// message (dal.TxWithMessage) is used as the commit message in place of the
// construction-time default.
func TestBatchingGitHubDB_TxMessageOverridesDefault(t *testing.T) {
var captured string
srv := newCommitMessageCapturingServer(t, &captured)
defer srv.Close()
cfg := Config{Owner: "owner", Repo: "repo", Ref: "main", APIBaseURL: srv.URL + "/"}
bdb, err := NewBatchingGitHubDB(cfg, commitMessageTestDef(), "default construction message")
if err != nil {
t.Fatalf("NewBatchingGitHubDB: %v", err)
}
runOneSetTx(t, bdb, dal.TxWithMessage("tx-level commit message"))
if captured != "tx-level commit message" {
t.Errorf("commit message: got %q, want %q", captured, "tx-level commit message")
}
}
// TestBatchingGitHubDB_NoTxMessageUsesDefault verifies the construction-time
// default is used when the transaction sets no message.
func TestBatchingGitHubDB_NoTxMessageUsesDefault(t *testing.T) {
var captured string
srv := newCommitMessageCapturingServer(t, &captured)
defer srv.Close()
cfg := Config{Owner: "owner", Repo: "repo", Ref: "main", APIBaseURL: srv.URL + "/"}
bdb, err := NewBatchingGitHubDB(cfg, commitMessageTestDef(), "default construction message")
if err != nil {
t.Fatalf("NewBatchingGitHubDB: %v", err)
}
runOneSetTx(t, bdb)
if captured != "default construction message" {
t.Errorf("commit message: got %q, want %q", captured, "default construction message")
}
}