forked from Mirrors/jsonapi
Use subtests (can only test against go >= 1.7 now). (#63)
This commit is contained in:
parent
2b01775d0f
commit
bce7629682
|
@ -22,31 +22,36 @@ func TestErrorObjectWritesExpectedErrorMessage(t *testing.T) {
|
||||||
|
|
||||||
func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
|
func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
|
||||||
var marshalErrorsTableTasts = []struct {
|
var marshalErrorsTableTasts = []struct {
|
||||||
In []*ErrorObject
|
Title string
|
||||||
Out map[string]interface{}
|
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{}{
|
Out: map[string]interface{}{"errors": []interface{}{
|
||||||
map[string]interface{}{"id": "0", "title": "Test title.", "detail": "Test detail", "status": "400", "code": "E1100"},
|
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{}{
|
Out: map[string]interface{}{"errors": []interface{}{
|
||||||
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "meta": map[string]interface{}{"key": "val"}},
|
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "meta": map[string]interface{}{"key": "val"}},
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, testRow := range marshalErrorsTableTasts {
|
for _, testRow := range marshalErrorsTableTasts {
|
||||||
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
|
t.Run(testRow.Title, func(t *testing.T) {
|
||||||
var writer io.Writer = buffer
|
buffer, output := bytes.NewBuffer(nil), map[string]interface{}{}
|
||||||
|
var writer io.Writer = buffer
|
||||||
|
|
||||||
_ = MarshalErrors(writer, testRow.In)
|
_ = MarshalErrors(writer, testRow.In)
|
||||||
json.Unmarshal(buffer.Bytes(), &output)
|
json.Unmarshal(buffer.Bytes(), &output)
|
||||||
|
|
||||||
if !reflect.DeepEqual(output, testRow.Out) {
|
if !reflect.DeepEqual(output, testRow.Out) {
|
||||||
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
|
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package jsonapi
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -201,19 +202,21 @@ func TestUnmarshalInvalidJSON_BadType(t *testing.T) {
|
||||||
{Field: "time_ptr_field", BadValue: "A string.", Error: ErrInvalidTime}, // Expected *time / int64.
|
{Field: "time_ptr_field", BadValue: "A string.", Error: ErrInvalidTime}, // Expected *time / int64.
|
||||||
}
|
}
|
||||||
for _, test := range badTypeTests {
|
for _, test := range badTypeTests {
|
||||||
out := new(ModelBadTypes)
|
t.Run(fmt.Sprintf("Test_%s", test.Field), func(t *testing.T) {
|
||||||
in := map[string]interface{}{}
|
out := new(ModelBadTypes)
|
||||||
in[test.Field] = test.BadValue
|
in := map[string]interface{}{}
|
||||||
expectedErrorMessage := test.Error.Error()
|
in[test.Field] = test.BadValue
|
||||||
|
expectedErrorMessage := test.Error.Error()
|
||||||
|
|
||||||
err := UnmarshalPayload(samplePayloadWithBadTypes(in), out)
|
err := UnmarshalPayload(samplePayloadWithBadTypes(in), out)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("Expected error due to invalid type.")
|
t.Fatalf("Expected error due to invalid type.")
|
||||||
}
|
}
|
||||||
if err.Error() != expectedErrorMessage {
|
if err.Error() != expectedErrorMessage {
|
||||||
t.Fatalf("Unexpected error message: %s", err.Error())
|
t.Fatalf("Unexpected error message: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue