morpheus 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rvmrc +1 -0
- data/Gemfile +21 -0
- data/Gemfile.lock +134 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +44 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/autotest/discover.rb +7 -0
- data/lib/ext/typhoeus.rb +37 -0
- data/lib/morpheus/associations/association.rb +110 -0
- data/lib/morpheus/associations/belongs_to_association.rb +45 -0
- data/lib/morpheus/associations/has_many_association.rb +70 -0
- data/lib/morpheus/associations/has_one_association.rb +46 -0
- data/lib/morpheus/base.rb +66 -0
- data/lib/morpheus/client/associations.rb +47 -0
- data/lib/morpheus/client/inflections.rb +3 -0
- data/lib/morpheus/client/log_subscriber.rb +64 -0
- data/lib/morpheus/client/railtie.rb +25 -0
- data/lib/morpheus/configuration.rb +49 -0
- data/lib/morpheus/errors.rb +32 -0
- data/lib/morpheus/filter.rb +18 -0
- data/lib/morpheus/mixins/associations.rb +55 -0
- data/lib/morpheus/mixins/attributes.rb +133 -0
- data/lib/morpheus/mixins/conversion.rb +21 -0
- data/lib/morpheus/mixins/filtering.rb +18 -0
- data/lib/morpheus/mixins/finders.rb +58 -0
- data/lib/morpheus/mixins/introspection.rb +25 -0
- data/lib/morpheus/mixins/persistence.rb +46 -0
- data/lib/morpheus/mixins/reflections.rb +24 -0
- data/lib/morpheus/mixins/request_handling.rb +34 -0
- data/lib/morpheus/mixins/response_parsing.rb +27 -0
- data/lib/morpheus/mixins/url_support.rb +36 -0
- data/lib/morpheus/mock.rb +66 -0
- data/lib/morpheus/reflection.rb +22 -0
- data/lib/morpheus/relation.rb +57 -0
- data/lib/morpheus/request.rb +41 -0
- data/lib/morpheus/request_cache.rb +18 -0
- data/lib/morpheus/request_queue.rb +44 -0
- data/lib/morpheus/response.rb +24 -0
- data/lib/morpheus/response_parser.rb +80 -0
- data/lib/morpheus/type_caster.rb +80 -0
- data/lib/morpheus/url_builder.rb +52 -0
- data/lib/morpheus.rb +64 -0
- data/morpheus.gemspec +191 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/purchase.rb +3 -0
- data/spec/dummy/app/resources/attendee.rb +2 -0
- data/spec/dummy/app/resources/author.rb +5 -0
- data/spec/dummy/app/resources/automobile.rb +6 -0
- data/spec/dummy/app/resources/book.rb +5 -0
- data/spec/dummy/app/resources/conference.rb +3 -0
- data/spec/dummy/app/resources/dog.rb +10 -0
- data/spec/dummy/app/resources/item.rb +5 -0
- data/spec/dummy/app/resources/meeting.rb +7 -0
- data/spec/dummy/app/resources/speaker.rb +3 -0
- data/spec/dummy/app/resources/state.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config/application.rb +45 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +22 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +26 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +35 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/morpheus.rb +3 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +58 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/20110605002144_create_purchases.rb +13 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/morpheus/associations/association_spec.rb +44 -0
- data/spec/morpheus/associations/belongs_to_association_spec.rb +5 -0
- data/spec/morpheus/associations/has_many_association_spec.rb +17 -0
- data/spec/morpheus/associations/has_one_association_spec.rb +5 -0
- data/spec/morpheus/base_spec.rb +126 -0
- data/spec/morpheus/client/associations_spec.rb +44 -0
- data/spec/morpheus/configuration_spec.rb +136 -0
- data/spec/morpheus/mixins/associations_spec.rb +141 -0
- data/spec/morpheus/mixins/attributes_spec.rb +99 -0
- data/spec/morpheus/mixins/conversion_spec.rb +76 -0
- data/spec/morpheus/mixins/finders_spec.rb +255 -0
- data/spec/morpheus/mixins/introspection_spec.rb +154 -0
- data/spec/morpheus/mixins/persistence_spec.rb +161 -0
- data/spec/morpheus/mixins/reflection_spec.rb +100 -0
- data/spec/morpheus/mixins/response_parsing_spec.rb +5 -0
- data/spec/morpheus/mock_spec.rb +133 -0
- data/spec/morpheus/relation_spec.rb +71 -0
- data/spec/morpheus/request_cache_spec.rb +5 -0
- data/spec/morpheus/request_spec.rb +5 -0
- data/spec/morpheus/response_spec.rb +73 -0
- data/spec/morpheus/type_caster_spec.rb +343 -0
- data/spec/shared/active_model_lint_test.rb +14 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/support/configuration.rb +26 -0
- metadata +427 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Morpheus::Persistence, "#save" do
|
|
4
|
+
|
|
5
|
+
context "when the instance is a new record" do
|
|
6
|
+
|
|
7
|
+
before(:each) do
|
|
8
|
+
stub_web!
|
|
9
|
+
@california = State.new(
|
|
10
|
+
:name => 'California',
|
|
11
|
+
:capital => 'Sacramento',
|
|
12
|
+
:motto => 'Eureka!'
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "updates the attributes to match those returned in the response" do
|
|
17
|
+
@california.id.should be_nil
|
|
18
|
+
@california.save
|
|
19
|
+
@california.id.should eql(1)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "marks the instance as not new" do
|
|
23
|
+
@california.should be_new_record
|
|
24
|
+
@california.save
|
|
25
|
+
@california.should_not be_new_record
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "sets the errors hash based on the response" do
|
|
29
|
+
@california.save
|
|
30
|
+
@california.errors.should be_empty
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "sets the valid? flag based on the response" do
|
|
34
|
+
@california.save
|
|
35
|
+
@california.should be_valid
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context "when the instance is not a new record" do
|
|
41
|
+
|
|
42
|
+
before(:each) do
|
|
43
|
+
stub_web!
|
|
44
|
+
@state = State.new(
|
|
45
|
+
:name => 'California',
|
|
46
|
+
:capital => 'Sacramento',
|
|
47
|
+
:motto => 'Eureka!'
|
|
48
|
+
)
|
|
49
|
+
@state.save
|
|
50
|
+
|
|
51
|
+
@state.name = 'Oregon'
|
|
52
|
+
@state.capital = 'Salem'
|
|
53
|
+
@state.motto = 'She flies with her own wings'
|
|
54
|
+
@state.save
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'has a new name attribute' do
|
|
58
|
+
@state.name.should eql('Oregon')
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it 'has a new capital attribute' do
|
|
62
|
+
@state.capital.should eql('Salem')
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it 'has a new motto attribute' do
|
|
66
|
+
@state.motto.should eql('She flies with her own wings')
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it 'is still valid' do
|
|
70
|
+
@state.should be_valid
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'still has no errors' do
|
|
74
|
+
@state.errors.should be_empty
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def stub_web!
|
|
80
|
+
Morpheus::Configuration.hydra.stub(:post, "#{Morpheus::Configuration.host}/states").and_return(build_morpheus_response(
|
|
81
|
+
201,
|
|
82
|
+
{
|
|
83
|
+
:id => 1,
|
|
84
|
+
:valid => true,
|
|
85
|
+
:errors => {},
|
|
86
|
+
:name => 'California',
|
|
87
|
+
:capital => 'Sacramento',
|
|
88
|
+
:motto => 'Eureka!'
|
|
89
|
+
}
|
|
90
|
+
))
|
|
91
|
+
|
|
92
|
+
Morpheus::Configuration.hydra.stub(:post, "#{Morpheus::Configuration.host}/states/1").and_return(build_morpheus_response(
|
|
93
|
+
200,
|
|
94
|
+
{
|
|
95
|
+
:id => 1,
|
|
96
|
+
:valid => true,
|
|
97
|
+
:errors => {},
|
|
98
|
+
:name => 'Oregon',
|
|
99
|
+
:capital => 'Salem',
|
|
100
|
+
:motto => 'She flies with her own wings'
|
|
101
|
+
}
|
|
102
|
+
))
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
describe Morpheus::Persistence, "#update_attributes" do
|
|
108
|
+
|
|
109
|
+
before(:each) do
|
|
110
|
+
stub_web!
|
|
111
|
+
@state = State.new(
|
|
112
|
+
:name => "California",
|
|
113
|
+
:capital => "Sacramento",
|
|
114
|
+
:motto => "Eureka!"
|
|
115
|
+
)
|
|
116
|
+
@state.save
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it "sets the attributes to their new values" do
|
|
120
|
+
@state.update_attributes(:name => "Oregon", :capital => "Salem", :motto => "She flies with her own wings")
|
|
121
|
+
@state.name.should eql("Oregon")
|
|
122
|
+
@state.capital.should eql("Salem")
|
|
123
|
+
@state.motto.should eql("She flies with her own wings")
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
it "calls .save on the record" do
|
|
127
|
+
@state.should_receive(:save)
|
|
128
|
+
@state.update_attributes(
|
|
129
|
+
:name => 'Oregon',
|
|
130
|
+
:capital => 'Salem',
|
|
131
|
+
:motto => 'She flies with her own wings'
|
|
132
|
+
)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def stub_web!
|
|
136
|
+
Morpheus::Configuration.hydra.stub(:post, "#{Morpheus::Configuration.host}/states").and_return(build_morpheus_response(
|
|
137
|
+
201,
|
|
138
|
+
{
|
|
139
|
+
:id => 1,
|
|
140
|
+
:valid => true,
|
|
141
|
+
:errors => {},
|
|
142
|
+
:name => 'California',
|
|
143
|
+
:capital => 'Sacramento',
|
|
144
|
+
:motto => 'Eureka!'
|
|
145
|
+
}
|
|
146
|
+
))
|
|
147
|
+
|
|
148
|
+
Morpheus::Configuration.hydra.stub(:post, "#{Morpheus::Configuration.host}/states/1").and_return(build_morpheus_response(
|
|
149
|
+
200,
|
|
150
|
+
{
|
|
151
|
+
:id => 1,
|
|
152
|
+
:valid => true,
|
|
153
|
+
:errors => {},
|
|
154
|
+
:name => 'Oregon',
|
|
155
|
+
:capital => 'Salem',
|
|
156
|
+
:motto => 'She flies with her own wings'
|
|
157
|
+
}
|
|
158
|
+
))
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Morpheus::Reflections, "ClassMethods" do
|
|
4
|
+
context "when the resource has a belongs_to association" do
|
|
5
|
+
before(:each) do
|
|
6
|
+
@meeting = Meeting.new(:id => 1, :conference_id=>2, :conference => {:id=>2, :name=>'A conference', :notes=>'There be notes'})
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "returns the association with the correct type and all nested attributes" do
|
|
10
|
+
@conference = @meeting.conference
|
|
11
|
+
@conference.should be_a(Conference)
|
|
12
|
+
@conference.id.should eql(2)
|
|
13
|
+
@conference.name.should eql('A conference')
|
|
14
|
+
@conference.notes.should eql('There be notes')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "still allows setting the association the old-fashioned way" do
|
|
18
|
+
@conference = Conference.new(:id => 3, :name=>'Different')
|
|
19
|
+
@meeting.conference = @conference
|
|
20
|
+
@meeting.conference_id.should eql(3)
|
|
21
|
+
@conference = @meeting.conference
|
|
22
|
+
@conference.should be_a(Conference)
|
|
23
|
+
@conference.id.should eql(3)
|
|
24
|
+
@conference.name.should eql('Different')
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'allows setting with actual class objects in an initial hash' do
|
|
28
|
+
@conference = Conference.new(:id => 4, :name=>'Another')
|
|
29
|
+
@meeting2 = Meeting.new(:id => 2, :conference => @conference)
|
|
30
|
+
@meeting2.conference_id.should eql(4)
|
|
31
|
+
@conference = @meeting2.conference
|
|
32
|
+
@conference.should be_a(Conference)
|
|
33
|
+
@conference.id.should eql(4)
|
|
34
|
+
@conference.name.should eql('Another')
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
context "when the resource has a has_one association" do
|
|
39
|
+
before(:each) do
|
|
40
|
+
@meeting = Meeting.new(:id => 1, :speaker => {:id=>22, :name=>'Speaking'})
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "returns the association with the correct type and all nested attributes" do
|
|
44
|
+
@speaker = @meeting.speaker
|
|
45
|
+
@speaker.should be_a(Speaker)
|
|
46
|
+
@speaker.id.should eql(22)
|
|
47
|
+
@speaker.name.should eql('Speaking')
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "still allows setting the association the old-fashioned way" do
|
|
51
|
+
@speaker = Speaker.new({:id=>30, :name=>'Speaking up'})
|
|
52
|
+
@meeting.speaker = @speaker
|
|
53
|
+
@speaker = @meeting.speaker
|
|
54
|
+
@speaker.should be_a(Speaker)
|
|
55
|
+
@speaker.id.should eql(30)
|
|
56
|
+
@speaker.name.should eql('Speaking up')
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it 'allows setting with actual class objects in an initial hash' do
|
|
60
|
+
@speaker = Speaker.new({:id=>40, :name=>'Speaking out'})
|
|
61
|
+
@meeting2 = Meeting.new(:id => 3, :speaker => @speaker)
|
|
62
|
+
@speaker = @meeting2.speaker
|
|
63
|
+
@speaker.should be_a(Speaker)
|
|
64
|
+
@speaker.id.should eql(40)
|
|
65
|
+
@speaker.name.should eql('Speaking out')
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
context "when the resource has a has_many association" do
|
|
70
|
+
before(:each) do
|
|
71
|
+
@meeting = Meeting.new(:id => 1, :attendees => [{:id=>1, :name=>'First'}, {:id=>2, :name=>'Second'}, {:id=>7, :name=>'Last'}])
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "returns the association with the correct type and all nested attributes" do
|
|
75
|
+
@attendees = @meeting.attendees
|
|
76
|
+
@attendees.size.should eql(3)
|
|
77
|
+
@attendees.each {|a| a.should be_a(Attendee) }
|
|
78
|
+
@attendees.first.id.should eql(1)
|
|
79
|
+
@attendees.first.name.should eql('First')
|
|
80
|
+
@attendees.second.id.should eql(2)
|
|
81
|
+
@attendees.second.name.should eql('Second')
|
|
82
|
+
@attendees.last.id.should eql(7)
|
|
83
|
+
@attendees.last.name.should eql('Last')
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it 'allows setting with actual class objects in an initial hash' do
|
|
87
|
+
@attendees = [Attendee.new(:id=>4,:name=>'Forth'), Attendee.new(:id=>5,:name=>'A fifth')]
|
|
88
|
+
@meeting2 = Meeting.new(:id => 3, :attendees => @attendees)
|
|
89
|
+
@attendees = @meeting2.attendees
|
|
90
|
+
@attendees.size.should eql(2)
|
|
91
|
+
@attendees.each {|a| a.should be_a(Attendee) }
|
|
92
|
+
@attendees.first.id.should eql(4)
|
|
93
|
+
@attendees.first.name.should eql('Forth')
|
|
94
|
+
@attendees.second.id.should eql(5)
|
|
95
|
+
@attendees.second.name.should eql('A fifth')
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
end
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
class Fake
|
|
4
|
+
def self.attributes
|
|
5
|
+
{
|
|
6
|
+
:string => "Test",
|
|
7
|
+
:boolean => true,
|
|
8
|
+
:datetime => 'May 14, 2011, 5:00PM',
|
|
9
|
+
:time => 'May 14, 2011, 5:00PM',
|
|
10
|
+
:date => 'May 14, 2011',
|
|
11
|
+
:array => [1,2,3],
|
|
12
|
+
:hash => { :one => 1, :two => "two" }
|
|
13
|
+
}
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe Morpheus::Mock, ".initialize" do
|
|
18
|
+
|
|
19
|
+
before(:each) do
|
|
20
|
+
@mock = Morpheus::Mock.new(Fake, 1, Fake.attributes)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "sets the @mock_class variable to the class passed in as a string" do
|
|
24
|
+
@mock.instance_variable_get('@mock_class').should eql("Fake")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "creates a new instance of this mock class with the given id" do
|
|
28
|
+
@mock.id.should eql(1)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "sets the @attributes variable using the attributes and id passed in" do
|
|
32
|
+
@mock.instance_variable_get('@attributes').should eql(HashWithIndifferentAccess.new(Fake.attributes.merge({ :id => 1 })))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "creates attr_accessors for each attribute" do
|
|
36
|
+
@mock.instance_variable_get('@attributes').keys.each do |key|
|
|
37
|
+
@mock.should respond_to(key)
|
|
38
|
+
@mock.should respond_to("#{key}=")
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "stubs the find method by stubbing the find url at '/:mock_class/:id'" do
|
|
43
|
+
body = {
|
|
44
|
+
:status => 200,
|
|
45
|
+
:content => Fake.attributes.merge({ :id => 1 })
|
|
46
|
+
}.to_json
|
|
47
|
+
response = Typhoeus::Response.new(:code => 200, :headers => "", :body => body, :time => 0.3)
|
|
48
|
+
@test_stub = Typhoeus::HydraMock.new("#{Morpheus::Configuration.host}/fakes/1", :get)
|
|
49
|
+
@test_stub.and_return(response)
|
|
50
|
+
@stub = Morpheus::Configuration.hydra.stubs.find do |stub|
|
|
51
|
+
stub.url == @test_stub.url
|
|
52
|
+
end
|
|
53
|
+
@stub.should be_matches(Typhoeus::Request.new("#{Morpheus::Configuration.host}/fakes/1", :method => :get))
|
|
54
|
+
Yajl::Parser.parse(@stub.response.body).should eql(Yajl::Parser.parse(@test_stub.response.body))
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
context "when given a specific status code" do
|
|
58
|
+
|
|
59
|
+
before(:each) do
|
|
60
|
+
Morpheus::Mock.clear!
|
|
61
|
+
Morpheus::Mock.new(Fake, 1, Fake.attributes, :status => 500)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "returns a response with the given status code" do
|
|
65
|
+
Morpheus::Configuration.hydra.should eql(Typhoeus::Hydra.hydra)
|
|
66
|
+
response = Typhoeus::Request.get("#{Morpheus::Configuration.host}/fakes/1")
|
|
67
|
+
response.code.should eql(500)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
describe Morpheus::Mock, ".id" do
|
|
75
|
+
|
|
76
|
+
before(:each) do
|
|
77
|
+
@mock = Morpheus::Mock.new(Fake, 1, Fake.attributes)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "returns the id attribute" do
|
|
81
|
+
@mock.id.should eql(1)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
describe Morpheus::Mock, ".status" do
|
|
87
|
+
|
|
88
|
+
context "when a status code has not been set" do
|
|
89
|
+
|
|
90
|
+
before(:each) do
|
|
91
|
+
@mock = Morpheus::Mock.new(Fake, 1, Fake.attributes)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "returns 200" do
|
|
95
|
+
@mock.status.should eql(200)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
context "when a status code has been set to 500" do
|
|
101
|
+
|
|
102
|
+
before(:each) do
|
|
103
|
+
@mock = Morpheus::Mock.new(Fake, 1, Fake.attributes, :status => 500)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it "returns 500" do
|
|
107
|
+
@mock.status.should eql(500)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
describe Morpheus::Mock, "#mock" do
|
|
115
|
+
|
|
116
|
+
it "returns a new instance of Mock" do
|
|
117
|
+
Morpheus::Mock.mock(Fake, 1).should be_a(Morpheus::Mock)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
describe Morpheus::Mock, "#clear!" do
|
|
123
|
+
|
|
124
|
+
it "clears all mock requests from Typhoeus" do
|
|
125
|
+
Morpheus::Mock.new(Fake, 1, Fake.attributes)
|
|
126
|
+
Morpheus::Mock.new(Fake, 2, Fake.attributes)
|
|
127
|
+
Morpheus::Mock.new(Fake, 3, Fake.attributes)
|
|
128
|
+
Morpheus::Configuration.hydra.stubs.should have(3).stubs
|
|
129
|
+
Morpheus::Mock.clear!
|
|
130
|
+
Morpheus::Configuration.hydra.stubs.should be_empty
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Morpheus::Relation, ".where" do
|
|
4
|
+
|
|
5
|
+
before(:each) do
|
|
6
|
+
@dogs = [
|
|
7
|
+
Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1),
|
|
8
|
+
Dog.new(:name => "Wilbur", :breed => "Hounddog").attributes.merge(:id => 2),
|
|
9
|
+
Dog.new(:name => "Fido", :breed => "Dalmatian").attributes.merge(:id => 3)
|
|
10
|
+
]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "makes a request to find all of the given records, with the where parameters" do
|
|
14
|
+
Dog.should_receive(:get).with("/dogs", { :name => "Daisy", :breed => "English Bulldog" }).and_return(@dogs)
|
|
15
|
+
Dog.where(:name => "Daisy", :breed => "English Bulldog").all
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe Morpheus::Relation, ".limit" do
|
|
21
|
+
|
|
22
|
+
before(:each) do
|
|
23
|
+
@dogs = [
|
|
24
|
+
Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1),
|
|
25
|
+
Dog.new(:name => "Wilbur", :breed => "Hounddog").attributes.merge(:id => 2),
|
|
26
|
+
Dog.new(:name => "Fido", :breed => "Dalmatian").attributes.merge(:id => 3)
|
|
27
|
+
]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "makes a request to find all of the given records, with the limit parameter" do
|
|
31
|
+
Dog.should_receive(:get).with("/dogs", { :limit => 2 }).and_return(@dogs)
|
|
32
|
+
Dog.limit(2).all
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe Morpheus::Relation, ".page" do
|
|
38
|
+
|
|
39
|
+
before(:each) do
|
|
40
|
+
@dogs = [
|
|
41
|
+
Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1),
|
|
42
|
+
Dog.new(:name => "Wilbur", :breed => "Hounddog").attributes.merge(:id => 2),
|
|
43
|
+
Dog.new(:name => "Fido", :breed => "Dalmatian").attributes.merge(:id => 3)
|
|
44
|
+
]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "makes a request to find all of the given records, with the chained parameters" do
|
|
48
|
+
Dog.results_per_page = 10
|
|
49
|
+
Dog.should_receive(:get).with("/dogs", { :limit => 10, :offset => 20 }).and_return(@dogs)
|
|
50
|
+
Dog.page(3).all
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
describe Morpheus::Relation, "chaining relations" do
|
|
56
|
+
|
|
57
|
+
before(:each) do
|
|
58
|
+
@dogs = [
|
|
59
|
+
Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1),
|
|
60
|
+
Dog.new(:name => "Wilbur", :breed => "Hounddog").attributes.merge(:id => 2),
|
|
61
|
+
Dog.new(:name => "Fido", :breed => "Dalmatian").attributes.merge(:id => 3)
|
|
62
|
+
]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "makes a request to find all of the given records, with the chained parameters" do
|
|
66
|
+
Dog.results_per_page = 10
|
|
67
|
+
Dog.should_receive(:get).with("/dogs", { :name => "Daisy", :breed => "English Bulldog", :limit => 2, :offset => 20 }).and_return(@dogs)
|
|
68
|
+
Dog.where(:name => "Daisy", :breed => "English Bulldog").page(3).limit(2).all
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Morpheus::Response, ".cached?" do
|
|
4
|
+
|
|
5
|
+
context "when the response is marked as cached" do
|
|
6
|
+
|
|
7
|
+
before(:each) do
|
|
8
|
+
@response = Morpheus::Response.new({ :code => 200, :headers => "", :body => "", :time => 0.3 }, true)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "returns true" do
|
|
12
|
+
@response.should be_cached
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context "when the response is marked as not cached" do
|
|
18
|
+
|
|
19
|
+
before(:each) do
|
|
20
|
+
@response = Morpheus::Response.new({ :code => 200, :headers => "", :body => "", :time => 0.3 }, false)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "returns false" do
|
|
24
|
+
@response.should_not be_cached
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context "when the response is not marked as cached or otherwise" do
|
|
30
|
+
|
|
31
|
+
before(:each) do
|
|
32
|
+
@response = Morpheus::Response.new(:code => 200, :headers => "", :body => "", :time => 0.3)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "returns false" do
|
|
36
|
+
@response.should_not be_cached
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
describe Morpheus::Response, ".respond_to?" do
|
|
44
|
+
|
|
45
|
+
before(:each) do
|
|
46
|
+
@typhoeus_response = Typhoeus::Response.new(:code => 200, :headers => "", :body => "", :time => 0.3)
|
|
47
|
+
@response = Morpheus::Response.new(:code => 200, :headers => "", :body => "", :time => 0.3)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "responds to all methods defined on itself" do
|
|
51
|
+
@response.methods.reject { |method| method == :respond_to? }.each do |method|
|
|
52
|
+
@response.should respond_to(method)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "responds to all methods defined on the Typhoeus::Response object it wraps" do
|
|
57
|
+
@typhoeus_response.methods.each do |method|
|
|
58
|
+
@response.should respond_to(method)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
describe Morpheus::Response, ".method_missing" do
|
|
65
|
+
|
|
66
|
+
it "wraps a Typhoeus::Response object" do
|
|
67
|
+
@response = Morpheus::Response.new
|
|
68
|
+
Typhoeus::Response.new.methods.each do |method|
|
|
69
|
+
@response.should respond_to(method.to_sym)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
end
|