From b75048202219f2e07a211df3400a0ee88ccfd208 Mon Sep 17 00:00:00 2001 From: Rick Bradley Date: Tue, 1 Apr 2008 23:40:28 -0500 Subject: unit specs and implementation for Indirector::REST#search method --- spec/unit/indirector/rest.rb | 60 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 8 deletions(-) (limited to 'spec') 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 -- cgit