morpheus 0.3.6 → 0.3.7
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/.autotest +12 -0
- data/.rspec +1 -0
- data/README.md +77 -0
- data/Rakefile +16 -0
- data/lib/morpheus.rb +2 -2
- data/lib/morpheus/base.rb +1 -1
- data/lib/morpheus/client/inflections.rb +1 -1
- data/lib/morpheus/configuration.rb +41 -43
- data/lib/morpheus/errors.rb +1 -1
- data/lib/morpheus/mixins/associations.rb +3 -1
- data/lib/morpheus/mixins/attributes.rb +66 -67
- data/lib/morpheus/mixins/conversion.rb +9 -13
- data/lib/morpheus/mixins/filtering.rb +4 -1
- data/lib/morpheus/mixins/finders.rb +4 -1
- data/lib/morpheus/mixins/introspection.rb +12 -16
- data/lib/morpheus/mixins/persistence.rb +25 -26
- data/lib/morpheus/mixins/reflections.rb +4 -1
- data/lib/morpheus/mixins/request_handling.rb +4 -1
- data/lib/morpheus/mixins/response_parsing.rb +12 -12
- data/lib/morpheus/mixins/url_support.rb +4 -1
- data/lib/morpheus/request.rb +34 -34
- data/lib/morpheus/response.rb +2 -2
- data/lib/morpheus/response_parser.rb +9 -4
- data/lib/morpheus/version.rb +1 -1
- data/morpheus.gemspec +4 -4
- data/spec/dummy/app/resources/author.rb +0 -1
- data/spec/dummy/app/resources/book.rb +0 -1
- data/spec/dummy/app/resources/dog.rb +3 -3
- data/spec/dummy/app/resources/meeting.rb +1 -2
- data/spec/dummy/config/application.rb +7 -36
- data/spec/dummy/config/environments/production.rb +1 -1
- data/spec/dummy/config/initializers/morpheus.rb +1 -1
- data/spec/dummy/config/locales/en.yml +1 -1
- data/spec/dummy/config/routes.rb +0 -56
- data/spec/morpheus/associations/association_spec.rb +51 -33
- data/spec/morpheus/associations/belongs_to_association_spec.rb +14 -2
- data/spec/morpheus/associations/has_many_association_spec.rb +31 -11
- data/spec/morpheus/associations/has_one_association_spec.rb +14 -2
- data/spec/morpheus/base_spec.rb +104 -100
- data/spec/morpheus/client/associations_spec.rb +43 -36
- data/spec/morpheus/client/log_subscriber_spec.rb +33 -0
- data/spec/morpheus/client/railtie_spec.rb +5 -0
- data/spec/morpheus/configuration_spec.rb +92 -113
- data/spec/morpheus/mixins/associations_spec.rb +90 -108
- data/spec/morpheus/mixins/attributes_spec.rb +71 -77
- data/spec/morpheus/mixins/conversion_spec.rb +49 -59
- data/spec/morpheus/mixins/filtering_spec.rb +13 -0
- data/spec/morpheus/mixins/finders_spec.rb +180 -217
- data/spec/morpheus/mixins/introspection_spec.rb +81 -124
- data/spec/morpheus/mixins/persistence_spec.rb +140 -133
- data/spec/morpheus/mixins/{reflection_spec.rb → reflections_spec.rb} +28 -28
- data/spec/morpheus/mixins/request_handling_spec.rb +21 -0
- data/spec/morpheus/mixins/response_parsing_spec.rb +10 -2
- data/spec/morpheus/mixins/url_support_spec.rb +29 -0
- data/spec/morpheus/reflection_spec.rb +21 -0
- data/spec/morpheus/relation_spec.rb +34 -58
- data/spec/morpheus/request_cache_spec.rb +33 -2
- data/spec/morpheus/request_queue_spec.rb +37 -0
- data/spec/morpheus/request_spec.rb +102 -1
- data/spec/morpheus/response_parser_spec.rb +17 -0
- data/spec/morpheus/response_spec.rb +55 -51
- data/spec/morpheus/type_caster_spec.rb +128 -118
- data/spec/morpheus/url_builder_spec.rb +41 -0
- data/spec/shared/active_model_lint_test.rb +2 -2
- data/spec/spec_helper.rb +7 -14
- data/spec/support/configuration.rb +3 -4
- metadata +32 -16
- data/README.rdoc +0 -44
- data/autotest/discover.rb +0 -7
- data/lib/morpheus/mock.rb +0 -66
- data/spec/morpheus/mock_spec.rb +0 -133
@@ -1,99 +1,93 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Morpheus::Attributes
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
3
|
+
describe Morpheus::Attributes do
|
4
|
+
|
5
|
+
describe '.property' do
|
6
|
+
it 'defines setter and getter methods for the given property' do
|
7
|
+
@book = Book.new
|
8
|
+
@book.should respond_to(:title)
|
9
|
+
@book.title = 'Test Title'
|
10
|
+
@book.title.should eql('Test Title')
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'creates an attribute entry for this property' do
|
14
|
+
Book.new.attributes.should include(:title)
|
15
|
+
end
|
14
16
|
end
|
15
|
-
|
16
|
-
end
|
17
17
|
|
18
|
-
describe
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
describe '.default_attributes' do
|
19
|
+
it 'returns the default attributes has which will be applied as the attributes when a new object is instantiated' do
|
20
|
+
Book.default_attributes.should eql(HashWithIndifferentAccess.new({ :id => nil, :title => nil, :author_id => nil }))
|
21
|
+
end
|
22
22
|
end
|
23
|
-
|
24
|
-
end
|
25
23
|
|
26
|
-
describe
|
27
|
-
|
28
|
-
end
|
24
|
+
describe '.typecast_attributes' do
|
25
|
+
pending
|
26
|
+
end
|
29
27
|
|
30
|
-
describe
|
31
|
-
|
32
|
-
end
|
28
|
+
describe '.attribute_defined?' do
|
29
|
+
pending
|
30
|
+
end
|
33
31
|
|
34
|
-
describe
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
describe '#attributes' do
|
33
|
+
it 'returns the attributes hash' do
|
34
|
+
Book.new.attributes.should eql(HashWithIndifferentAccess.new({ :id => nil, :title => nil, :author_id => nil }))
|
35
|
+
end
|
38
36
|
end
|
39
|
-
|
40
|
-
end
|
41
37
|
|
42
|
-
describe
|
43
|
-
|
44
|
-
end
|
38
|
+
describe '#read_attribute_for_validation' do
|
39
|
+
pending
|
40
|
+
end
|
45
41
|
|
46
|
-
describe
|
47
|
-
|
48
|
-
end
|
42
|
+
describe '#typecast' do
|
43
|
+
pending
|
44
|
+
end
|
49
45
|
|
50
|
-
describe
|
51
|
-
|
52
|
-
end
|
46
|
+
describe '#attributes_without_basic_attributes' do
|
47
|
+
pending
|
48
|
+
end
|
53
49
|
|
54
|
-
describe
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
50
|
+
describe "#update_attribute" do
|
51
|
+
it 'updates the attribute with the given value' do
|
52
|
+
@book = Book.new(:title => 'Horton Hears a Who')
|
53
|
+
@book.title.should eql('Horton Hears a Who')
|
54
|
+
@book.update_attribute(:title, 'One Fish, Two Fish, Red Fish, Blue Fish')
|
55
|
+
@book.title.should eql('One Fish, Two Fish, Red Fish, Blue Fish')
|
56
|
+
end
|
61
57
|
end
|
62
|
-
|
63
|
-
end
|
64
58
|
|
65
|
-
describe
|
66
|
-
|
67
|
-
it "updates each attribute with the given value" do
|
68
|
-
@book = Book.new(:title => "Green Eggs and Ham")
|
69
|
-
@book.id.should be_nil
|
70
|
-
@book.title.should eql("Green Eggs and Ham")
|
71
|
-
@book.send(:merge_attributes, { :title => "Red Fish, Blue Fish, One Fish, Two Fish", :id => 5 })
|
72
|
-
@book.id.should eql(5)
|
73
|
-
@book.title.should eql("Red Fish, Blue Fish, One Fish, Two Fish")
|
59
|
+
describe '#update_reflection' do
|
60
|
+
pending
|
74
61
|
end
|
75
|
-
|
76
|
-
end
|
77
62
|
|
78
|
-
describe
|
79
|
-
|
80
|
-
|
81
|
-
|
63
|
+
describe '#merge_attributes' do
|
64
|
+
it 'updates each attribute with the given value' do
|
65
|
+
@book = Book.new(:title => 'Green Eggs and Ham')
|
66
|
+
@book.id.should be_nil
|
67
|
+
@book.title.should eql('Green Eggs and Ham')
|
68
|
+
@book.send(:merge_attributes, { :title => 'One Fish, Two Fish, Red Fish, Blue Fish', :id => 5 })
|
69
|
+
@book.id.should eql(5)
|
70
|
+
@book.title.should eql('One Fish, Two Fish, Red Fish, Blue Fish')
|
71
|
+
end
|
82
72
|
end
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
73
|
+
|
74
|
+
describe 'dirty tracking' do
|
75
|
+
it 'includes the ActiveModel::Dirty module' do
|
76
|
+
Morpheus::Base.included_modules.should include(ActiveModel::Dirty)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'tracks dirty state for property values' do
|
80
|
+
@book = Book.new
|
81
|
+
@book.should_not be_title_changed
|
82
|
+
@book.title = 'Dirty title'
|
83
|
+
@book.should be_title_changed
|
84
|
+
end
|
89
85
|
end
|
90
|
-
|
91
|
-
end
|
92
86
|
|
93
|
-
describe
|
94
|
-
|
95
|
-
|
96
|
-
|
87
|
+
describe 'validations' do
|
88
|
+
it 'includes the ActiveModel::Validations module' do
|
89
|
+
Morpheus::Base.included_modules.should include(ActiveModel::Validations)
|
90
|
+
end
|
97
91
|
end
|
98
|
-
|
92
|
+
|
99
93
|
end
|
@@ -1,76 +1,66 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Morpheus::Conversion
|
4
|
-
|
5
|
-
before(:each) do
|
6
|
-
@dog = Dog.new
|
7
|
-
end
|
8
|
-
|
9
|
-
it "returns this object" do
|
10
|
-
@dog.to_model.should eql(@dog)
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
3
|
+
describe Morpheus::Conversion do
|
14
4
|
|
15
|
-
describe
|
16
|
-
|
17
|
-
context "when the id attribute is set" do
|
18
|
-
|
5
|
+
describe '#to_model' do
|
19
6
|
before(:each) do
|
20
|
-
|
21
|
-
@base = Morpheus::Base.find(1)
|
7
|
+
@dog = Dog.new
|
22
8
|
end
|
23
|
-
|
24
|
-
it
|
25
|
-
@
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
context "when the id attribute is set" do
|
31
|
-
|
32
|
-
before(:each) do
|
33
|
-
@base = Morpheus::Base.new
|
34
|
-
end
|
35
|
-
|
36
|
-
it "returns the id as a string" do
|
37
|
-
@base.to_param.should be_nil
|
9
|
+
|
10
|
+
it 'returns this object' do
|
11
|
+
@dog.to_model.should eql(@dog)
|
38
12
|
end
|
39
|
-
|
40
13
|
end
|
41
|
-
|
42
|
-
end
|
43
14
|
|
44
|
-
describe
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
15
|
+
describe '#to_param' do
|
16
|
+
context 'when the id attribute is set' do
|
17
|
+
before(:each) do
|
18
|
+
Morpheus::Base.stub(:get).and_return(Morpheus::Base.new(:id => 1))
|
19
|
+
@base = Morpheus::Base.find(1)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns the id as a string' do
|
23
|
+
@base.to_param.should eql('1')
|
24
|
+
end
|
50
25
|
end
|
51
|
-
|
52
|
-
|
53
|
-
|
26
|
+
|
27
|
+
context 'when the id attribute is set' do
|
28
|
+
before(:each) do
|
29
|
+
@base = Morpheus::Base.new
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'returns the id as a string' do
|
33
|
+
@base.to_param.should be_nil
|
34
|
+
end
|
54
35
|
end
|
55
|
-
|
56
36
|
end
|
57
|
-
|
58
|
-
context "when the object is persisted" do
|
59
37
|
|
60
|
-
|
61
|
-
|
62
|
-
|
38
|
+
describe '#to_key' do
|
39
|
+
context 'when the object is a new record' do
|
40
|
+
before(:each) do
|
41
|
+
@dog = Dog.new(:name => 'Daisy', :breed => 'English Bulldog', :age => 2)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'returns nil' do
|
45
|
+
@dog.to_key.should be_nil
|
46
|
+
end
|
63
47
|
end
|
64
48
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
49
|
+
context 'when the object is persisted' do
|
50
|
+
before(:each) do
|
51
|
+
Dog.stub(:get).and_return(Dog.new(:id => 1, :name => 'Daisy', :breed => 'English Bulldog', :age => 2))
|
52
|
+
@dog = Dog.find(1)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'returns the attribute keys of the object' do
|
56
|
+
@keys = @dog.to_key
|
57
|
+
@keys.should have(4).keys
|
58
|
+
@keys.should include('id')
|
59
|
+
@keys.should include('name')
|
60
|
+
@keys.should include('breed')
|
61
|
+
@keys.should include('age')
|
62
|
+
end
|
72
63
|
end
|
73
|
-
|
74
64
|
end
|
75
|
-
|
65
|
+
|
76
66
|
end
|
@@ -1,255 +1,218 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Morpheus::Finders
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
context "when this record does not exist on the external service" do
|
30
|
-
|
31
|
-
before(:each) do
|
32
|
-
Morpheus::Configuration.hydra.stub(:get, "#{Morpheus::Configuration.host}/dogs/1").and_return(build_morpheus_response(404, nil))
|
3
|
+
describe Morpheus::Finders do
|
4
|
+
|
5
|
+
describe '#find' do
|
6
|
+
context 'when the argument is a single integer' do
|
7
|
+
context 'when this record exists on the external service' do
|
8
|
+
before(:each) do
|
9
|
+
@dog = Dog.new(:id => 1, :name => 'Daisy', :breed => 'English Bulldog')
|
10
|
+
Dog.stub(:get).and_return(@dog)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'makes a request to /dogs/1 at the external service' do
|
14
|
+
Dog.should_receive(:get).with('/dogs/1', nil, { :id => 1 }).and_return(@dog)
|
15
|
+
Dog.find(1)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'returns the instance representing this found record' do
|
19
|
+
@dog = Dog.find(1)
|
20
|
+
@dog.should be_a(Dog)
|
21
|
+
@dog.id.should eql(1)
|
22
|
+
@dog.name.should eql('Daisy')
|
23
|
+
@dog.breed.should eql('English Bulldog')
|
24
|
+
end
|
33
25
|
end
|
34
|
-
|
35
|
-
|
36
|
-
|
26
|
+
|
27
|
+
context 'when this record does not exist on the external service' do
|
28
|
+
before(:each) do
|
29
|
+
Morpheus::Configuration.hydra.stub(:get, "#{Morpheus::Configuration.host}/dogs/1").and_return(build_morpheus_response(404, nil))
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'raises a ResourceNotFound error' do
|
33
|
+
lambda { Dog.find(1) }.should raise_error(Morpheus::ResourceNotFound, "Couldn't find Dog with ID=1")
|
34
|
+
end
|
37
35
|
end
|
38
|
-
|
39
36
|
end
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
Dog.
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
37
|
+
|
38
|
+
context 'when the argument is a single array' do
|
39
|
+
context 'when all records exists on the external service' do
|
40
|
+
before(:each) do
|
41
|
+
@dogs = [
|
42
|
+
Dog.new(:id => 1, :name => 'Daisy', :breed => 'English Bulldog'),
|
43
|
+
Dog.new(:id => 2, :name => 'Wilbur', :breed => 'Hounddog'),
|
44
|
+
Dog.new(:id => 3, :name => 'Fido', :breed => 'Dalmatian')
|
45
|
+
]
|
46
|
+
Dog.stub(:get).and_return(@dogs)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'makes a request to /dogs?ids=1,2,3 at the external service' do
|
50
|
+
Dog.should_receive(:get).with('/dogs', { :ids => [1,2,3] }, { :ids => [1,2,3] }).and_return(@dogs)
|
51
|
+
Dog.find([1,2,3])
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returns an array of instances representing these found records' do
|
55
|
+
@dogs = Dog.find([1,2,3])
|
56
|
+
@dogs.should be_an(Array)
|
57
|
+
@dogs.should have(3).dogs
|
58
|
+
@dogs.each do |dog|
|
59
|
+
dog.should be_a(Dog)
|
60
|
+
end
|
61
|
+
end
|
59
62
|
end
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
63
|
+
|
64
|
+
context 'when there is a requested record that does not exist on the external service' do
|
65
|
+
before(:each) do
|
66
|
+
Morpheus::Configuration.hydra.stub(:get, "#{Morpheus::Configuration.host}/dogs?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3").and_return(build_morpheus_response(404, nil))
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'raises a ResourceNotFound error' do
|
70
|
+
lambda { Dog.find([1,2,3]) }.should raise_error(Morpheus::ResourceNotFound, "Couldn't find all Dogs with IDs (1, 2, 3)")
|
67
71
|
end
|
68
72
|
end
|
69
|
-
|
70
73
|
end
|
71
|
-
|
72
|
-
context
|
73
|
-
|
74
|
-
|
75
|
-
|
74
|
+
|
75
|
+
context 'when there are multiple arguments, each being an integer' do
|
76
|
+
context 'when all records exists on the external service' do
|
77
|
+
before(:each) do
|
78
|
+
@dogs = [
|
79
|
+
Dog.new(:id => 1, :name => 'Daisy', :breed => 'English Bulldog'),
|
80
|
+
Dog.new(:id => 2, :name => 'Wilbur', :breed => 'Hounddog'),
|
81
|
+
Dog.new(:id => 3, :name => 'Fido', :breed => 'Dalmatian')
|
82
|
+
]
|
83
|
+
Dog.stub(:get).and_return(@dogs)
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'makes a request to /dogs?ids=1,2,3 at the external service' do
|
87
|
+
Dog.should_receive(:get).with('/dogs', { :ids => [1,2,3] }, { :ids => [1,2,3] }).and_return(@dogs)
|
88
|
+
Dog.find(1,2,3)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'returns an array of instances representing these found records' do
|
92
|
+
@dogs = Dog.find(1,2,3)
|
93
|
+
@dogs.should be_an(Array)
|
94
|
+
@dogs.should have(3).dogs
|
95
|
+
@dogs.each do |dog|
|
96
|
+
dog.should be_a(Dog)
|
97
|
+
end
|
98
|
+
end
|
76
99
|
end
|
77
|
-
|
78
|
-
|
79
|
-
|
100
|
+
|
101
|
+
context 'when there is a requested record that does not exist on the external service' do
|
102
|
+
before(:each) do
|
103
|
+
Morpheus::Configuration.hydra.stub(:get, "#{Morpheus::Configuration.host}/dogs?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3").and_return(build_morpheus_response(404, nil))
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'raises a ResourceNotFound error' do
|
107
|
+
lambda { Dog.find(1,2,3) }.should raise_error(Morpheus::ResourceNotFound, "Couldn't find all Dogs with IDs (1, 2, 3)")
|
108
|
+
end
|
80
109
|
end
|
81
|
-
|
82
110
|
end
|
83
|
-
|
84
111
|
end
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
Dog.new(:id => 3, :name => "Fido", :breed => "Dalmatian")
|
95
|
-
]
|
96
|
-
Dog.stub(:get).and_return(@dogs)
|
97
|
-
end
|
98
|
-
|
99
|
-
it "makes a request to /dogs?ids=1,2,3 at the external service" do
|
100
|
-
Dog.should_receive(:get).with("/dogs", { :ids => [1,2,3] }, { :ids => [1,2,3] }).and_return(@dogs)
|
101
|
-
Dog.find(1,2,3)
|
102
|
-
end
|
103
|
-
|
104
|
-
it "returns an array of instances representing these found records" do
|
105
|
-
@dogs = Dog.find(1,2,3)
|
106
|
-
@dogs.should be_an(Array)
|
107
|
-
@dogs.should have(3).dogs
|
108
|
-
@dogs.each do |dog|
|
109
|
-
dog.should be_a(Dog)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
112
|
+
|
113
|
+
describe '#all' do
|
114
|
+
before(:each) do
|
115
|
+
@dogs = [
|
116
|
+
Dog.new(:id => 1, :name => 'Daisy', :breed => 'English Bulldog'),
|
117
|
+
Dog.new(:id => 2, :name => 'Wilbur', :breed => 'Hounddog'),
|
118
|
+
Dog.new(:id => 3, :name => 'Fido', :breed => 'Dalmatian')
|
119
|
+
]
|
120
|
+
Dog.stub(:get).and_return(@dogs)
|
113
121
|
end
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
it "raises a ResourceNotFound error" do
|
122
|
-
lambda { Dog.find(1,2,3) }.should raise_error(Morpheus::ResourceNotFound, "Couldn't find all Dogs with IDs (1, 2, 3)")
|
122
|
+
|
123
|
+
it 'returns all found records' do
|
124
|
+
@dogs = Dog.all
|
125
|
+
@dogs.should be_an(Array)
|
126
|
+
@dogs.should have(3).dogs
|
127
|
+
@dogs.each do |dog|
|
128
|
+
dog.should be_a(Dog)
|
123
129
|
end
|
124
|
-
|
125
130
|
end
|
126
|
-
|
127
131
|
end
|
128
|
-
|
129
|
-
end
|
130
132
|
|
131
|
-
describe
|
133
|
+
describe '#first' do
|
134
|
+
before(:each) do
|
135
|
+
@dogs = [Dog.new(:id => 1, :name => 'Daisy', :breed => 'English Bulldog')]
|
136
|
+
Dog.stub(:get).and_return(@dogs)
|
137
|
+
end
|
132
138
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
Dog.new(:id => 2, :name => "Wilbur", :breed => "Hounddog"),
|
137
|
-
Dog.new(:id => 3, :name => "Fido", :breed => "Dalmatian")
|
138
|
-
]
|
139
|
-
Dog.stub(:get).and_return(@dogs)
|
140
|
-
end
|
141
|
-
|
142
|
-
it "returns all found records" do
|
143
|
-
@dogs = Dog.all
|
144
|
-
@dogs.should be_an(Array)
|
145
|
-
@dogs.should have(3).dogs
|
146
|
-
@dogs.each do |dog|
|
147
|
-
dog.should be_a(Dog)
|
139
|
+
it 'returns first found record' do
|
140
|
+
@dog = Dog.first
|
141
|
+
@dog.should be_a(Dog)
|
148
142
|
end
|
149
143
|
end
|
150
144
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
Dog.stub(:get).and_return(@dogs)
|
158
|
-
end
|
159
|
-
|
160
|
-
it "returns first found record" do
|
161
|
-
@dog = Dog.first
|
162
|
-
@dog.should be_a(Dog)
|
163
|
-
end
|
164
|
-
|
165
|
-
end
|
145
|
+
describe '#where' do
|
146
|
+
context 'when the where attribute is an acceptable attribute' do
|
147
|
+
context 'for a where clause with one attribute' do
|
148
|
+
before(:each) do
|
149
|
+
@dogs = [Dog.new(:name => 'Daisy', :breed => 'English Bulldog').attributes.merge(:id => 1)]
|
150
|
+
end
|
166
151
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
context "for a where clause with one attribute" do
|
172
|
-
|
173
|
-
before(:each) do
|
174
|
-
@dogs = [Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1)]
|
175
|
-
end
|
176
|
-
|
177
|
-
it "makes a request to find all records with the given query parameter" do
|
178
|
-
Dog.should_receive(:get).with("/dogs", { :name => "Daisy" }).and_return(@dogs)
|
179
|
-
Dog.where(:name => "Daisy").first
|
180
|
-
end
|
181
|
-
|
182
|
-
end
|
183
|
-
|
184
|
-
context "for a where clause with multiple attributes" do
|
185
|
-
|
186
|
-
before(:each) do
|
187
|
-
@dogs = [Dog.new(:name => "Daisy", :breed => "English Bulldog").attributes.merge(:id => 1)]
|
152
|
+
it 'makes a request to find all records with the given query parameter' do
|
153
|
+
Dog.should_receive(:get).with('/dogs', { :name => 'Daisy' }).and_return(@dogs)
|
154
|
+
Dog.where(:name => 'Daisy').first
|
155
|
+
end
|
188
156
|
end
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
157
|
+
|
158
|
+
context 'for a where clause with multiple attributes' do
|
159
|
+
before(:each) do
|
160
|
+
@dogs = [Dog.new(:name => 'Daisy', :breed => 'English Bulldog').attributes.merge(:id => 1)]
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'makes a request to find all records with the given query parameters' do
|
164
|
+
Dog.should_receive(:get).with('/dogs', { :breed => 'English Bulldog', :name => 'Daisy' }).and_return(@dogs)
|
165
|
+
Dog.where(:name => 'Daisy', :breed => 'English Bulldog').first
|
166
|
+
end
|
193
167
|
end
|
194
|
-
|
195
168
|
end
|
196
|
-
|
197
169
|
end
|
198
|
-
|
199
|
-
end
|
200
170
|
|
201
|
-
describe
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
end
|
171
|
+
describe '#limit' do
|
172
|
+
before(:each) do
|
173
|
+
@dogs = [
|
174
|
+
Dog.new(:id => 1, :name => 'Daisy', :breed => 'English Bulldog'),
|
175
|
+
Dog.new(:id => 2, :name => 'Wilbur', :breed => 'Hounddog'),
|
176
|
+
Dog.new(:id => 3, :name => 'Fido', :breed => 'Dalmatian')
|
177
|
+
]
|
178
|
+
end
|
210
179
|
|
211
|
-
|
212
|
-
|
213
|
-
|
180
|
+
it 'makes a request to find all of the given records, but with a given limit' do
|
181
|
+
Dog.should_receive(:get).with('/dogs', { :limit => 10 }).and_return(@dogs)
|
182
|
+
Dog.limit(10).all
|
183
|
+
end
|
214
184
|
end
|
215
185
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
Dog.new(:name => "Wilbur", :breed => "Hounddog").attributes.merge(:id => 2),
|
226
|
-
Dog.new(:name => "Fido", :breed => "Dalmatian").attributes.merge(:id => 3)
|
227
|
-
]
|
228
|
-
end
|
186
|
+
describe '#page' do
|
187
|
+
before(:each) do
|
188
|
+
Dog.results_per_page = 10
|
189
|
+
@dogs = [
|
190
|
+
Dog.new(:id => 1, :name => 'Daisy', :breed => 'English Bulldog'),
|
191
|
+
Dog.new(:id => 2, :name => 'Wilbur', :breed => 'Hounddog'),
|
192
|
+
Dog.new(:id => 3, :name => 'Fido', :breed => 'Dalmatian')
|
193
|
+
]
|
194
|
+
end
|
229
195
|
|
230
|
-
|
231
|
-
|
232
|
-
|
196
|
+
it 'makes a request to find all of the given records, but with a given limit and offset' do
|
197
|
+
Dog.should_receive(:get).with('/dogs', { :limit => 10, :offset => 20 }).and_return(@dogs)
|
198
|
+
Dog.page(3).all
|
199
|
+
end
|
233
200
|
end
|
234
|
-
|
235
|
-
end
|
236
201
|
|
237
|
-
describe
|
238
|
-
|
239
|
-
|
240
|
-
Dog.results_per_page.should eql(10)
|
241
|
-
end
|
242
|
-
|
243
|
-
context "when set to something other than the default" do
|
244
|
-
|
245
|
-
before(:each) do
|
246
|
-
Dog.results_per_page = 25
|
202
|
+
describe '#results_per_page' do
|
203
|
+
it 'defaults to 10' do
|
204
|
+
Dog.results_per_page.should eql(10)
|
247
205
|
end
|
248
|
-
|
249
|
-
|
250
|
-
|
206
|
+
|
207
|
+
context 'when set to something other than the default' do
|
208
|
+
before(:each) do
|
209
|
+
Dog.results_per_page = 25
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'returns the number of results to be returned on a page request' do
|
213
|
+
Dog.results_per_page.should eql(25)
|
214
|
+
end
|
251
215
|
end
|
252
|
-
|
253
216
|
end
|
254
|
-
|
217
|
+
|
255
218
|
end
|