her 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -4
- data/lib/her/api.rb +8 -8
- data/lib/her/collection.rb +2 -2
- data/lib/her/middleware/accept_json.rb +4 -4
- data/lib/her/middleware/first_level_parse_json.rb +4 -4
- data/lib/her/middleware/second_level_parse_json.rb +4 -4
- data/lib/her/model/hooks.rb +12 -12
- data/lib/her/model/http.rb +56 -56
- data/lib/her/model/introspection.rb +13 -13
- data/lib/her/model/orm.rb +85 -59
- data/lib/her/model/paths.rb +8 -8
- data/lib/her/model/relationships.rb +15 -13
- data/lib/her/version.rb +1 -1
- data/spec/api_spec.rb +16 -16
- data/spec/middleware/accept_json_spec.rb +2 -2
- data/spec/middleware/first_level_parse_json_spec.rb +4 -4
- data/spec/middleware/second_level_parse_json_spec.rb +4 -4
- data/spec/model/hooks_spec.rb +66 -66
- data/spec/model/http_spec.rb +56 -56
- data/spec/model/introspection_spec.rb +12 -12
- data/spec/model/orm_spec.rb +113 -94
- data/spec/model/paths_spec.rb +74 -74
- data/spec/model/relationships_spec.rb +52 -52
- metadata +5 -5
data/spec/model/http_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
3
3
|
|
4
4
|
describe Her::Model::HTTP do
|
5
5
|
context "binding a model with an API" do
|
6
|
-
it "binds a model to an instance of Her::API" do
|
6
|
+
it "binds a model to an instance of Her::API" do
|
7
7
|
api = Her::API.new
|
8
8
|
api.setup :url => "https://api.example.com"
|
9
9
|
|
@@ -11,18 +11,18 @@ describe Her::Model::HTTP do
|
|
11
11
|
Foo::User.uses_api api
|
12
12
|
Foo::User.her_api.should_not == nil
|
13
13
|
Foo::User.her_api.base_uri.should == "https://api.example.com"
|
14
|
-
end
|
14
|
+
end
|
15
15
|
|
16
|
-
it "binds a model directly to Her::API" do
|
16
|
+
it "binds a model directly to Her::API" do
|
17
17
|
Her::API.setup :url => "https://api.example.com"
|
18
18
|
|
19
19
|
spawn_model "Foo::User"
|
20
20
|
|
21
21
|
Foo::User.her_api.should_not == nil
|
22
22
|
Foo::User.her_api.base_uri.should == "https://api.example.com"
|
23
|
-
end
|
23
|
+
end
|
24
24
|
|
25
|
-
it "binds two models to two different instances of Her::API" do
|
25
|
+
it "binds two models to two different instances of Her::API" do
|
26
26
|
api1 = Her::API.new
|
27
27
|
api1.setup :url => "https://api1.example.com" do |builder|
|
28
28
|
builder.use Her::Middleware::FirstLevelParseJSON
|
@@ -42,9 +42,9 @@ describe Her::Model::HTTP do
|
|
42
42
|
spawn_model "Foo::Comment"
|
43
43
|
Foo::Comment.uses_api api2
|
44
44
|
Foo::Comment.her_api.base_uri.should == "https://api2.example.com"
|
45
|
-
end
|
45
|
+
end
|
46
46
|
|
47
|
-
it "binds one model to Her::API and another one to an instance of Her::API" do
|
47
|
+
it "binds one model to Her::API and another one to an instance of Her::API" do
|
48
48
|
Her::API.setup :url => "https://api1.example.com" do |builder|
|
49
49
|
builder.use Her::Middleware::FirstLevelParseJSON
|
50
50
|
builder.use Faraday::Request::UrlEncoded
|
@@ -63,9 +63,9 @@ describe Her::Model::HTTP do
|
|
63
63
|
spawn_model "Foo::Comment"
|
64
64
|
Foo::Comment.uses_api api
|
65
65
|
Foo::Comment.her_api.base_uri.should == "https://api2.example.com"
|
66
|
-
end
|
66
|
+
end
|
67
67
|
|
68
|
-
it "binds a a model to it's superclass' her_api" do
|
68
|
+
it "binds a a model to it's superclass' her_api" do
|
69
69
|
api = Her::API.new
|
70
70
|
api.setup :url => "http://api.example.com" do |builder|
|
71
71
|
builder.use Her::Middleware::FirstLevelParseJSON
|
@@ -78,9 +78,9 @@ describe Her::Model::HTTP do
|
|
78
78
|
|
79
79
|
Foo::Subclass = Class.new(Foo::Superclass)
|
80
80
|
Foo::Subclass.her_api.should == Foo::Superclass.her_api
|
81
|
-
end
|
81
|
+
end
|
82
82
|
|
83
|
-
it "allows subclasses to change her_api without changing the parent class' her_api" do
|
83
|
+
it "allows subclasses to change her_api without changing the parent class' her_api" do
|
84
84
|
api1 = Her::API.new
|
85
85
|
api1.setup :url => "http://api.example.com" do |builder|
|
86
86
|
builder.use Her::Middleware::FirstLevelParseJSON
|
@@ -102,11 +102,11 @@ describe Her::Model::HTTP do
|
|
102
102
|
end
|
103
103
|
|
104
104
|
Foo::Subclass.her_api.should_not == Foo::Superclass.her_api
|
105
|
-
end
|
105
|
+
end
|
106
106
|
end
|
107
107
|
|
108
108
|
context "making HTTP requests" do
|
109
|
-
before do
|
109
|
+
before do
|
110
110
|
Her::API.setup :url => "https://api.example.com" do |builder|
|
111
111
|
builder.use Her::Middleware::FirstLevelParseJSON
|
112
112
|
builder.use Faraday::Request::UrlEncoded
|
@@ -128,109 +128,109 @@ describe Her::Model::HTTP do
|
|
128
128
|
end
|
129
129
|
|
130
130
|
spawn_model "Foo::User"
|
131
|
-
end
|
131
|
+
end
|
132
132
|
|
133
|
-
it "handles GET wrapper method" do
|
133
|
+
it "handles GET wrapper method" do
|
134
134
|
@users = Foo::User.get(:popular)
|
135
135
|
@users.length.should == 2
|
136
136
|
@users.first.id.should == 1
|
137
137
|
|
138
138
|
@user = Foo::User.get(:"1")
|
139
139
|
@user.id.should == 1
|
140
|
-
end
|
140
|
+
end
|
141
141
|
|
142
|
-
it "handles raw GET with a block" do
|
142
|
+
it "handles raw GET with a block" do
|
143
143
|
Foo::User.get_raw("/users") do |parsed_data|
|
144
144
|
parsed_data[:data].should == [{ :id => 1 }]
|
145
145
|
end
|
146
|
-
end
|
146
|
+
end
|
147
147
|
|
148
|
-
it "handles raw GET with return value" do
|
148
|
+
it "handles raw GET with return value" do
|
149
149
|
parsed_data = Foo::User.get_raw("/users")
|
150
150
|
parsed_data[:data].should == [{ :id => 1 }]
|
151
|
-
end
|
151
|
+
end
|
152
152
|
|
153
|
-
it "handles raw POST with a block" do
|
153
|
+
it "handles raw POST with a block" do
|
154
154
|
Foo::User.post_raw("/users") do |parsed_data|
|
155
155
|
parsed_data[:data].should == [{ :id => 3 }]
|
156
156
|
end
|
157
|
-
end
|
157
|
+
end
|
158
158
|
|
159
|
-
it "handles raw POST with return value" do
|
159
|
+
it "handles raw POST with return value" do
|
160
160
|
parsed_data = Foo::User.post_raw("/users")
|
161
161
|
parsed_data[:data].should == [{ :id => 3 }]
|
162
|
-
end
|
162
|
+
end
|
163
163
|
|
164
|
-
it "handles raw PUT with a block" do
|
164
|
+
it "handles raw PUT with a block" do
|
165
165
|
Foo::User.put_raw("/users/4") do |parsed_data|
|
166
166
|
parsed_data[:data].should == [{ :id => 4 }]
|
167
167
|
end
|
168
|
-
end
|
168
|
+
end
|
169
169
|
|
170
|
-
it "handles raw PUT with return value" do
|
170
|
+
it "handles raw PUT with return value" do
|
171
171
|
parsed_data = Foo::User.put_raw("/users/4")
|
172
172
|
parsed_data[:data].should == [{ :id => 4 }]
|
173
|
-
end
|
173
|
+
end
|
174
174
|
|
175
|
-
it "handles raw PATCH with a block" do
|
175
|
+
it "handles raw PATCH with a block" do
|
176
176
|
Foo::User.patch_raw("/users/6") do |parsed_data|
|
177
177
|
parsed_data[:data].should == [{ :id => 6 }]
|
178
178
|
end
|
179
|
-
end
|
179
|
+
end
|
180
180
|
|
181
|
-
it "handles raw PATCH with return value" do
|
181
|
+
it "handles raw PATCH with return value" do
|
182
182
|
parsed_data = Foo::User.patch_raw("/users/6")
|
183
183
|
parsed_data[:data].should == [{ :id => 6 }]
|
184
|
-
end
|
184
|
+
end
|
185
185
|
|
186
|
-
it "handles raw DELETE with a block" do
|
186
|
+
it "handles raw DELETE with a block" do
|
187
187
|
Foo::User.delete_raw("/users/5") do |parsed_data|
|
188
188
|
parsed_data[:data].should == [{ :id => 5 }]
|
189
189
|
end
|
190
|
-
end
|
190
|
+
end
|
191
191
|
|
192
|
-
it "handles raw DELETE with return value" do
|
192
|
+
it "handles raw DELETE with return value" do
|
193
193
|
parsed_data = Foo::User.delete_raw("/users/5")
|
194
194
|
parsed_data[:data].should == [{ :id => 5 }]
|
195
|
-
end
|
195
|
+
end
|
196
196
|
|
197
|
-
it "handles querystring parameters" do
|
197
|
+
it "handles querystring parameters" do
|
198
198
|
Foo::User.get_raw("/users", :page => 2) do |parsed_data|
|
199
199
|
parsed_data[:data].should == [{ :id => 2 }]
|
200
200
|
end
|
201
|
-
end
|
201
|
+
end
|
202
202
|
|
203
|
-
it "handles GET collection" do
|
203
|
+
it "handles GET collection" do
|
204
204
|
@users = Foo::User.get_collection("/users/popular")
|
205
205
|
@users.length.should == 2
|
206
206
|
@users.first.id.should == 1
|
207
|
-
end
|
207
|
+
end
|
208
208
|
|
209
|
-
it "handles GET resource" do
|
209
|
+
it "handles GET resource" do
|
210
210
|
@user = Foo::User.get_resource("/users/1")
|
211
211
|
@user.id.should == 1
|
212
|
-
end
|
212
|
+
end
|
213
213
|
|
214
|
-
it "handles GET collection through a symbol" do
|
214
|
+
it "handles GET collection through a symbol" do
|
215
215
|
@users = Foo::User.get_collection(:popular)
|
216
216
|
@users.length.should == 2
|
217
217
|
@users.first.id.should == 1
|
218
|
-
end
|
218
|
+
end
|
219
219
|
|
220
|
-
it "handles GET resource through a symbol" do
|
220
|
+
it "handles GET resource through a symbol" do
|
221
221
|
@user = Foo::User.get_resource(:"1")
|
222
222
|
@user.id.should == 1
|
223
|
-
end
|
223
|
+
end
|
224
224
|
|
225
|
-
it "handles raw GET through a symbol" do
|
225
|
+
it "handles raw GET through a symbol" do
|
226
226
|
Foo::User.get_raw(:popular) do |parsed_data|
|
227
227
|
parsed_data[:data].should == [{ :id => 1 }, { :id => 2 }]
|
228
228
|
end
|
229
|
-
end
|
229
|
+
end
|
230
230
|
end
|
231
231
|
|
232
232
|
context "setting custom requests" do
|
233
|
-
before do
|
233
|
+
before do
|
234
234
|
Her::API.setup :url => "https://api.example.com" do |builder|
|
235
235
|
builder.use Her::Middleware::FirstLevelParseJSON
|
236
236
|
builder.use Faraday::Request::UrlEncoded
|
@@ -243,23 +243,23 @@ describe Her::Model::HTTP do
|
|
243
243
|
spawn_model "Foo::User"
|
244
244
|
Foo::User.custom_get :popular, :foobar
|
245
245
|
Foo::User.custom_post :from_default
|
246
|
-
end
|
246
|
+
end
|
247
247
|
|
248
|
-
it "handles custom methods" do
|
248
|
+
it "handles custom methods" do
|
249
249
|
Foo::User.respond_to?(:popular).should be_true
|
250
250
|
Foo::User.respond_to?(:foobar).should be_true
|
251
251
|
Foo::User.respond_to?(:from_default).should be_true
|
252
|
-
end
|
252
|
+
end
|
253
253
|
|
254
|
-
it "handles custom GET requests" do
|
254
|
+
it "handles custom GET requests" do
|
255
255
|
@users = Foo::User.popular
|
256
256
|
@users.length.should == 2
|
257
257
|
@users.first.id.should == 1
|
258
|
-
end
|
258
|
+
end
|
259
259
|
|
260
|
-
it "handles custom POST requests" do
|
260
|
+
it "handles custom POST requests" do
|
261
261
|
@user = Foo::User.from_default(:name => "Tobias Fünke")
|
262
262
|
@user.id.should be_true
|
263
|
-
end
|
263
|
+
end
|
264
264
|
end
|
265
265
|
end
|
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
3
3
|
|
4
4
|
describe Her::Model::Introspection do
|
5
5
|
context "introspecting a resource" do
|
6
|
-
before do
|
6
|
+
before do
|
7
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
|
@@ -16,36 +16,36 @@ describe Her::Model::Introspection do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
spawn_model "Foo::User"
|
19
|
-
end
|
19
|
+
end
|
20
20
|
|
21
21
|
describe "#inspect" do
|
22
|
-
it "outputs resource attributs for an existing resource" do
|
22
|
+
it "outputs resource attributs for an existing resource" do
|
23
23
|
@user = Foo::User.find(1)
|
24
24
|
["#<Foo::User(users/1) name=\"Tobias Funke\" id=1>", "#<Foo::User(users/1) id=1 name=\"Tobias Funke\">"].should include(@user.inspect)
|
25
|
-
end
|
25
|
+
end
|
26
26
|
|
27
|
-
it "outputs resource attributs for an not-saved-yet resource" do
|
27
|
+
it "outputs resource attributs for an not-saved-yet resource" do
|
28
28
|
@user = Foo::User.new(:name => "Tobias Funke")
|
29
29
|
@user.inspect.should == "#<Foo::User(users) name=\"Tobias Funke\">"
|
30
|
-
end
|
30
|
+
end
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
describe "#nearby_class" do
|
35
|
-
context "for a class inside of a module" do
|
36
|
-
before do
|
35
|
+
context "for a class inside of a module" do
|
36
|
+
before do
|
37
37
|
spawn_model "Foo::User"
|
38
38
|
spawn_model "Foo::AccessRecord"
|
39
39
|
spawn_model "AccessRecord"
|
40
40
|
spawn_model "Log"
|
41
|
-
end
|
41
|
+
end
|
42
42
|
|
43
|
-
it "returns a sibling class, if found" do
|
43
|
+
it "returns a sibling class, if found" do
|
44
44
|
Foo::User.nearby_class("AccessRecord").should == Foo::AccessRecord
|
45
45
|
AccessRecord.nearby_class("Log").should == Log
|
46
46
|
Foo::User.nearby_class("Log").should == Log
|
47
47
|
Foo::User.nearby_class("X").should be_nil
|
48
|
-
end
|
49
|
-
end
|
48
|
+
end
|
49
|
+
end
|
50
50
|
end
|
51
51
|
end
|
data/spec/model/orm_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
3
3
|
|
4
4
|
describe Her::Model::ORM do
|
5
5
|
context "mapping data to Ruby objects" do
|
6
|
-
before do
|
6
|
+
before do
|
7
7
|
api = Her::API.new
|
8
8
|
api.setup :url => "https://api.example.com" do |builder|
|
9
9
|
builder.use Her::Middleware::FirstLevelParseJSON
|
@@ -22,15 +22,15 @@ describe Her::Model::ORM do
|
|
22
22
|
spawn_model "Foo::AdminUser" do
|
23
23
|
uses_api api
|
24
24
|
end
|
25
|
-
end
|
25
|
+
end
|
26
26
|
|
27
|
-
it "maps a single resource to a Ruby object" do
|
27
|
+
it "maps a single resource to a Ruby object" do
|
28
28
|
@user = Foo::User.find(1)
|
29
29
|
@user.id.should == 1
|
30
30
|
@user.name.should == "Tobias Fünke"
|
31
|
-
end
|
31
|
+
end
|
32
32
|
|
33
|
-
it "maps a collection of resources to an array of Ruby objects" do
|
33
|
+
it "maps a collection of resources to an array of Ruby objects" do
|
34
34
|
@users = Foo::User.all
|
35
35
|
@users.length.should == 2
|
36
36
|
@users.first.name.should == "Tobias Fünke"
|
@@ -38,52 +38,64 @@ describe Her::Model::ORM do
|
|
38
38
|
@users = Foo::AdminUser.all
|
39
39
|
@users.length.should == 2
|
40
40
|
@users.first.name.should == "Tobias Fünke"
|
41
|
-
end
|
41
|
+
end
|
42
42
|
|
43
|
-
it "handles new resource" do
|
43
|
+
it "handles new resource" do
|
44
44
|
@new_user = Foo::User.new(:fullname => "Tobias Fünke")
|
45
45
|
@new_user.new?.should be_true
|
46
46
|
@new_user.fullname.should == "Tobias Fünke"
|
47
47
|
|
48
48
|
@existing_user = Foo::User.find(1)
|
49
49
|
@existing_user.new?.should be_false
|
50
|
-
end
|
50
|
+
end
|
51
51
|
|
52
|
-
it "handles method missing for getter" do
|
52
|
+
it "handles method missing for getter" do
|
53
53
|
@new_user = Foo::User.new(:fullname => 'Mayonegg')
|
54
54
|
lambda { @new_user.unknown_method_for_a_user }.should raise_error(NoMethodError)
|
55
55
|
expect { @new_user.fullname }.to_not raise_error(NoMethodError)
|
56
|
-
end
|
56
|
+
end
|
57
57
|
|
58
|
-
it "handles method missing for setter" do
|
58
|
+
it "handles method missing for setter" do
|
59
59
|
@new_user = Foo::User.new
|
60
60
|
expect { @new_user.fullname = "Tobias Fünke" }.to_not raise_error(NoMethodError)
|
61
|
-
end
|
61
|
+
end
|
62
62
|
|
63
|
-
it "handles method missing for query" do
|
63
|
+
it "handles method missing for query" do
|
64
64
|
@new_user = Foo::User.new
|
65
65
|
expect { @new_user.fullname? }.to_not raise_error(NoMethodError)
|
66
|
-
end
|
66
|
+
end
|
67
67
|
|
68
|
-
it "handles respond_to for getter" do
|
68
|
+
it "handles respond_to for getter" do
|
69
69
|
@new_user = Foo::User.new(:fullname => 'Mayonegg')
|
70
70
|
@new_user.should_not respond_to(:unknown_method_for_a_user)
|
71
71
|
@new_user.should respond_to(:fullname)
|
72
|
-
end
|
72
|
+
end
|
73
73
|
|
74
|
-
it "handles respond_to for setter" do
|
74
|
+
it "handles respond_to for setter" do
|
75
75
|
@new_user = Foo::User.new
|
76
76
|
@new_user.should respond_to(:fullname=)
|
77
|
-
end
|
77
|
+
end
|
78
78
|
|
79
|
-
it "handles respond_to for query" do
|
79
|
+
it "handles respond_to for query" do
|
80
80
|
@new_user = Foo::User.new
|
81
81
|
@new_user.should respond_to(:fullname?)
|
82
|
-
end
|
82
|
+
end
|
83
|
+
|
84
|
+
it "handles has_key for getter" do
|
85
|
+
@new_user = Foo::User.new(:fullname => 'Mayonegg')
|
86
|
+
@new_user.should_not have_key(:unknown_method_for_a_user)
|
87
|
+
@new_user.should have_key(:fullname)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "handles [] for getter" do
|
91
|
+
@new_user = Foo::User.new(:fullname => 'Mayonegg')
|
92
|
+
@new_user[:unknown_method_for_a_user].should be_nil
|
93
|
+
@new_user[:fullname].should == 'Mayonegg'
|
94
|
+
end
|
83
95
|
end
|
84
96
|
|
85
97
|
context "mapping data, metadata and error data to Ruby objects" do
|
86
|
-
before do
|
98
|
+
before do
|
87
99
|
api = Her::API.new
|
88
100
|
api.setup :url => "https://api.example.com" do |builder|
|
89
101
|
builder.use Her::Middleware::SecondLevelParseJSON
|
@@ -97,32 +109,32 @@ describe Her::Model::ORM do
|
|
97
109
|
spawn_model :User do
|
98
110
|
uses_api api
|
99
111
|
end
|
100
|
-
end
|
112
|
+
end
|
101
113
|
|
102
|
-
it "handles metadata on a collection" do
|
114
|
+
it "handles metadata on a collection" do
|
103
115
|
@users = User.all
|
104
116
|
@users.metadata[:total_pages].should == 10
|
105
|
-
end
|
117
|
+
end
|
106
118
|
|
107
|
-
it "handles error data on a collection" do
|
119
|
+
it "handles error data on a collection" do
|
108
120
|
@users = User.all
|
109
121
|
@users.errors.length.should == 3
|
110
|
-
end
|
122
|
+
end
|
111
123
|
|
112
|
-
it "handles metadata on a resource" do
|
124
|
+
it "handles metadata on a resource" do
|
113
125
|
@user = User.create(:name => "George Michael Bluth")
|
114
126
|
@user.metadata[:foo].should == "bar"
|
115
|
-
end
|
127
|
+
end
|
116
128
|
|
117
|
-
it "handles error data on a resource" do
|
129
|
+
it "handles error data on a resource" do
|
118
130
|
@user = User.create(:name => "George Michael Bluth")
|
119
131
|
@user.errors.should == ["Yes", "Sir"]
|
120
132
|
@user.should be_invalid
|
121
|
-
end
|
133
|
+
end
|
122
134
|
end
|
123
135
|
|
124
136
|
context "defining custom getters and setters" do
|
125
|
-
before do
|
137
|
+
before do
|
126
138
|
api = Her::API.new
|
127
139
|
api.setup :url => "https://api.example.com" do |builder|
|
128
140
|
builder.use Her::Middleware::FirstLevelParseJSON
|
@@ -151,33 +163,33 @@ describe Her::Model::ORM do
|
|
151
163
|
@data[:organization] = { :foo => :bar }
|
152
164
|
end
|
153
165
|
end
|
154
|
-
end
|
166
|
+
end
|
155
167
|
|
156
|
-
it "handles custom setters" do
|
168
|
+
it "handles custom setters" do
|
157
169
|
@user = User.find(1)
|
158
170
|
@user.friends.should == "* Maeby\n* GOB\n* Anne"
|
159
171
|
@user.instance_eval do
|
160
172
|
@data[:friends] = ["Maeby", "GOB", "Anne"]
|
161
173
|
end
|
162
|
-
end
|
174
|
+
end
|
163
175
|
|
164
|
-
it "handles custom setters with relationships" do
|
176
|
+
it "handles custom setters with relationships" do
|
165
177
|
@user = User.find(2)
|
166
178
|
@user.organization.should == { :foo => :bar }
|
167
|
-
end
|
179
|
+
end
|
168
180
|
|
169
|
-
it "handles custom getters" do
|
181
|
+
it "handles custom getters" do
|
170
182
|
@user = User.new
|
171
183
|
@user.friends = "* George\n* Oscar\n* Lucille"
|
172
184
|
@user.friends.should == "* George\n* Oscar\n* Lucille"
|
173
185
|
@user.instance_eval do
|
174
186
|
@data[:friends] = ["George", "Oscar", "Lucille"]
|
175
187
|
end
|
176
|
-
end
|
188
|
+
end
|
177
189
|
end
|
178
190
|
|
179
191
|
context "finding resources" do
|
180
|
-
before do
|
192
|
+
before do
|
181
193
|
api = Her::API.new
|
182
194
|
api.setup :url => "https://api.example.com" do |builder|
|
183
195
|
builder.use Her::Middleware::FirstLevelParseJSON
|
@@ -192,20 +204,20 @@ describe Her::Model::ORM do
|
|
192
204
|
spawn_model :User do
|
193
205
|
uses_api api
|
194
206
|
end
|
195
|
-
end
|
207
|
+
end
|
196
208
|
|
197
|
-
it "handles finding by a single id" do
|
209
|
+
it "handles finding by a single id" do
|
198
210
|
@user = User.find(1)
|
199
211
|
@user.id.should == 1
|
200
|
-
end
|
212
|
+
end
|
201
213
|
|
202
|
-
it "handles finding by multiple ids" do
|
214
|
+
it "handles finding by multiple ids" do
|
203
215
|
@users = User.find(1, 2)
|
204
216
|
@users.should be_kind_of(Array)
|
205
217
|
@users.length.should == 2
|
206
218
|
@users[0].id.should == 1
|
207
219
|
@users[1].id.should == 2
|
208
|
-
end
|
220
|
+
end
|
209
221
|
|
210
222
|
it "handles finding by an array of ids" do
|
211
223
|
@users = User.find([1, 2])
|
@@ -222,41 +234,48 @@ describe Her::Model::ORM do
|
|
222
234
|
@users[0].id.should == 1
|
223
235
|
end
|
224
236
|
|
225
|
-
it "handles finding with other parameters" do
|
237
|
+
it "handles finding with other parameters" do
|
226
238
|
@users = User.all(:age => 42)
|
227
239
|
@users.should be_kind_of(Array)
|
228
240
|
@users.should be_all { |u| u.age == 42 }
|
229
|
-
end
|
241
|
+
end
|
230
242
|
end
|
231
243
|
|
232
244
|
context "creating resources" do
|
233
|
-
before do
|
245
|
+
before do
|
234
246
|
Her::API.setup :url => "https://api.example.com" do |builder|
|
235
247
|
builder.use Her::Middleware::FirstLevelParseJSON
|
236
248
|
builder.use Faraday::Request::UrlEncoded
|
237
249
|
builder.adapter :test do |stub|
|
238
250
|
stub.post("/users") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
|
251
|
+
stub.post("/companies") { |env| [200, {}, { :errors => ["name is required"] }.to_json] }
|
239
252
|
end
|
240
253
|
end
|
241
254
|
|
242
255
|
spawn_model "Foo::User"
|
243
|
-
|
256
|
+
spawn_model "Foo::Company"
|
257
|
+
end
|
244
258
|
|
245
|
-
it "handle one-line resource creation" do
|
259
|
+
it "handle one-line resource creation" do
|
246
260
|
@user = Foo::User.create(:fullname => "Tobias Fünke")
|
247
261
|
@user.id.should == 1
|
248
262
|
@user.fullname.should == "Tobias Fünke"
|
249
|
-
end
|
263
|
+
end
|
250
264
|
|
251
|
-
it "handle resource creation through Model.new + #save" do
|
265
|
+
it "handle resource creation through Model.new + #save" do
|
252
266
|
@user = Foo::User.new(:fullname => "Tobias Fünke")
|
253
|
-
@user.save
|
267
|
+
@user.save.should be_true
|
254
268
|
@user.fullname.should == "Tobias Fünke"
|
255
|
-
end
|
269
|
+
end
|
270
|
+
|
271
|
+
it "returns false when #save gets errors" do
|
272
|
+
@company = Foo::Company.new
|
273
|
+
@company.save.should be_false
|
274
|
+
end
|
256
275
|
end
|
257
276
|
|
258
277
|
context "updating resources" do
|
259
|
-
before do
|
278
|
+
before do
|
260
279
|
Her::API.setup :url => "https://api.example.com" do |builder|
|
261
280
|
builder.use Her::Middleware::FirstLevelParseJSON
|
262
281
|
builder.use Faraday::Request::UrlEncoded
|
@@ -267,30 +286,30 @@ describe Her::Model::ORM do
|
|
267
286
|
end
|
268
287
|
|
269
288
|
spawn_model "Foo::User"
|
270
|
-
end
|
289
|
+
end
|
271
290
|
|
272
|
-
it "handle resource data update without saving it" do
|
291
|
+
it "handle resource data update without saving it" do
|
273
292
|
@user = Foo::User.find(1)
|
274
293
|
@user.fullname.should == "Tobias Fünke"
|
275
294
|
@user.fullname = "Kittie Sanchez"
|
276
295
|
@user.fullname.should == "Kittie Sanchez"
|
277
|
-
end
|
296
|
+
end
|
278
297
|
|
279
|
-
it "handle resource update through the .update class method" do
|
298
|
+
it "handle resource update through the .update class method" do
|
280
299
|
@user = Foo::User.save_existing(1, { :fullname => "Lindsay Fünke" })
|
281
300
|
@user.fullname.should == "Lindsay Fünke"
|
282
|
-
end
|
301
|
+
end
|
283
302
|
|
284
|
-
it "handle resource update through #save on an existing resource" do
|
303
|
+
it "handle resource update through #save on an existing resource" do
|
285
304
|
@user = Foo::User.find(1)
|
286
305
|
@user.fullname = "Lindsay Fünke"
|
287
306
|
@user.save
|
288
307
|
@user.fullname.should == "Lindsay Fünke"
|
289
|
-
end
|
308
|
+
end
|
290
309
|
end
|
291
310
|
|
292
311
|
context "deleting resources" do
|
293
|
-
before do
|
312
|
+
before do
|
294
313
|
Her::API.setup :url => "https://api.example.com" do |builder|
|
295
314
|
builder.use Her::Middleware::FirstLevelParseJSON
|
296
315
|
builder.use Faraday::Request::UrlEncoded
|
@@ -301,22 +320,22 @@ describe Her::Model::ORM do
|
|
301
320
|
end
|
302
321
|
|
303
322
|
spawn_model "Foo::User"
|
304
|
-
end
|
323
|
+
end
|
305
324
|
|
306
|
-
it "handle resource deletion through the .destroy class method" do
|
325
|
+
it "handle resource deletion through the .destroy class method" do
|
307
326
|
@user = Foo::User.destroy_existing(1)
|
308
327
|
@user.active.should be_false
|
309
|
-
end
|
328
|
+
end
|
310
329
|
|
311
|
-
it "handle resource deletion through #destroy on an existing resource" do
|
330
|
+
it "handle resource deletion through #destroy on an existing resource" do
|
312
331
|
@user = Foo::User.find(1)
|
313
332
|
@user.destroy
|
314
333
|
@user.active.should be_false
|
315
|
-
end
|
334
|
+
end
|
316
335
|
end
|
317
336
|
|
318
337
|
context "saving resources with overridden to_params" do
|
319
|
-
before do
|
338
|
+
before do
|
320
339
|
Her::API.setup :url => "https://api.example.com" do |builder|
|
321
340
|
builder.use Her::Middleware::FirstLevelParseJSON
|
322
341
|
builder.use Faraday::Request::UrlEncoded
|
@@ -329,29 +348,29 @@ describe Her::Model::ORM do
|
|
329
348
|
[200, {}, body]
|
330
349
|
end
|
331
350
|
end
|
332
|
-
end
|
351
|
+
end
|
333
352
|
|
334
353
|
spawn_model "Foo::User" do
|
335
354
|
def to_params
|
336
355
|
{ :fullname => "Lindsay Fünke" }
|
337
356
|
end
|
338
357
|
end
|
339
|
-
end
|
358
|
+
end
|
340
359
|
|
341
|
-
it "changes the request parameters for one-line resource creation" do
|
360
|
+
it "changes the request parameters for one-line resource creation" do
|
342
361
|
@user = Foo::User.create(:fullname => "Tobias Fünke")
|
343
362
|
@user.fullname.should == "Lindsay Fünke"
|
344
|
-
end
|
363
|
+
end
|
345
364
|
|
346
|
-
it "changes the request parameters for Model.new + #save" do
|
365
|
+
it "changes the request parameters for Model.new + #save" do
|
347
366
|
@user = Foo::User.new(:fullname => "Tobias Fünke")
|
348
367
|
@user.save
|
349
368
|
@user.fullname.should == "Lindsay Fünke"
|
350
|
-
end
|
369
|
+
end
|
351
370
|
end
|
352
371
|
|
353
372
|
context "checking resource equality" do
|
354
|
-
before do
|
373
|
+
before do
|
355
374
|
Her::API.setup :url => "https://api.example.com" do |builder|
|
356
375
|
builder.use Her::Middleware::FirstLevelParseJSON
|
357
376
|
builder.use Faraday::Request::UrlEncoded
|
@@ -364,52 +383,52 @@ describe Her::Model::ORM do
|
|
364
383
|
|
365
384
|
spawn_model "Foo::User"
|
366
385
|
spawn_model "Foo::Admin"
|
367
|
-
end
|
386
|
+
end
|
368
387
|
|
369
388
|
let(:user) { Foo::User.find(1) }
|
370
389
|
|
371
|
-
it "returns true for the exact same object" do
|
390
|
+
it "returns true for the exact same object" do
|
372
391
|
user.should == user
|
373
|
-
end
|
392
|
+
end
|
374
393
|
|
375
|
-
it "returns true for the same resource via find" do
|
394
|
+
it "returns true for the same resource via find" do
|
376
395
|
user.should == Foo::User.find(1)
|
377
|
-
end
|
396
|
+
end
|
378
397
|
|
379
|
-
it "returns true for the same class with identical data" do
|
398
|
+
it "returns true for the same class with identical data" do
|
380
399
|
user.should == Foo::User.new(:id => 1, :fullname => "Lindsay Fünke")
|
381
|
-
end
|
400
|
+
end
|
382
401
|
|
383
|
-
it "returns true for a different resource with the same data" do
|
402
|
+
it "returns true for a different resource with the same data" do
|
384
403
|
user.should == Foo::Admin.find(1)
|
385
|
-
end
|
404
|
+
end
|
386
405
|
|
387
|
-
it "returns false for the same class with different data" do
|
406
|
+
it "returns false for the same class with different data" do
|
388
407
|
user.should_not == Foo::User.new(:id => 2, :fullname => "Tobias Fünke")
|
389
|
-
end
|
408
|
+
end
|
390
409
|
|
391
|
-
it "returns false for a non-resource with the same data" do
|
410
|
+
it "returns false for a non-resource with the same data" do
|
392
411
|
fake_user = stub(:data => { :id => 1, :fullname => "Lindsay Fünke" })
|
393
412
|
user.should_not == fake_user
|
394
|
-
end
|
413
|
+
end
|
395
414
|
|
396
|
-
it "delegates eql? to ==" do
|
415
|
+
it "delegates eql? to ==" do
|
397
416
|
other = Object.new
|
398
417
|
user.expects(:==).with(other).returns(true)
|
399
418
|
user.eql?(other).should be_true
|
400
|
-
end
|
419
|
+
end
|
401
420
|
|
402
|
-
it "treats equal resources as equal for Array#uniq" do
|
421
|
+
it "treats equal resources as equal for Array#uniq" do
|
403
422
|
user2 = Foo::User.find(1)
|
404
423
|
[user, user2].uniq.should == [user]
|
405
|
-
end
|
424
|
+
end
|
406
425
|
|
407
|
-
it "treats equal resources as equal for hash keys" do
|
426
|
+
it "treats equal resources as equal for hash keys" do
|
408
427
|
Foo::User.find(1)
|
409
428
|
hash = { user => true }
|
410
429
|
hash[Foo::User.find(1)] = false
|
411
430
|
hash.size.should == 1
|
412
431
|
hash.should == { user => false }
|
413
|
-
end
|
432
|
+
end
|
414
433
|
end
|
415
434
|
end
|