Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use subtests. Need to stob building on go < 1.7. #63

Merged
merged 2 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: go
go:
- 1.6.x
- 1.7.x
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subtests were introduced in 1.7, so we can not use 1.6 in tests now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 1.6 should be supported until 1.8 is released (any day now).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.8 has dropped. We should be good to go on this now.

- 1.8.x
- tip
Expand Down
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)
}
})
}
}
32 changes: 17 additions & 15 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"
"strings"
Expand Down Expand Up @@ -186,21 +187,22 @@ func TestUnmarshalInvalidJSON_BadType(t *testing.T) {
{Field: "time_field", BadValue: "A string.", Error: ErrInvalidTime}, // Expected int64.
{Field: "time_ptr_field", BadValue: "A string.", Error: ErrInvalidTime}, // Expected *time / int64.
}
for idx, test := range badTypeTests {
println("index:", idx)
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())
}
for _, test := range badTypeTests {
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