diff options
author | Rick Bradley <rick@rickbradley.com> | 2008-04-01 23:40:28 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-04-11 13:10:38 -0500 |
commit | b75048202219f2e07a211df3400a0ee88ccfd208 (patch) | |
tree | 9b2c72f77d8cdcfd8f4faf92dc2264a369aae34b /spec | |
parent | a7f2dd464b46bfd55470a6bf679235552d200216 (diff) | |
download | puppet-b75048202219f2e07a211df3400a0ee88ccfd208.tar.gz puppet-b75048202219f2e07a211df3400a0ee88ccfd208.tar.xz puppet-b75048202219f2e07a211df3400a0ee88ccfd208.zip |
unit specs and implementation for Indirector::REST#search method
Diffstat (limited to 'spec')
-rwxr-xr-x | spec/unit/indirector/rest.rb | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/spec/unit/indirector/rest.rb b/spec/unit/indirector/rest.rb index 56db8f509..791ef862a 100755 --- a/spec/unit/indirector/rest.rb +++ b/spec/unit/indirector/rest.rb @@ -6,8 +6,9 @@ require 'puppet/indirector/rest' describe Puppet::Indirector::REST do before do Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + @model = stub('model') + @instance = stub('model instance') + @indirection = stub('indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model) Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) @rest_class = Class.new(Puppet::Indirector::REST) do @@ -21,12 +22,23 @@ describe Puppet::Indirector::REST do describe "when doing a find" do before :each do - @searcher.stubs(:network_fetch).returns({:foo => 'bar'}.to_yaml) # neuter the network connection + @result = { :foo => 'bar'}.to_yaml + @searcher.stubs(:network_fetch).returns(@result) # neuter the network connection @model.stubs(:from_yaml).returns(@instance) end it "should look up the model instance over the network" do - @searcher.expects(:network_fetch).returns({:foo => 'bar'}.to_yaml) + @searcher.expects(:network_fetch).returns(@result) + @searcher.find('foo') + end + + it "should look up the model instance using the named indirection" do + @searcher.expects(:network_fetch).with {|path| path =~ %r{^#{@indirection.name.to_s}/} }.returns(@result) + @searcher.find('foo') + end + + it "should look up the model instance using the provided key" do + @searcher.expects(:network_fetch).with {|path| path =~ %r{/foo$} }.returns(@result) @searcher.find('foo') end @@ -40,7 +52,7 @@ describe Puppet::Indirector::REST do end it "should return nil when deserialized model instance is nil" do - @model.stubs(:from_yaml).returns(@instance) + @model.stubs(:from_yaml).returns(nil) @searcher.find('foo').should be_nil end @@ -56,9 +68,41 @@ describe Puppet::Indirector::REST do end describe "when doing a search" do - it "should deserialize result data into a list of Model instances" - it "should generate an error when result data deserializes improperly" - it "should generate an error when result data specifies an error" + before :each do + @result = [1, 2] + @searcher.stubs(:network_fetch).returns(@result) + @model.stubs(:from_yaml).returns(@instance) + end + + it "should look up the model data over the network" do + @searcher.expects(:network_fetch).returns(@result) + @searcher.search('foo') + end + + it "should look up the model instance using the named indirection" do + @searcher.expects(:network_fetch).with {|path| path =~ %r{^#{@indirection.name.to_s}s/} }.returns(@result) + @searcher.search('foo') + end + + it "should look up the model instance using the provided key" do + @searcher.expects(:network_fetch).with {|path| path =~ %r{/foo$} }.returns(@result) + @searcher.search('foo') + end + + it "should deserialize result data into a list of Model instances" do + @model.expects(:from_yaml).at_least(2) + @searcher.search('foo') + end + + it "should generate an error when result data deserializes improperly" do + @model.stubs(:from_yaml).raises(ArgumentError) + lambda { @searcher.search('foo') }.should raise_error(ArgumentError) + end + + it "should generate an error when result data specifies an error" do + @searcher.stubs(:network_fetch).returns(RuntimeError.new("bogus").to_yaml) + lambda { @searcher.search('foo') }.should raise_error(RuntimeError) + end end describe "when doing a save" do |