Skip to content

Commit 863f70e

Browse files
committed
Add test
1 parent f43aa0d commit 863f70e

File tree

2 files changed

+163
-8
lines changed

2 files changed

+163
-8
lines changed

models_test.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,36 @@ type GenericInterface struct {
8989
Data interface{} `jsonapi:"attr,interface"`
9090
}
9191

92+
type Organization struct {
93+
ID int `jsonapi:"primary,organizations"`
94+
ClientID string `jsonapi:"client-id"`
95+
Name string `jsonapi:"attr,title"`
96+
DefaultProject *Project `jsonapi:"relation,default_project"`
97+
CreatedAt time.Time `jsonapi:"attr,created_at"`
98+
99+
Links Links `jsonapi:"links,omitempty"`
100+
}
101+
102+
type Project struct {
103+
ID int `jsonapi:"primary,projects"`
104+
ClientID string `jsonapi:"client-id"`
105+
Name string `jsonapi:"attr,name"`
106+
Organization *Organization `jsonapi:"relation,organization"`
107+
108+
Links Links `jsonapi:"links,omitempty"`
109+
}
110+
92111
type Blog struct {
93-
ID int `jsonapi:"primary,blogs"`
94-
ClientID string `jsonapi:"client-id"`
95-
Title string `jsonapi:"attr,title"`
96-
Posts []*Post `jsonapi:"relation,posts"`
97-
CurrentPost *Post `jsonapi:"relation,current_post"`
98-
CurrentPostID int `jsonapi:"attr,current_post_id"`
99-
CreatedAt time.Time `jsonapi:"attr,created_at"`
100-
ViewCount int `jsonapi:"attr,view_count"`
112+
ID int `jsonapi:"primary,blogs"`
113+
ClientID string `jsonapi:"client-id"`
114+
Title string `jsonapi:"attr,title"`
115+
CurrentPostID int `jsonapi:"attr,current_post_id"`
116+
CreatedAt time.Time `jsonapi:"attr,created_at"`
117+
ViewCount int `jsonapi:"attr,view_count"`
118+
Posts []*Post `jsonapi:"relation,posts"`
119+
CurrentPost *Post `jsonapi:"relation,current_post"`
120+
Organization *Organization `jsonapi:"relation,organization"`
121+
Project *Project `jsonapi:"relation,project"`
101122

102123
Links Links `jsonapi:"links,omitempty"`
103124
}

request_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,41 @@ func TestUnmarshalRelationships(t *testing.T) {
689689
}
690690
}
691691

692+
func TestUnmarshalMany_relationships_with_circular_inclusion(t *testing.T) {
693+
data := samplePayloadWithCircularInclusion()
694+
payload, err := json.Marshal(data)
695+
if err != nil {
696+
t.Fatal(err)
697+
}
698+
in := bytes.NewReader(payload)
699+
model := reflect.TypeOf(new(Blog))
700+
701+
out, err := UnmarshalManyPayload(in, model)
702+
if err != nil {
703+
t.Fatal(err)
704+
}
705+
706+
result_1 := out[0].(*Blog)
707+
708+
if result_1.Project != result_1.Organization.DefaultProject {
709+
t.Errorf("expected blog.project (%p) to hold the same pointer as blog.organization.default-project (%p) ", result_1.Project, result_1.Organization.DefaultProject)
710+
}
711+
712+
if result_1.Organization != result_1.Project.Organization {
713+
t.Errorf("expected blog.organization (%p) to hold the same pointer as blog.project.organization (%p)", result_1.Organization, result_1.Project.Organization)
714+
}
715+
716+
result_2 := out[1].(*Blog)
717+
718+
if result_2.Project != result_2.Organization.DefaultProject {
719+
t.Errorf("expected blog.project (%p) to hold the same pointer as blog.organization.default-project (%p) ", result_2.Project, result_2.Organization.DefaultProject)
720+
}
721+
722+
if result_2.Organization != result_2.Project.Organization {
723+
t.Errorf("expected blog.organization (%p) to hold the same pointer as blog.project.organization (%p)", result_2.Organization, result_2.Project.Organization)
724+
}
725+
}
726+
692727
func Test_UnmarshalPayload_polymorphicRelations(t *testing.T) {
693728
in := bytes.NewReader([]byte(`{
694729
"data": {
@@ -1378,6 +1413,105 @@ func TestUnmarshalCustomTypeAttributes_ErrInvalidType(t *testing.T) {
13781413
}
13791414
}
13801415

1416+
func samplePayloadWithCircularInclusion() *ManyPayload {
1417+
payload := &ManyPayload{
1418+
Data: []*Node{
1419+
{
1420+
Type: "blogs",
1421+
ClientID: "1",
1422+
ID: "1",
1423+
Attributes: map[string]interface{}{
1424+
"title": "Foo",
1425+
"current_post_id": 1,
1426+
"created_at": 1436216820,
1427+
"view_count": 1000,
1428+
},
1429+
Relationships: map[string]interface{}{
1430+
"project": &RelationshipOneNode{
1431+
Data: &Node{
1432+
Type: "projects",
1433+
ClientID: "1",
1434+
ID: "1",
1435+
},
1436+
},
1437+
"organization": &RelationshipOneNode{
1438+
Data: &Node{
1439+
Type: "organizations",
1440+
ClientID: "1",
1441+
ID: "1",
1442+
},
1443+
},
1444+
},
1445+
},
1446+
{
1447+
Type: "blogs",
1448+
ClientID: "2",
1449+
ID: "2",
1450+
Attributes: map[string]interface{}{
1451+
"title": "Foo2",
1452+
"current_post_id": 1,
1453+
"created_at": 1436216820,
1454+
"view_count": 1000,
1455+
},
1456+
Relationships: map[string]interface{}{
1457+
"project": &RelationshipOneNode{
1458+
Data: &Node{
1459+
Type: "projects",
1460+
ClientID: "1",
1461+
ID: "1",
1462+
},
1463+
},
1464+
"organization": &RelationshipOneNode{
1465+
Data: &Node{
1466+
Type: "organizations",
1467+
ClientID: "1",
1468+
ID: "1",
1469+
},
1470+
},
1471+
},
1472+
},
1473+
},
1474+
Included: []*Node{
1475+
{
1476+
Type: "projects",
1477+
ClientID: "1",
1478+
ID: "1",
1479+
Attributes: map[string]interface{}{
1480+
"name": "Bar",
1481+
},
1482+
Relationships: map[string]interface{}{
1483+
"organization": &RelationshipOneNode{
1484+
Data: &Node{
1485+
Type: "organizations",
1486+
ClientID: "1",
1487+
ID: "1",
1488+
},
1489+
},
1490+
},
1491+
},
1492+
{
1493+
Type: "organizations",
1494+
ClientID: "1",
1495+
ID: "1",
1496+
Attributes: map[string]interface{}{
1497+
"name": "Baz",
1498+
},
1499+
Relationships: map[string]interface{}{
1500+
"default_project": &RelationshipOneNode{
1501+
Data: &Node{
1502+
Type: "projects",
1503+
ClientID: "1",
1504+
ID: "1",
1505+
},
1506+
},
1507+
},
1508+
},
1509+
},
1510+
}
1511+
1512+
return payload
1513+
}
1514+
13811515
func samplePayloadWithoutIncluded() map[string]interface{} {
13821516
return map[string]interface{}{
13831517
"data": map[string]interface{}{

0 commit comments

Comments
 (0)