-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathentity_spec.rb
More file actions
204 lines (164 loc) · 5.86 KB
/
entity_spec.rb
File metadata and controls
204 lines (164 loc) · 5.86 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
describe ApiStruct::Entity do
extend Support::Stub
stub_api('https://jsonplaceholder.typicode.com')
class StubClient < ApiStruct::Client
stub_api 'posts'
def show(id)
get(id)
end
def index(params = {})
get(json: params)
end
end
class AnotherStubClient < ApiStruct::Client
stub_api 'posts'
def pull(id)
get(id)
end
end
class StubNestedEntity < ApiStruct::Entity
attr_entity :name
end
class StubEntity < ApiStruct::Entity
client_service StubClient
client_service StubClient, prefix: true
client_service StubClient, prefix: :custom
client_service StubClient, prefix: :only, only: :index
client_service StubClient, prefix: :except, except: :show
client_service AnotherStubClient
attr_entity :id, :title, :camel_case
has_entity :nested_entity, as: StubNestedEntity
has_entities :another_nested_entities, as: StubNestedEntity
end
let(:response) { { title: FFaker::Name.name, 'id' => rand(1..100), another_attributes: FFaker::Name.name } }
let(:nested_response) { { name: FFaker::Name.name } }
it '.new' do
entity = StubEntity.new(response)
expect(entity).to be_success
expect(entity.id).to eq(response['id'])
expect(entity.title).to eq(response[:title])
expect { entity.another_attributes }.to raise_error(NoMethodError)
end
it '.collection' do
entities = StubEntity.collection([response, response])
expect(entities.count).to eq(2)
expect(entities.success?).to eq(true)
expect(entities.failure?).to eq(false)
expect(entities.class).to eq(ApiStruct::Collection)
expect(entities.first.title).to eq(response[:title])
end
context 'Nested entity' do
it 'when response is valid' do
response[:nested_entity] = nested_response
entity = StubEntity.new(response)
nested_entity = entity.nested_entity
expect(nested_entity.class).to eq(StubNestedEntity)
expect(nested_entity.name).to eq(nested_response[:name])
end
it 'when response is invalid' do
response[:nested_entity] = [nested_response]
entity = StubEntity.new(response)
expect { entity.nested_entity }.to raise_error(ApiStruct::EntityError)
end
end
context 'Nested entities' do
it 'when response is valid' do
response[:another_nested_entities] = [nested_response]
entity = StubEntity.new(response)
nested_entity = entity.another_nested_entities.first
expect(nested_entity.class).to eq(StubNestedEntity)
expect(nested_entity.name).to eq(nested_response[:name])
end
it 'when response is invalid' do
response[:another_nested_entities] = nested_response
entity = StubEntity.new(response)
expect { entity.another_nested_entities }.to raise_error(ApiStruct::EntityError)
end
end
context 'From monad' do
it 'convert to entity', type: :webmock do
VCR.use_cassette('posts/show_success') do
entity = StubEntity.from_monad(StubClient.new.show(1))
expect(entity).to be_success
expect(entity.id).to eq(1)
expect(entity.title).not_to be_empty
end
end
it 'convert to collection of entities', type: :webmock do
VCR.use_cassette('posts/index_success') do
entities = StubEntity.from_monad(StubClient.new.index)
expect(entities.class).to eq(ApiStruct::Collection)
expect(entities.first.id).to eq(1)
end
end
end
context 'From client service' do
it 'convert to entity', type: :webmock do
VCR.use_cassette('posts/show_success') do
entity = StubEntity.show(1)
expect(entity).to be_success
expect(entity.id).to eq(1)
expect(entity.title).not_to be_empty
expect(entity.camel_case).not_to be_empty
end
end
it 'convert to collection of entities', type: :webmock do
VCR.use_cassette('posts/index_success') do
entities = StubEntity.index
expect(entities.class).to eq(ApiStruct::Collection)
expect(entities.first.id).to eq(1)
end
end
end
describe '.client_service' do
context 'prefix' do
context 'empty' do
it 'should register client as its class name' do
expect(StubEntity.clients[:stub_client]).to eq StubClient
expect(StubEntity.clients[:another_stub_client]).to eq AnotherStubClient
end
it 'should define client methods without prefix' do
expect(StubEntity).to be_respond_to(:show)
expect(StubEntity).to be_respond_to(:index)
end
end
context 'eq true' do
it 'should register client as stubclient' do
expect(StubEntity.clients[:stub_client]).to eq StubClient
end
it 'should define client methods' do
expect(StubEntity).to be_respond_to(:stub_client_show)
expect(StubEntity).to be_respond_to(:stub_client_index)
end
end
context 'eq string' do
it 'should register client as custom' do
expect(StubEntity.clients[:custom]).to eq StubClient
end
it 'should define client methods' do
expect(StubEntity).to be_respond_to(:custom_show)
expect(StubEntity).to be_respond_to(:custom_index)
end
end
end
context 'only' do
it 'should define methods from options[:only]' do
expect(StubEntity).to be_respond_to(:only_index)
expect(StubEntity).not_to be_respond_to(:only_show)
end
end
context 'except' do
it 'should define all methods except options[:except]' do
expect(StubEntity).to be_respond_to(:except_index)
expect(StubEntity).not_to be_respond_to(:except_show)
end
end
context 'two clients' do
it 'should define methods from multiple clients' do
expect(StubEntity).to be_respond_to(:index)
expect(StubEntity).to be_respond_to(:show)
expect(StubEntity).to be_respond_to(:pull)
end
end
end
end