mass_insert 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. data/README.md +3 -3
  2. data/lib/mass_insert/adapters.rb +0 -1
  3. data/lib/mass_insert/adapters/adapter.rb +1 -7
  4. data/lib/mass_insert/adapters/column_value.rb +33 -29
  5. data/lib/mass_insert/adapters/helpers.rb +3 -2
  6. data/lib/mass_insert/adapters/helpers/abstract_query.rb +48 -0
  7. data/lib/mass_insert/adapters/helpers/sanitizer.rb +6 -0
  8. data/lib/mass_insert/adapters/mysql2_adapter.rb +1 -1
  9. data/lib/mass_insert/base.rb +49 -20
  10. data/lib/mass_insert/query_builder.rb +6 -6
  11. data/lib/mass_insert/version.rb +1 -1
  12. data/spec/active_record_dummy/Gemfile +1 -1
  13. data/spec/active_record_dummy/config/database.yml +1 -1
  14. data/spec/mass_insert/adapters/adapter_spec.rb +23 -45
  15. data/spec/mass_insert/adapters/column_value_spec.rb +107 -154
  16. data/spec/mass_insert/adapters/{abstract_query_spec.rb → helpers/abstract_query_spec.rb} +23 -27
  17. data/spec/mass_insert/adapters/helpers/sanitizer_spec.rb +16 -9
  18. data/spec/mass_insert/adapters/helpers/timestamp_spec.rb +11 -15
  19. data/spec/mass_insert/adapters/helpers_spec.rb +7 -3
  20. data/spec/mass_insert/adapters/mysql_adapter_spec.rb +6 -10
  21. data/spec/mass_insert/adapters/postgresql_adapter_spec.rb +4 -8
  22. data/spec/mass_insert/adapters/sqlite3_adapter_spec.rb +24 -30
  23. data/spec/mass_insert/adapters/sqlserver_adapter_spec.rb +16 -21
  24. data/spec/mass_insert/adapters_spec.rb +8 -12
  25. data/spec/mass_insert/base_spec.rb +13 -13
  26. data/spec/mass_insert/process_control_spec.rb +33 -40
  27. data/spec/mass_insert/query_builder_spec.rb +20 -24
  28. data/spec/mass_insert/query_execution_spec.rb +13 -16
  29. data/spec/mass_insert_spec.rb +6 -6
  30. metadata +7 -7
  31. data/lib/mass_insert/adapters/abstract_query.rb +0 -47
@@ -2,130 +2,117 @@ require './spec/spec_helper'
2
2
  require "./lib/mass_insert"
3
3
 
4
4
  describe MassInsert::Adapters::ColumnValue do
5
- before :each do
6
- @options = {
7
- :class_name => User,
8
- :table_name => "users",
9
- :primary_key => :id,
10
- :primary_key_mode => :auto
11
- }
12
- @row = {
13
- :name => "name",
14
- :email => "email",
15
- :age => 10
16
- }
17
- @colum_value = MassInsert::Adapters::ColumnValue.new(@row, :name, @options)
18
- end
19
-
20
- subject{ @colum_value }
5
+ let(:options) {{ :class_name => User }}
6
+ let(:row) {{ :name => "name", :age => 10 }}
7
+ let(:column){ :name }
8
+ let!(:subject){ MassInsert::Adapters::ColumnValue.new(row, column, options) }
21
9
 
22
10
  describe "#initialize" do
23
11
  it "should assign options param to option attribute" do
24
- subject.options.should eq(@options)
12
+ expect(subject.options).to eq(options)
25
13
  end
26
14
 
27
15
  it "should assign column param to column attribute" do
28
- subject.column.should eq(:name)
16
+ expect(subject.column).to eq(column)
29
17
  end
30
18
 
31
19
  it "should assign row param to row attribute" do
32
- subject.row.should eq(@row)
20
+ expect(subject.row).to eq(row)
33
21
  end
34
22
  end
35
23
 
36
24
  describe "#class_name" do
37
25
  it "should respond to class_name method" do
38
- subject.respond_to?(:class_name).should be_true
26
+ expect(subject).to respond_to(:class_name)
39
27
  end
40
28
 
41
29
  it "should return the class_name in options" do
42
- subject.class_name.should eq(User)
30
+ expect(subject.class_name).to eq(User)
43
31
  end
44
32
  end
45
33
 
46
34
  describe "#column_type" do
47
35
  it "should respond to column_type method" do
48
- subject.respond_to?(:column_type).should be_true
36
+ expect(subject).to respond_to(:column_type)
49
37
  end
50
38
 
51
- context "when is a string column" do
52
- it "should return symbol :string" do
53
- subject.column = :name
54
- subject.column_type.should eq(:string)
55
- end
56
- end
57
-
58
- context "when is a integer column" do
59
- it "should return symbol :integer" do
60
- subject.column = :age
61
- subject.column_type.should eq(:integer)
62
- end
39
+ it "should return symbol :string" do
40
+ expect(subject.column_type).to eq(:string)
63
41
  end
64
42
  end
65
43
 
66
44
  describe "#colum_value" do
67
45
  it "should respond to column_value method" do
68
- subject.respond_to?(:column_value).should be_true
46
+ expect(subject).to respond_to(:column_value)
69
47
  end
70
48
 
71
- it "should return symbol :string" do
72
- subject.column = :age
73
- subject.column_value.should eq(10)
49
+ it "should return row value to this column" do
50
+ expect(subject.column_value).to eq("name")
74
51
  end
75
52
  end
76
53
 
77
54
  describe "#adapter" do
78
55
  it "should respond to adapter method" do
79
- subject.respond_to?(:adapter).should be_true
56
+ expect(subject).to respond_to(:adapter)
80
57
  end
81
58
 
82
59
  it "should return the adapter type" do
83
60
  config = {"config" => {:adapter => "sql"}}
84
- ActiveRecord::Base.connection.stub(:instance_values).and_return(config)
85
- subject.adapter.should eq("sql")
61
+ connection = ActiveRecord::Base.connection
62
+ connection.stub(:instance_values).and_return(config)
63
+ expect(subject.adapter).to eq("sql")
86
64
  end
87
65
  end
88
66
 
89
67
  describe "#default_value" do
90
68
  it "should respond to default_value method" do
91
- subject.respond_to?(:default_value).should be_true
69
+ expect(subject).to respond_to(:default_value)
92
70
  end
93
71
 
94
72
  context "when default_db_value is nil" do
95
73
  it "should return 'null' string" do
96
74
  subject.stub(:default_db_value).and_return(nil)
97
- subject.default_value.should eq("null")
75
+ expect(subject.default_value).to eq("null")
98
76
  end
99
77
  end
100
78
 
101
79
  context "when default_db_value is not nil" do
102
80
  it "should return the correct value" do
103
81
  subject.stub(:default_db_value).and_return("default_value")
104
- subject.default_value.should eq("default_value")
82
+ expect(subject.default_value).to eq("default_value")
105
83
  end
106
84
  end
107
85
  end
108
86
 
109
87
  describe "#default_db_value" do
110
88
  it "should respond to default_db_value method" do
111
- subject.respond_to?(:default_db_value).should be_true
89
+ expect(subject).to respond_to(:default_db_value)
112
90
  end
113
91
 
114
92
  it "should return the default database value" do
115
- subject.column = :name
116
- subject.default_db_value.should eq(nil)
93
+ expect(subject.default_db_value).to eq(nil)
117
94
  end
118
95
  end
119
96
 
120
97
  describe "#build" do
121
98
  it "should respond to build method" do
122
- subject.respond_to?(:build).should be_true
99
+ expect(subject).to respond_to(:build)
100
+ end
101
+
102
+ context "when column_value is nil" do
103
+ it "should return the default value" do
104
+ subject.stub(:column_value).and_return(nil)
105
+ subject.stub(:default_value).and_return("default_value")
106
+ expect(subject.build).to eq("default_value")
107
+ end
123
108
  end
124
109
 
125
- it "should call a method according to column type" do
126
- subject.stub(:column_type).and_return("string")
127
- subject.stub(:column_value_string).and_return("column_value_string")
128
- subject.build.should eq("column_value_string")
110
+ context "when column_value is not nil" do
111
+ it "should call a method according to column type" do
112
+ subject.stub(:column_type).and_return("string")
113
+ subject.stub(:column_value_string).and_return("column_value_string")
114
+ expect(subject.build).to eq("column_value_string")
115
+ end
129
116
  end
130
117
  end
131
118
 
@@ -138,89 +125,59 @@ describe MassInsert::Adapters::ColumnValue do
138
125
  :timestamp,
139
126
  :binary
140
127
  ].each do |column_type|
141
- method = "column_value_#{column_type}".to_sym
128
+ method = :"column_value_#{column_type}"
142
129
 
143
- describe "#column_value_#{method.to_s}" do
130
+ describe "##{method.to_s}" do
144
131
  it "should respond to #{method.to_s} method" do
145
- subject.respond_to?(method).should be_true
132
+ expect(subject).to respond_to(method)
146
133
  end
147
134
 
148
- context "when column_value is nil" do
149
- it "should return the default value" do
150
- subject.stub(:column_value).and_return(nil)
151
- subject.stub(:default_value).and_return("default_value")
152
- subject.send(method).should eq("default_value")
153
- end
154
- end
155
-
156
- context "when column_value is not nil" do
157
- it "should return the column value" do
158
- subject.stub(:column_value).and_return("name")
159
- subject.send(method).should eq("'name'")
160
- end
135
+ it "should return the column value" do
136
+ subject.stub(:column_value).and_return("name")
137
+ expect(subject.send(method)).to eq("'name'")
161
138
  end
162
139
  end
163
140
  end
164
141
 
165
142
  describe "#column_value_integer" do
166
143
  it "should respond to column_value_integer method" do
167
- subject.respond_to?(:column_value_integer).should be_true
144
+ expect(subject).to respond_to(:column_value_integer)
168
145
  end
169
146
 
170
- context "when column_value is nil" do
171
- it "should return the default value" do
172
- subject.stub(:column_value).and_return(nil)
173
- subject.stub(:default_value).and_return("default_value")
174
- subject.column_value_integer.should eq("default_value")
147
+ context "when is a integer value" do
148
+ it "should return the same integer value" do
149
+ subject.stub(:column_value).and_return(20)
150
+ expect(subject.column_value_integer).to eq("20")
175
151
  end
176
152
  end
177
153
 
178
- context "when column_value is not nil" do
179
- context "when is a integer value" do
180
- it "should return the same integer value" do
181
- subject.stub(:column_value).and_return(20)
182
- subject.column_value_integer.should eq("20")
183
- end
184
- end
185
-
186
- context "when is not a integer value" do
187
- it "should convert it to integer value" do
188
- subject.stub(:column_value).and_return("name")
189
- subject.column_value_integer.should eq("0")
190
- end
154
+ context "when is not a integer value" do
155
+ it "should convert it to integer value" do
156
+ subject.stub(:column_value).and_return("name")
157
+ expect(subject.column_value_integer).to eq("0")
191
158
  end
192
159
  end
193
160
  end
194
161
 
195
162
  [:decimal, :float].each do |column_type|
196
- method = "column_value_#{column_type}".to_sym
163
+ method = :"column_value_#{column_type}"
197
164
 
198
- describe "#column_value_#{method.to_s}" do
165
+ describe "##{method.to_s}" do
199
166
  it "should respond to #{method.to_s} method" do
200
- subject.respond_to?(method).should be_true
167
+ expect(subject).to respond_to(method)
201
168
  end
202
169
 
203
- context "when column_value is nil" do
204
- it "should return the default value" do
205
- subject.stub(:column_value).and_return(nil)
206
- subject.stub(:default_value).and_return("default_value")
207
- subject.send(method).should eq("default_value")
170
+ context "when is a decimal value" do
171
+ it "should return the same decimal value" do
172
+ subject.stub(:column_value).and_return(20.5)
173
+ expect(subject.send(method)).to eq("20.5")
208
174
  end
209
175
  end
210
176
 
211
- context "when column_value is not nil" do
212
- context "when is a decimal value" do
213
- it "should return the same decimal value" do
214
- subject.stub(:column_value).and_return(20.5)
215
- subject.send(method).should eq("20.5")
216
- end
217
- end
218
-
219
- context "when is not a decimal value" do
220
- it "should convert it to decimal value" do
221
- subject.stub(:column_value).and_return("name")
222
- subject.send(method).should eq("0.0")
223
- end
177
+ context "when is not a decimal value" do
178
+ it "should convert it to decimal value" do
179
+ subject.stub(:column_value).and_return("name")
180
+ expect(subject.send(method)).to eq("0.0")
224
181
  end
225
182
  end
226
183
  end
@@ -228,64 +185,60 @@ describe MassInsert::Adapters::ColumnValue do
228
185
 
229
186
  describe "#column_value_boolean" do
230
187
  it "should respond to column_value_boolean method" do
231
- subject.respond_to?(:column_value_boolean).should be_true
188
+ expect(subject).to respond_to(:column_value_boolean)
232
189
  end
233
190
 
234
- context "when adapter is mysql2, postgresql or sqlserve" do
235
- context "when column value is nil" do
236
- it "should return database default value" do
237
- subject.stub(:adapter).and_return("mysql2")
238
- subject.stub(:column_value).and_return(nil)
239
- subject.stub(:default_value).and_return("default_value")
240
- subject.column_value_boolean.should eq("default_value")
241
- end
191
+ it "should call a method according to database adapter" do
192
+ subject.stub(:adapter).and_return("mysql2")
193
+ subject.stub(:mysql2_column_value_boolean).and_return("boolean_value")
194
+ expect(subject.column_value_boolean).to eq("boolean_value")
195
+ end
196
+ end
197
+
198
+ [
199
+ :mysql2,
200
+ :postgresql,
201
+ :sqlserver,
202
+ ].each do |adapter|
203
+ method = :"#{adapter}_column_value_boolean"
204
+
205
+ describe "##{method.to_s}" do
206
+ it "should respond to #{method.to_s} method" do
207
+ expect(subject).to respond_to(method)
242
208
  end
243
209
 
244
- context "when column value is not nil" do
245
- context "when column value is false" do
246
- it "should return 'false' string" do
247
- subject.stub(:adapter).and_return("mysql2")
248
- subject.stub(:column_value).and_return(false)
249
- subject.column_value_boolean.should eq("false")
250
- end
210
+ context "when column_value method return true value" do
211
+ it "should return true string" do
212
+ subject.stub(:column_value).and_return(true)
213
+ expect(subject.send(method)).to eq("true")
251
214
  end
215
+ end
252
216
 
253
- context "when column value is true" do
254
- it "should return 'true' string" do
255
- subject.stub(:adapter).and_return("mysql2")
256
- subject.stub(:column_value).and_return(true)
257
- subject.column_value_boolean.should eq("true")
258
- end
217
+ context "when column_value method return false value" do
218
+ it "should return false string" do
219
+ subject.stub(:column_value).and_return(false)
220
+ expect(subject.send(method)).to eq("false")
259
221
  end
260
222
  end
261
223
  end
224
+ end
262
225
 
263
- context "when adapter is sqlite3" do
264
- context "when column value is nil" do
265
- it "should return database default value" do
266
- subject.stub(:adapter).and_return("sqlite3")
267
- subject.stub(:column_value).and_return(nil)
268
- subject.stub(:default_value).and_return("default_value")
269
- subject.column_value_boolean.should eq("default_value")
270
- end
271
- end
226
+ describe "#sqlite3_column_value_boolean" do
227
+ it "should respond to sqlite3_column_value_boolean method" do
228
+ expect(subject).to respond_to(:sqlite3_column_value_boolean)
229
+ end
272
230
 
273
- context "when column value is not nil" do
274
- context "when column value is false" do
275
- it "should return '0' string" do
276
- subject.stub(:adapter).and_return("sqlite3")
277
- subject.stub(:column_value).and_return(false)
278
- subject.column_value_boolean.should eq("0")
279
- end
280
- end
231
+ context "when column_value method return true value" do
232
+ it "should return true string" do
233
+ subject.stub(:column_value).and_return(true)
234
+ expect(subject.sqlite3_column_value_boolean).to eq("1")
235
+ end
236
+ end
281
237
 
282
- context "when column value is true" do
283
- it "should return '1' string" do
284
- subject.stub(:adapter).and_return("sqlite3")
285
- subject.stub(:column_value).and_return(true)
286
- subject.column_value_boolean.should eq("1")
287
- end
288
- end
238
+ context "when column_value method return false value" do
239
+ it "should return false string" do
240
+ subject.stub(:column_value).and_return(false)
241
+ expect(subject.sqlite3_column_value_boolean).to eq("0")
289
242
  end
290
243
  end
291
244
  end
@@ -1,83 +1,80 @@
1
1
  require './spec/spec_helper'
2
2
  require "./lib/mass_insert"
3
3
 
4
- describe MassInsert::Adapters::AbstractQuery do
5
- before :each do
6
- @adapter = MassInsert::Adapters::Adapter.new([], {})
7
- end
8
-
9
- subject{ @adapter }
4
+ describe MassInsert::Adapters::Helpers::AbstractQuery do
5
+ let!(:subject){ MassInsert::Adapters::Adapter.new([], {}) }
10
6
 
11
7
  describe "#begin_string" do
12
8
  it "should respond to begin_string method" do
13
- subject.respond_to?(:begin_string).should be_true
9
+ expect(subject).to respond_to(:begin_string)
14
10
  end
15
11
 
16
12
  it "should returns the correct string" do
17
13
  subject.stub(:table_name).and_return("users")
18
- string = "INSERT INTO users "
19
- subject.begin_string.should eq(string)
14
+ expect(subject.begin_string).to eq("INSERT INTO users ")
20
15
  end
21
16
  end
22
17
 
23
18
  describe "#string_columns" do
24
19
  it "should respond to string_columns method" do
25
- subject.respond_to?(:string_columns).should be_true
20
+ expect(subject).to respond_to(:string_columns)
26
21
  end
27
22
 
28
23
  it "should returns correct string to columns" do
29
24
  subject.stub(:column_names).and_return([:name, :email])
30
- subject.string_columns.should eq("(name, email) ")
25
+ expect(subject.string_columns).to eq("(name, email) ")
31
26
  end
32
27
  end
33
28
 
34
29
  describe "#string_values" do
35
30
  it "should respond to string_values method" do
36
- subject.respond_to?(:string_values).should be_true
31
+ expect(subject).to respond_to(:string_values)
37
32
  end
38
33
 
39
34
  it "should returns correct string to values" do
40
- subject.stub(:string_rows_values).and_return("string_rows_values")
41
- subject.string_values.should eq("VALUES (string_rows_values);")
35
+ subject.stub(:string_rows_values).and_return("rows_values")
36
+ expect(subject.string_values).to eq("VALUES (rows_values);")
42
37
  end
43
38
  end
44
39
 
45
40
  describe "#string_rows_values" do
46
41
  it "should respond to string_rows_values method" do
47
- subject.respond_to?(:string_rows_values).should be_true
42
+ expect(subject).to respond_to(:string_rows_values)
48
43
  end
49
44
 
50
45
  context "when only have one value hash" do
51
46
  it "should returns the correct string" do
52
- subject.stub(:string_single_row_values).and_return("single_row")
47
+ subject.stub(:string_single_row_values).and_return("row")
53
48
  subject.values = [{}]
54
- subject.string_rows_values.should eq("single_row")
49
+ expect(subject.string_rows_values).to eq("row")
55
50
  end
56
51
  end
57
52
 
58
53
  context "when have two or more value hashes" do
59
54
  it "should returns the correct string" do
60
- subject.stub(:string_single_row_values).and_return("single_row")
55
+ subject.stub(:string_single_row_values).and_return("row")
61
56
  subject.values = [{}, {}]
62
- subject.string_rows_values.should eq("single_row), (single_row")
57
+ expect(subject.string_rows_values).to eq("row), (row")
63
58
  end
64
59
  end
65
60
  end
66
61
 
67
62
  describe "#string_single_row_values" do
63
+ before :each do
64
+ subject.stub(:string_single_value).and_return("value")
65
+ end
66
+
68
67
  it "should respond to string_single_row_values method" do
69
- subject.respond_to?(:string_single_row_values).should be_true
68
+ expect(subject).to respond_to(:string_single_row_values)
70
69
  end
71
70
 
72
71
  it "should returns the correct string" do
73
- subject.stub(:string_single_value).and_return("single_value")
74
72
  subject.stub(:column_names).and_return([:name, :email])
75
- subject.string_single_row_values({}).should eq("single_value, single_value")
73
+ expect(subject.string_single_row_values({})).to eq("value, value")
76
74
  end
77
75
 
78
76
  context "when respond to timestamp attributes" do
79
77
  it "should call timestamp_values method" do
80
- subject.stub(:string_single_value).and_return("single_value")
81
78
  subject.stub(:column_names).and_return([:created_at, :updated_at])
82
79
  subject.stub(:timestamp_values).and_return(:test => "test")
83
80
  subject.should_receive(:timestamp_values).exactly(1).times
@@ -87,7 +84,6 @@ describe MassInsert::Adapters::AbstractQuery do
87
84
 
88
85
  context "when not respond to timestamp attributes" do
89
86
  it "should returns the correct string" do
90
- subject.stub(:string_single_value).and_return("single_value")
91
87
  subject.stub(:column_names).and_return([:name, :email])
92
88
  subject.should_receive(:timestamp_values).exactly(0).times
93
89
  subject.string_single_row_values({})
@@ -97,13 +93,13 @@ describe MassInsert::Adapters::AbstractQuery do
97
93
 
98
94
  describe "#string_single_value" do
99
95
  it "should respond to string_single_value method" do
100
- subject.respond_to?(:string_single_value).should be_true
96
+ expect(subject).to respond_to(:string_single_value)
101
97
  end
102
98
 
103
99
  it "should call build method in ColumnValue class" do
104
100
  column_value = MassInsert::Adapters::ColumnValue.any_instance
105
- column_value.stub(:build).and_return("single_value")
106
- subject.string_single_value({}, :name).should eq("single_value")
101
+ column_value.stub(:build).and_return("value")
102
+ expect(subject.string_single_value({}, :name)).to eq("value")
107
103
  end
108
104
  end
109
105
  end