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