spec/unit/chunker_spec.rb in lhm-2.0.0 vs spec/unit/chunker_spec.rb in lhm-2.1.0

- old
+ new

@@ -4,108 +4,117 @@ require File.expand_path(File.dirname(__FILE__)) + '/unit_helper' require 'lhm/table' require 'lhm/migration' require 'lhm/chunker' +require 'lhm/throttler' describe Lhm::Chunker do include UnitHelper before(:each) do - @origin = Lhm::Table.new("origin") - @destination = Lhm::Table.new("destination") - @migration = Lhm::Migration.new(@origin, @destination) - @chunker = Lhm::Chunker.new(@migration, nil, { :start => 1, :limit => 10 }) - end - - describe "copy into" do - before(:each) do - @origin.columns["secret"] = { :metadata => "VARCHAR(255)"} - @destination.columns["secret"] = { :metadata => "VARCHAR(255)"} + @origin = Lhm::Table.new("foo") + @destination = Lhm::Table.new("bar") + @migration = Lhm::Migration.new(@origin, @destination) + @connection = MiniTest::Mock.new + # This is a poor man's stub + @throttler = Object.new + def @throttler.run + #noop end - - it "should copy the correct range and column" do - @chunker.copy(from = 1, to = 100).must_equal( - "insert ignore into `destination` (`secret`) " + - "select origin.`secret` from `origin` " + - "where origin.`id` between 1 and 100" - ) + def @throttler.stride + 1 end + @chunker = Lhm::Chunker.new(@migration, @connection, :throttler => @throttler, + :start => 1, + :limit => 10) end - describe "invalid" do - before do - @chunker = Lhm::Chunker.new(@migration, nil, { :start => 0, :limit => -1 }) - end + describe "#run" do + it "chunks the result set according to the stride size" do + def @throttler.stride + 2 + end - it "should have zero chunks" do - @chunker.traversable_chunks_size.must_equal 0 - end - - it "should not iterate" do - @chunker.up_to do |bottom, top| - raise "should not iterate" + @connection.expect(:update, 2) do |stmt| + stmt.first =~ /between 1 and 2/ end - end - end + @connection.expect(:update, 2) do |stmt| + stmt.first =~ /between 3 and 4/ + end + @connection.expect(:update, 2) do |stmt| + stmt.first =~ /between 5 and 6/ + end + @connection.expect(:update, 2) do |stmt| + stmt.first =~ /between 7 and 8/ + end + @connection.expect(:update, 2) do |stmt| + stmt.first =~ /between 9 and 10/ + end - describe "one" do - before do - @chunker = Lhm::Chunker.new(@migration, nil, { - :stride => 100_000, :start => 1, :limit => 300_000 - }) + @chunker.run + @connection.verify end - it "should have one chunk" do - @chunker.traversable_chunks_size.must_equal 3 - end + it "handles stride changes during execution" do + #roll our own stubbing + def @throttler.stride + @run_count ||= 0 + @run_count = @run_count + 1 + if @run_count > 1 + 3 + else + 2 + end + end - it "should lower bound chunk on 1" do - @chunker.bottom(chunk = 1).must_equal 1 - end + @connection.expect(:update, 2) do |stmt| + stmt.first =~ /between 1 and 2/ + end + @connection.expect(:update, 2) do |stmt| + stmt.first =~ /between 3 and 5/ + end + @connection.expect(:update, 2) do |stmt| + stmt.first =~ /between 6 and 8/ + end + @connection.expect(:update, 2) do |stmt| + stmt.first =~ /between 9 and 10/ + end - it "should upper bound chunk on 100" do - @chunker.top(chunk = 1).must_equal 100_000 + @chunker.run + @connection.verify end - end - describe "two" do - before do - @chunker = Lhm::Chunker.new(@migration, nil, { - :stride => 100_000, :start => 2, :limit => 150_000 - }) - end + it "separates filter conditions from chunking conditions" do + @chunker = Lhm::Chunker.new(@migration, @connection, :throttler => @throttler, + :start => 1, + :limit => 2) + @connection.expect(:update, 1) do |stmt| + stmt.first =~ /where \(foo.created_at > '2013-07-10' or foo.baz = 'quux'\) and `foo`/ + end - it "should have two chunks" do - @chunker.traversable_chunks_size.must_equal 2 - end + def @migration.conditions + "where foo.created_at > '2013-07-10' or foo.baz = 'quux'" + end - it "should lower bound second chunk on 100_000" do - @chunker.bottom(chunk = 2).must_equal 100_002 + @chunker.run + @connection.verify end - it "should upper bound second chunk on 150_000" do - @chunker.top(chunk = 2).must_equal 150_000 - end - end + it "doesn't mess with inner join filters" do + @chunker = Lhm::Chunker.new(@migration, @connection, :throttler => @throttler, + :start => 1, + :limit => 2) + @connection.expect(:update, 1) do |stmt| + puts stmt + stmt.first =~ /inner join bar on foo.id = bar.foo_id and/ + end - describe "iterating" do - before do - @chunker = Lhm::Chunker.new(@migration, nil, { - :stride => 100, :start => 53, :limit => 121 - }) - end - - it "should iterate" do - @chunker.up_to do |bottom, top| - bottom.must_equal 53 - top.must_equal 121 + def @migration.conditions + "inner join bar on foo.id = bar.foo_id" end - end - end - describe "throttling" do - it "should default to 100 milliseconds" do - @chunker.throttle_seconds.must_equal 0.1 + @chunker.run + @connection.verify end end end