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
@@ -10,22 +10,22 @@ describe MassInsert::Base do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should respond to mass_insert class method" do
|
13
|
-
Test.respond_to
|
13
|
+
expect(Test).to respond_to(:mass_insert)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should can receive values and many options" do
|
17
17
|
values = [{:name => "name"}]
|
18
18
|
options = {:option_one => "one", :option_two => "two"}
|
19
|
-
Test.mass_insert(values, options).
|
19
|
+
expect(Test.mass_insert(values, options)).to be_true
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should can receive only values" do
|
23
23
|
values = [{:name => "name"}]
|
24
|
-
Test.mass_insert(values).
|
24
|
+
expect(Test.mass_insert(values)).to be_true
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should not can called with values" do
|
28
|
-
lambda{ Test.mass_insert }.
|
28
|
+
expect(lambda{ Test.mass_insert }).to raise_error
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should call execute ProcessControl method" do
|
@@ -43,7 +43,7 @@ describe MassInsert::Base do
|
|
43
43
|
|
44
44
|
describe ".mass_insert_results" do
|
45
45
|
it "should respond to mass_insert_results class method" do
|
46
|
-
Test.respond_to
|
46
|
+
expect(Test).to respond_to(:mass_insert_results)
|
47
47
|
end
|
48
48
|
|
49
49
|
context "when mass_insert_process instance variable exists" do
|
@@ -61,52 +61,52 @@ describe MassInsert::Base do
|
|
61
61
|
describe "class_name" do
|
62
62
|
it "returns class name that call if that option doesn't exist" do
|
63
63
|
options = Test.send(:mass_insert_options)
|
64
|
-
options[:class_name].
|
64
|
+
expect(options[:class_name]).to eq(Test)
|
65
65
|
end
|
66
66
|
|
67
67
|
it "returns class_name option if is in the options" do
|
68
68
|
args = {:class_name => "OtherClass"}
|
69
69
|
options = Test.send(:mass_insert_options, args)
|
70
|
-
options[:class_name].
|
70
|
+
expect(options[:class_name]).to eq("OtherClass")
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
74
|
describe "table_name" do
|
75
75
|
it "returns class table_name that call if options doesn't exist" do
|
76
76
|
options = Test.send(:mass_insert_options)
|
77
|
-
options[:table_name].
|
77
|
+
expect(options[:table_name]).to eq(Test.table_name)
|
78
78
|
end
|
79
79
|
|
80
80
|
it "returns table_name option if is in the options" do
|
81
81
|
args = {:table_name => "OtherTable"}
|
82
82
|
options = Test.send(:mass_insert_options, args)
|
83
|
-
options[:table_name].
|
83
|
+
expect(options[:table_name]).to eq("OtherTable")
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
87
|
describe "primary_key" do
|
88
88
|
it "returns :id if option primary_key doesn't exist" do
|
89
89
|
options = Test.send(:mass_insert_options)
|
90
|
-
options[:primary_key].
|
90
|
+
expect(options[:primary_key]).to eq(:id)
|
91
91
|
end
|
92
92
|
|
93
93
|
it "returns primary_key option if is in the options" do
|
94
94
|
args = {:primary_key => :user_id}
|
95
95
|
options = Test.send(:mass_insert_options, args)
|
96
|
-
options[:primary_key].
|
96
|
+
expect(options[:primary_key]).to eq(:user_id)
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
100
100
|
describe "primary_key_mode" do
|
101
101
|
it "returns :auto if option primary_key_mode doesn't exist" do
|
102
102
|
options = Test.send(:mass_insert_options)
|
103
|
-
options[:primary_key_mode].
|
103
|
+
expect(options[:primary_key_mode]).to eq(:auto)
|
104
104
|
end
|
105
105
|
|
106
106
|
it "returns primary_key_mode option if is in the options" do
|
107
107
|
args = {:primary_key_mode => :manual}
|
108
108
|
options = Test.send(:mass_insert_options, args)
|
109
|
-
options[:primary_key_mode].
|
109
|
+
expect(options[:primary_key_mode]).to eq(:manual)
|
110
110
|
end
|
111
111
|
end
|
112
112
|
end
|
@@ -2,39 +2,29 @@ require './spec/spec_helper'
|
|
2
2
|
require "./lib/mass_insert"
|
3
3
|
|
4
4
|
describe MassInsert::ProcessControl do
|
5
|
-
|
6
|
-
before :each do
|
7
|
-
@process = MassInsert::ProcessControl.new([], {})
|
8
|
-
end
|
9
|
-
|
10
|
-
subject{ @process }
|
5
|
+
let!(:subject){ MassInsert::ProcessControl.new([], {}) }
|
11
6
|
|
12
7
|
describe "instance methods" do
|
13
8
|
describe "#initialize" do
|
14
|
-
|
15
|
-
before :each do
|
16
|
-
@values = [{:name => "name"}]
|
17
|
-
@options = {:option_one => 10}
|
18
|
-
@process = MassInsert::ProcessControl.new(@values, @options)
|
19
|
-
end
|
9
|
+
let(:process){ MassInsert::ProcessControl.new("values", "options") }
|
20
10
|
|
21
11
|
it "should initialize the values" do
|
22
|
-
|
12
|
+
expect(process.values).to eq("values")
|
23
13
|
end
|
24
14
|
|
25
15
|
it "should initialize the options" do
|
26
|
-
|
16
|
+
expect(process.options).to eq("options")
|
27
17
|
end
|
28
18
|
end
|
29
19
|
|
30
20
|
describe "#start" do
|
31
21
|
before :each do
|
32
|
-
subject.stub(:build_query)
|
33
|
-
subject.stub(:execute_query)
|
22
|
+
subject.stub(:build_query)
|
23
|
+
subject.stub(:execute_query)
|
34
24
|
end
|
35
25
|
|
36
26
|
it "should respond to start method" do
|
37
|
-
subject.respond_to
|
27
|
+
expect(subject).to respond_to(:start)
|
38
28
|
end
|
39
29
|
|
40
30
|
it "should call build_query" do
|
@@ -44,7 +34,7 @@ describe MassInsert::ProcessControl do
|
|
44
34
|
|
45
35
|
it "should define instance variable @build_time" do
|
46
36
|
subject.start
|
47
|
-
subject.instance_variables.include
|
37
|
+
expect(subject.instance_variables).to include(:@build_time)
|
48
38
|
end
|
49
39
|
|
50
40
|
it "should call execute_query" do
|
@@ -54,31 +44,32 @@ describe MassInsert::ProcessControl do
|
|
54
44
|
|
55
45
|
it "should define instance variable @execute_time" do
|
56
46
|
subject.start
|
57
|
-
subject.instance_variables.include
|
47
|
+
expect(subject.instance_variables).to include(:@execute_time)
|
58
48
|
end
|
59
49
|
end
|
60
50
|
|
61
51
|
describe "#execute_query" do
|
52
|
+
before :each do
|
53
|
+
@execution = MassInsert::QueryExecution.any_instance
|
54
|
+
@execution.stub(:execute)
|
55
|
+
end
|
56
|
+
|
62
57
|
it "should respond to execute_query method" do
|
63
|
-
subject.respond_to
|
58
|
+
expect(subject).to respond_to(:execute_query)
|
64
59
|
end
|
65
60
|
|
66
|
-
context "when query instance variable
|
61
|
+
context "when query instance variable returns not nil" do
|
67
62
|
it "should instance and call QueryExecution class" do
|
68
63
|
subject.instance_variable_set(:@query, "query")
|
69
|
-
execution
|
70
|
-
execution.stub(:execute).and_return("executed")
|
71
|
-
execution.should_receive(:execute).exactly(1).times
|
64
|
+
@execution.should_receive(:execute).exactly(1).times
|
72
65
|
subject.execute_query
|
73
66
|
end
|
74
67
|
end
|
75
68
|
|
76
|
-
context "when query instance variable
|
69
|
+
context "when query instance variable returns nil" do
|
77
70
|
it "should instance and call QueryExecution class" do
|
78
71
|
subject.instance_variable_set(:@query, nil)
|
79
|
-
execution
|
80
|
-
execution.stub(:execute).and_return("executed")
|
81
|
-
execution.should_not_receive(:execute)
|
72
|
+
@execution.should_not_receive(:execute)
|
82
73
|
subject.execute_query
|
83
74
|
end
|
84
75
|
end
|
@@ -86,47 +77,49 @@ describe MassInsert::ProcessControl do
|
|
86
77
|
|
87
78
|
describe "#build_query" do
|
88
79
|
it "should respond to build_query method" do
|
89
|
-
subject.respond_to
|
80
|
+
expect(subject).to respond_to(:build_query)
|
90
81
|
end
|
91
82
|
|
92
83
|
it "should instance and call QueryBuilder class" do
|
93
|
-
|
94
|
-
|
95
|
-
subject.build_query.should eq("query")
|
84
|
+
MassInsert::QueryBuilder.any_instance.stub(:build => "query")
|
85
|
+
expect(subject.build_query).to eq("query")
|
96
86
|
end
|
97
87
|
|
98
88
|
it "should set query instance variable" do
|
99
|
-
|
100
|
-
builder.stub(:build).and_return("query")
|
89
|
+
MassInsert::QueryBuilder.any_instance.stub(:build)
|
101
90
|
subject.build_query
|
102
|
-
subject.instance_variables.include
|
91
|
+
expect(subject.instance_variables).to include(:@query)
|
103
92
|
end
|
104
93
|
end
|
105
94
|
end
|
106
95
|
|
107
96
|
describe "#results" do
|
108
97
|
before :each do
|
109
|
-
subject.stub(:values).and_return([{}, {}])
|
110
98
|
a = Benchmark.measure{}
|
111
99
|
a.stub(:total).and_return(10.0)
|
112
100
|
subject.instance_variable_set(:@build_time, a)
|
113
101
|
subject.instance_variable_set(:@execute_time, a)
|
114
102
|
end
|
115
103
|
|
104
|
+
it "should respond to results method" do
|
105
|
+
expect(subject).to respond_to(:results)
|
106
|
+
end
|
107
|
+
|
116
108
|
it "should return total time in results" do
|
117
|
-
subject.results.time.
|
109
|
+
expect(subject.results.time).to eql(20.0)
|
118
110
|
end
|
119
111
|
|
120
112
|
it "should return records persisted in results" do
|
121
|
-
subject.
|
113
|
+
subject.stub(:values => [{}, {}])
|
114
|
+
expect(subject.results.records).to eql(2)
|
122
115
|
end
|
123
116
|
|
124
117
|
it "should return build time in results" do
|
125
|
-
subject.results.build_time.
|
118
|
+
expect(subject.results.build_time).to eql(10.0)
|
126
119
|
end
|
127
120
|
|
128
121
|
it "should return execute time in results" do
|
129
|
-
subject.results.execute_time.
|
122
|
+
expect(subject.results.execute_time).to eql(10.0)
|
130
123
|
end
|
131
124
|
end
|
132
125
|
end
|
@@ -2,61 +2,57 @@ require './spec/spec_helper'
|
|
2
2
|
require "./lib/mass_insert"
|
3
3
|
|
4
4
|
describe MassInsert::QueryBuilder do
|
5
|
-
|
6
|
-
@builder = MassInsert::QueryBuilder.new([], {})
|
7
|
-
end
|
8
|
-
|
9
|
-
subject{ @builder }
|
5
|
+
let!(:subject){ MassInsert::QueryBuilder.new([], {}) }
|
10
6
|
|
11
7
|
describe "instance methods" do
|
12
8
|
describe "#initialize" do
|
13
|
-
|
14
|
-
@builder = MassInsert::QueryBuilder.new("values", "options")
|
15
|
-
end
|
9
|
+
let(:builder){ MassInsert::QueryBuilder.new("values", "options") }
|
16
10
|
|
17
11
|
it "should initialize the values attribute" do
|
18
|
-
|
12
|
+
expect(builder.values).to eq("values")
|
19
13
|
end
|
20
14
|
|
21
15
|
it "should initialize the options attribute" do
|
22
|
-
|
16
|
+
expect(builder.options).to eq("options")
|
23
17
|
end
|
24
18
|
end
|
25
19
|
|
26
20
|
describe "#build" do
|
27
21
|
it "should respond to build method" do
|
28
|
-
subject.respond_to
|
22
|
+
expect(subject).to respond_to(:build)
|
29
23
|
end
|
30
24
|
|
31
25
|
it "should return the query string" do
|
32
|
-
subject.stub(:
|
33
|
-
subject.
|
34
|
-
subject.
|
26
|
+
subject.stub(:adapter_class).and_return("adapter_class")
|
27
|
+
subject.adapter_class.stub(:new).and_return("adapter_instance")
|
28
|
+
subject.adapter_class.new.stub(:execute).and_return("query")
|
29
|
+
expect(subject.build).to eq("query")
|
35
30
|
end
|
36
31
|
end
|
37
32
|
|
38
33
|
describe "#adapter" do
|
39
34
|
it "should respond to adapter method" do
|
40
|
-
subject.respond_to
|
35
|
+
expect(subject).to respond_to(:adapter)
|
41
36
|
end
|
42
37
|
|
43
38
|
it "should return the adapter type" do
|
44
39
|
config = {"config" => {:adapter => "sql"}}
|
45
|
-
ActiveRecord::Base.connection
|
46
|
-
|
40
|
+
connection = ActiveRecord::Base.connection
|
41
|
+
connection.stub(:instance_values).and_return(config)
|
42
|
+
expect(subject.adapter).to eq("sql")
|
47
43
|
end
|
48
44
|
end
|
49
45
|
|
50
|
-
describe "#
|
51
|
-
it "should respond to
|
52
|
-
subject.respond_to
|
46
|
+
describe "#adapter_class" do
|
47
|
+
it "should respond to adapter_class method" do
|
48
|
+
expect(subject).to respond_to(:adapter_class)
|
53
49
|
end
|
54
50
|
|
55
51
|
context "when adapter is mysql2" do
|
56
52
|
it "should return a Mysql2Adapter instance" do
|
57
53
|
subject.stub(:adapter).and_return("mysql2")
|
58
54
|
instance_class = MassInsert::Adapters::Mysql2Adapter
|
59
|
-
subject.
|
55
|
+
expect(subject.adapter_class).to eq(instance_class)
|
60
56
|
end
|
61
57
|
end
|
62
58
|
|
@@ -64,7 +60,7 @@ describe MassInsert::QueryBuilder do
|
|
64
60
|
it "should return a PostgreSQLAdapter instance" do
|
65
61
|
subject.stub(:adapter).and_return("postgresql")
|
66
62
|
instance_class = MassInsert::Adapters::PostgreSQLAdapter
|
67
|
-
subject.
|
63
|
+
expect(subject.adapter_class).to eq(instance_class)
|
68
64
|
end
|
69
65
|
end
|
70
66
|
|
@@ -72,7 +68,7 @@ describe MassInsert::QueryBuilder do
|
|
72
68
|
it "should return a SQLite3Adapter instance" do
|
73
69
|
subject.stub(:adapter).and_return("sqlite3")
|
74
70
|
instance_class = MassInsert::Adapters::SQLite3Adapter
|
75
|
-
subject.
|
71
|
+
expect(subject.adapter_class).to eq(instance_class)
|
76
72
|
end
|
77
73
|
end
|
78
74
|
|
@@ -80,7 +76,7 @@ describe MassInsert::QueryBuilder do
|
|
80
76
|
it "should return a SQLServerAdapter instance" do
|
81
77
|
subject.stub(:adapter).and_return("sqlserver")
|
82
78
|
instance_class = MassInsert::Adapters::SQLServerAdapter
|
83
|
-
subject.
|
79
|
+
expect(subject.adapter_class).to eq(instance_class)
|
84
80
|
end
|
85
81
|
end
|
86
82
|
end
|
@@ -2,49 +2,46 @@ require './spec/spec_helper'
|
|
2
2
|
require "./lib/mass_insert"
|
3
3
|
|
4
4
|
describe MassInsert::QueryExecution do
|
5
|
-
|
6
|
-
before :each do
|
7
|
-
@execution = MassInsert::QueryExecution.new("query string")
|
8
|
-
end
|
9
|
-
|
10
|
-
subject{ @execution }
|
5
|
+
let!(:subject){ MassInsert::QueryExecution.new("query string") }
|
11
6
|
|
12
7
|
describe "instance methods" do
|
13
8
|
describe "#initialize" do
|
14
9
|
context "when params passed is a string" do
|
15
10
|
it "query_container should be an array with the param" do
|
16
11
|
execution = MassInsert::QueryExecution.new("option")
|
17
|
-
execution.query_container.
|
12
|
+
expect(execution.query_container).to eq(["option"])
|
18
13
|
end
|
19
14
|
end
|
20
15
|
|
21
16
|
context "when params passed is an array" do
|
22
17
|
it "query_container should be the array passed by param" do
|
23
|
-
|
24
|
-
execution
|
25
|
-
execution.query_container.should eq(params)
|
18
|
+
execution = MassInsert::QueryExecution.new(["one", "two"])
|
19
|
+
expect(execution.query_container).to eq(["one", "two"])
|
26
20
|
end
|
27
21
|
end
|
28
22
|
end
|
29
23
|
|
30
24
|
describe "#execute" do
|
25
|
+
before :each do
|
26
|
+
@connection = ActiveRecord::Base.connection
|
27
|
+
@connection.stub(:execute)
|
28
|
+
end
|
29
|
+
|
31
30
|
it "should respond to execute method" do
|
32
|
-
subject.respond_to
|
31
|
+
expect(subject).to respond_to(:execute)
|
33
32
|
end
|
34
33
|
|
35
34
|
context "when query container has one query" do
|
36
35
|
it "should call ActiveRecord execute one time" do
|
37
|
-
|
38
|
-
ActiveRecord::Base.connection.should_receive(:execute).exactly(1).times
|
36
|
+
@connection.should_receive(:execute).exactly(1).times
|
39
37
|
subject.execute
|
40
38
|
end
|
41
39
|
end
|
42
40
|
|
43
41
|
context "when query container is an array with two queries" do
|
44
42
|
it "should call ActiveRecord execute one time" do
|
45
|
-
subject.query_container = ["
|
46
|
-
|
47
|
-
ActiveRecord::Base.connection.should_receive(:execute).exactly(2).times
|
43
|
+
subject.query_container = ["one", "two"]
|
44
|
+
@connection.should_receive(:execute).exactly(2).times
|
48
45
|
subject.execute
|
49
46
|
end
|
50
47
|
end
|
data/spec/mass_insert_spec.rb
CHANGED
@@ -3,26 +3,26 @@ require "./lib/mass_insert"
|
|
3
3
|
|
4
4
|
describe MassInsert do
|
5
5
|
it 'should be defined' do
|
6
|
-
|
6
|
+
expect(MassInsert).to be
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'should define Adapters module' do
|
10
|
-
MassInsert::Adapters.
|
10
|
+
expect(MassInsert::Adapters).to be
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'should define Base' do
|
14
|
-
MassInsert::Base.
|
14
|
+
expect(MassInsert::Base).to be
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'should define ProcessControl' do
|
18
|
-
MassInsert::ProcessControl.
|
18
|
+
expect(MassInsert::ProcessControl).to be
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'should define QueryBuilder' do
|
22
|
-
MassInsert::QueryBuilder.
|
22
|
+
expect(MassInsert::QueryBuilder).to be
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'should define QueryExecution' do
|
26
|
-
MassInsert::QueryExecution.
|
26
|
+
expect(MassInsert::QueryExecution).to be
|
27
27
|
end
|
28
28
|
end
|