spec/beso/job_spec.rb in beso-0.2.1 vs spec/beso/job_spec.rb in beso-0.3.0

- old
+ new

@@ -1,19 +1,17 @@ require 'spec_helper' describe Beso::Job do + fixtures :all - before do - User.destroy_all - end + let!( :foo ){ users( :foo ) } + let!( :bar ){ users( :bar ) } + let!( :baz ){ users( :baz ) } describe 'to_csv' do subject { Beso::Job.new :message_sent, :table => :users } - let!( :foo ){ User.create! :name => 'Foo' } - let!( :bar ){ User.create! :name => 'Bar' } - context 'without an identity defined' do before do subject.timestamp :created_at end it 'should raise an error' do @@ -40,61 +38,57 @@ before do subject.identity { |user| user.id } subject.timestamp :created_at end - its( :to_csv ){ should eq( <<-EOS -Identity,Timestamp,Event -#{foo.id},#{foo.created_at.to_i},Message Sent -#{bar.id},#{bar.created_at.to_i},Message Sent - EOS - ) } + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end context 'with custom properties defined' do before do subject.identity { |user| user.id } subject.timestamp :created_at subject.prop( :user_name ){ |user| user.name } end - its( :to_csv ){ should eq( <<-EOS -Identity,Timestamp,Event,Prop:User Name -#{foo.id},#{foo.created_at.to_i},Message Sent,Foo -#{bar.id},#{bar.created_at.to_i},Message Sent,Bar - EOS - ) } + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end context 'with literal properties defined' do before do subject.identity 22 subject.timestamp :created_at subject.prop :foo, 'bar' end - its( :to_csv ){ should eq( <<-EOS -Identity,Timestamp,Event,Prop:Foo -22,#{foo.created_at.to_i},Message Sent,bar -22,#{bar.created_at.to_i},Message Sent,bar - EOS - ) } + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end context 'with symbol properties defined' do before do subject.identity :id subject.timestamp :created_at subject.prop :name end - its( :to_csv ){ should eq( <<-EOS -Identity,Timestamp,Event,Prop:Name -#{foo.id},#{foo.created_at.to_i},Message Sent,#{foo.name} -#{bar.id},#{bar.created_at.to_i},Message Sent,#{bar.name} - EOS - ) } + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end context 'with 10 custom properties defined' do before do subject.identity 22 @@ -102,16 +96,15 @@ (1..10).each do |i| subject.prop :"foo #{i}", i end end - its( :to_csv ){ should eq( <<-EOS -Identity,Timestamp,Event,Prop:Foo 1,Prop:Foo 2,Prop:Foo 3,Prop:Foo 4,Prop:Foo 5,Prop:Foo 6,Prop:Foo 7,Prop:Foo 8,Prop:Foo 9,Prop:Foo 10 -22,#{foo.created_at.to_i},Message Sent,1,2,3,4,5,6,7,8,9,10 -22,#{bar.created_at.to_i},Message Sent,1,2,3,4,5,6,7,8,9,10 - EOS - ) } + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end context 'with more than 10 custom properties defined' do before do subject.identity 22 @@ -130,17 +123,14 @@ before do subject.identity { |user| user.id } subject.timestamp :created_at end - it 'should support all options that CSV supports' do - subject.to_csv( :force_quotes => true, :col_sep => ';' ).should eq( <<-EOS -"Identity";"Timestamp";"Event" -"#{foo.id}";"#{foo.created_at.to_i}";"Message Sent" -"#{bar.id}";"#{bar.created_at.to_i}";"Message Sent" - EOS - ) + it 'should generate the correct to_csv' do + verify do + subject.to_csv :force_quotes => true, :col_sep => ';' + end end end context 'with custom options given to constructor' do subject { Beso::Job.new :message_sent, :table => :users, :col_sep => ';' } @@ -148,16 +138,15 @@ before do subject.identity { |user| user.id } subject.timestamp :created_at end - its( :to_csv ){ should eq( <<-EOS -Identity;Timestamp;Event -#{foo.id};#{foo.created_at.to_i};Message Sent -#{bar.id};#{bar.created_at.to_i};Message Sent - EOS - ) } + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end context 'when no records match the query' do subject { Beso::Job.new :message_sent, :table => :users } @@ -170,29 +159,25 @@ its( :to_csv ){ should be_nil } end end describe 'with since specified' do - let!( :foo ){ User.create :name => 'Foo', :created_at => 100, :updated_at => 300 } - let!( :bar ){ User.create :name => 'Bar', :created_at => 200, :updated_at => 200 } - let!( :baz ){ User.create :name => 'Baz', :created_at => 300, :updated_at => 300 } context 'in the constructor' do context 'and the timestamp keyed on `created_at`' do subject { Beso::Job.new :message_sent, :table => :users, :since => 101 } before do subject.identity { |user| user.id } subject.timestamp :created_at end - its( :to_csv ){ should eq( <<-EOS -Identity,Timestamp,Event -#{bar.id},#{bar.created_at.to_i},Message Sent -#{baz.id},#{baz.created_at.to_i},Message Sent - EOS - ) } + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end end context 'in to_csv' do context 'and the timestamp keyed on `created_at`' do @@ -201,17 +186,14 @@ before do subject.identity { |user| user.id } subject.timestamp :created_at end - it 'should find records after `since`' do - subject.to_csv( :since => 101 ).should eq( <<-EOS -Identity,Timestamp,Event -#{bar.id},#{bar.created_at.to_i},Message Sent -#{baz.id},#{baz.created_at.to_i},Message Sent - EOS - ) + it 'should generate the correct to_csv' do + verify do + subject.to_csv :since => 101 + end end end end context 'in the constructor' do @@ -221,16 +203,15 @@ before do subject.identity { |user| user.id } subject.timestamp :updated_at end - its( :to_csv ){ should eq( <<-EOS -Identity,Timestamp,Event -#{foo.id},#{foo.updated_at.to_i},Message Sent -#{baz.id},#{baz.updated_at.to_i},Message Sent - EOS - ) } + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end end context 'in to_csv' do context 'and the timestamp keyed on `updated_at`' do @@ -239,44 +220,35 @@ before do subject.identity { |user| user.id } subject.timestamp :updated_at end - it 'should find records after `since`' do - subject.to_csv( :since => 201 ).should eq( <<-EOS -Identity,Timestamp,Event -#{foo.id},#{foo.updated_at.to_i},Message Sent -#{baz.id},#{baz.updated_at.to_i},Message Sent - EOS - ) + it 'should generate the correct to_csv' do + verify do + subject.to_csv :since => 201 + end end end end end describe 'custom event names' do - let!( :foo ){ User.create :name => 'Foo' } - subject { Beso::Job.new :message_sent, :table => :users, :event => 'Messages Sent Action' } before do subject.identity :id subject.timestamp :created_at end - its( :to_csv ){ should eq( <<-EOS -Identity,Timestamp,Event -#{foo.id},#{foo.updated_at.to_i},Messages Sent Action - EOS - ) } + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end describe '#last_timestamp' do - let!( :foo ){ User.create :name => 'Foo', :created_at => 100, :updated_at => 301 } - let!( :bar ){ User.create :name => 'Bar', :created_at => 200, :updated_at => 200 } - let!( :baz ){ User.create :name => 'Baz', :created_at => 300, :updated_at => 300 } - subject { Beso::Job.new :message_sent, :table => :users } before do subject.identity :id end @@ -293,8 +265,40 @@ before do subject.timestamp :updated_at end its( :last_timestamp ){ should eq( 301 ) } + end + end + + describe 'scopes' do + context 'when no explicit scope is given' do + subject { Beso::Job.new :message_sent, :table => :users } + + before do + subject.identity :id + subject.timestamp :updated_at + end + + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end + end + + context 'overriding the default scope' do + subject { Beso::Job.new :message_sent, :table => :users, :scope => lambda { unscoped } } + + before do + subject.identity :id + subject.timestamp :updated_at + end + + it 'should generate the correct to_csv' do + verify do + subject.to_csv + end + end end end end