Skip to content

Instantly share code, notes, and snippets.

@gangster
Created January 10, 2011 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangster/772497 to your computer and use it in GitHub Desktop.
Save gangster/772497 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe Iterator do
let(:bar) { Factory(:bar) }
let(:worker) { Factory(:worker) }
before(:all) do
hit_type = Factory(:hit_type)
hit_type.register!
end
before(:each) do
@hit_type = HitType.first
raise if @hit_type.nil?
end
after(:all) do
HitType.delete_all
end
context "after initialization" do
before do
@iterator = Factory.build(:iterator, :original => bar, :hit_type => @hit_type)
end
subject { @iterator }
context "the current iteration" do
subject { @iterator.current_iteration }
it { should eql 0 }
end
context "the hit count" do
subject { @iterator.hits.size }
specify { should eql 0 }
end
end
context "after created it" do
before do
@iterator = Factory(:iterator, :original => bar, :hit_type => @hit_type)
end
subject { @iterator }
context "the target" do
subject { @iterator.target }
it { should_not be_nil }
it { should_not be_empty }
it { should respond_to :keys } # make sure its a hash
it { should respond_to :values }
it { should have_key :model_name }
it { should have_key :_id }
# TODO: Figure out a way to loop over attributes and test them here.
it { should have_key :name }
end
end
context "max_iterations" do
before do
@worker1 = worker
@worker2 = Factory(:worker)
@worker3 = Factory(:worker)
@worker4 = Factory(:worker)
end
context "is 1" do
context "attempting to submit a second assignment" do
before do
@iterator = Factory(:iterator, :max_iterations => 1, :original => bar, :hit_type => @hit_type)
@iterator.iterate!
@assignment1 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => Factory.next(:workerId),
:hit => @iterator.hit)
@assignment1.answers = Factory.build(:completed_assignment).answers
@iterator.submit(@assignment1)
@assignment2 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => Factory.next(:workerId),
:hit => @iterator.hit)
end
subject { @assignment2 }
specify { should be_nil }
end
end
context "is 3" do
context "attempting to submit a fourth assignment" do
before do
@iterator = Factory(:iterator, :max_iterations => 3, :original => bar, :hit_type => @hit_type)
@iterator.iterate!
@assignment1 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => Factory.next(:workerId),
:hit => @iterator.hit)
@iterator.submit(@assignment1)
@assignment2 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => Factory.next(:workerId),
:hit => @iterator.hit)
@iterator.submit(@assignment2)
@assignment3 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => Factory.next(:workerId),
:hit => @iterator.hit)
@iterator.submit(@assignment3)
@assignment4 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => Factory.next(:workerId),
:hit => @iterator.hit)
end
subject { @assignment4 }
specify { should be_nil}
end
end
end
context "worker submits more than one assignment" do
before do
@iterator = Factory(:iterator, :max_iterations => 2, :original => bar, :hit_type => @hit_type)
@iterator.iterate!
workerId = Factory.next(:workerId)
@assignment1 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => workerId,
:hit => @iterator.hit)
@iterator.submit(@assignment1)
@assignment2 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => workerId,
:hit => @iterator.hit)
end
context "submit" do
subject { @iterator }
specify { subject.submit(@assignment2).should be_false }
end
end
context "iterate!" do
before do
@iterator = Factory(:iterator, :original => bar, :hit_type => @hit_type )
@iterator.iterate!
@iterator
end
context "the current iteration" do
subject { @iterator }
specify { subject.current_iteration.should eql 1 }
end
context "the hit count" do
subject { @iterator.hits }
specify { subject.size.should eql 1 }
end
context "calling iterate! when a hit is already assigned " do
subject { @iterator }
# specify { expect{subject.iterate!}.to raise_error }
end
end
context "submit" do
before do
@iterator = Factory(:iterator, :original => bar, :hit_type => @hit_type, :max_iterations => 2 )
@iterator.iterate!
end
context "a valid assignment" do
before do
@assignment = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => Factory.next(:workerId),
:hit => @iterator.hit)
end
subject { @iterator }
specify { subject.submit(@assignment).should be_true }
context "after being submitted" do
before do
@iterator.submit(@assignment)
end
context "the assignment state" do
subject { @assignment }
it { should be_reviewing }
end
context "the hit state" do
subject { @assignment.hit }
it { should be_reviewing }
end
context "the total hit count" do
subject { @iterator.hits.count }
specify { should eql 2 }
end
context "the assignable hit count" do
subject { @iterator.hits.assignable.count }
specify { should eql 1 }
end
context "the reviewing hit count" do
subject { @iterator.hits.reviewing.count }
specify { should eql 1 }
end
context "the rejected hit count" do
subject { @iterator.hits.rejected.count }
specify { should eql 0 }
end
end
end
context "an invalid assignment with all required fields missing" do
before do
@assignment = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => Factory.next(:workerId),
:hit => @iterator.hit)
@assignment.answers = Factory.build(:incomplete_assignment).answers
end
subject { @iterator }
context "after being submitted" do
before do
@iterator.submit(@assignment)
end
context "the assignment state" do
subject { @assignment }
it { should be_rejected }
end
context "the hit state" do
subject { @assignment.hit }
it { should be_rejected }
end
context "the rejection reason" do
subject { @assignment.rejection_reason }
it { should eql I18n.translate('assignment.rejection_reason.invalid_answers') }
end
context "the total hit count" do
subject { @iterator.hits.count }
specify { should eql 2 }
end
context "the assignable hit count" do
subject { @iterator.hits.assignable.count }
specify { should eql 1 }
end
context "the rejected hit count" do
subject { @iterator.hits.rejected.count }
specify { should eql 1 }
end
context "the reviewing hit count" do
subject { @iterator.hits.reviewing.count }
specify { should eql 0 }
end
end
end
end
context "when creating assignments" do
before do
@iterator = Factory(:iterator, :original => bar, :hit_type => @hit_type)
@iterator.iterate!
@hit = @iterator.hit
end
subject { @iterator }
specify { should respond_to :assign }
context "first time assignment for worker" do
before do
@assignment = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => Factory.next(:workerId),
:hit => @iterator.hit)
end
subject { @assignment }
it { should be }
context "state" do
subject { @assignment }
it { should be_assigned }
end
context "hit" do
subject {@assignment.hit}
it { should eql @hit }
end
end
context "second time assignment for worker" do
before do
@workerId = Factory.next(:workerId)
@assignment = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => @workerId,
:hit => @iterator.hit )
@assignment.answers = Factory.build(:completed_assignment).answers
@iterator.submit(@assignment)
end
subject { @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => @workerId,
:hit => @iterator.hit ) }
it { should be_nil }
end
end
context "when 2 workers submit valid assignments to iterator w/ 3 iterations" do
before do
@iterator = Factory(:iterator, :original => bar, :hit_type => @hit_type, :max_iterations => 3)
@iterator.iterate!
@hit = @iterator.hit
@workerId1 = Factory.next(:workerId)
@workerId2 = Factory.next(:workerId)
@workerId3 = Factory.next(:workerId)
@assignment1 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => @workerId1,
:hit => @iterator.hit )
@assignment1.answers = Factory.build(:completed_assignment).answers
@iterator.submit(@assignment1)
@assignment2 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => @workerId2,
:hit => @iterator.hit )
end
subject { @assignment2 }
it { should_not be_nil }
context "after second guy submits, his assignment" do
before do
@iterator.submit(@assignment2)
end
subject { @assignment2 }
it { should be_reviewing }
end
context "after second guy submits, the iterator" do
before do
@iterator.submit(@assignment2)
end
subject { @iterator }
it { should be_iterating }
end
end
context "when 3 workers submit valid assignments to iterator w/3 iterations" do
before do
@iterator = Factory(:iterator, :original => bar, :hit_type => @hit_type, :max_iterations => 3)
@iterator.iterate!
@hit = @iterator.hit
@workerId1 = Factory.next(:workerId)
@workerId2 = Factory.next(:workerId)
@workerId3 = Factory.next(:workerId)
@assignment1 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => @workerId1,
:hit => @iterator.hit )
@assignment1.answers = Factory.build(:completed_assignment).answers
@iterator.submit(@assignment1)
@assignment2 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => @workerId2,
:hit => @iterator.hit )
@iterator.submit(@assignment2)
@assignment3 = @iterator.assign(:assignmentId => Factory.next(:assignmentId),
:workerId => @workerId3,
:hit => @iterator.hit )
end
subject { @assignment3 }
it { should_not be_nil }
context "after third guy submits, his assignment" do
before do
@iterator.submit(@assignment3)
end
subject { @assignment3 }
it { should be_reviewing }
end
context "after third guy submits, the iterator" do
before do
@iterator.submit(@assignment3)
end
subject { @iterator }
it { should be_completed }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment