Skip to content

Commit 4a09056

Browse files
author
Thomas Osterbind
committed
test: added tests for func (cm *Commit) MarshalJSONObject() and func (q Transform) MarshalJSONObject()
1 parent b96527c commit 4a09056

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

commit_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,34 @@ func TestCommitMarshalJSON(t *testing.T) {
132132
}
133133
}
134134

135+
func TestCommitMarshalJSONObject(t *testing.T) {
136+
ts := time.Date(2001, 01, 01, 01, 01, 01, 0, time.UTC)
137+
cases := []struct {
138+
in *Commit
139+
out []byte
140+
err error
141+
}{
142+
{&Commit{Title: "title", Timestamp: ts}, []byte(`{"qri":"cm:0","timestamp":"2001-01-01T01:01:01Z","title":"title"}`), nil},
143+
{&Commit{Author: &User{ID: "foo"}, Timestamp: ts}, []byte(`{"author":{"id":"foo"},"qri":"cm:0","timestamp":"2001-01-01T01:01:01Z","title":""}`), nil},
144+
}
145+
146+
for i, c := range cases {
147+
got, err := c.in.MarshalJSON()
148+
if err != c.err {
149+
t.Errorf("case %d error mismatch. expected: '%s', got: '%s'", i, c.err, err)
150+
continue
151+
}
152+
check := &map[string]interface{}{}
153+
err = json.Unmarshal(got, check)
154+
if err != nil {
155+
t.Errorf("case %d error: failed to unmarshal to object: %s", err.Error())
156+
continue
157+
}
158+
159+
}
160+
161+
}
162+
135163
func TestCommitUnmarshalJSON(t *testing.T) {
136164
cases := []struct {
137165
data string

transform_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,32 @@ func TestTransformMarshalJSON(t *testing.T) {
137137
}
138138
}
139139

140+
func TestTransformMarshalJSON(t *testing.T) {
141+
cases := []struct {
142+
q *Transform
143+
out string
144+
err error
145+
}{
146+
{&Transform{}, `{"qri":"tf:0"}`, nil},
147+
{&Transform{Syntax: "sql", Data: "select a from b"}, `{"data":"select a from b","qri":"tf:0","syntax":"sql"}`, nil},
148+
}
149+
150+
for i, c := range cases {
151+
data, err := json.Marshal(c.q)
152+
if err != c.err {
153+
t.Errorf("case %d error mismatch. expected: %s, got: %s", i, c.err, err)
154+
continue
155+
}
156+
check := &map[string]interface{}{}
157+
err = json.Unmarshal(data, check)
158+
if err != nil {
159+
t.Errorf("case %d error: failed to unmarshal to object: %s", err.Error())
160+
continue
161+
}
162+
}
163+
164+
}
165+
140166
func TestTransformIsEmpty(t *testing.T) {
141167
cases := []struct {
142168
tf *Transform

0 commit comments

Comments
 (0)