dump 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.autotest +13 -0
  2. data/.gitignore +12 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.markdown +250 -0
  5. data/dump.gemspec +22 -0
  6. data/lib/dump.rb +3 -0
  7. data/lib/dump/capistrano.rb +1 -0
  8. data/lib/dump/railtie.rb +8 -0
  9. data/lib/dump_rake.rb +85 -0
  10. data/lib/dump_rake/archive_tar_minitar_fix.rb +8 -0
  11. data/lib/dump_rake/assets.rb +22 -0
  12. data/lib/dump_rake/continious_timeout.rb +38 -0
  13. data/lib/dump_rake/dump.rb +175 -0
  14. data/lib/dump_rake/dump_reader.rb +289 -0
  15. data/lib/dump_rake/dump_writer.rb +119 -0
  16. data/lib/dump_rake/env.rb +139 -0
  17. data/lib/dump_rake/env/filter.rb +26 -0
  18. data/lib/dump_rake/rails_root.rb +12 -0
  19. data/lib/dump_rake/table_manipulation.rb +131 -0
  20. data/lib/generators/assets_config/assets_config_generator.rb +16 -0
  21. data/lib/generators/assets_config/templates/assets +8 -0
  22. data/lib/tasks/assets.rake +17 -0
  23. data/lib/tasks/dump.rake +27 -0
  24. data/recipes/dump.rb +343 -0
  25. data/script/update_readme +21 -0
  26. data/spec/.gitignore +1 -0
  27. data/spec/.tmignore +1 -0
  28. data/spec/cycle_spec.rb +229 -0
  29. data/spec/db/database.example.yml +19 -0
  30. data/spec/db/schema.rb +7 -0
  31. data/spec/dummy-3.1.3/.gitignore +15 -0
  32. data/spec/dummy-3.1.3/.rspec +1 -0
  33. data/spec/dummy-3.1.3/Gemfile +23 -0
  34. data/spec/dummy-3.1.3/Gemfile.lock +159 -0
  35. data/spec/dummy-3.1.3/README +261 -0
  36. data/spec/dummy-3.1.3/Rakefile +7 -0
  37. data/spec/dummy-3.1.3/app/assets/images/rails.png +0 -0
  38. data/spec/dummy-3.1.3/app/assets/javascripts/application.js +9 -0
  39. data/spec/dummy-3.1.3/app/assets/stylesheets/application.css +7 -0
  40. data/spec/dummy-3.1.3/app/controllers/application_controller.rb +3 -0
  41. data/spec/dummy-3.1.3/app/helpers/application_helper.rb +2 -0
  42. data/spec/dummy-3.1.3/app/mailers/.gitkeep +0 -0
  43. data/spec/dummy-3.1.3/app/models/.gitkeep +0 -0
  44. data/spec/dummy-3.1.3/app/views/layouts/application.html.erb +14 -0
  45. data/spec/dummy-3.1.3/config.ru +4 -0
  46. data/spec/dummy-3.1.3/config/application.rb +54 -0
  47. data/spec/dummy-3.1.3/config/boot.rb +6 -0
  48. data/spec/dummy-3.1.3/config/database.yml +25 -0
  49. data/spec/dummy-3.1.3/config/environment.rb +5 -0
  50. data/spec/dummy-3.1.3/config/environments/development.rb +30 -0
  51. data/spec/dummy-3.1.3/config/environments/production.rb +60 -0
  52. data/spec/dummy-3.1.3/config/environments/test.rb +39 -0
  53. data/spec/dummy-3.1.3/config/initializers/backtrace_silencers.rb +7 -0
  54. data/spec/dummy-3.1.3/config/initializers/inflections.rb +10 -0
  55. data/spec/dummy-3.1.3/config/initializers/mime_types.rb +5 -0
  56. data/spec/dummy-3.1.3/config/initializers/secret_token.rb +7 -0
  57. data/spec/dummy-3.1.3/config/initializers/session_store.rb +8 -0
  58. data/spec/dummy-3.1.3/config/initializers/wrap_parameters.rb +14 -0
  59. data/spec/dummy-3.1.3/config/locales/en.yml +5 -0
  60. data/spec/dummy-3.1.3/config/routes.rb +58 -0
  61. data/spec/dummy-3.1.3/db/seeds.rb +7 -0
  62. data/spec/dummy-3.1.3/doc/README_FOR_APP +2 -0
  63. data/spec/dummy-3.1.3/lib/assets/.gitkeep +0 -0
  64. data/spec/dummy-3.1.3/lib/tasks/.gitkeep +0 -0
  65. data/spec/dummy-3.1.3/log/.gitkeep +0 -0
  66. data/spec/dummy-3.1.3/public/404.html +26 -0
  67. data/spec/dummy-3.1.3/public/422.html +26 -0
  68. data/spec/dummy-3.1.3/public/500.html +26 -0
  69. data/spec/dummy-3.1.3/public/favicon.ico +0 -0
  70. data/spec/dummy-3.1.3/public/index.html +241 -0
  71. data/spec/dummy-3.1.3/public/robots.txt +5 -0
  72. data/spec/dummy-3.1.3/script/rails +6 -0
  73. data/spec/dummy-3.1.3/spec/spec_helper.rb +32 -0
  74. data/spec/dummy-3.1.3/vendor/assets/stylesheets/.gitkeep +0 -0
  75. data/spec/dummy-3.1.3/vendor/plugins/.gitkeep +0 -0
  76. data/spec/lib/dump_rake/dump_reader_spec.rb +638 -0
  77. data/spec/lib/dump_rake/dump_spec.rb +291 -0
  78. data/spec/lib/dump_rake/dump_writer_spec.rb +328 -0
  79. data/spec/lib/dump_rake/env/filter_spec.rb +56 -0
  80. data/spec/lib/dump_rake/env_spec.rb +139 -0
  81. data/spec/lib/dump_rake/rails_root_spec.rb +45 -0
  82. data/spec/lib/dump_rake/table_manipulation_spec.rb +256 -0
  83. data/spec/lib/dump_rake_spec.rb +326 -0
  84. data/spec/recipes/dump_spec.rb +553 -0
  85. data/spec/spec.opts +4 -0
  86. data/spec/spec_helper.rb +34 -0
  87. data/spec/tasks/assets_spec.rb +92 -0
  88. data/spec/tasks/dump_spec.rb +107 -0
  89. metadata +272 -0
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+
3
+ Filter = DumpRake::Env::Filter
4
+ describe Filter do
5
+ it "should pass everything if initialized with nil" do
6
+ filter = Filter.new(nil)
7
+ filter.pass?('a').should be_true
8
+ filter.pass?('b').should be_true
9
+ filter.pass?('c').should be_true
10
+ filter.pass?('d').should be_true
11
+ end
12
+
13
+ it "should pass only specified values" do
14
+ filter = Filter.new('a,c')
15
+ filter.pass?('a').should be_true
16
+ filter.pass?('b').should be_false
17
+ filter.pass?('c').should be_true
18
+ filter.pass?('d').should be_false
19
+ end
20
+
21
+ it "should not pass anything if initialized empty" do
22
+ filter = Filter.new('')
23
+ filter.pass?('a').should be_false
24
+ filter.pass?('b').should be_false
25
+ filter.pass?('c').should be_false
26
+ filter.pass?('d').should be_false
27
+ end
28
+
29
+ describe "when initialized with -" do
30
+ it "should pass everything except specified values" do
31
+ filter = Filter.new('-a,c')
32
+ filter.pass?('a').should be_false
33
+ filter.pass?('b').should be_true
34
+ filter.pass?('c').should be_false
35
+ filter.pass?('d').should be_true
36
+ end
37
+
38
+ it "should pass everything if initialized empty" do
39
+ filter = Filter.new('-')
40
+ filter.pass?('a').should be_true
41
+ filter.pass?('b').should be_true
42
+ filter.pass?('c').should be_true
43
+ filter.pass?('d').should be_true
44
+ end
45
+ end
46
+
47
+ describe "custom_pass?" do
48
+ it "should pass only when any call to block returns true" do
49
+ filter = Filter.new('a,c')
50
+ filter.custom_pass?{ |value| value == 'a' }.should be_true
51
+ filter.custom_pass?{ |value| value == 'b' }.should be_false
52
+ filter.custom_pass?{ |value| value == 'c' }.should be_true
53
+ filter.custom_pass?{ |value| value == 'd' }.should be_false
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,139 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ Env = DumpRake::Env
4
+ describe Env do
5
+ def silence_warnings
6
+ old_verbose, $VERBOSE = $VERBOSE, nil
7
+ yield
8
+ ensure
9
+ $VERBOSE = old_verbose
10
+ end
11
+
12
+ before do
13
+ silence_warnings do
14
+ @old_env, ENV = ENV, {}
15
+ end
16
+ end
17
+
18
+ after do
19
+ silence_warnings do
20
+ ENV = @old_env
21
+ end
22
+ end
23
+
24
+ describe "with_env" do
25
+ it "should set env to new_value for duration of block" do
26
+ ENV['LIKE'] = 'old_value'
27
+
28
+ ENV['LIKE'].should == 'old_value'
29
+ Env.with_env('LIKE' => 'new_value') do
30
+ ENV['LIKE'].should == 'new_value'
31
+ end
32
+ ENV['LIKE'].should == 'old_value'
33
+ end
34
+
35
+ it "should use dictionary" do
36
+ ENV['LIKE'] = 'old_value'
37
+
38
+ ENV['LIKE'].should == 'old_value'
39
+ Env.with_env(:like => 'new_value') do
40
+ ENV['LIKE'].should == 'new_value'
41
+ end
42
+ ENV['LIKE'].should == 'old_value'
43
+ end
44
+ end
45
+
46
+ describe "[]" do
47
+ it "should mimic ENV" do
48
+ ENV['VERSION'] = 'VERSION_value'
49
+ Env['VERSION'].should == ENV['VERSION']
50
+ end
51
+
52
+ it "should return nil on non existing env variable" do
53
+ Env['DESCRIPTON'].should == nil
54
+ end
55
+
56
+ it "should get first value that is set" do
57
+ ENV['VERSION'] = 'VERSION_value'
58
+ Env[:like].should == 'VERSION_value'
59
+ ENV['VER'] = 'VER_value'
60
+ Env[:like].should == 'VER_value'
61
+ ENV['LIKE'] = 'LIKE_value'
62
+ Env[:like].should == 'LIKE_value'
63
+ end
64
+
65
+ it "should return nil for unset variable" do
66
+ Env[:desc].should == nil
67
+ end
68
+ end
69
+
70
+ describe "filter" do
71
+ before do
72
+ Env.instance_variable_set(:@filters, nil)
73
+ end
74
+
75
+ it "should return Filter" do
76
+ ENV['TABLES'] = 'a,b,c'
77
+ filter = Env.filter('TABLES')
78
+ filter.should be_instance_of(Env::Filter)
79
+ filter.invert.should be_false
80
+ filter.values.should == %w[a b c]
81
+ end
82
+
83
+ it "should cache created filter" do
84
+ ENV['TABLES'] = 'a,b,c'
85
+ ENV['TABLES2'] = 'a,b,c'
86
+ Env::Filter.should_receive(:new).with('a,b,c', nil).once
87
+ Env.filter('TABLES')
88
+ Env.filter('TABLES')
89
+ Env.filter('TABLES2')
90
+ end
91
+ end
92
+
93
+ describe "for_command" do
94
+ describe "when no vars present" do
95
+ it "should return empty hash for every command" do
96
+ Env.for_command(:create).should == {}
97
+ Env.for_command(:restore).should == {}
98
+ Env.for_command(:versions).should == {}
99
+ Env.for_command(:bad).should == {}
100
+ end
101
+
102
+ it "should return empty hash for every command when asking for string keys" do
103
+ Env.for_command(:create, true).should == {}
104
+ Env.for_command(:restore, true).should == {}
105
+ Env.for_command(:versions, true).should == {}
106
+ Env.for_command(:bad, true).should == {}
107
+ end
108
+ end
109
+
110
+ describe "when vars are present" do
111
+ before do
112
+ ENV['LIKE'] = 'Version'
113
+ ENV['DESC'] = 'Description'
114
+ end
115
+
116
+ it "should return hash with symbol keys for every command" do
117
+ Env.for_command(:create).should == {:desc => 'Description'}
118
+ Env.for_command(:restore).should == {:like => 'Version'}
119
+ Env.for_command(:versions).should == {:like => 'Version'}
120
+ Env.for_command(:bad).should == {}
121
+ end
122
+
123
+ it "should return hash with symbol keys for every command when asking for string keys" do
124
+ Env.for_command(:create, true).should == {'DESC' => 'Description'}
125
+ Env.for_command(:restore, true).should == {'LIKE' => 'Version'}
126
+ Env.for_command(:versions, true).should == {'LIKE' => 'Version'}
127
+ Env.for_command(:bad, true).should == {}
128
+ end
129
+ end
130
+ end
131
+
132
+ describe "stringify!" do
133
+ it "should convert keys to strings" do
134
+ @env = {:desc => 'text', :tags => 'a b c', 'LEAVE' => 'none', 'OTHER' => 'data'}
135
+ Env.stringify!(@env)
136
+ @env.should == {'DESC' => 'text', 'TAGS' => 'a b c', 'LEAVE' => 'none', 'OTHER' => 'data'}
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ def temp_remove_const(where, which)
4
+ around do |example|
5
+ if where.const_defined?(which)
6
+ old = where.send(:const_get, which)
7
+ where.send(:remove_const, which)
8
+ example.run
9
+ where.send(:remove_const, which) if where.const_defined?(which)
10
+ where.const_set(which, old)
11
+ else
12
+ example.run
13
+ end
14
+ end
15
+ end
16
+
17
+ describe 'RailsRoot' do
18
+ before do
19
+ @root = mock('root')
20
+ @root.should_receive(:to_s).and_return(@root)
21
+ end
22
+
23
+ temp_remove_const Object, :Rails
24
+ temp_remove_const Object, :RAILS_ROOT
25
+ temp_remove_const DumpRake, :RailsRoot
26
+
27
+ it "should use Rails if it is present" do
28
+ Object.const_set('Rails', mock('rails'))
29
+ Rails.should_receive(:root).and_return(@root)
30
+ load 'dump_rake/rails_root.rb'
31
+ DumpRake::RailsRoot.should === @root
32
+ end
33
+
34
+ it "should use RAILS_ROOT if it is present" do
35
+ Object.const_set('RAILS_ROOT', @root)
36
+ load 'dump_rake/rails_root.rb'
37
+ DumpRake::RailsRoot.should === @root
38
+ end
39
+
40
+ it "should use Dir.pwd else" do
41
+ Dir.should_receive(:pwd).and_return(@root)
42
+ load 'dump_rake/rails_root.rb'
43
+ DumpRake::RailsRoot.should === @root
44
+ end
45
+ end
@@ -0,0 +1,256 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ require File.dirname(__FILE__) + '/../../../lib/dump_rake'
4
+
5
+ TableManipulation = DumpRake::TableManipulation
6
+ describe TableManipulation do
7
+ include TableManipulation
8
+
9
+ describe "schema_tables" do
10
+ it "should return schema_tables" do
11
+ schema_tables.should == %w[schema_info schema_migrations]
12
+ end
13
+ end
14
+
15
+ describe "verify_connection" do
16
+ it "should return result of ActiveRecord::Base.connection.verify!" do
17
+ ActiveRecord::Base.connection.should_receive(:verify!).and_return(:result)
18
+ verify_connection.should == :result
19
+ end
20
+ end
21
+
22
+ describe "quote_table_name" do
23
+ it "should return result of ActiveRecord::Base.connection.quote_table_name" do
24
+ ActiveRecord::Base.connection.should_receive(:quote_table_name).with('first').and_return('`first`')
25
+ quote_table_name('first').should == '`first`'
26
+ end
27
+ end
28
+
29
+ describe "quote_column_name" do
30
+ it "should return result of ActiveRecord::Base.connection.quote_column_name" do
31
+ ActiveRecord::Base.connection.should_receive(:quote_column_name).with('first').and_return('`first`')
32
+ quote_column_name('first').should == '`first`'
33
+ end
34
+ end
35
+
36
+ describe "quote_value" do
37
+ it "should return result of ActiveRecord::Base.connection.quote_value" do
38
+ ActiveRecord::Base.connection.should_receive(:quote).with('first').and_return('`first`')
39
+ quote_value('first').should == '`first`'
40
+ end
41
+ end
42
+
43
+
44
+ describe "clear_table" do
45
+ it "should call ActiveRecord::Base.connection.delete with sql for deleting everything from table" do
46
+ ActiveRecord::Base.connection.should_receive(:delete).with('DELETE FROM `first`', anything)
47
+ clear_table('`first`')
48
+ end
49
+ end
50
+
51
+ describe "insert_into_table" do
52
+ it "should call ActiveRecord::Base.connection.insert with sql for insert if values is string" do
53
+ ActiveRecord::Base.connection.should_receive(:insert).with("INSERT INTO `table` (`c1`,`c2`) VALUES (`v1`,`v2`)", anything)
54
+ insert_into_table('`table`', '(`c1`,`c2`)', '(`v1`,`v2`)')
55
+ end
56
+
57
+ it "should call ActiveRecord::Base.connection.insert with sql for insert if values is array" do
58
+ ActiveRecord::Base.connection.should_receive(:insert).with("INSERT INTO `table` (`c1`,`c2`) VALUES (`v11`,`v12`),(`v21`,`v22`)", anything)
59
+ insert_into_table('`table`', '(`c1`,`c2`)', ['(`v11`,`v12`)', '(`v21`,`v22`)'])
60
+ end
61
+ end
62
+
63
+ describe "join_for_sql" do
64
+ it "should convert array ['`a`', '`b`'] to \"(`a`,`b`)\"" do
65
+ join_for_sql(%w[`a` `b`]).should == '(`a`,`b`)'
66
+ end
67
+ end
68
+
69
+ describe "columns_insert_sql" do
70
+ it "should return columns sql part for insert" do
71
+ should_receive(:quote_column_name).with('a').and_return('`a`')
72
+ should_receive(:quote_column_name).with('b').and_return('`b`')
73
+
74
+ columns_insert_sql(%w[a b]).should == '(`a`,`b`)'
75
+ end
76
+ end
77
+
78
+ describe "values_insert_sql" do
79
+ it "should return values sql part for insert" do
80
+ should_receive(:quote_value).with('a').and_return('`a`')
81
+ should_receive(:quote_value).with('b').and_return('`b`')
82
+
83
+ values_insert_sql(%w[a b]).should == '(`a`,`b`)'
84
+ end
85
+ end
86
+
87
+
88
+ describe "tables_to_dump" do
89
+ it "should call ActiveRecord::Base.connection.tables" do
90
+ ActiveRecord::Base.connection.should_receive(:tables).and_return([])
91
+ tables_to_dump
92
+ end
93
+
94
+ it "should exclude sessions table from result" do
95
+ ActiveRecord::Base.connection.should_receive(:tables).and_return(%w[first second schema_info schema_migrations sessions])
96
+ tables_to_dump.should == %w[first second schema_info schema_migrations]
97
+ end
98
+
99
+ describe "with user defined tables" do
100
+ before do
101
+ ActiveRecord::Base.connection.should_receive(:tables).and_return(%w[first second schema_info schema_migrations sessions])
102
+ end
103
+
104
+ it "should select certain tables" do
105
+ DumpRake::Env.with_env(:tables => 'first,third,-fifth') do
106
+ tables_to_dump.should == %w[first schema_info schema_migrations]
107
+ end
108
+ end
109
+
110
+ it "should select skip certain tables" do
111
+ DumpRake::Env.with_env(:tables => '-first,third,-fifth') do
112
+ tables_to_dump.should == %w[second schema_info schema_migrations sessions]
113
+ end
114
+ end
115
+
116
+ it "should not exclude sessions table from result if asked to exclude nothing" do
117
+ DumpRake::Env.with_env(:tables => '-') do
118
+ tables_to_dump.should == %w[first second schema_info schema_migrations sessions]
119
+ end
120
+ end
121
+
122
+ it "should not exclude schema tables" do
123
+ DumpRake::Env.with_env(:tables => '-second,schema_info,schema_migrations') do
124
+ tables_to_dump.should == %w[first schema_info schema_migrations sessions]
125
+ end
126
+ end
127
+
128
+ it "should not exclude schema tables ever if asked to dump only certain tables" do
129
+ DumpRake::Env.with_env(:tables => 'second') do
130
+ tables_to_dump.should == %w[second schema_info schema_migrations]
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ describe "table_row_count" do
137
+ it "should ruturn row count for table" do
138
+ ActiveRecord::Base.connection.should_receive(:select_value).with("SELECT COUNT(*) FROM #{quote_table_name('first')}").and_return('666')
139
+ table_row_count('first').should == 666
140
+ end
141
+ end
142
+
143
+ describe "table_chunk_size" do
144
+ it "should return chunk_size based on estimated average for row size" do
145
+ should_receive(:table_columns).with('first').and_return(
146
+ [mock(:column, :type => :integer, :limit => nil)] * 3 +
147
+ [mock(:column, :type => :string, :limit => nil)] * 3 +
148
+ [mock(:column, :type => :text, :limit => nil)]
149
+ )
150
+ table_chunk_size('first').should satisfy { |n|
151
+ (TableManipulation::CHUNK_SIZE_MIN..TableManipulation::CHUNK_SIZE_MAX).include?(n)
152
+ }
153
+ end
154
+
155
+ it "should not return value less than CHUNK_SIZE_MIN" do
156
+ should_receive(:table_columns).with('first').and_return(
157
+ [mock(:column, :type => :text, :limit => nil)] * 100
158
+ )
159
+ table_chunk_size('first').should == TableManipulation::CHUNK_SIZE_MIN
160
+ end
161
+
162
+ it "should not return value more than CHUNK_SIZE_MAX" do
163
+ should_receive(:table_columns).with('first').and_return(
164
+ [mock(:column, :type => :boolean, :limit => 1)] * 10
165
+ )
166
+ table_chunk_size('first').should == TableManipulation::CHUNK_SIZE_MAX
167
+ end
168
+ end
169
+
170
+ describe "table_columns" do
171
+ it "should return table column definitions" do
172
+ columns = [mock(:column), mock(:column), mock(:column)]
173
+ ActiveRecord::Base.connection.should_receive(:columns).with('first').and_return(columns)
174
+ table_columns('first').should == columns
175
+ end
176
+ end
177
+
178
+ describe "table_has_primary_column?" do
179
+ it "should return true only if table has column with name id and type :integer" do
180
+ should_receive(:table_primary_key).at_least(3).times.and_return('id')
181
+
182
+ should_receive(:table_columns).with('first').and_return([mock(:column, :name => 'id', :type => :integer), mock(:column, :name => 'title', :type => :integer)])
183
+ table_has_primary_column?('first').should be_true
184
+
185
+ should_receive(:table_columns).with('second').and_return([mock(:column, :name => 'id', :type => :string), mock(:column, :name => 'title', :type => :integer)])
186
+ table_has_primary_column?('second').should be_false
187
+
188
+ should_receive(:table_columns).with('third').and_return([mock(:column, :name => 'name', :type => :integer), mock(:column, :name => 'title', :type => :integer)])
189
+ table_has_primary_column?('third').should be_false
190
+ end
191
+ end
192
+
193
+ describe "table_primary_key" do
194
+ it "should return id" do
195
+ table_primary_key('first').should == 'id'
196
+ table_primary_key(nil).should == 'id'
197
+ end
198
+ end
199
+
200
+ describe "each_table_row" do
201
+ before do
202
+ @row_count = 550
203
+ @rows = Array.new(@row_count){ |i| {'id' => "#{i + 1}"} }
204
+ end
205
+
206
+ def verify_getting_rows
207
+ i = 0
208
+ each_table_row('first', @row_count) do |row|
209
+ row.should == {'id' => "#{i + 1}"}
210
+ i += 1
211
+ end
212
+ i.should == @row_count
213
+ end
214
+
215
+ it "should get rows in chunks if table has primary column and chunk size is less than row count" do
216
+ should_receive(:table_has_primary_column?).with('first').and_return(true)
217
+ should_receive(:table_chunk_size).with('first').and_return(100)
218
+ quoted_table_name = quote_table_name('first')
219
+ quoted_primary_key = "#{quoted_table_name}.#{quote_column_name(table_primary_key('first'))}"
220
+ sql = "SELECT * FROM #{quoted_table_name} WHERE #{quoted_primary_key} %s ORDER BY #{quoted_primary_key} ASC LIMIT 100"
221
+
222
+ should_receive(:select_all_by_sql).with(sql % '>= 0').and_return(@rows[0, 100])
223
+ 5.times do |i|
224
+ last_primary_key = 100 + i * 100
225
+ should_receive(:select_all_by_sql).with(sql % "> #{last_primary_key}").and_return(@rows[last_primary_key, 100])
226
+ end
227
+
228
+ verify_getting_rows
229
+ end
230
+
231
+ def verify_getting_rows_in_one_pass
232
+ should_receive(:select_all_by_sql).with("SELECT * FROM #{quote_table_name('first')}").and_return(@rows)
233
+ verify_getting_rows
234
+ end
235
+
236
+ it "should get rows in one pass if table has primary column but chunk size is not less than row count" do
237
+ should_receive(:table_has_primary_column?).with('first').and_return(true)
238
+ should_receive(:table_chunk_size).with('first').and_return(3_000)
239
+ verify_getting_rows_in_one_pass
240
+ end
241
+
242
+ it "should get rows in one pass if table has no primary column" do
243
+ should_receive(:table_has_primary_column?).with('first').and_return(false)
244
+ should_not_receive(:table_chunk_size)
245
+ verify_getting_rows_in_one_pass
246
+ end
247
+ end
248
+
249
+ describe "select_all_by_sql" do
250
+ it "should return all rows returned by database" do
251
+ rows = [mock(:row), mock(:row), mock(:row)]
252
+ ActiveRecord::Base.connection.should_receive(:select_all).with("SELECT * FROM abc WHERE x = y").and_return(rows)
253
+ select_all_by_sql("SELECT * FROM abc WHERE x = y").should == rows
254
+ end
255
+ end
256
+ end