-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommit_test.go
258 lines (223 loc) · 6.65 KB
/
commit_test.go
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
253
254
255
256
257
258
package dataset
import (
"bytes"
"encoding/json"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func compareCommits(a, b *Commit) string {
return cmp.Diff(a, b, cmpopts.IgnoreUnexported(Commit{}))
}
func TestCommit(t *testing.T) {
ref := NewCommitRef("a")
if !ref.IsEmpty() {
t.Errorf("expected reference to be empty")
}
if ref.Path != "a" {
t.Errorf("expected ref path to equal /a")
}
}
func TestCommitAssign(t *testing.T) {
t1 := time.Now()
doug := &User{ID: "doug_id", Email: "doug@example.com"}
expect := &Commit{
Path: "a",
Qri: KindCommit.String(),
Author: doug,
Timestamp: t1,
Title: "expect title",
Message: "expect message",
Signature: "sig",
RunID: "runid",
}
got := &Commit{
Author: &User{ID: "maha_id", Email: "maha@example.com"},
Title: "title",
Message: "message",
}
got.Assign(&Commit{
Author: doug,
Qri: KindCommit.String(),
Title: "expect title",
}, &Commit{
Path: "a",
Timestamp: t1,
Message: "expect message",
Signature: "sig",
RunID: "runid",
})
if diff := compareCommits(expect, got); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
got.Assign(nil, nil)
if diff := compareCommits(expect, got); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
emptyMsg := &Commit{}
emptyMsg.Assign(expect)
if diff := compareCommits(expect, emptyMsg); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
}
func TestCommitDropTransientValues(t *testing.T) {
cm := &Commit{
Path: "/ipfs/QmHash",
}
cm.DropTransientValues()
if !cmp.Equal(cm, &Commit{}) {
t.Errorf("expected dropping a commit of only transient values to be empty")
}
}
func TestCommitDropDerivedValues(t *testing.T) {
cm := &Commit{
Path: "/ipfs/QmHash",
Qri: "oh you know it's qri",
}
cm.DropDerivedValues()
if !cmp.Equal(cm, &Commit{}) {
t.Errorf("expected dropping commit of only derived values to be empty")
}
}
func TestCommitIsEmpty(t *testing.T) {
cases := []struct {
cm *Commit
}{
{&Commit{Title: "a"}},
{&Commit{Author: &User{}}},
{&Commit{Message: "a"}},
{&Commit{Signature: "a"}},
{&Commit{Timestamp: time.Now()}},
{&Commit{RunID: "runid"}},
}
for i, c := range cases {
if c.cm.IsEmpty() == true {
t.Errorf("case %d improperly reported commit as empty", i)
continue
}
}
}
func TestCommitMarshalJSON(t *testing.T) {
ts := time.Date(2001, 01, 01, 01, 01, 01, 0, time.UTC)
cases := []struct {
in *Commit
out []byte
err error
}{
{&Commit{Title: "title", Timestamp: ts}, []byte(`{"qri":"cm:0","timestamp":"2001-01-01T01:01:01Z","title":"title"}`), nil},
{&Commit{Author: &User{ID: "foo"}, Timestamp: ts}, []byte(`{"author":{"id":"foo"},"qri":"cm:0","timestamp":"2001-01-01T01:01:01Z","title":""}`), nil},
{&Commit{Author: &User{ID: "foo"}, Timestamp: ts, RunID: "runID"}, []byte(`{"author":{"id":"foo"},"qri":"cm:0","timestamp":"2001-01-01T01:01:01Z","title":"","runID":"runID"}`), nil},
}
for i, c := range cases {
got, err := c.in.MarshalJSON()
if err != c.err {
t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
continue
}
if !bytes.Equal(c.out, got) {
t.Errorf("case %d error mismatch. %s != %s", i, string(c.out), string(got))
continue
}
}
strbytes, err := json.Marshal(&Commit{Path: "/path/to/dataset"})
if err != nil {
t.Errorf("unexpected string marshal error: %s", err.Error())
return
}
if !bytes.Equal(strbytes, []byte("\"/path/to/dataset\"")) {
t.Errorf("marshal strbyte interface byte mismatch: %s != %s", string(strbytes), "\"/path/to/dataset\"")
}
}
func TestCommitMarshalJSONObject(t *testing.T) {
ts := time.Date(2001, 01, 01, 01, 01, 01, 0, time.UTC)
cases := []struct {
in *Commit
out []byte
err error
}{
{&Commit{Title: "title", Timestamp: ts}, []byte(`{"qri":"cm:0","timestamp":"2001-01-01T01:01:01Z","title":"title"}`), nil},
{&Commit{Author: &User{ID: "foo"}, Timestamp: ts}, []byte(`{"author":{"id":"foo"},"qri":"cm:0","timestamp":"2001-01-01T01:01:01Z","title":""}`), nil},
{&Commit{Author: &User{ID: "foo"}, Timestamp: ts, RunID: "runID"}, []byte(`{"author":{"id":"foo"},"qri":"cm:0","timestamp":"2001-01-01T01:01:01Z","title":"","runID":"runID"}`), nil},
}
for i, c := range cases {
got, err := c.in.MarshalJSON()
if err != c.err {
t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
continue
}
check := &map[string]interface{}{}
err = json.Unmarshal(got, check)
if err != nil {
t.Errorf("case %d error: failed to unmarshal to object: %s", i, err.Error())
continue
}
}
}
func TestCommitUnmarshalJSON(t *testing.T) {
cases := []struct {
data string
result *Commit
}{
{`{}`, &Commit{}},
{`{"runID":"foo"}`, &Commit{RunID: "foo"}},
{`{ "title": "title", "message": "message"}`, &Commit{Title: "title", Message: "message"}},
{`{ "author" : { "id": "id", "email": "email@email.com"} }`, &Commit{Author: &User{ID: "id", Email: "email@email.com"}}},
}
for i, c := range cases {
cm := &Commit{}
if err := cm.UnmarshalJSON([]byte(c.data)); err != nil {
t.Errorf("case %d unexpected error %q", i, err)
continue
}
if diff := compareCommits(cm, c.result); diff != "" {
t.Errorf("result %d mismatch (-want +got):\n%s", i, diff)
continue
}
}
cm := &Commit{}
err := cm.UnmarshalJSON([]byte(`{`))
if err == nil {
t.Errorf("expected error unmarshaling bad JSON, got nil")
} else {
expect := "error unmarshling commit: unexpected end of JSON input"
if expect != err.Error() {
t.Errorf("output mismatch.\nwant: %q\ngot: %q", expect, err)
}
}
strq := &Commit{}
path := "/path/to/msg"
if err := json.Unmarshal([]byte(`"`+path+`"`), strq); err != nil {
t.Errorf("unmarshal string path error: %s", err.Error())
return
}
if strq.Path != path {
t.Errorf("unmarshal didn't set proper Path: %s != %s", path, strq.Path)
return
}
}
func TestUnmarshalCommit(t *testing.T) {
cma := Commit{Qri: KindCommit.String(), Message: "foo"}
cases := []struct {
value interface{}
out *Commit
err string
}{
{cma, &cma, ""},
{&cma, &cma, ""},
{[]byte("{\"runID\":\"foo\"}"), &Commit{RunID: "foo"}, ""},
{[]byte("{\"qri\":\"cm:0\"}"), &Commit{Qri: KindCommit.String()}, ""},
{5, nil, "couldn't parse commitMsg, value is invalid type"},
}
for i, c := range cases {
got, err := UnmarshalCommit(c.value)
if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
continue
}
if diff := compareCommits(c.out, got); diff != "" {
t.Errorf("result %d mismatch (-want +got):\n%s", i, diff)
continue
}
}
}