Skip to content

Commit

Permalink
Use subtests (can only test against go >= 1.7 now). (google#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
thedodd authored and aren55555 committed Feb 17, 2017
1 parent 2b01775 commit bce7629
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
31 changes: 18 additions & 13 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,36 @@ func TestErrorObjectWritesExpectedErrorMessage(t *testing.T) {

func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
var marshalErrorsTableTasts = []struct {
In []*ErrorObject
Out map[string]interface{}
Title string
In []*ErrorObject
Out map[string]interface{}
}{
{ // This tests that given fields are turned into the appropriate JSON representation.
In: []*ErrorObject{{ID: "0", Title: "Test title.", Detail: "Test detail", Status: "400", Code: "E1100"}},
{
Title: "TestFieldsAreSerializedAsNeeded",
In: []*ErrorObject{{ID: "0", Title: "Test title.", Detail: "Test detail", Status: "400", Code: "E1100"}},
Out: map[string]interface{}{"errors": []interface{}{
map[string]interface{}{"id": "0", "title": "Test title.", "detail": "Test detail", "status": "400", "code": "E1100"},
}},
},
{ // This tests that the `Meta` field is serialized properly.
In: []*ErrorObject{{Title: "Test title.", Detail: "Test detail", Meta: &map[string]interface{}{"key": "val"}}},
{
Title: "TestMetaFieldIsSerializedProperly",
In: []*ErrorObject{{Title: "Test title.", Detail: "Test detail", Meta: &map[string]interface{}{"key": "val"}}},
Out: map[string]interface{}{"errors": []interface{}{
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "meta": map[string]interface{}{"key": "val"}},
}},
},
}
for _, testRow := range marshalErrorsTableTasts {
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
var writer io.Writer = buffer
t.Run(testRow.Title, func(t *testing.T) {
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
var writer io.Writer = buffer

_ = MarshalErrors(writer, testRow.In)
json.Unmarshal(buffer.Bytes(), &output)
_ = MarshalErrors(writer, testRow.In)
json.Unmarshal(buffer.Bytes(), &output)

if !reflect.DeepEqual(output, testRow.Out) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
}
if !reflect.DeepEqual(output, testRow.Out) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
}
})
}
}
29 changes: 16 additions & 13 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jsonapi
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"sort"
Expand Down Expand Up @@ -201,19 +202,21 @@ func TestUnmarshalInvalidJSON_BadType(t *testing.T) {
{Field: "time_ptr_field", BadValue: "A string.", Error: ErrInvalidTime}, // Expected *time / int64.
}
for _, test := range badTypeTests {
out := new(ModelBadTypes)
in := map[string]interface{}{}
in[test.Field] = test.BadValue
expectedErrorMessage := test.Error.Error()

err := UnmarshalPayload(samplePayloadWithBadTypes(in), out)

if err == nil {
t.Fatalf("Expected error due to invalid type.")
}
if err.Error() != expectedErrorMessage {
t.Fatalf("Unexpected error message: %s", err.Error())
}
t.Run(fmt.Sprintf("Test_%s", test.Field), func(t *testing.T) {
out := new(ModelBadTypes)
in := map[string]interface{}{}
in[test.Field] = test.BadValue
expectedErrorMessage := test.Error.Error()

err := UnmarshalPayload(samplePayloadWithBadTypes(in), out)

if err == nil {
t.Fatalf("Expected error due to invalid type.")
}
if err.Error() != expectedErrorMessage {
t.Fatalf("Unexpected error message: %s", err.Error())
}
})
}
}

Expand Down

0 comments on commit bce7629

Please sign in to comment.