require 'spec_helper' describe CouchPotato::View::ViewQuery, 'query_view!' do before(:each) do CouchRest.stub(:get => nil) end it "does not pass a key if conditions are empty" do db = mock 'db', :get => nil, :save_doc => nil db.should_receive(:view).with(anything, {}) CouchPotato::View::ViewQuery.new(db, '', {:view0 => {}}).query_view! end it 'updates a view if it does not exist' do db = mock 'db', :get => nil, :view => nil db.should_receive(:save_doc).with( 'views' => {'view' => {'map' => '', 'reduce' => ''}}, 'lists' => {}, "_id" => "_design/design", "language" => "javascript") CouchPotato::View::ViewQuery.new(db, 'design', :view => {:map => '', :reduce => ''}).query_view! end it 'updates a view in erlang if it does not exist' do db = mock 'db', :get => nil, :view => nil db.should_receive(:save_doc).with( 'views' => {'view' => {'map' => '', 'reduce' => ''}}, 'lists' => {}, "_id" => "_design/design", "language" => "erlang") CouchPotato::View::ViewQuery.new(db, 'design', {:view => {:map => '', :reduce => ''}}, nil, :erlang).query_view! end it "does not update a view when the map/reduce functions haven't changed" do db = mock 'db', :get => {'views' => {'view' => {'map' => '', 'reduce' => ''}}}, :view => nil db.should_not_receive(:save_doc) CouchPotato::View::ViewQuery.new(db, 'design', :view => {:map => '', :reduce => ''}).query_view! end it "does not update a view when the list function hasn't changed" do db = mock 'db', :get => {'views' => {'view' => {'map' => '', 'reduce' => ''}}, 'lists' => {'list0' => ''}}, :view => nil db.should_not_receive(:save_doc) CouchPotato::View::ViewQuery.new(db, 'design', {:view => {:map => '', :reduce => ''}}, :list0 => '').query_view! end it "updates a view when the map function has changed" do db = mock 'db', :get => {'views' => {'view2' => {'map' => '', 'reduce' => ''}}}, :view => nil db.should_receive(:save_doc) CouchPotato::View::ViewQuery.new(db, 'design', :view2 => {:map => '', :reduce => ''}).query_view! end it "does not pass in a reduce key if there is no reduce function" do db = mock 'db', :get => {'views' => {}}, :view => nil db.should_receive(:save_doc).with('views' => {'view3' => {'map' => ''}}) CouchPotato::View::ViewQuery.new(db, 'design', :view3 => {:map => '', :reduce => nil}).query_view! end it "updates a view when the reduce function has changed" do db = mock 'db', :get => {'views' => {'view4' => {'map' => '', 'reduce' => ''}}}, :view => nil db.should_receive(:save_doc) CouchPotato::View::ViewQuery.new(db, 'design', :view4 => {:map => '', :reduce => ''}).query_view! end it "updates a view when the list function has changed" do db = mock 'db', :get => { 'views' => {'view5' => {'map' => '', 'reduce' => ''}}, 'lists' => {'list1' => ''} }, :view => nil db.should_receive(:save_doc) CouchPotato::View::ViewQuery.new(db, 'design', {:view5 => {:map => '', :reduce => ''}}, :list1 => '').query_view! end it "updates a view when there wasn't a list function but now there is one" do db = mock 'db', :get => { 'views' => {'view6' => {'map' => '', 'reduce' => ''}} }, :view => nil db.should_receive(:save_doc) CouchPotato::View::ViewQuery.new(db, 'design', {:view6 => {:map => '', :reduce => ''}}, :list1 => '').query_view! end it "does not update a view when there is a list function but no list function is passed" do db = mock 'db', :get => { 'views' => {'view7' => {'map' => '', 'reduce' => ''}}, 'lists' => {'list1' => ''} }, :view => nil db.should_not_receive(:save_doc) CouchPotato::View::ViewQuery.new(db, 'design', {:view7 => {:map => '', :reduce => ''}}, {}).query_view! end it "does not update a view when there were no lists before and no list function is passed" do db = mock 'db', :get => { 'views' => {'view8' => {'map' => '', 'reduce' => ''}} }, :view => nil db.should_not_receive(:save_doc) CouchPotato::View::ViewQuery.new(db, 'design', {:view8 => {:map => '', :reduce => ''}}, {}).query_view! end it "queries CouchRest directly when querying a list" do db = stub('db').as_null_object CouchRest.should_receive(:get).with('http://127.0.0.1:5984/couch_potato_test/_design/my_design/_list/list1/view9?key=1') CouchPotato::View::ViewQuery.new(db, 'my_design', {:view9 => {:map => '', :reduce => ''}}, :list1 => '').query_view!(:key => 1) end end