api_resource 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +37 -0
- data/Gemfile.lock +190 -0
- data/Guardfile +27 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/api_resource.gemspec +111 -0
- data/coverage/assets/0.5.3/app.js +88 -0
- data/coverage/assets/0.5.3/fancybox/blank.gif +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_close.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_loading.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_nav_left.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_nav_right.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_e.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_n.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_ne.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_nw.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_s.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_se.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_sw.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_shadow_w.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_left.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_main.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_over.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancy_title_right.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancybox-x.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancybox-y.png +0 -0
- data/coverage/assets/0.5.3/fancybox/fancybox.png +0 -0
- data/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.css +363 -0
- data/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.pack.js +44 -0
- data/coverage/assets/0.5.3/favicon_green.png +0 -0
- data/coverage/assets/0.5.3/favicon_red.png +0 -0
- data/coverage/assets/0.5.3/favicon_yellow.png +0 -0
- data/coverage/assets/0.5.3/highlight.css +129 -0
- data/coverage/assets/0.5.3/highlight.pack.js +1 -0
- data/coverage/assets/0.5.3/jquery-1.6.2.min.js +18 -0
- data/coverage/assets/0.5.3/jquery.dataTables.min.js +152 -0
- data/coverage/assets/0.5.3/jquery.timeago.js +141 -0
- data/coverage/assets/0.5.3/jquery.url.js +174 -0
- data/coverage/assets/0.5.3/loading.gif +0 -0
- data/coverage/assets/0.5.3/magnify.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/assets/0.5.3/smoothness/jquery-ui-1.8.4.custom.css +295 -0
- data/coverage/assets/0.5.3/stylesheet.css +383 -0
- data/coverage/index.html +3573 -0
- data/lib/api_resource.rb +130 -0
- data/lib/api_resource/association_activation.rb +19 -0
- data/lib/api_resource/associations.rb +218 -0
- data/lib/api_resource/associations/association_proxy.rb +116 -0
- data/lib/api_resource/associations/belongs_to_remote_object_proxy.rb +16 -0
- data/lib/api_resource/associations/dynamic_resource_scope.rb +23 -0
- data/lib/api_resource/associations/generic_scope.rb +68 -0
- data/lib/api_resource/associations/has_many_remote_object_proxy.rb +16 -0
- data/lib/api_resource/associations/has_many_through_remote_object_proxy.rb +13 -0
- data/lib/api_resource/associations/has_one_remote_object_proxy.rb +24 -0
- data/lib/api_resource/associations/multi_argument_resource_scope.rb +15 -0
- data/lib/api_resource/associations/multi_object_proxy.rb +84 -0
- data/lib/api_resource/associations/related_object_hash.rb +12 -0
- data/lib/api_resource/associations/relation_scope.rb +25 -0
- data/lib/api_resource/associations/resource_scope.rb +32 -0
- data/lib/api_resource/associations/scope.rb +132 -0
- data/lib/api_resource/associations/single_object_proxy.rb +82 -0
- data/lib/api_resource/attributes.rb +243 -0
- data/lib/api_resource/base.rb +717 -0
- data/lib/api_resource/callbacks.rb +45 -0
- data/lib/api_resource/connection.rb +195 -0
- data/lib/api_resource/core_extensions.rb +7 -0
- data/lib/api_resource/custom_methods.rb +117 -0
- data/lib/api_resource/decorators.rb +6 -0
- data/lib/api_resource/decorators/caching_decorator.rb +20 -0
- data/lib/api_resource/exceptions.rb +99 -0
- data/lib/api_resource/formats.rb +22 -0
- data/lib/api_resource/formats/json_format.rb +25 -0
- data/lib/api_resource/formats/xml_format.rb +36 -0
- data/lib/api_resource/local.rb +12 -0
- data/lib/api_resource/log_subscriber.rb +15 -0
- data/lib/api_resource/mocks.rb +285 -0
- data/lib/api_resource/model_errors.rb +82 -0
- data/lib/api_resource/observing.rb +27 -0
- data/lib/api_resource/railtie.rb +24 -0
- data/lib/api_resource/scopes.rb +48 -0
- data/nohup.out +63 -0
- data/spec/lib/api_resource_spec.rb +43 -0
- data/spec/lib/associations_spec.rb +751 -0
- data/spec/lib/attributes_spec.rb +191 -0
- data/spec/lib/base_spec.rb +655 -0
- data/spec/lib/callbacks_spec.rb +68 -0
- data/spec/lib/connection_spec.rb +137 -0
- data/spec/lib/local_spec.rb +20 -0
- data/spec/lib/mocks_spec.rb +74 -0
- data/spec/lib/model_errors_spec.rb +29 -0
- data/spec/lib/prefixes_spec.rb +107 -0
- data/spec/spec_helper.rb +82 -0
- data/spec/support/mocks/association_mocks.rb +63 -0
- data/spec/support/mocks/error_resource_mocks.rb +21 -0
- data/spec/support/mocks/prefix_model_mocks.rb +5 -0
- data/spec/support/mocks/test_resource_mocks.rb +44 -0
- data/spec/support/requests/association_requests.rb +31 -0
- data/spec/support/requests/error_resource_requests.rb +25 -0
- data/spec/support/requests/prefix_model_requests.rb +7 -0
- data/spec/support/requests/test_resource_requests.rb +38 -0
- data/spec/support/test_resource.rb +72 -0
- data/spec/tmp/DIR +0 -0
- data/spec/tmp/api_resource_test_db.sqlite +0 -0
- metadata +119 -3
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
include ApiResource
|
4
|
+
|
5
|
+
describe "Should put callbacks around save, create, update, and destroy by default" do
|
6
|
+
|
7
|
+
before(:all) do
|
8
|
+
# This defines all the callbacks to check and see if they are fired
|
9
|
+
TestResource.class_eval <<-EOE, __FILE__, __LINE__ + 1
|
10
|
+
attr_accessor :s_val, :c_val, :u_val, :d_val
|
11
|
+
before_save :bs_cb; after_save :as_cb
|
12
|
+
before_create :bc_cb; after_create :ac_cb
|
13
|
+
before_update :bu_cb; after_update :au_cb
|
14
|
+
before_destroy :bd_cb; after_destroy :ad_cb
|
15
|
+
|
16
|
+
def bs_cb
|
17
|
+
@s_val = 1
|
18
|
+
end
|
19
|
+
def as_cb
|
20
|
+
@s_val += 1
|
21
|
+
end
|
22
|
+
def bc_cb
|
23
|
+
@c_val = 1
|
24
|
+
end
|
25
|
+
def ac_cb
|
26
|
+
@c_val += 1
|
27
|
+
end
|
28
|
+
def bu_cb
|
29
|
+
@u_val = 1
|
30
|
+
end
|
31
|
+
def au_cb
|
32
|
+
@u_val += 1
|
33
|
+
end
|
34
|
+
def bd_cb
|
35
|
+
@d_val = 1
|
36
|
+
end
|
37
|
+
def ad_cb
|
38
|
+
@d_val += 1
|
39
|
+
end
|
40
|
+
EOE
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should fire save and create callbacks when saving a new record" do
|
44
|
+
tr = TestResource.new(:name => "Ethan", :age => 20)
|
45
|
+
tr.save.should be_true
|
46
|
+
tr.s_val.should eql(2)
|
47
|
+
tr.c_val.should eql(2)
|
48
|
+
tr.u_val.should be_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should fire save and update callbacks when updating a record" do
|
52
|
+
tr = TestResource.new(:id => 1, :name => "Ethan", :age => 20)
|
53
|
+
tr.name = "Test"
|
54
|
+
tr.age = 21
|
55
|
+
tr.save.should be_true
|
56
|
+
tr.s_val.should eql(2)
|
57
|
+
tr.c_val.should be_nil
|
58
|
+
tr.u_val.should eql(2)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should only fire destroy callbacks when destroying a record" do
|
62
|
+
tr = TestResource.new(:id => 1, :name => "Ethan", :age => 20)
|
63
|
+
tr.destroy.should be_true
|
64
|
+
tr.d_val.should eql(2)
|
65
|
+
tr.s_val.should be_nil
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
include ApiResource
|
4
|
+
|
5
|
+
describe Connection do
|
6
|
+
|
7
|
+
it "should be able to set the token directly on ApiResource" do
|
8
|
+
ApiResource.token = "123"
|
9
|
+
ApiResource::Base.token.should eql "123"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should be able to set a default token value, which is passed through each request" do
|
13
|
+
ApiResource::Mocks::Connection.expects(:get).with("/test_resources/1.json", {"Accept"=>"application/json", "Lifebooker-Token" => "abc"}).returns(ApiResource::Mocks::MockResponse.new({}))
|
14
|
+
ApiResource::Base.token = "abc"
|
15
|
+
TestResource.find(1)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should set the Lifebooker-Token if one is present for GET requests" do
|
19
|
+
token = Kernel.rand(100000).to_s
|
20
|
+
ApiResource::Mocks::Connection.expects(:get).with("/test_resources/1.json", {"Accept"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
|
21
|
+
|
22
|
+
ApiResource::Base.token = token
|
23
|
+
|
24
|
+
TestResource.connection.get("/test_resources/1.json")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should set the Lifebooker-Token if one is present for DELETE requests" do
|
28
|
+
token = Kernel.rand(100000).to_s
|
29
|
+
ApiResource::Mocks::Connection.expects(:delete).with("/test_resources/1.json", {"Accept"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
|
30
|
+
|
31
|
+
ApiResource::Base.token = token
|
32
|
+
|
33
|
+
TestResource.connection.delete("/test_resources/1.json")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should set the Lifebooker-Token if one is present for :head requests" do
|
37
|
+
token = Kernel.rand(100000).to_s
|
38
|
+
ApiResource::Mocks::Connection.expects(:head).with("/test_resources/1.json", {"Accept"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
|
39
|
+
|
40
|
+
ApiResource::Base.token = token
|
41
|
+
|
42
|
+
TestResource.connection.head("/test_resources/1.json")
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should set the Lifebooker-Token if one is present for POST requests" do
|
46
|
+
token = Kernel.rand(100000).to_s
|
47
|
+
ApiResource::Mocks::Connection.expects(:post).with("/test_resources/1.json", {}, {"Content-Type"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
|
48
|
+
|
49
|
+
ApiResource::Base.token = token
|
50
|
+
|
51
|
+
TestResource.connection.post("/test_resources/1.json")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should set the Lifebooker-Token if one is present for PUT requests" do
|
55
|
+
token = Kernel.rand(100000).to_s
|
56
|
+
ApiResource::Mocks::Connection.expects(:put).with("/test_resources/1.json", {}, {"Content-Type"=>"application/json", 'Lifebooker-Token' => "#{token}"}).returns(ApiResource::Mocks::MockResponse.new({}))
|
57
|
+
|
58
|
+
ApiResource::Base.token = token
|
59
|
+
|
60
|
+
TestResource.connection.put("/test_resources/1.json")
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should set its headers upon initialization" do
|
64
|
+
token = Kernel.rand(100000).to_s
|
65
|
+
ApiResource::Base.token = token
|
66
|
+
|
67
|
+
TestResource.connection.headers.include?("Lifebooker-Token").should eql true
|
68
|
+
TestResource.connection.headers["Lifebooker-Token"] = token
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should reset headers upon initialization" do
|
72
|
+
token = Kernel.rand(100000).to_s
|
73
|
+
ApiResource::Base.token = token
|
74
|
+
|
75
|
+
TestResource.connection.headers.include?("Lifebooker-Token").should eql true
|
76
|
+
TestResource.connection.headers["Lifebooker-Token"] = token
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should be able to set a token for a given block" do
|
80
|
+
ApiResource::Base.token = "123456"
|
81
|
+
begin
|
82
|
+
ApiResource.with_token("testing") do
|
83
|
+
ApiResource::Base.token.should eql "testing"
|
84
|
+
raise "AAAH"
|
85
|
+
end
|
86
|
+
rescue => e
|
87
|
+
# should still reset the token
|
88
|
+
end
|
89
|
+
ApiResource::Base.token.should eql "123456"
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should provider a method to regenerate its connection" do
|
93
|
+
conn = ApiResource::Base.connection
|
94
|
+
conn.should be ApiResource::Base.connection
|
95
|
+
ApiResource.reset_connection
|
96
|
+
conn.should_not be ApiResource::Base.connection
|
97
|
+
end
|
98
|
+
|
99
|
+
context "No Mocks" do
|
100
|
+
before(:all) do
|
101
|
+
ApiResource::Mocks.remove
|
102
|
+
end
|
103
|
+
after(:all) do
|
104
|
+
ApiResource::Mocks.init
|
105
|
+
ApiResource.timeout = 10
|
106
|
+
ApiResource.open_timeout = 10
|
107
|
+
end
|
108
|
+
it "should be able to set a timeout for its connection" do
|
109
|
+
ApiResource.timeout = 1
|
110
|
+
ApiResource.timeout.should eql 1
|
111
|
+
ApiResource.open_timeout = 1
|
112
|
+
ApiResource.open_timeout.should eql 1
|
113
|
+
|
114
|
+
ApiResource::Base.connection.send(:http, "/test").options[:timeout].should eql 1
|
115
|
+
ApiResource::Base.connection.send(:http, "/test").options[:open_timeout].should eql 1
|
116
|
+
|
117
|
+
ApiResource.timeout = 100
|
118
|
+
ApiResource::Base.connection.send(:http, "/test").options[:timeout].should eql 100
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should time out if RestClient takes too long" do
|
123
|
+
|
124
|
+
# hopefully google won't actually respond this fast :)
|
125
|
+
ApiResource.timeout = 0.001
|
126
|
+
ApiResource::Base.site = "http://www.google.com"
|
127
|
+
lambda{
|
128
|
+
ApiResource::Base.connection.get("/")
|
129
|
+
}.should raise_error(ApiResource::RequestTimeout)
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
include ApiResource
|
5
|
+
|
6
|
+
describe "Local" do
|
7
|
+
|
8
|
+
it "should not go to the server to fetch a resource definition" do
|
9
|
+
ApiResource::Connection.any_instance.expects(:get).never
|
10
|
+
class MyTestResource < ApiResource::Local
|
11
|
+
scope :test, {:test => true}
|
12
|
+
end
|
13
|
+
mtr = MyTestResource.new
|
14
|
+
# should still have scopes
|
15
|
+
MyTestResource.reload_class_attributes
|
16
|
+
mtr.scopes.should_not be_blank
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
include ApiResource
|
5
|
+
|
6
|
+
describe Mocks, :focus do
|
7
|
+
|
8
|
+
# we set up the mocks in spec helper, so we can just assert this
|
9
|
+
it "should hijack the connection" do
|
10
|
+
ApiResource::Mocks::Interface.any_instance.expects(:get).once.returns(
|
11
|
+
ApiResource::Mocks::MockResponse.new({}, {:headers => {"Content-type" => "application/json"}, :status_code => 200})
|
12
|
+
)
|
13
|
+
TestResource.reload_class_attributes
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should allow the user to raise errors for invalid responsed" do
|
17
|
+
old_err_status = ApiResource.raise_missing_definition_error
|
18
|
+
ApiResource::Base.raise_missing_definition_error = true
|
19
|
+
|
20
|
+
lambda {
|
21
|
+
class MyNewInvalidResource < ApiResource::Base; end
|
22
|
+
MyNewInvalidResource.new
|
23
|
+
}.should raise_error(ApiResource::ResourceNotFound)
|
24
|
+
|
25
|
+
ApiResource.raise_missing_definition_error = old_err_status
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should merge params for a request" do
|
29
|
+
resp = ApiResource::Base.connection.get(
|
30
|
+
"/mock_with_block/1?#{{:test => "abc"}.to_query}"
|
31
|
+
)
|
32
|
+
resp["id"].should eql "1"
|
33
|
+
resp["test"].should eql "abc"
|
34
|
+
end
|
35
|
+
|
36
|
+
context "Mock Request" do
|
37
|
+
context "Initialize" do
|
38
|
+
it "should correctly assign blank params" do
|
39
|
+
request = ApiResource::Mocks::MockRequest.new(:get, "/authenticate.json?token=")
|
40
|
+
request.params.should eql({"token" => ""})
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should handle nested params" do
|
44
|
+
params = {
|
45
|
+
"a" => {
|
46
|
+
"b" => ["c", "d"]
|
47
|
+
}
|
48
|
+
}
|
49
|
+
request = ApiResource::Mocks::MockRequest.new(
|
50
|
+
:get, "/authenticate.json?#{params.to_query}"
|
51
|
+
)
|
52
|
+
|
53
|
+
request.params.should eql(params)
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should parse true and false values into booleans" do
|
58
|
+
|
59
|
+
params = {
|
60
|
+
"a" => true,
|
61
|
+
"b" => false
|
62
|
+
}
|
63
|
+
request = ApiResource::Mocks::MockRequest.new(
|
64
|
+
:get, "/authenticate.json?#{params.to_query}"
|
65
|
+
)
|
66
|
+
|
67
|
+
request.params.should eql(params)
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
include ApiResource
|
4
|
+
|
5
|
+
describe "Saving Resources with errors" do
|
6
|
+
|
7
|
+
before(:all) do
|
8
|
+
ErrorResource.include_root_in_json = true
|
9
|
+
end
|
10
|
+
|
11
|
+
context "Remote Errors" do
|
12
|
+
|
13
|
+
it "should be able to handle errors as a hash" do
|
14
|
+
t = ErrorResource.new(:name => "Ethan", :age => 12)
|
15
|
+
t.save.should be_false
|
16
|
+
t.errors.should_not be_nil
|
17
|
+
t.errors['name'].should_not be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be able to handle errors as full messages" do
|
21
|
+
t = ErrorFullMessageResource.new(:name => "Ethan", :age => 12)
|
22
|
+
t.save.should be_false
|
23
|
+
t.errors.should_not be_nil
|
24
|
+
t.errors['name'].should_not be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "With Prefixes" do
|
4
|
+
|
5
|
+
let(:prefix_model) do
|
6
|
+
PrefixModel.new({:foreign_key_id => "123", :name => "test"})
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
PrefixModel.reload_class_attributes
|
11
|
+
end
|
12
|
+
|
13
|
+
context ".find" do
|
14
|
+
|
15
|
+
it "should use the prefix to find a single record when given as a param" do
|
16
|
+
PrefixModel.connection.expects(:get)
|
17
|
+
.with(
|
18
|
+
"/foreign/123/prefix_models/456.json",
|
19
|
+
instance_of(Hash)
|
20
|
+
)
|
21
|
+
.returns({})
|
22
|
+
PrefixModel.find(456, :params => {:foreign_key_id => 123})
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should not use the prefix to find a single record when not given as a param to avoid automatic failure" do
|
26
|
+
PrefixModel.connection.expects(:get)
|
27
|
+
.with(
|
28
|
+
"/prefix_models/456.json",
|
29
|
+
instance_of(Hash)
|
30
|
+
)
|
31
|
+
.returns({})
|
32
|
+
PrefixModel.find(456)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "#create" do
|
37
|
+
|
38
|
+
it "should use the prefix to create a new record" do
|
39
|
+
prefix_model.send(:connection).expects(:post)
|
40
|
+
.with(
|
41
|
+
"/foreign/123/prefix_models.json",
|
42
|
+
{"prefix_model" => {"name" => "test"}}.to_json,
|
43
|
+
instance_of(Hash)
|
44
|
+
)
|
45
|
+
prefix_model.save
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
context "#first" do
|
51
|
+
|
52
|
+
it "should use the prefix to find records" do
|
53
|
+
prefix_model.send(:connection).expects(:get)
|
54
|
+
.with(
|
55
|
+
"/foreign/123/prefix_models.json",
|
56
|
+
instance_of(Hash)
|
57
|
+
)
|
58
|
+
.returns([])
|
59
|
+
PrefixModel.first(:params => {:foreign_key_id => 123})
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not use the prefix to find records when not given as a param to avoid automatic failure" do
|
63
|
+
prefix_model.send(:connection).expects(:get)
|
64
|
+
.with(
|
65
|
+
"/prefix_models.json",
|
66
|
+
instance_of(Hash)
|
67
|
+
)
|
68
|
+
.returns([])
|
69
|
+
PrefixModel.first
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
context "#destroy" do
|
75
|
+
|
76
|
+
it "should use the prefix to destroy a record" do
|
77
|
+
|
78
|
+
prefix_model.id = 456
|
79
|
+
prefix_model.send(:connection).expects(:delete)
|
80
|
+
.with(
|
81
|
+
"/foreign/123/prefix_models/456.json",
|
82
|
+
instance_of(Hash)
|
83
|
+
)
|
84
|
+
prefix_model.destroy
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
context "#update" do
|
91
|
+
|
92
|
+
it "should use the prefix to update a record" do
|
93
|
+
prefix_model.id = 456
|
94
|
+
prefix_model.name = "changed name"
|
95
|
+
prefix_model.send(:connection).expects(:put)
|
96
|
+
.with(
|
97
|
+
"/foreign/123/prefix_models/456.json",
|
98
|
+
{"prefix_model" => {"name" => "changed name"}}.to_json,
|
99
|
+
instance_of(Hash)
|
100
|
+
)
|
101
|
+
prefix_model.save
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spork'
|
3
|
+
#uncomment the following line to use spork with the debugger
|
4
|
+
#require 'spork/ext/ruby-debug'
|
5
|
+
|
6
|
+
Spork.prefork do
|
7
|
+
# Loading more in this block will cause your tests to run faster. However,
|
8
|
+
# if you change any configuration or code from libraries loaded here, you'll
|
9
|
+
# need to restart spork for it take effect.
|
10
|
+
|
11
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
12
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
13
|
+
require 'bundler'
|
14
|
+
require 'api_resource'
|
15
|
+
require 'simplecov'
|
16
|
+
|
17
|
+
|
18
|
+
SimpleCov.start do
|
19
|
+
add_filter "/spec/"
|
20
|
+
end
|
21
|
+
|
22
|
+
SimpleCov.at_exit do
|
23
|
+
SimpleCov.result.format!
|
24
|
+
end
|
25
|
+
|
26
|
+
# Requires supporting files with custom matchers and macros, etc,
|
27
|
+
# in ./support/ and its subdirectories.
|
28
|
+
Bundler.require(:default, :development)
|
29
|
+
Debugger.start
|
30
|
+
|
31
|
+
# Requires supporting files with custom matchers and macros, etc,
|
32
|
+
# in ./support/ and its subdirectories.
|
33
|
+
#ApiResource.load_mocks_and_factories
|
34
|
+
ApiResource.site = 'http://localhost:3000'
|
35
|
+
ApiResource.format = :json
|
36
|
+
ApiResource.load_mocks_and_factories
|
37
|
+
|
38
|
+
ApiResource.logger.level = Log4r::INFO
|
39
|
+
|
40
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
RSpec.configure do |config|
|
45
|
+
config.mock_with :mocha
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
Spork.each_run do
|
51
|
+
# This code will be run each time you run your specs.
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
# --- Instructions ---
|
56
|
+
# Sort the contents of this file into a Spork.prefork and a Spork.each_run
|
57
|
+
# block.
|
58
|
+
#
|
59
|
+
# The Spork.prefork block is run only once when the spork server is started.
|
60
|
+
# You typically want to place most of your (slow) initializer code in here, in
|
61
|
+
# particular, require'ing any 3rd-party gems that you don't normally modify
|
62
|
+
# during development.
|
63
|
+
#
|
64
|
+
# The Spork.each_run block is run each time you run your specs. In case you
|
65
|
+
# need to load files that tend to change during development, require them here.
|
66
|
+
# With Rails, your application modules are loaded automatically, so sometimes
|
67
|
+
# this block can remain empty.
|
68
|
+
#
|
69
|
+
# Note: You can modify files loaded *from* the Spork.each_run block without
|
70
|
+
# restarting the spork server. However, this file itself will not be reloaded,
|
71
|
+
# so if you change any of the code inside the each_run block, you still need to
|
72
|
+
# restart the server. In general, if you have non-trivial code in this file,
|
73
|
+
# it's advisable to move it into a separate file so you can easily edit it
|
74
|
+
# without restarting spork. (For example, with RSpec, you could move
|
75
|
+
# non-trivial code into a file spec/support/my_helper.rb, making sure that the
|
76
|
+
# spec/support/* files are require'd from inside the each_run block.)
|
77
|
+
#
|
78
|
+
# Any code that is left outside the two blocks will be run during preforking
|
79
|
+
# *and* during each_run -- that's probably not what you want.
|
80
|
+
#
|
81
|
+
# These instructions should self-destruct in 10 seconds. If they don't, feel
|
82
|
+
# free to delete them.
|