spec/lib/associations_spec.rb in api_resource-0.6.4 vs spec/lib/associations_spec.rb in api_resource-0.6.5

- old
+ new

@@ -7,16 +7,18 @@ before(:each) do TestResource.reload_class_attributes end let(:ap) do - Associations::SingleObjectProxy.new( + res = Associations::SingleObjectProxy.new( "TestResource", - HasManyObject.new, { - :service_uri => '/single_object_association' - } + HasManyObject.new ) + res.internal_object = { + :service_uri => '/single_object_association' + } + res end context "Comparison" do context "&group_by" do @@ -236,28 +238,31 @@ end it "should be able to extract a service uri from the contents hash" do ap = Associations::SingleObjectProxy.new( "TestResource", - HasManyObject.new, { - :service_uri => '/path' - } + HasManyObject.new ) + ap.internal_object = { + :service_uri => '/path' + } ap.remote_path.should eql("/path") end it "should be able to recognize the attributes of an object and not make them scopes" do TestResource.define_attributes :test ap = Associations::SingleObjectProxy.new( "TestResource", - HasManyObject.new, { - :service_uri => '/path', - :test => "testval" - } + HasManyObject.new ) + + ap.internal_object = { + :service_uri => '/path', + :test => "testval" + } ap.scope?("test").should be_false ap.test.should eql("testval") end @@ -300,64 +305,55 @@ end it "should be able to recognize a settings hash if it has a service_uri" do ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject.new, - [{:service_uri => "/route"}] + BelongsToObject.new ) + ap.internal_object = [{:service_uri => "/route"}] ap.remote_path.should eql("/route") end it "should be able to recognize a settings hash if it has a 'service_uri' with another preset name" do Associations::MultiObjectProxy.remote_path_element = :the_element ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject.new, - [{:the_element => "/route"}] + BelongsToObject.new ) + ap.internal_object = [{:the_element => "/route"}] ap.remote_path.should eql("/route") end end context "Loading hash contents" do - it "should not be able to load a hash without a 'service_uri'" do - lambda { - Associations::MultiObjectProxy.new( - "TestResource", - BelongsToObject, - {:hi => 3} - ) - }.should raise_error - end it "should be able to recognize settings from a hash" do ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject, - {:service_uri => "/route"} + BelongsToObject ) + ap.internal_object = {:service_uri => "/route"} ap.remote_path.should eql("/route") end it "should be able to recognize settings from a hash as a string" do ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject, - {"service_uri" => "/route"} + BelongsToObject ) + ap.internal_object = {"service_uri" => "/route"} ap.remote_path.should eql("/route") end it "should recognize settings with differing 'service_uri' names" do Associations::MultiObjectProxy.remote_path_element = :the_element ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject, - {:the_element => "/route"} + BelongsToObject ) + ap.internal_object = {:the_element => "/route"} ap.remote_path.should eql("/route") end it "should include the foreign_key_id when saving" do tr = TestResource.new.tap do |tr| @@ -455,26 +451,28 @@ it "should proxy unknown methods to the object loading if it hasn't already" do ap = Associations::SingleObjectProxy.new( "TestResource", - HasManyObject.new, { - :service_uri => '/single_object_association' - } + HasManyObject.new ) + ap.internal_object = { + :service_uri => '/single_object_association' + } ap.should_not be_loaded ap.id.should_not be_blank ap.should be_loaded end it "should load scopes with caching" do ap = Associations::SingleObjectProxy.new( "TestResource", - HasManyObject.new, { - :service_uri => '/single_object_association' - } + HasManyObject.new ) + ap.internal_object = { + :service_uri => "/single_object_association" + } ap.should_not be_loaded ap.active.expires_in(30).internal_object # should only be called once TestResource.connection.expects(:request).never @@ -482,34 +480,37 @@ end it "should check that ttl matches the expiration parameter" do ap = Associations::SingleObjectProxy.new( "TestResource", - HasManyObject.new, { - :service_uri => '/single_object_association' - } + HasManyObject.new ) + ap.internal_object = { + :service_uri => "/single_object_association" + } ap.active.expires_in(10).ttl.should eql(10) end it "should cache scopes when caching enabled" do ap = Associations::SingleObjectProxy.new( "TestResource", - HasManyObject.new, { - :service_uri => '/single_object_association' - } + HasManyObject.new ) + ap.internal_object = { + :service_uri => "/single_object_association" + } ap.active(:expires_in => 10).internal_object end it "should be able to clear it's loading cache" do ap = Associations::SingleObjectProxy.new( "TestResource", - HasManyObject.new, { - :service_uri => '/single_object_association' - } + HasManyObject.new ) + ap.internal_object = { + :service_uri => '/single_object_association' + } active = ap.active active.internal_object active.should be_loaded active.reload @@ -521,14 +522,15 @@ end it "should be able to reload a single-object association" do ap = Associations::SingleObjectProxy.new( "TestResource", - HasManyObject.new, { - :service_uri => '/single_object_association' - } + HasManyObject.new ) + ap.internal_object = { + :service_uri => '/single_object_association' + } old_name = ap.name str = "krdflkjsd" @@ -542,14 +544,15 @@ it "should be able to reload a multi-object association" do ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject.new, { - :service_uri => '/multi_object_association' - } + BelongsToObject.new ) + ap.internal_object = { + :service_uri => '/multi_object_association' + } old_name = ap.first.name str = "krdflkjsd" @@ -564,53 +567,57 @@ context "Multi Object" do it "should be able to load 'all'" do ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject.new, { - :service_uri => '/multi_object_association' - } + BelongsToObject.new ) + ap.internal_object = { + :service_uri => '/multi_object_association' + } results = ap.all results.size.should eql(5) results.first.is_active?.should be_false end it "should be able to load a scope" do ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject.new, { - :service_uri => '/multi_object_association' - } + BelongsToObject.new ) + ap.internal_object = { + :service_uri => '/multi_object_association' + } results = ap.active results.size.should eql(5) record = results.first record.is_active.should be_true end it "should be able to load a chain of scopes" do ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject.new, { - :service_uri => '/multi_object_association' - } + BelongsToObject.new ) + ap.internal_object = { + :service_uri => '/multi_object_association' + } results = ap.active.birthday(Date.today) results.first.is_active.should be_true results.first.bday.should_not be_blank end it "should be able to clear it's loading cache" do ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject.new, { - :service_uri => '/multi_object_association' - } + BelongsToObject.new ) + ap.internal_object = { + :service_uri => '/multi_object_association' + } active = ap.active active.internal_object active.should be_loaded active.reload @@ -620,13 +627,14 @@ end it "should be enumerable" do ap = Associations::MultiObjectProxy.new( "TestResource", - BelongsToObject.new, { - :service_uri => '/multi_object_association' - } + BelongsToObject.new ) + ap.internal_object = { + :service_uri => '/multi_object_association' + } ap.each do |tr| tr.name.should_not be_blank end end