require File.dirname(__FILE__) + '/spec_helper' describe TwitterSearchWatcher, 'searching' do it 'should be able to execute search' do watcher = TSW.new 'chunky bacon' TSW.should_receive(:get).with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(fake_response) response = watcher.search! response.should be_a_kind_of(Hash) response['query'].should == 'remitaylor' # make sure it's getting our fake date end it 'should be able to search!(:rpp => 15) ... inotherwords, should be able to pass additional parameters to search!' do watcher = TSW.new 'foo' TSW.should_receive(:get).with(/99/, 'User-Agent' => watcher.user_agent).and_return(fake_response) watcher.search! :rpp => 99 TSW.should_receive(:get).with(/foo/, 'User-Agent' => watcher.user_agent).and_return(fake_response) watcher.search! :rpp => 99 end it 'should be able to search!(:rpp => nil) to override an existing parameter' do watcher = TSW.new 'foo', :rpp => 50 watcher.search_url.should include('rpp=50') watcher.search_url.should include('q=foo') # double check that normal overrides are working properly watcher.search_url( :rpp => 99 ).should_not include('rpp=50') watcher.search_url( :rpp => 99 ).should include('rpp=99') watcher.search_url( :rpp => nil ).should_not include('rpp=50') watcher.search_url( :rpp => nil ).should_not include('rpp') end it 'should be able to search_newer!(result_set) to get results newer than a given result set' do watcher = TSW.new 'chunky bacon' TSW.should_receive(:get).with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(fake_response(:max_id => 5555)) response = watcher.search! TSW.should_receive(:get).with(/since_id=5555/, 'User-Agent' => watcher.user_agent).and_return(fake_response) watcher.search_newer! response end it 'should be able to search_more!(result_set) to get paginated results' do with_next_page = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=444&q=chunky+bacon' without_next_page = fake_response :page => 1, :max_id => 2, :next_page => nil watcher = TSW.new 'chunky bacon' TSW.should_receive(:get).once.with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(with_next_page) response = watcher.search! TSW.should_receive(:get).once.with(/page=2/, 'User-Agent' => watcher.user_agent).and_return(without_next_page) response2 = watcher.search_more! response watcher.should_not_receive(:open) watcher.search_more!( response2 ).should be_nil # because no next_page end it 'should be able to watch! on a watcher' do # it's a PITA to test the loop, so i'm not currently testing that check_every and max_pages are actually used correctly watcher = TSW.new 'chunky bacon', :check_every => 120, :max_pages => 5 watcher.check_every.should == 120 watcher.max_pages.should == 5 watcher.should respond_to(:watch!) end it 'should be able to TSW.search_with_pagination!' do _2_pages_left = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=1&q=foo' _1_page_left = fake_response :page => 2, :max_id => 2, :next_page => '?page=3&max_id=1&q=foo' _0_pages_left = fake_response :page => 3, :max_id => 3, :next_page => nil TSW.should_receive(:get).once.with(/q=foo/, anything).and_return(_2_pages_left) TSW.should_receive(:get).once.with(/page=2/, anything).and_return(_1_page_left) TSW.should_receive(:get).once.with(/page=3/, anything).and_return(_0_pages_left) TSW.search_with_pagination! 'foo' end it 'should be able to search_with_pagination!' do _2_pages_left = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=1&q=foo' _1_page_left = fake_response :page => 2, :max_id => 2, :next_page => '?page=3&max_id=1&q=foo' _0_pages_left = fake_response :page => 3, :max_id => 3, :next_page => nil watcher = TSW.new 'foo' TSW.should_receive(:get).once.with(/q=foo/, anything).and_return(_2_pages_left) TSW.should_receive(:get).once.with(/page=2/, anything).and_return(_1_page_left) TSW.should_receive(:get).once.with(/page=3/, anything).and_return(_0_pages_left) watcher.search_with_pagination! end it 'search_with_pagination! should respect max_pages (instance)' do _2_pages_left = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=1&q=foo' _1_page_left = fake_response :page => 2, :max_id => 2, :next_page => '?page=3&max_id=1&q=foo' _0_pages_left = fake_response :page => 3, :max_id => 3, :next_page => nil watcher = TSW.new 'foo', :max_pages => 1 # it'll only get 1 page *after* doing the original search! request TSW.should_receive(:get).once.with(/q=foo/, 'User-Agent' => watcher.user_agent).and_return(_2_pages_left) TSW.should_receive(:get).once.with(/page=2/, 'User-Agent' => watcher.user_agent).and_return(_1_page_left) watcher.should_not_receive(:open) watcher.search_with_pagination! end it 'search_with_pagination! should respect max_pages (override)' do _2_pages_left = fake_response :page => 1, :max_id => 1, :next_page => '?page=2&max_id=1&q=foo' _1_page_left = fake_response :page => 2, :max_id => 2, :next_page => '?page=3&max_id=1&q=foo' _0_pages_left = fake_response :page => 3, :max_id => 3, :next_page => nil watcher = TSW.new 'foo', :max_pages => 99 TSW.should_receive(:get).once.with(watcher.search_url, 'User-Agent' => watcher.user_agent).and_return(_2_pages_left) TSW.should_receive(:get).once.with(/page=2/, 'User-Agent' => watcher.user_agent).and_return(_1_page_left) watcher.should_not_receive(:open) watcher.search_with_pagination! :max_pages => 1 end # Ideas ... #it 'should be able to search! (with pagination)' #it 'should default to getting additional pages when doing a search!' #it 'default rpp should be 100 (the max)' #it 'should have a nice method to call that will make a new watcher and start watching' #it 'should be able to watch! with pagination (will search! while there is a :next_page to request)' end