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.
- data/README.md +3 -3
- data/lib/mass_insert/adapters.rb +0 -1
- data/lib/mass_insert/adapters/adapter.rb +1 -7
- data/lib/mass_insert/adapters/column_value.rb +33 -29
- data/lib/mass_insert/adapters/helpers.rb +3 -2
- data/lib/mass_insert/adapters/helpers/abstract_query.rb +48 -0
- data/lib/mass_insert/adapters/helpers/sanitizer.rb +6 -0
- data/lib/mass_insert/adapters/mysql2_adapter.rb +1 -1
- data/lib/mass_insert/base.rb +49 -20
- data/lib/mass_insert/query_builder.rb +6 -6
- data/lib/mass_insert/version.rb +1 -1
- data/spec/active_record_dummy/Gemfile +1 -1
- data/spec/active_record_dummy/config/database.yml +1 -1
- data/spec/mass_insert/adapters/adapter_spec.rb +23 -45
- data/spec/mass_insert/adapters/column_value_spec.rb +107 -154
- data/spec/mass_insert/adapters/{abstract_query_spec.rb → helpers/abstract_query_spec.rb} +23 -27
- data/spec/mass_insert/adapters/helpers/sanitizer_spec.rb +16 -9
- data/spec/mass_insert/adapters/helpers/timestamp_spec.rb +11 -15
- data/spec/mass_insert/adapters/helpers_spec.rb +7 -3
- data/spec/mass_insert/adapters/mysql_adapter_spec.rb +6 -10
- data/spec/mass_insert/adapters/postgresql_adapter_spec.rb +4 -8
- data/spec/mass_insert/adapters/sqlite3_adapter_spec.rb +24 -30
- data/spec/mass_insert/adapters/sqlserver_adapter_spec.rb +16 -21
- data/spec/mass_insert/adapters_spec.rb +8 -12
- data/spec/mass_insert/base_spec.rb +13 -13
- data/spec/mass_insert/process_control_spec.rb +33 -40
- data/spec/mass_insert/query_builder_spec.rb +20 -24
- data/spec/mass_insert/query_execution_spec.rb +13 -16
- data/spec/mass_insert_spec.rb +6 -6
- metadata +7 -7
- 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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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.
|
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.
|
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.
|
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
|
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.
|
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
|
36
|
+
expect(subject).to respond_to(:column_type)
|
49
37
|
end
|
50
38
|
|
51
|
-
|
52
|
-
|
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
|
46
|
+
expect(subject).to respond_to(:column_value)
|
69
47
|
end
|
70
48
|
|
71
|
-
it "should return
|
72
|
-
subject.
|
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
|
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
|
85
|
-
|
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
|
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.
|
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.
|
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
|
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.
|
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
|
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
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
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}"
|
128
|
+
method = :"column_value_#{column_type}"
|
142
129
|
|
143
|
-
describe "
|
130
|
+
describe "##{method.to_s}" do
|
144
131
|
it "should respond to #{method.to_s} method" do
|
145
|
-
subject.respond_to
|
132
|
+
expect(subject).to respond_to(method)
|
146
133
|
end
|
147
134
|
|
148
|
-
|
149
|
-
|
150
|
-
|
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
|
144
|
+
expect(subject).to respond_to(:column_value_integer)
|
168
145
|
end
|
169
146
|
|
170
|
-
context "when
|
171
|
-
it "should return the
|
172
|
-
subject.stub(:column_value).and_return(
|
173
|
-
subject.
|
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
|
179
|
-
|
180
|
-
|
181
|
-
|
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}"
|
163
|
+
method = :"column_value_#{column_type}"
|
197
164
|
|
198
|
-
describe "
|
165
|
+
describe "##{method.to_s}" do
|
199
166
|
it "should respond to #{method.to_s} method" do
|
200
|
-
subject.respond_to
|
167
|
+
expect(subject).to respond_to(method)
|
201
168
|
end
|
202
169
|
|
203
|
-
context "when
|
204
|
-
it "should return the
|
205
|
-
subject.stub(:column_value).and_return(
|
206
|
-
subject.
|
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
|
212
|
-
|
213
|
-
|
214
|
-
|
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
|
188
|
+
expect(subject).to respond_to(:column_value_boolean)
|
232
189
|
end
|
233
190
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
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
|
245
|
-
|
246
|
-
|
247
|
-
|
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
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
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
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
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
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
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
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
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
|
-
|
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
|
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
|
-
|
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
|
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.
|
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
|
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("
|
41
|
-
subject.string_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
|
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("
|
47
|
+
subject.stub(:string_single_row_values).and_return("row")
|
53
48
|
subject.values = [{}]
|
54
|
-
subject.string_rows_values.
|
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("
|
55
|
+
subject.stub(:string_single_row_values).and_return("row")
|
61
56
|
subject.values = [{}, {}]
|
62
|
-
subject.string_rows_values.
|
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
|
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({}).
|
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
|
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("
|
106
|
-
subject.string_single_value({}, :name).
|
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
|