-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathto_resource_test.go
More file actions
152 lines (114 loc) · 3.53 KB
/
Copy pathto_resource_test.go
File metadata and controls
152 lines (114 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package jsonapi_test
import (
"testing"
"github.com/smotes/jsonapi"
)
func TestToResource_WhenNoAdapter(t *testing.T) {
s := struct{}{}
r, err := jsonapi.ToResource(s, false)
if r != nil || err == nil {
t.Error("ToResource() should return nil/error when adapter does not satisfy identity read interface")
}
}
// id
type badIDReadAdapter struct{}
func (a *badIDReadAdapter) GetID() (string, error) {
return "", testErr
}
func (a *badIDReadAdapter) GetType() (string, error) {
return "tests", nil
}
func TestToResource_WhenGetIDError(t *testing.T) {
adapter := badIDReadAdapter{}
r, err := jsonapi.ToResource(&adapter, false)
if r != nil || err != testErr {
t.Error("ToResource should return nil/error from adapter.GetID when it returns an error")
}
}
// type
type badTypeReadAdapter struct{}
func (a *badTypeReadAdapter) GetID() (string, error) {
return "1", nil
}
func (a *badTypeReadAdapter) GetType() (string, error) {
return "", testErr
}
func TestToResource_WhenGetTypeError(t *testing.T) {
adapter := badTypeReadAdapter{}
r, err := jsonapi.ToResource(&adapter, false)
if r != nil || err != testErr {
t.Error("ToResource should return nil/error from adapter.GetType when it returns an error")
}
}
// attributes
type badAttributesReadAdapter struct{}
func (a *badAttributesReadAdapter) GetID() (string, error) {
return "1", nil
}
func (a *badAttributesReadAdapter) GetType() (string, error) {
return "tests", nil
}
func (a *badAttributesReadAdapter) GetAttributes() (map[string]interface{}, error) {
return nil, testErr
}
func TestToResource_WhenGetAttributesError(t *testing.T) {
adapter := badAttributesReadAdapter{}
r, err := jsonapi.ToResource(&adapter, true)
if r != nil || err != testErr {
t.Error("ToResource should return nil/error from adapter.GetAttributes when it returns an error")
}
}
// relationships
type badRelationshipsReadAdapter struct{}
func (a *badRelationshipsReadAdapter) GetID() (string, error) {
return "1", nil
}
func (a *badRelationshipsReadAdapter) GetType() (string, error) {
return "tests", nil
}
func (a *badRelationshipsReadAdapter) GetRelationships() (jsonapi.Relationships, error) {
return nil, testErr
}
func TestToResource_WhenGetRelationshipsError(t *testing.T) {
adapter := badRelationshipsReadAdapter{}
r, err := jsonapi.ToResource(&adapter, true)
if r != nil || err != testErr {
t.Error("ToResource should return nil/error from adapter.GetRelationships when it returns an error")
}
}
// links
type badLinksReadAdapter struct{}
func (a *badLinksReadAdapter) GetID() (string, error) {
return "1", nil
}
func (a *badLinksReadAdapter) GetType() (string, error) {
return "tests", nil
}
func (a *badLinksReadAdapter) GetLinks() (jsonapi.Links, error) {
return nil, testErr
}
func TestToResource_WhenGetLinksError(t *testing.T) {
adapter := badLinksReadAdapter{}
r, err := jsonapi.ToResource(&adapter, true)
if r != nil || err != testErr {
t.Error("ToResource should return nil/error from adapter.GetLinks when it returns an error")
}
}
// meta
type badMetaReadAdapter struct{}
func (a *badMetaReadAdapter) GetID() (string, error) {
return "1", nil
}
func (a *badMetaReadAdapter) GetType() (string, error) {
return "tests", nil
}
func (a *badMetaReadAdapter) GetMeta() (map[string]interface{}, error) {
return nil, testErr
}
func TestToResource_WhenGetMetaError(t *testing.T) {
adapter := badMetaReadAdapter{}
r, err := jsonapi.ToResource(&adapter, true)
if r != nil || err != testErr {
t.Error("ToResource should return nil/error from adapter.GetMeta when it returns an error")
}
}