diff --git a/errors_test.go b/errors_test.go index e251d09..683a1d1 100644 --- a/errors_test.go +++ b/errors_test.go @@ -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) + } + }) } } diff --git a/request_test.go b/request_test.go index a2e40e8..1066733 100644 --- a/request_test.go +++ b/request_test.go @@ -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()) + } + }) } }