1919require File . expand_path ( File . join ( File . dirname ( __FILE__ ) , ".." , "spec_helper" ) )
2020
2121describe Chef ::CouchDB , "new" do
22+ before do
23+ @mock_rest = mock ( "Chef::REST" , :null_object => true )
24+ @mock_rest . stub! ( :run_request ) . and_return ( { "couchdb" => "Welcome" , "version" => "0.9.0" } )
25+ @mock_rest . stub! ( :url ) . and_return ( "http://localhost:5984" )
26+ Chef ::REST . stub! ( :new ) . and_return ( @mock_rest )
27+ end
28+
2229 it "should create a new Chef::REST object from the default url" do
2330 Chef ::Config [ :couchdb_url ] = "http://monkey"
24- Chef ::REST . should_receive ( :new ) . with ( "http://monkey" ) . and_return ( true )
31+ Chef ::REST . should_receive ( :new ) . with ( "http://monkey" )
2532 Chef ::CouchDB . new
2633 end
2734
2835 it "should create a new Chef::REST object from a provided url" do
29- Chef ::REST . should_receive ( :new ) . with ( "http://monkeypants" ) . and_return ( true )
36+ Chef ::REST . should_receive ( :new ) . with ( "http://monkeypants" )
3037 Chef ::CouchDB . new ( "http://monkeypants" )
38+ end
39+
40+ it "should parse the CouchDB version number" do
41+
3142 end
3243end
3344
@@ -185,18 +196,42 @@ def do_delete(rev=nil)
185196
186197describe Chef ::CouchDB , "list" do
187198 before ( :each ) do
188- @mock_rest = mock ( "Chef::REST" , :null_object => true )
199+ @mock_rest = mock ( "Chef::REST" , :null_object => true , :url => "http://monkeypants" )
189200 Chef ::REST . stub! ( :new ) . and_return ( @mock_rest )
201+ @couch = Chef ::CouchDB . new ( "http://monkeypants" )
202+ Chef ::Config . stub! ( :[] ) . with ( :couchdb_database ) . and_return ( "chef" )
190203 end
191204
192- it "should get the view for all objects if inflate is true" do
193- @mock_rest . should_receive ( :get_rest ) . with ( "chef/_view/node/all" ) . and_return ( true )
194- Chef ::CouchDB . new . list ( "node" , true )
205+ describe "on couchdb 0.8" do
206+ before do
207+ Chef ::Config . stub! ( :[] ) . with ( :couchdb_version ) . and_return ( 0.8 )
208+ end
209+
210+ it "should get the view for all objects if inflate is true" do
211+ @mock_rest . should_receive ( :get_rest ) . with ( "chef/_view/node/all" ) . and_return ( true )
212+ @couch . list ( "node" , true )
213+ end
214+
215+ it "should get the view for just the object id's if inflate is false" do
216+ @mock_rest . should_receive ( :get_rest ) . with ( "chef/_view/node/all_id" ) . and_return ( true )
217+ @couch . list ( "node" , false )
218+ end
195219 end
196-
197- it "should get the view for just the object id's if inflate is false" do
198- @mock_rest . should_receive ( :get_rest ) . with ( "chef/_view/node/all_id" ) . and_return ( true )
199- Chef ::CouchDB . new . list ( "node" , false )
220+
221+ describe "on couchdb 0.9" do
222+ before do
223+ Chef ::Config . stub! ( :[] ) . with ( :couchdb_version ) . and_return ( 0.9 )
224+ end
225+
226+ it "should get the view for all objects if inflate is true" do
227+ @mock_rest . should_receive ( :get_rest ) . with ( "chef/_design/node/_view/all" ) . and_return ( true )
228+ @couch . list ( "node" , true )
229+ end
230+
231+ it "should get the view for just the object id's if inflate is false" do
232+ @mock_rest . should_receive ( :get_rest ) . with ( "chef/_design/node/_view/all_id" ) . and_return ( true )
233+ @couch . list ( "node" , false )
234+ end
200235 end
201236end
202237
@@ -216,3 +251,45 @@ def do_delete(rev=nil)
216251 Chef ::CouchDB . new . has_key? ( "node" , "bob" ) . should eql ( false )
217252 end
218253end
254+
255+ describe Chef ::CouchDB , "safe_name" do
256+ before do
257+ @couchdb = mock ( "Chef::CouchDB" , :null_object => true )
258+ Chef ::CouchDB . stub! ( :new ) . and_return ( @couchdb )
259+ end
260+
261+ it "should convert the name to a safe name" do
262+ @couchdb . should_receive ( :safe_name ) . with ( "asdf.lol.com" ) . and_return ( "asdf_lol_com" )
263+ @couchdb . safe_name ( "asdf.lol.com" )
264+ end
265+ end
266+
267+ describe Chef ::CouchDB , "view_uri" do
268+ before do
269+ @couchdb = mock ( "Chef::CouchDB" , :null_object => true )
270+ Chef ::CouchDB . stub! ( :new ) . and_return ( @couchdb )
271+ Chef ::Config . stub! ( :[] ) . with ( :couchdb_database ) . and_return ( "chef" )
272+ end
273+
274+ describe "on couchdb 0.8" do
275+ before do
276+ Chef ::Config . stub! ( :[] ) . with ( :couchdb_version ) . and_return ( 0.8 )
277+ end
278+
279+ it "should output an appropriately formed view URI" do
280+ @couchdb . should_receive ( :view_uri ) . with ( "nodes" , "all" ) . and_return ( "chef/_view/nodes/all" )
281+ @couchdb . view_uri ( "nodes" , "all" )
282+ end
283+ end
284+
285+ describe "on couchdb 0.9" do
286+ before do
287+ Chef ::Config . stub! ( :[] ) . with ( :couchdb_version ) . and_return ( 0.9 )
288+ end
289+
290+ it "should output an appropriately formed view URI" do
291+ @couchdb . should_receive ( :view_uri ) . with ( "nodes" , "all" ) . and_return ( "chef/_design/nodes/_view/all" )
292+ @couchdb . view_uri ( "nodes" , "all" )
293+ end
294+ end
295+ end
0 commit comments