spec/hotseat/queue_spec.rb in hotseat-0.3.0 vs spec/hotseat/queue_spec.rb in hotseat-0.4.0

- old
+ new

@@ -112,10 +112,31 @@ patch = @doc[@q.config[:object_name]] patch.should have_key('done') end end + describe "#done?" do + before(:each) do + reset_test_queue! + end + it "should be true if the document was marked as done" do + doc = @q.mark_done( @q.patch( sample_doc ) ) + @q.done?(doc).should be_true + end + it "should be false for a patched document that was not marked as done" do + doc = @q.patch( sample_doc ) + @q.done?(doc).should be_false + end + it "should be false for a locked document" do + doc = @q.add_lock( @q.patch( sample_doc ) ) + @q.done?(doc).should be_false + end + it "should be false for an un-patched document (not part of the queue)" do + @q.done?(sample_doc).should be_false + end + end + describe "#add" do before(:each) do reset_test_queue! @doc_id = DB.save_doc(sample_doc)['id'] end @@ -413,9 +434,65 @@ @q.remove_bulk @doc_ids, :forget => true docs = DB.get_bulk(@doc_ids)['rows'].map{|row| row['doc']} docs.each do |doc| doc.should_not have_key(@q.config[:object_name]) end + end + end + + describe "#undo" do + before(:all) do + reset_test_queue! + doc_ids = create_some_docs(3) + enqueue doc_ids + @done_ids = [doc_ids[1]] + @undone_ids = [doc_ids[0], doc_ids[2]] + @done_ids.each do |done_id| + done_doc = DB.get done_id + @q.mark_done done_doc + done_doc.save + end + end + + it "should mark the done document as pending again" do + @q.undo @done_ids.first + done_doc = DB.get @done_ids.first + patch = done_doc[@q.config[:object_name]] + patch.should_not have_key('done') + patch.should_not have_key('lock') + end + + it "should raise an error if the document is not done" do + undone_doc = DB.get @undone_ids.first + expect { + @q.undo @undone_ids.first + }.to raise_error(Hotseat::QueueError) + end + end + + describe "#undo_bulk" do + before(:each) do + reset_test_queue! + doc_ids = create_some_docs(5) + enqueue doc_ids + @done_docs = doc_ids[0..2].map{|id| DB.get id } + @done_docs.each do |doc| + @q.mark_done doc + doc.save + end + end + + it "should mark all 'done' documents as pending again" do + @q.undo_bulk @done_docs.map{|doc| doc['_id']} + @q.num_done.should == 0 + @q.num_pending.should == 5 + end + + it "should leave not-done documents intact" do + @q.lease + @q.undo_bulk @done_docs.map{|doc| doc['_id']} + @q.num_locked.should == 1 + @q.num_pending.should == 4 end end describe "#num_done" do it "should return the number of documents done" do