dry off, error if tag does not contain two arguments

This commit is contained in:
Sam Woodard 2015-07-08 11:49:36 -07:00
parent 8de6c952ef
commit 7430fd0b0c
4 changed files with 62 additions and 12 deletions

View File

@ -42,6 +42,11 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
args := strings.Split(tag, ",") args := strings.Split(tag, ",")
if len(args) != 2 {
er = errors.New(fmt.Sprintf("jsonapi tag, on %s, had two few arguments", fieldType.Name))
return false
}
if len(args) >= 1 && args[0] != "" { if len(args) >= 1 && args[0] != "" {
annotation := args[0] annotation := args[0]

View File

@ -4,9 +4,28 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io" "io"
"regexp"
"testing" "testing"
) )
type BadModel struct {
Id int `jsonapi:"primary"`
}
func TestMalformedTag(t *testing.T) {
out := new(BadModel)
err := UnmarshalJsonApiPayload(samplePayload(), out)
if err == nil {
t.Fatalf("Did not error out with wrong number of arguments in tag")
}
r := regexp.MustCompile(`two few arguments`)
if !r.Match([]byte(err.Error())) {
t.Fatalf("The err was not due two two few arguments in a tag")
}
}
//func TestUnmarshalSetsId(t *testing.T) { //func TestUnmarshalSetsId(t *testing.T) {
//in := samplePayload() //in := samplePayload()
//out := new(Blog) //out := new(Blog)
@ -21,10 +40,8 @@ import (
//} //}
func TestUnmarshalSetsAttrs(t *testing.T) { func TestUnmarshalSetsAttrs(t *testing.T) {
in := samplePayload() out, err := unmarshalSamplePayload()
out := new(Blog) if err != nil {
if err := UnmarshalJsonApiPayload(in, out); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -43,10 +60,8 @@ func TestUnmarshalSetsAttrs(t *testing.T) {
} }
func TestUnmarshalRelationships(t *testing.T) { func TestUnmarshalRelationships(t *testing.T) {
in := samplePayload() out, err := unmarshalSamplePayload()
out := new(Blog) if err != nil {
if err := UnmarshalJsonApiPayload(in, out); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -64,10 +79,8 @@ func TestUnmarshalRelationships(t *testing.T) {
} }
func TestUnmarshalNestedRelationships(t *testing.T) { func TestUnmarshalNestedRelationships(t *testing.T) {
in := samplePayload() out, err := unmarshalSamplePayload()
out := new(Blog) if err != nil {
if err := UnmarshalJsonApiPayload(in, out); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -84,6 +97,17 @@ func TestUnmarshalNestedRelationships(t *testing.T) {
} }
} }
func unmarshalSamplePayload() (*Blog, error) {
in := samplePayload()
out := new(Blog)
if err := UnmarshalJsonApiPayload(in, out); err != nil {
return nil, err
}
return out, nil
}
func samplePayload() io.Reader { func samplePayload() io.Reader {
payload := &JsonApiOnePayload{ payload := &JsonApiOnePayload{
Data: &JsonApiNode{ Data: &JsonApiNode{

View File

@ -84,6 +84,11 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) {
args := strings.Split(tag, ",") args := strings.Split(tag, ",")
if len(args) != 2 {
er = errors.New(fmt.Sprintf("jsonapi tag, on %s, had two few arguments", structField.Name))
return false
}
if len(args) >= 1 && args[0] != "" { if len(args) >= 1 && args[0] != "" {
annotation := args[0] annotation := args[0]

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"regexp"
"testing" "testing"
"time" "time"
) )
@ -40,6 +41,21 @@ func (b Blogs) GetData() []interface{} {
return d return d
} }
func TestMalformedTagResposne(t *testing.T) {
testModel := &BadModel{}
_, err := MarshalJsonApiOnePayload(testModel)
if err == nil {
t.Fatalf("Did not error out with wrong number of arguments in tag")
}
r := regexp.MustCompile(`two few arguments`)
if !r.Match([]byte(err.Error())) {
t.Fatalf("The err was not due two two few arguments in a tag")
}
}
func TestHasPrimaryAnnotation(t *testing.T) { func TestHasPrimaryAnnotation(t *testing.T) {
testModel := &Blog{ testModel := &Blog{
Id: 5, Id: 5,