her 0.8.2 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.rubocop.yml +1291 -0
- data/.travis.yml +1 -0
- data/her.gemspec +1 -3
- data/lib/her/model/orm.rb +4 -1
- data/lib/her/version.rb +1 -1
- data/spec/api_spec.rb +34 -31
- data/spec/collection_spec.rb +25 -10
- data/spec/json_api/model_spec.rb +75 -72
- data/spec/middleware/accept_json_spec.rb +1 -1
- data/spec/middleware/first_level_parse_json_spec.rb +20 -20
- data/spec/middleware/json_api_parser_spec.rb +6 -7
- data/spec/middleware/second_level_parse_json_spec.rb +8 -9
- data/spec/model/associations/association_proxy_spec.rb +2 -5
- data/spec/model/associations_spec.rb +199 -158
- data/spec/model/attributes_spec.rb +98 -99
- data/spec/model/callbacks_spec.rb +58 -26
- data/spec/model/dirty_spec.rb +30 -29
- data/spec/model/http_spec.rb +67 -35
- data/spec/model/introspection_spec.rb +26 -22
- data/spec/model/nested_attributes_spec.rb +31 -31
- data/spec/model/orm_spec.rb +166 -154
- data/spec/model/parse_spec.rb +77 -77
- data/spec/model/paths_spec.rb +109 -109
- data/spec/model/relation_spec.rb +68 -68
- data/spec/model/validations_spec.rb +6 -6
- data/spec/model_spec.rb +17 -17
- data/spec/spec_helper.rb +2 -3
- data/spec/support/macros/model_macros.rb +2 -2
- metadata +32 -59
data/spec/model/parse_spec.rb
CHANGED
@@ -4,14 +4,14 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
4
4
|
describe Her::Model::Parse do
|
5
5
|
context "when include_root_in_json is set" do
|
6
6
|
before do
|
7
|
-
Her::API.setup :
|
7
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
8
8
|
builder.use Her::Middleware::FirstLevelParseJSON
|
9
9
|
builder.use Faraday::Request::UrlEncoded
|
10
10
|
end
|
11
11
|
|
12
12
|
Her::API.default_api.connection.adapter :test do |stub|
|
13
|
-
stub.post("/users") { |env| [200, {}, { :
|
14
|
-
stub.post("/users/admins") { |env| [200, {}, { :
|
13
|
+
stub.post("/users") { |env| [200, {}, { user: { id: 1, fullname: params(env)[:user][:fullname] } }.to_json] }
|
14
|
+
stub.post("/users/admins") { |env| [200, {}, { user: { id: 1, fullname: params(env)[:user][:fullname] } }.to_json] }
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -25,13 +25,13 @@ describe Her::Model::Parse do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "wraps params in the element name in `to_params`" do
|
28
|
-
@new_user = Foo::User.new(:
|
29
|
-
@new_user.to_params.
|
28
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
29
|
+
expect(@new_user.to_params).to eq(user: { fullname: "Tobias Fünke" })
|
30
30
|
end
|
31
31
|
|
32
32
|
it "wraps params in the element name in `.create`" do
|
33
|
-
@new_user = Foo::User.admins(:
|
34
|
-
@new_user.fullname.
|
33
|
+
@new_user = Foo::User.admins(fullname: "Tobias Fünke")
|
34
|
+
expect(@new_user.fullname).to eq("Tobias Fünke")
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -44,8 +44,8 @@ describe Her::Model::Parse do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
it "wraps params in the specified value" do
|
47
|
-
@new_user = Foo::User.new(:
|
48
|
-
@new_user.to_params.
|
47
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
48
|
+
expect(@new_user.to_params).to eq(person: { fullname: "Tobias Fünke" })
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -58,15 +58,15 @@ describe Her::Model::Parse do
|
|
58
58
|
end
|
59
59
|
|
60
60
|
it "wraps params with the class name" do
|
61
|
-
@new_user = User.new(:
|
62
|
-
@new_user.to_params.
|
61
|
+
@new_user = User.new(fullname: "Tobias Fünke")
|
62
|
+
expect(@new_user.to_params).to eq(user: { fullname: "Tobias Fünke" })
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
67
|
context "when parse_root_in_json is set" do
|
68
68
|
before do
|
69
|
-
Her::API.setup :
|
69
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
70
70
|
builder.use Her::Middleware::FirstLevelParseJSON
|
71
71
|
builder.use Faraday::Request::UrlEncoded
|
72
72
|
end
|
@@ -75,11 +75,11 @@ describe Her::Model::Parse do
|
|
75
75
|
context "to true" do
|
76
76
|
before do
|
77
77
|
Her::API.default_api.connection.adapter :test do |stub|
|
78
|
-
stub.post("/users") {
|
79
|
-
stub.get("/users") {
|
80
|
-
stub.get("/users/admins") {
|
81
|
-
stub.get("/users/1") {
|
82
|
-
stub.put("/users/1") {
|
78
|
+
stub.post("/users") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
79
|
+
stub.get("/users") { [200, {}, [{ user: { id: 1, fullname: "Lindsay Fünke" } }].to_json] }
|
80
|
+
stub.get("/users/admins") { [200, {}, [{ user: { id: 1, fullname: "Lindsay Fünke" } }].to_json] }
|
81
|
+
stub.get("/users/1") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
82
|
+
stub.put("/users/1") { [200, {}, { user: { id: 1, fullname: "Tobias Fünke Jr." } }.to_json] }
|
83
83
|
end
|
84
84
|
|
85
85
|
spawn_model("Foo::User") do
|
@@ -89,53 +89,53 @@ describe Her::Model::Parse do
|
|
89
89
|
end
|
90
90
|
|
91
91
|
it "parse the data from the JSON root element after .create" do
|
92
|
-
@new_user = Foo::User.create(:
|
93
|
-
@new_user.fullname.
|
92
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
93
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
94
94
|
end
|
95
95
|
|
96
96
|
it "parse the data from the JSON root element after an arbitrary HTTP request" do
|
97
97
|
@new_user = Foo::User.admins
|
98
|
-
@new_user.first.fullname.
|
98
|
+
expect(@new_user.first.fullname).to eq("Lindsay Fünke")
|
99
99
|
end
|
100
100
|
|
101
101
|
it "parse the data from the JSON root element after .all" do
|
102
102
|
@users = Foo::User.all
|
103
|
-
@users.first.fullname.
|
103
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
104
104
|
end
|
105
105
|
|
106
106
|
it "parse the data from the JSON root element after .find" do
|
107
107
|
@user = Foo::User.find(1)
|
108
|
-
@user.fullname.
|
108
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
109
109
|
end
|
110
110
|
|
111
111
|
it "parse the data from the JSON root element after .save" do
|
112
112
|
@user = Foo::User.find(1)
|
113
113
|
@user.fullname = "Tobias Fünke"
|
114
114
|
@user.save
|
115
|
-
@user.fullname.
|
115
|
+
expect(@user.fullname).to eq("Tobias Fünke Jr.")
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
119
|
context "to a symbol" do
|
120
120
|
before do
|
121
121
|
Her::API.default_api.connection.adapter :test do |stub|
|
122
|
-
stub.post("/users") {
|
122
|
+
stub.post("/users") { [200, {}, { person: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
123
123
|
end
|
124
124
|
|
125
125
|
spawn_model("Foo::User") { parse_root_in_json :person }
|
126
126
|
end
|
127
127
|
|
128
128
|
it "parse the data with the symbol" do
|
129
|
-
@new_user = Foo::User.create(:
|
130
|
-
@new_user.fullname.
|
129
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
130
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
134
134
|
context "in the parent class" do
|
135
135
|
before do
|
136
136
|
Her::API.default_api.connection.adapter :test do |stub|
|
137
|
-
stub.post("/users") {
|
138
|
-
stub.get("/users") {
|
137
|
+
stub.post("/users") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
138
|
+
stub.get("/users") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
139
139
|
end
|
140
140
|
|
141
141
|
spawn_model("Foo::Model") { parse_root_in_json true, format: :active_model_serializers }
|
@@ -147,155 +147,155 @@ describe Her::Model::Parse do
|
|
147
147
|
end
|
148
148
|
|
149
149
|
it "parse the data with the symbol" do
|
150
|
-
@new_user = User.create(:
|
151
|
-
@new_user.fullname.
|
150
|
+
@new_user = User.create(fullname: "Lindsay Fünke")
|
151
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
152
152
|
end
|
153
153
|
|
154
154
|
it "parses the collection of data" do
|
155
155
|
@users = User.all
|
156
|
-
@users.first.fullname.
|
156
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
157
157
|
end
|
158
158
|
end
|
159
159
|
|
160
|
-
context "to true with :
|
160
|
+
context "to true with format: :active_model_serializers" do
|
161
161
|
before do
|
162
162
|
Her::API.default_api.connection.adapter :test do |stub|
|
163
|
-
stub.post("/users") {
|
164
|
-
stub.get("/users") {
|
165
|
-
stub.get("/users/admins") {
|
166
|
-
stub.get("/users/1") {
|
167
|
-
stub.put("/users/1") {
|
163
|
+
stub.post("/users") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
164
|
+
stub.get("/users") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
165
|
+
stub.get("/users/admins") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
166
|
+
stub.get("/users/1") { [200, {}, { user: { id: 1, fullname: "Lindsay Fünke" } }.to_json] }
|
167
|
+
stub.put("/users/1") { [200, {}, { user: { id: 1, fullname: "Tobias Fünke Jr." } }.to_json] }
|
168
168
|
end
|
169
169
|
|
170
170
|
spawn_model("Foo::User") do
|
171
|
-
parse_root_in_json true, :
|
171
|
+
parse_root_in_json true, format: :active_model_serializers
|
172
172
|
custom_get :admins
|
173
173
|
end
|
174
174
|
end
|
175
175
|
|
176
176
|
it "parse the data from the JSON root element after .create" do
|
177
|
-
@new_user = Foo::User.create(:
|
178
|
-
@new_user.fullname.
|
177
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
178
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
179
179
|
end
|
180
180
|
|
181
181
|
it "parse the data from the JSON root element after an arbitrary HTTP request" do
|
182
182
|
@users = Foo::User.admins
|
183
|
-
@users.first.fullname.
|
183
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
184
184
|
end
|
185
185
|
|
186
186
|
it "parse the data from the JSON root element after .all" do
|
187
187
|
@users = Foo::User.all
|
188
|
-
@users.first.fullname.
|
188
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
189
189
|
end
|
190
190
|
|
191
191
|
it "parse the data from the JSON root element after .find" do
|
192
192
|
@user = Foo::User.find(1)
|
193
|
-
@user.fullname.
|
193
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
194
194
|
end
|
195
195
|
|
196
196
|
it "parse the data from the JSON root element after .save" do
|
197
197
|
@user = Foo::User.find(1)
|
198
198
|
@user.fullname = "Tobias Fünke"
|
199
199
|
@user.save
|
200
|
-
@user.fullname.
|
200
|
+
expect(@user.fullname).to eq("Tobias Fünke Jr.")
|
201
201
|
end
|
202
202
|
end
|
203
203
|
end
|
204
204
|
|
205
205
|
context "when to_params is set" do
|
206
206
|
before do
|
207
|
-
Her::API.setup :
|
207
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
208
208
|
builder.use Her::Middleware::FirstLevelParseJSON
|
209
209
|
builder.use Faraday::Request::UrlEncoded
|
210
210
|
builder.adapter :test do |stub|
|
211
|
-
stub.post("/users") { |env| ok! :
|
211
|
+
stub.post("/users") { |env| ok! id: 1, fullname: params(env)["fullname"] }
|
212
212
|
end
|
213
213
|
end
|
214
214
|
|
215
215
|
spawn_model "Foo::User" do
|
216
216
|
def to_params
|
217
|
-
{ :
|
217
|
+
{ fullname: "Lindsay Fünke" }
|
218
218
|
end
|
219
219
|
end
|
220
220
|
end
|
221
221
|
|
222
222
|
it "changes the request parameters for one-line resource creation" do
|
223
|
-
@user = Foo::User.create(:
|
224
|
-
@user.fullname.
|
223
|
+
@user = Foo::User.create(fullname: "Tobias Fünke")
|
224
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
225
225
|
end
|
226
226
|
|
227
227
|
it "changes the request parameters for Model.new + #save" do
|
228
|
-
@user = Foo::User.new(:
|
228
|
+
@user = Foo::User.new(fullname: "Tobias Fünke")
|
229
229
|
@user.save
|
230
|
-
@user.fullname.
|
230
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
231
231
|
end
|
232
232
|
end
|
233
233
|
|
234
234
|
context "when parse_root_in_json set json_api to true" do
|
235
235
|
before do
|
236
|
-
Her::API.setup :
|
236
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
237
237
|
builder.use Her::Middleware::FirstLevelParseJSON
|
238
238
|
builder.use Faraday::Request::UrlEncoded
|
239
239
|
builder.adapter :test do |stub|
|
240
|
-
stub.get("/users") {
|
241
|
-
stub.get("/users/admins") {
|
242
|
-
stub.get("/users/1") {
|
243
|
-
stub.post("/users") {
|
244
|
-
stub.put("/users/1") {
|
240
|
+
stub.get("/users") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
241
|
+
stub.get("/users/admins") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
242
|
+
stub.get("/users/1") { [200, {}, { users: [{ id: 1, fullname: "Lindsay Fünke" }] }.to_json] }
|
243
|
+
stub.post("/users") { [200, {}, { users: [{ fullname: "Lindsay Fünke" }] }.to_json] }
|
244
|
+
stub.put("/users/1") { [200, {}, { users: [{ id: 1, fullname: "Tobias Fünke Jr." }] }.to_json] }
|
245
245
|
end
|
246
246
|
end
|
247
247
|
|
248
248
|
spawn_model("Foo::User") do
|
249
|
-
parse_root_in_json true, :
|
249
|
+
parse_root_in_json true, format: :json_api
|
250
250
|
include_root_in_json true
|
251
251
|
custom_get :admins
|
252
252
|
end
|
253
253
|
end
|
254
254
|
|
255
255
|
it "parse the data from the JSON root element after .create" do
|
256
|
-
@new_user = Foo::User.create(:
|
257
|
-
@new_user.fullname.
|
256
|
+
@new_user = Foo::User.create(fullname: "Lindsay Fünke")
|
257
|
+
expect(@new_user.fullname).to eq("Lindsay Fünke")
|
258
258
|
end
|
259
259
|
|
260
260
|
it "parse the data from the JSON root element after an arbitrary HTTP request" do
|
261
261
|
@new_user = Foo::User.admins
|
262
|
-
@new_user.first.fullname.
|
262
|
+
expect(@new_user.first.fullname).to eq("Lindsay Fünke")
|
263
263
|
end
|
264
264
|
|
265
265
|
it "parse the data from the JSON root element after .all" do
|
266
266
|
@users = Foo::User.all
|
267
|
-
@users.first.fullname.
|
267
|
+
expect(@users.first.fullname).to eq("Lindsay Fünke")
|
268
268
|
end
|
269
269
|
|
270
270
|
it "parse the data from the JSON root element after .find" do
|
271
271
|
@user = Foo::User.find(1)
|
272
|
-
@user.fullname.
|
272
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
273
273
|
end
|
274
274
|
|
275
275
|
it "parse the data from the JSON root element after .save" do
|
276
276
|
@user = Foo::User.find(1)
|
277
277
|
@user.fullname = "Tobias Fünke"
|
278
278
|
@user.save
|
279
|
-
@user.fullname.
|
279
|
+
expect(@user.fullname).to eq("Tobias Fünke Jr.")
|
280
280
|
end
|
281
281
|
|
282
282
|
it "parse the data from the JSON root element after new/save" do
|
283
283
|
@user = Foo::User.new
|
284
284
|
@user.fullname = "Lindsay Fünke (before save)"
|
285
285
|
@user.save
|
286
|
-
@user.fullname.
|
286
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
287
287
|
end
|
288
288
|
end
|
289
289
|
|
290
290
|
context "when include_root_in_json set json_api" do
|
291
291
|
before do
|
292
|
-
Her::API.setup :
|
292
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
293
293
|
builder.use Her::Middleware::FirstLevelParseJSON
|
294
294
|
builder.use Faraday::Request::UrlEncoded
|
295
295
|
end
|
296
296
|
|
297
297
|
Her::API.default_api.connection.adapter :test do |stub|
|
298
|
-
stub.post("/users") { |env| [200, {}, { :
|
298
|
+
stub.post("/users") { |env| [200, {}, { users: [{ id: 1, fullname: params(env)[:users][:fullname] }] }.to_json] }
|
299
299
|
end
|
300
300
|
end
|
301
301
|
|
@@ -309,26 +309,26 @@ describe Her::Model::Parse do
|
|
309
309
|
end
|
310
310
|
|
311
311
|
it "wraps params in the element name in `to_params`" do
|
312
|
-
@new_user = Foo::User.new(:
|
313
|
-
@new_user.to_params.
|
312
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke")
|
313
|
+
expect(@new_user.to_params).to eq(users: [{ fullname: "Tobias Fünke" }])
|
314
314
|
end
|
315
315
|
|
316
316
|
it "wraps params in the element name in `.where`" do
|
317
|
-
@new_user = Foo::User.where(:
|
318
|
-
@new_user.fullname.
|
317
|
+
@new_user = Foo::User.where(fullname: "Tobias Fünke").build
|
318
|
+
expect(@new_user.fullname).to eq("Tobias Fünke")
|
319
319
|
end
|
320
320
|
end
|
321
321
|
end
|
322
322
|
|
323
|
-
context
|
323
|
+
context "when send_only_modified_attributes is set" do
|
324
324
|
before do
|
325
|
-
Her::API.setup :
|
325
|
+
Her::API.setup url: "https://api.example.com", send_only_modified_attributes: true do |builder|
|
326
326
|
builder.use Her::Middleware::FirstLevelParseJSON
|
327
327
|
builder.use Faraday::Request::UrlEncoded
|
328
328
|
end
|
329
329
|
|
330
330
|
Her::API.default_api.connection.adapter :test do |stub|
|
331
|
-
stub.get("/users/1") {
|
331
|
+
stub.get("/users/1") { [200, {}, { id: 1, first_name: "Gooby", last_name: "Pls" }.to_json] }
|
332
332
|
end
|
333
333
|
|
334
334
|
spawn_model "Foo::User" do
|
@@ -336,10 +336,10 @@ describe Her::Model::Parse do
|
|
336
336
|
end
|
337
337
|
end
|
338
338
|
|
339
|
-
it
|
339
|
+
it "only sends the attributes that were modified" do
|
340
340
|
user = Foo::User.find 1
|
341
|
-
user.first_name =
|
342
|
-
expect(user.to_params).to eql(:
|
341
|
+
user.first_name = "Someone"
|
342
|
+
expect(user.to_params).to eql(user: { first_name: "Someone" })
|
343
343
|
end
|
344
344
|
end
|
345
345
|
end
|
data/spec/model/paths_spec.rb
CHANGED
@@ -10,64 +10,64 @@ describe Her::Model::Paths do
|
|
10
10
|
|
11
11
|
describe "#request_path" do
|
12
12
|
it "builds paths with defaults" do
|
13
|
-
Foo::User.new(:
|
14
|
-
Foo::User.new(:
|
15
|
-
Foo::User.new
|
13
|
+
expect(Foo::User.new(id: "foo").request_path).to eq("users/foo")
|
14
|
+
expect(Foo::User.new(id: nil).request_path).to eq("users")
|
15
|
+
expect(Foo::User.new.request_path).to eq("users")
|
16
16
|
end
|
17
17
|
|
18
18
|
it "builds paths with custom collection path" do
|
19
19
|
Foo::User.collection_path "/utilisateurs"
|
20
|
-
Foo::User.new(:
|
21
|
-
Foo::User.new
|
20
|
+
expect(Foo::User.new(id: "foo").request_path).to eq("/utilisateurs/foo")
|
21
|
+
expect(Foo::User.new.request_path).to eq("/utilisateurs")
|
22
22
|
end
|
23
23
|
|
24
24
|
it "builds paths with custom relative collection path" do
|
25
25
|
Foo::User.collection_path "utilisateurs"
|
26
|
-
Foo::User.new(:
|
27
|
-
Foo::User.new
|
26
|
+
expect(Foo::User.new(id: "foo").request_path).to eq("utilisateurs/foo")
|
27
|
+
expect(Foo::User.new.request_path).to eq("utilisateurs")
|
28
28
|
end
|
29
29
|
|
30
30
|
it "builds paths with custom collection path with multiple variables" do
|
31
31
|
Foo::User.collection_path "/organizations/:organization_id/utilisateurs"
|
32
32
|
|
33
|
-
Foo::User.new(:
|
34
|
-
Foo::User.new
|
33
|
+
expect(Foo::User.new(id: "foo").request_path(_organization_id: "acme")).to eq("/organizations/acme/utilisateurs/foo")
|
34
|
+
expect(Foo::User.new.request_path(_organization_id: "acme")).to eq("/organizations/acme/utilisateurs")
|
35
35
|
|
36
|
-
Foo::User.new(:
|
37
|
-
Foo::User.new(:
|
36
|
+
expect(Foo::User.new(id: "foo", organization_id: "acme").request_path).to eq("/organizations/acme/utilisateurs/foo")
|
37
|
+
expect(Foo::User.new(organization_id: "acme").request_path).to eq("/organizations/acme/utilisateurs")
|
38
38
|
end
|
39
39
|
|
40
40
|
it "builds paths with custom relative collection path with multiple variables" do
|
41
41
|
Foo::User.collection_path "organizations/:organization_id/utilisateurs"
|
42
42
|
|
43
|
-
Foo::User.new(:
|
44
|
-
Foo::User.new
|
43
|
+
expect(Foo::User.new(id: "foo").request_path(_organization_id: "acme")).to eq("organizations/acme/utilisateurs/foo")
|
44
|
+
expect(Foo::User.new.request_path(_organization_id: "acme")).to eq("organizations/acme/utilisateurs")
|
45
45
|
|
46
|
-
Foo::User.new(:
|
47
|
-
Foo::User.new(:
|
46
|
+
expect(Foo::User.new(id: "foo", organization_id: "acme").request_path).to eq("organizations/acme/utilisateurs/foo")
|
47
|
+
expect(Foo::User.new(organization_id: "acme").request_path).to eq("organizations/acme/utilisateurs")
|
48
48
|
end
|
49
49
|
|
50
50
|
it "builds paths with custom item path" do
|
51
51
|
Foo::User.resource_path "/utilisateurs/:id"
|
52
|
-
Foo::User.new(:
|
53
|
-
Foo::User.new
|
52
|
+
expect(Foo::User.new(id: "foo").request_path).to eq("/utilisateurs/foo")
|
53
|
+
expect(Foo::User.new.request_path).to eq("users")
|
54
54
|
end
|
55
55
|
|
56
56
|
it "builds paths with custom relative item path" do
|
57
57
|
Foo::User.resource_path "utilisateurs/:id"
|
58
|
-
Foo::User.new(:
|
59
|
-
Foo::User.new
|
58
|
+
expect(Foo::User.new(id: "foo").request_path).to eq("utilisateurs/foo")
|
59
|
+
expect(Foo::User.new.request_path).to eq("users")
|
60
60
|
end
|
61
61
|
|
62
62
|
it "raises exceptions when building a path without required custom variables" do
|
63
63
|
Foo::User.collection_path "/organizations/:organization_id/utilisateurs"
|
64
|
-
expect { Foo::User.new(:
|
64
|
+
expect { Foo::User.new(id: "foo").request_path }.to raise_error(Her::Errors::PathError, "Missing :_organization_id parameter to build the request path. Path is `/organizations/:organization_id/utilisateurs/:id`. Parameters are `{:id=>\"foo\"}`.")
|
65
65
|
end
|
66
66
|
|
67
67
|
it "escapes the variable values" do
|
68
68
|
Foo::User.collection_path "organizations/:organization_id/utilisateurs"
|
69
|
-
Foo::User.new(:
|
70
|
-
Foo::User.new(:
|
69
|
+
expect(Foo::User.new(id: "Привет").request_path(_organization_id: "лол")).to eq("organizations/%D0%BB%D0%BE%D0%BB/utilisateurs/%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82")
|
70
|
+
expect(Foo::User.new(organization_id: "лол", id: "Привет").request_path).to eq("organizations/%D0%BB%D0%BE%D0%BB/utilisateurs/%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82")
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
@@ -79,65 +79,65 @@ describe Her::Model::Paths do
|
|
79
79
|
|
80
80
|
describe "#request_path" do
|
81
81
|
it "builds paths with defaults" do
|
82
|
-
Foo::AdminUser.new(:
|
83
|
-
Foo::AdminUser.new
|
82
|
+
expect(Foo::AdminUser.new(id: "foo").request_path).to eq("admin_users/foo")
|
83
|
+
expect(Foo::AdminUser.new.request_path).to eq("admin_users")
|
84
84
|
end
|
85
85
|
|
86
86
|
it "builds paths with custom collection path" do
|
87
87
|
Foo::AdminUser.collection_path "/users"
|
88
|
-
Foo::AdminUser.new(:
|
89
|
-
Foo::AdminUser.new
|
88
|
+
expect(Foo::AdminUser.new(id: "foo").request_path).to eq("/users/foo")
|
89
|
+
expect(Foo::AdminUser.new.request_path).to eq("/users")
|
90
90
|
end
|
91
91
|
|
92
92
|
it "builds paths with custom relative collection path" do
|
93
93
|
Foo::AdminUser.collection_path "users"
|
94
|
-
Foo::AdminUser.new(:
|
95
|
-
Foo::AdminUser.new
|
94
|
+
expect(Foo::AdminUser.new(id: "foo").request_path).to eq("users/foo")
|
95
|
+
expect(Foo::AdminUser.new.request_path).to eq("users")
|
96
96
|
end
|
97
97
|
|
98
98
|
it "builds paths with custom collection path with multiple variables" do
|
99
99
|
Foo::AdminUser.collection_path "/organizations/:organization_id/users"
|
100
|
-
Foo::AdminUser.new(:
|
101
|
-
Foo::AdminUser.new
|
100
|
+
expect(Foo::AdminUser.new(id: "foo").request_path(_organization_id: "acme")).to eq("/organizations/acme/users/foo")
|
101
|
+
expect(Foo::AdminUser.new.request_path(_organization_id: "acme")).to eq("/organizations/acme/users")
|
102
102
|
end
|
103
103
|
|
104
104
|
it "builds paths with custom relative collection path with multiple variables" do
|
105
105
|
Foo::AdminUser.collection_path "organizations/:organization_id/users"
|
106
|
-
Foo::AdminUser.new(:
|
107
|
-
Foo::AdminUser.new
|
106
|
+
expect(Foo::AdminUser.new(id: "foo").request_path(_organization_id: "acme")).to eq("organizations/acme/users/foo")
|
107
|
+
expect(Foo::AdminUser.new.request_path(_organization_id: "acme")).to eq("organizations/acme/users")
|
108
108
|
end
|
109
109
|
|
110
110
|
it "builds paths with custom item path" do
|
111
111
|
Foo::AdminUser.resource_path "/users/:id"
|
112
|
-
Foo::AdminUser.new(:
|
113
|
-
Foo::AdminUser.new
|
112
|
+
expect(Foo::AdminUser.new(id: "foo").request_path).to eq("/users/foo")
|
113
|
+
expect(Foo::AdminUser.new.request_path).to eq("admin_users")
|
114
114
|
end
|
115
115
|
|
116
116
|
it "builds paths with custom relative item path" do
|
117
117
|
Foo::AdminUser.resource_path "users/:id"
|
118
|
-
Foo::AdminUser.new(:
|
119
|
-
Foo::AdminUser.new
|
118
|
+
expect(Foo::AdminUser.new(id: "foo").request_path).to eq("users/foo")
|
119
|
+
expect(Foo::AdminUser.new.request_path).to eq("admin_users")
|
120
120
|
end
|
121
121
|
|
122
122
|
it "raises exceptions when building a path without required custom variables" do
|
123
123
|
Foo::AdminUser.collection_path "/organizations/:organization_id/users"
|
124
|
-
expect { Foo::AdminUser.new(:
|
124
|
+
expect { Foo::AdminUser.new(id: "foo").request_path }.to raise_error(Her::Errors::PathError, "Missing :_organization_id parameter to build the request path. Path is `/organizations/:organization_id/users/:id`. Parameters are `{:id=>\"foo\"}`.")
|
125
125
|
end
|
126
126
|
|
127
127
|
it "raises exceptions when building a relative path without required custom variables" do
|
128
128
|
Foo::AdminUser.collection_path "organizations/:organization_id/users"
|
129
|
-
expect { Foo::AdminUser.new(:
|
129
|
+
expect { Foo::AdminUser.new(id: "foo").request_path }.to raise_error(Her::Errors::PathError, "Missing :_organization_id parameter to build the request path. Path is `organizations/:organization_id/users/:id`. Parameters are `{:id=>\"foo\"}`.")
|
130
130
|
end
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
134
134
|
context "children model" do
|
135
135
|
before do
|
136
|
-
Her::API.setup :
|
136
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
137
137
|
builder.use Her::Middleware::FirstLevelParseJSON
|
138
138
|
builder.use Faraday::Request::UrlEncoded
|
139
139
|
builder.adapter :test do |stub|
|
140
|
-
stub.get("/users/foo") {
|
140
|
+
stub.get("/users/foo") { [200, {}, { id: "foo" }.to_json] }
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
@@ -148,8 +148,8 @@ describe Her::Model::Paths do
|
|
148
148
|
end
|
149
149
|
|
150
150
|
it "builds path using the children model name" do
|
151
|
-
User.find(
|
152
|
-
User.find(
|
151
|
+
expect(User.find("foo").id).to eq("foo")
|
152
|
+
expect(User.find("foo").id).to eq("foo")
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
@@ -160,34 +160,34 @@ describe Her::Model::Paths do
|
|
160
160
|
|
161
161
|
describe "#request_path" do
|
162
162
|
it "builds paths with defaults" do
|
163
|
-
Foo::User.new(:
|
164
|
-
Foo::User.new.request_path.
|
163
|
+
expect(Foo::User.new(id: "foo").request_path).to eq("users/foo")
|
164
|
+
expect(Foo::User.new.request_path).to eq("users")
|
165
165
|
end
|
166
166
|
end
|
167
167
|
end
|
168
168
|
|
169
|
-
context
|
169
|
+
context "custom primary key" do
|
170
170
|
before do
|
171
|
-
spawn_model
|
172
|
-
primary_key
|
173
|
-
resource_path
|
171
|
+
spawn_model "User" do
|
172
|
+
primary_key "UserId"
|
173
|
+
resource_path "users/:UserId"
|
174
174
|
end
|
175
175
|
|
176
|
-
spawn_model
|
176
|
+
spawn_model "Customer" do
|
177
177
|
primary_key :customer_id
|
178
|
-
resource_path
|
178
|
+
resource_path "customers/:id"
|
179
179
|
end
|
180
180
|
end
|
181
181
|
|
182
|
-
describe
|
183
|
-
it
|
184
|
-
User.new(:
|
185
|
-
User.new(:
|
182
|
+
describe "#request_path" do
|
183
|
+
it "uses the correct primary key attribute" do
|
184
|
+
expect(User.new(UserId: "foo").request_path).to eq("users/foo")
|
185
|
+
expect(User.new(id: "foo").request_path).to eq("users")
|
186
186
|
end
|
187
187
|
|
188
|
-
it
|
189
|
-
Customer.new(:
|
190
|
-
Customer.new(:
|
188
|
+
it "replaces :id with the appropriate primary key" do
|
189
|
+
expect(Customer.new(customer_id: "joe").request_path).to eq("customers/joe")
|
190
|
+
expect(Customer.new(id: "joe").request_path).to eq("customers")
|
191
191
|
end
|
192
192
|
end
|
193
193
|
end
|
@@ -195,15 +195,15 @@ describe Her::Model::Paths do
|
|
195
195
|
|
196
196
|
context "making subdomain HTTP requests" do
|
197
197
|
before do
|
198
|
-
Her::API.setup :
|
198
|
+
Her::API.setup url: "https://api.example.com/" do |builder|
|
199
199
|
builder.use Her::Middleware::FirstLevelParseJSON
|
200
200
|
builder.use Faraday::Request::UrlEncoded
|
201
201
|
builder.adapter :test do |stub|
|
202
|
-
stub.get("organizations/2/users") {
|
203
|
-
stub.post("organizations/2/users") {
|
204
|
-
stub.put("organizations/2/users/1") {
|
205
|
-
stub.get("organizations/2/users/1") {
|
206
|
-
stub.delete("organizations/2/users/1") {
|
202
|
+
stub.get("organizations/2/users") { [200, {}, [{ id: 1, fullname: "Tobias Fünke", organization_id: 2 }, { id: 2, fullname: "Lindsay Fünke", organization_id: 2 }].to_json] }
|
203
|
+
stub.post("organizations/2/users") { [200, {}, { id: 1, fullname: "Tobias Fünke", organization_id: 2 }.to_json] }
|
204
|
+
stub.put("organizations/2/users/1") { [200, {}, { id: 1, fullname: "Lindsay Fünke", organization_id: 2 }.to_json] }
|
205
|
+
stub.get("organizations/2/users/1") { [200, {}, { id: 1, fullname: "Tobias Fünke", organization_id: 2, active: true }.to_json] }
|
206
|
+
stub.delete("organizations/2/users/1") { [200, {}, { id: 1, fullname: "Lindsay Fünke", organization_id: 2, active: false }.to_json] }
|
207
207
|
end
|
208
208
|
end
|
209
209
|
|
@@ -214,94 +214,94 @@ describe Her::Model::Paths do
|
|
214
214
|
|
215
215
|
describe "fetching a resource" do
|
216
216
|
it "maps a single resource to a Ruby object" do
|
217
|
-
@user = Foo::User.find(1, :
|
218
|
-
@user.id.
|
219
|
-
@user.fullname.
|
217
|
+
@user = Foo::User.find(1, _organization_id: 2)
|
218
|
+
expect(@user.id).to eq(1)
|
219
|
+
expect(@user.fullname).to eq("Tobias Fünke")
|
220
220
|
end
|
221
221
|
|
222
222
|
it "maps a single resource using a scope to a Ruby object" do
|
223
|
-
Foo::User.scope :for_organization,
|
223
|
+
Foo::User.scope :for_organization, ->(o) { where(organization_id: o) }
|
224
224
|
@user = Foo::User.for_organization(2).find(1)
|
225
|
-
@user.id.
|
226
|
-
@user.fullname.
|
225
|
+
expect(@user.id).to eq(1)
|
226
|
+
expect(@user.fullname).to eq("Tobias Fünke")
|
227
227
|
end
|
228
228
|
end
|
229
229
|
|
230
230
|
describe "fetching a collection" do
|
231
231
|
it "maps a collection of resources to an array of Ruby objects" do
|
232
|
-
@users = Foo::User.where(:
|
233
|
-
@users.length.
|
234
|
-
@users.first.fullname.
|
232
|
+
@users = Foo::User.where(_organization_id: 2).all
|
233
|
+
expect(@users.length).to eq(2)
|
234
|
+
expect(@users.first.fullname).to eq("Tobias Fünke")
|
235
235
|
end
|
236
236
|
end
|
237
237
|
|
238
238
|
describe "handling new resource" do
|
239
239
|
it "handles new resource" do
|
240
|
-
@new_user = Foo::User.new(:
|
241
|
-
@new_user.new
|
240
|
+
@new_user = Foo::User.new(fullname: "Tobias Fünke", organization_id: 2)
|
241
|
+
expect(@new_user.new?).to be_truthy
|
242
242
|
|
243
|
-
@existing_user = Foo::User.find(1, :
|
244
|
-
@existing_user.new
|
243
|
+
@existing_user = Foo::User.find(1, _organization_id: 2)
|
244
|
+
expect(@existing_user.new?).to be_falsey
|
245
245
|
end
|
246
246
|
end
|
247
247
|
|
248
248
|
describe "creating resources" do
|
249
249
|
it "handle one-line resource creation" do
|
250
|
-
@user = Foo::User.create(:
|
251
|
-
@user.id.
|
252
|
-
@user.fullname.
|
250
|
+
@user = Foo::User.create(fullname: "Tobias Fünke", organization_id: 2)
|
251
|
+
expect(@user.id).to eq(1)
|
252
|
+
expect(@user.fullname).to eq("Tobias Fünke")
|
253
253
|
end
|
254
254
|
|
255
255
|
it "handle resource creation through Model.new + #save" do
|
256
|
-
@user = Foo::User.new(:
|
256
|
+
@user = Foo::User.new(fullname: "Tobias Fünke", organization_id: 2)
|
257
257
|
@user.save
|
258
|
-
@user.fullname.
|
258
|
+
expect(@user.fullname).to eq("Tobias Fünke")
|
259
259
|
end
|
260
260
|
end
|
261
261
|
|
262
262
|
context "updating resources" do
|
263
263
|
it "handle resource data update without saving it" do
|
264
|
-
@user = Foo::User.find(1, :
|
265
|
-
@user.fullname.
|
264
|
+
@user = Foo::User.find(1, _organization_id: 2)
|
265
|
+
expect(@user.fullname).to eq("Tobias Fünke")
|
266
266
|
@user.fullname = "Kittie Sanchez"
|
267
|
-
@user.fullname.
|
267
|
+
expect(@user.fullname).to eq("Kittie Sanchez")
|
268
268
|
end
|
269
269
|
|
270
270
|
it "handle resource update through the .update class method" do
|
271
|
-
@user = Foo::User.save_existing(1,
|
272
|
-
@user.fullname.
|
271
|
+
@user = Foo::User.save_existing(1, fullname: "Lindsay Fünke", organization_id: 2)
|
272
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
273
273
|
end
|
274
274
|
|
275
275
|
it "handle resource update through #save on an existing resource" do
|
276
|
-
@user = Foo::User.find(1, :
|
276
|
+
@user = Foo::User.find(1, _organization_id: 2)
|
277
277
|
@user.fullname = "Lindsay Fünke"
|
278
278
|
@user.save
|
279
|
-
@user.fullname.
|
279
|
+
expect(@user.fullname).to eq("Lindsay Fünke")
|
280
280
|
end
|
281
281
|
end
|
282
282
|
|
283
283
|
context "deleting resources" do
|
284
284
|
it "handle resource deletion through the .destroy class method" do
|
285
|
-
@user = Foo::User.destroy_existing(1, :
|
286
|
-
@user.active.
|
285
|
+
@user = Foo::User.destroy_existing(1, _organization_id: 2)
|
286
|
+
expect(@user.active).to be_falsey
|
287
287
|
end
|
288
288
|
|
289
289
|
it "handle resource deletion through #destroy on an existing resource" do
|
290
|
-
@user = Foo::User.find(1, :
|
290
|
+
@user = Foo::User.find(1, _organization_id: 2)
|
291
291
|
@user.destroy
|
292
|
-
@user.active.
|
292
|
+
expect(@user.active).to be_falsey
|
293
293
|
end
|
294
294
|
end
|
295
295
|
end
|
296
296
|
|
297
297
|
context "making path HTTP requests" do
|
298
298
|
before do
|
299
|
-
Her::API.setup :
|
299
|
+
Her::API.setup url: "https://example.com/api/" do |builder|
|
300
300
|
builder.use Her::Middleware::FirstLevelParseJSON
|
301
301
|
builder.use Faraday::Request::UrlEncoded
|
302
302
|
builder.adapter :test do |stub|
|
303
|
-
stub.get("/api/organizations/2/users") {
|
304
|
-
stub.get("/api/organizations/2/users/1") {
|
303
|
+
stub.get("/api/organizations/2/users") { [200, {}, [{ id: 1, fullname: "Tobias Fünke", organization_id: 2 }, { id: 2, fullname: "Lindsay Fünke", organization_id: 2 }].to_json] }
|
304
|
+
stub.get("/api/organizations/2/users/1") { [200, {}, { id: 1, fullname: "Tobias Fünke", organization_id: 2, active: true }.to_json] }
|
305
305
|
end
|
306
306
|
end
|
307
307
|
|
@@ -312,35 +312,35 @@ describe Her::Model::Paths do
|
|
312
312
|
|
313
313
|
describe "fetching a resource" do
|
314
314
|
it "maps a single resource to a Ruby object" do
|
315
|
-
@user = Foo::User.find(1, :
|
316
|
-
@user.id.
|
317
|
-
@user.fullname.
|
315
|
+
@user = Foo::User.find(1, _organization_id: 2)
|
316
|
+
expect(@user.id).to eq(1)
|
317
|
+
expect(@user.fullname).to eq("Tobias Fünke")
|
318
318
|
end
|
319
319
|
end
|
320
320
|
|
321
321
|
describe "fetching a collection" do
|
322
322
|
it "maps a collection of resources to an array of Ruby objects" do
|
323
|
-
@users = Foo::User.where(:
|
324
|
-
@users.length.
|
325
|
-
@users.first.fullname.
|
323
|
+
@users = Foo::User.where(_organization_id: 2).all
|
324
|
+
expect(@users.length).to eq(2)
|
325
|
+
expect(@users.first.fullname).to eq("Tobias Fünke")
|
326
326
|
end
|
327
327
|
end
|
328
328
|
|
329
329
|
describe "fetching a resource with absolute path" do
|
330
330
|
it "maps a single resource to a Ruby object" do
|
331
|
-
Foo::User.resource_path
|
332
|
-
@user = Foo::User.find(1, :
|
333
|
-
@user.id.
|
334
|
-
@user.fullname.
|
331
|
+
Foo::User.resource_path "/api/" + Foo::User.resource_path
|
332
|
+
@user = Foo::User.find(1, _organization_id: 2)
|
333
|
+
expect(@user.id).to eq(1)
|
334
|
+
expect(@user.fullname).to eq("Tobias Fünke")
|
335
335
|
end
|
336
336
|
end
|
337
337
|
|
338
338
|
describe "fetching a collection with absolute path" do
|
339
339
|
it "maps a collection of resources to an array of Ruby objects" do
|
340
|
-
Foo::User.collection_path
|
341
|
-
@users = Foo::User.where(:
|
342
|
-
@users.length.
|
343
|
-
@users.first.fullname.
|
340
|
+
Foo::User.collection_path "/api/" + Foo::User.collection_path
|
341
|
+
@users = Foo::User.where(_organization_id: 2).all
|
342
|
+
expect(@users.length).to eq(2)
|
343
|
+
expect(@users.first.fullname).to eq("Tobias Fünke")
|
344
344
|
end
|
345
345
|
end
|
346
346
|
end
|