diff options
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/data/map+reduce.txt | 3 | ||||
-rw-r--r-- | spec/unit/data/map.txt | 4 | ||||
-rw-r--r-- | spec/unit/data/missing.txt | 1 | ||||
-rw-r--r-- | spec/unit/data/one-document.txt | 1 | ||||
-rw-r--r-- | spec/unit/data/proxy-failure.txt | 1 | ||||
-rwxr-xr-x | spec/unit/puppet/parser/functions/couchdblookup_spec.rb | 89 |
6 files changed, 99 insertions, 0 deletions
diff --git a/spec/unit/data/map+reduce.txt b/spec/unit/data/map+reduce.txt new file mode 100644 index 0000000..71e1836 --- /dev/null +++ b/spec/unit/data/map+reduce.txt @@ -0,0 +1,3 @@ +{"rows":[ +{"key":1,"value":["foo","bar","baz"]} +]} diff --git a/spec/unit/data/map.txt b/spec/unit/data/map.txt new file mode 100644 index 0000000..4335913 --- /dev/null +++ b/spec/unit/data/map.txt @@ -0,0 +1,4 @@ +{"total_rows":2,"offset":0,"rows":[ +{"id":"foo","key":1,"value":"foo"}, +{"id":"bar","key":2,"value":"bar"} +]} diff --git a/spec/unit/data/missing.txt b/spec/unit/data/missing.txt new file mode 100644 index 0000000..4e6d97e --- /dev/null +++ b/spec/unit/data/missing.txt @@ -0,0 +1 @@ +{"error":"not_found","reason":"missing"} diff --git a/spec/unit/data/one-document.txt b/spec/unit/data/one-document.txt new file mode 100644 index 0000000..071cd7e --- /dev/null +++ b/spec/unit/data/one-document.txt @@ -0,0 +1 @@ +{"_id":"foobar","_rev":"1-1750d8614a19943f36b545e7c5549503","wiki":true,"rt":true,"svn":true,"dav":null} diff --git a/spec/unit/data/proxy-failure.txt b/spec/unit/data/proxy-failure.txt new file mode 100644 index 0000000..f5f1c37 --- /dev/null +++ b/spec/unit/data/proxy-failure.txt @@ -0,0 +1 @@ +<html><body><h1>It works!</h1></body></html> diff --git a/spec/unit/puppet/parser/functions/couchdblookup_spec.rb b/spec/unit/puppet/parser/functions/couchdblookup_spec.rb new file mode 100755 index 0000000..8777d71 --- /dev/null +++ b/spec/unit/puppet/parser/functions/couchdblookup_spec.rb @@ -0,0 +1,89 @@ +#!/usr/bin/env rspec + +require 'puppet' + +describe "the couchdblookup function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + @datapath = File.dirname(__FILE__) + '/../../../data/' + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("couchdblookup").should == "function_couchdblookup" + end + + it "should raise a ParseError unless there is exactly 2 arguments" do + lambda { @scope.function_couchdblookup([]) }.should raise_error(Puppet::ParseError) + lambda { @scope.function_couchdblookup([1]) }.should raise_error(Puppet::ParseError) + lambda { @scope.function_couchdblookup([1,2,3]) }.should raise_error(Puppet::ParseError) + end + + it "should return the value of a key from a single couchdb document" do + sample_json = File.open(@datapath + 'one-document.txt') + OpenURI.stub!(:open_uri).and_return(sample_json) + + result = @scope.function_couchdblookup(["http://fake/uri", "wiki"]) + result.should eq(true) + end + + it "should raise a ParseError if a key can't be found in a couchdb document" do + sample_json = File.open(@datapath + 'one-document.txt') + OpenURI.stub!(:open_uri).and_return(sample_json) + + result = lambda { @scope.function_couchdblookup(["http://fake/uri", "fake-key"]) } + result.should raise_error(Puppet::ParseError) + end + + it "should return an array from the values of a couchdb view" do + sample_json = File.open(@datapath + 'map.txt') + OpenURI.stub!(:open_uri).and_return(sample_json) + + result = @scope.function_couchdblookup(["http://fake/uri", "value"]) + result.should eq(["foo", "bar"]) + end + + it "should raise a ParseError if a key can't be found in the rows of a couchdb view" do + sample_json = File.open(@datapath + 'map.txt') + OpenURI.stub!(:open_uri).and_return(sample_json) + + result = lambda { @scope.function_couchdblookup(["http://fake/uri", "fake-key"]) } + result.should raise_error(Puppet::ParseError) + end + + it "should return an array the values from a couchdb reduced view" do + sample_json = File.open(@datapath + 'map+reduce.txt') + OpenURI.stub!(:open_uri).and_return(sample_json) + + result = @scope.function_couchdblookup(["http://fake/uri", "value"]) + result.should eq(["foo", "bar", "baz"]) + end + + it "should raise a ParseError if a key can't be found in the rows of a couchdb reduced view" do + sample_json = File.open(@datapath + 'map+reduce.txt') + OpenURI.stub!(:open_uri).and_return(sample_json) + + result = lambda { @scope.function_couchdblookup(["http://fake/uri", "fake-key"]) } + result.should raise_error(Puppet::ParseError) + end + + it "should raise a ParseError if couchdb can't find the requested document" do + sample_json = File.open(@datapath + 'missing.txt') + OpenURI.stub!(:open_uri).and_return(sample_json) + + result = lambda { @scope.function_couchdblookup(["http://fake/uri", "a-key"]) } + result.should raise_error(Puppet::ParseError) + end + + it "should raise a ParseError if input in not valid JSON" do + sample_json = File.open(@datapath + 'proxy-failure.txt') + OpenURI.stub!(:open_uri).and_return(sample_json) + + result = lambda { @scope.function_couchdblookup(["http://fake/uri", "a-key"]) } + result.should raise_error(Puppet::ParseError) + end + +end |