Use subtests (can only test against go >= 1.7 now). (#63)

This commit is contained in:
Anthony Dodd 2017-02-17 17:11:39 -06:00 committed by Aren Patel
parent 2b01775d0f
commit bce7629682
2 changed files with 32 additions and 24 deletions

View File

@ -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)
}
})
}
}

View File

@ -3,6 +3,7 @@ package jsonapi
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"sort"
@ -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()
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)
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())
}
if err == nil {
t.Fatalf("Expected error due to invalid type.")
}
if err.Error() != expectedErrorMessage {
t.Fatalf("Unexpected error message: %s", err.Error())
}
})
}
}