-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommit_test.go
177 lines (154 loc) · 4.38 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
package dataset
import (
"bytes"
"encoding/json"
"testing"
"time"
"github.com/ipfs/go-datastore"
)
func TestCommit(t *testing.T) {
ref := NewCommitRef(datastore.NewKey("a"))
if !ref.IsEmpty() {
t.Errorf("expected reference to be empty")
}
if !ref.Path().Equal(datastore.NewKey("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: datastore.NewKey("a"),
Qri: KindCommit,
Author: doug,
Timestamp: t1,
Title: "expect title",
Message: "expect message",
Signature: "sig",
}
got := &Commit{
Author: &User{ID: "maha_id", Email: "maha@example.com"},
Title: "title",
Message: "message",
}
got.Assign(&Commit{
Author: doug,
Qri: KindCommit,
Title: "expect title",
}, &Commit{
path: datastore.NewKey("a"),
Timestamp: t1,
Message: "expect message",
Signature: "sig",
})
if err := CompareCommits(expect, got); err != nil {
t.Error(err)
}
got.Assign(nil, nil)
if err := CompareCommits(expect, got); err != nil {
t.Error(err)
}
emptyMsg := &Commit{}
emptyMsg.Assign(expect)
if err := CompareCommits(expect, emptyMsg); err != nil {
t.Error(err)
}
}
func TestCommitSignableBytes(t *testing.T) {
expect := []byte("2001-01-01T01:01:01Z\nI'm a commit message")
cm := &Commit{
Timestamp: time.Date(2001, 01, 01, 01, 01, 01, 0, time.UTC),
Title: "I'm a commit message",
}
got := cm.SignableBytes()
if !bytes.Equal(expect, got) {
t.Errorf("mismatch. expected:\n'%s',got:\n'%s'", string(expect), string(got))
}
}
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},
}
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: datastore.NewKey("/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 TestCommitUnmarshalJSON(t *testing.T) {
cases := []struct {
data string
result *Commit
err error
}{
{`{}`, &Commit{}, nil},
{`{ "title": "title", "message": "message"}`, &Commit{Title: "title", Message: "message"}, nil},
{`{ "author" : { "id": "id", "email": "email@email.com"} }`, &Commit{Author: &User{ID: "id", Email: "email@email.com"}}, nil},
}
for i, c := range cases {
cm := &Commit{}
if err := json.Unmarshal([]byte(c.data), cm); err != c.err {
t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
continue
}
if err := CompareCommits(cm, c.result); err != nil {
t.Errorf("case %d comparison error: %s", i, err)
continue
}
}
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.String() != path {
t.Errorf("unmarshal didn't set proper path: %s != %s", path, strq.path)
return
}
}
func TestUnmarshalCommit(t *testing.T) {
cma := Commit{Qri: KindCommit, Message: "foo"}
cases := []struct {
value interface{}
out *Commit
err string
}{
{cma, &cma, ""},
{&cma, &cma, ""},
{[]byte("{\"qri\":\"cm:0\"}"), &Commit{Qri: KindCommit}, ""},
{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 err := CompareCommits(c.out, got); err != nil {
t.Errorf("case %d dataset mismatch: %s", i, err.Error())
continue
}
}
}