spec/plaza/models/restful_model_spec.rb in plaza-0.0.4 vs spec/plaza/models/restful_model_spec.rb in plaza-0.1.0
- old
+ new
@@ -6,27 +6,35 @@
attribute :id, Integer
attribute :name, String
attribute :amajig_id, Integer
end
-class Amajig
- include Plaza::RestfulModel
-
- has_many :things, :amabobs
- attribute :name
-end
-
-Plaza.configure :amabob do
+Plaza.configure :foobar do
base_url 'http://www.example.com/rest'
logger NullLogger.new # Specs should STFU
end
-class Amabob
+module Foobar
+ class Amabob
+ include Plaza::RestfulModel
+ plaza_config :foobar
+
+ has_many 'Foobar::Amajing'
+ attribute :amajig_id, Integer
+ end
+
+ class Amajing
+ include Plaza::RestfulModel
+ plaza_config :foobar
+ end
+end
+
+class Amajig
include Plaza::RestfulModel
- plaza_config :amabob
- attribute :amajig_id, Integer
+ has_many :things, Foobar::Amabob
+ attribute :name
end
describe Thing do
let(:things_hash){
@@ -61,10 +69,19 @@
'name'=>'Jigger'
}
}
}
+ let(:amabob_hash){
+ {
+ 'amabob' => {
+ 'id' => 3,
+ 'amajig_id' => 2
+ }
+ }
+ }
+
let(:amabobs_hash){
{
'amabobs' =>[
{
'id' => 3,
@@ -76,10 +93,25 @@
}
]
}
}
+ let(:amajings_hash){
+ {
+ 'amajings'=>[
+ {
+ 'id'=> 5,
+ 'amabob_id'=> 4
+ },
+ {
+ 'id'=> 6,
+ 'amabob_id'=> 4
+ }
+ ]
+ }
+ }
+
describe 'attributes' do
it{ should respond_to :errors }
it 'should raise NoMethodError for unknown method' do
expect{ Thing.new(id:10).foobar }.to raise_error NoMethodError
@@ -149,10 +181,21 @@
to_raise(Faraday::Error::ConnectionFailed.new('Connection Failed'))
expect{Thing.find(1)}.to raise_error(Plaza::ConnectionError)
end
end
+
+ context 'with caching' do
+ it 'second request should be cached and not hit server' do
+ stub_request(:get, 'http://www.example.com/rest/amabobs/3.json').to_return(
+ headers: {'Cache-Control' => 'max-age=200'},
+ body:amabob_hash.to_json
+ ).times(1).then.to_raise('Cache Not Working')
+ Foobar::Amabob.find(3)
+ expect{ Foobar::Amabob.find(3) }.not_to raise_error
+ end
+ end
end
describe '.where()' do
it 'should attempt to get list of things that meet where clause' do
stub_request(:get, 'http://example.com/rest/things.json?name=fred&id=3').to_return(body:things_hash.to_json)
@@ -217,10 +260,16 @@
end
it 'gets second has_many relationships' do
amajig = Amajig.new(amajig_hash['amajig'])
stub_request(:get, 'http://example.com/rest/amajigs/2/amabobs.json').to_return(body:amabobs_hash.to_json)
- expect(amajig.amabobs.first.class).to be Amabob
+ expect(amajig.amabobs.first.class).to be Foobar::Amabob
+ end
+
+ it 'gets string has_many relationships' do
+ amabob = Foobar::Amabob.new(id:4)
+ stub_request(:get, 'http://www.example.com/rest/amabobs/4/amajings.json').to_return(body:amajings_hash.to_json)
+ expect(amabob.amajings.first.class).to be Foobar::Amajing
end
end
describe 'error messages' do
it 'should return an empty array' do