mass_insert 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +1 -0
  3. data/.rvmrc +53 -0
  4. data/.travis.yml +4 -0
  5. data/Gemfile +13 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +58 -0
  8. data/Rakefile +45 -0
  9. data/lib/mass_insert.rb +13 -0
  10. data/lib/mass_insert/adapters.rb +12 -0
  11. data/lib/mass_insert/adapters/abstract_query.rb +47 -0
  12. data/lib/mass_insert/adapters/adapter.rb +56 -0
  13. data/lib/mass_insert/adapters/column_value.rb +107 -0
  14. data/lib/mass_insert/adapters/helpers.rb +8 -0
  15. data/lib/mass_insert/adapters/helpers/sanitizer.rb +17 -0
  16. data/lib/mass_insert/adapters/helpers/timestamp.rb +38 -0
  17. data/lib/mass_insert/adapters/mysql2_adapter.rb +21 -0
  18. data/lib/mass_insert/adapters/postgresql_adapter.rb +15 -0
  19. data/lib/mass_insert/adapters/sqlite3_adapter.rb +37 -0
  20. data/lib/mass_insert/adapters/sqlserver_adapter.rb +23 -0
  21. data/lib/mass_insert/base.rb +43 -0
  22. data/lib/mass_insert/process_control.rb +24 -0
  23. data/lib/mass_insert/query_builder.rb +42 -0
  24. data/lib/mass_insert/query_execution.rb +29 -0
  25. data/lib/mass_insert/version.rb +3 -0
  26. data/mass_insert.gemspec +22 -0
  27. data/spec/active_record_dummy/.gitignore +15 -0
  28. data/spec/active_record_dummy/Gemfile +40 -0
  29. data/spec/active_record_dummy/README.rdoc +261 -0
  30. data/spec/active_record_dummy/Rakefile +7 -0
  31. data/spec/active_record_dummy/app/assets/images/rails.png +0 -0
  32. data/spec/active_record_dummy/app/assets/javascripts/application.js +15 -0
  33. data/spec/active_record_dummy/app/assets/stylesheets/application.css +13 -0
  34. data/spec/active_record_dummy/app/controllers/application_controller.rb +3 -0
  35. data/spec/active_record_dummy/app/helpers/application_helper.rb +2 -0
  36. data/spec/active_record_dummy/app/mailers/.gitkeep +0 -0
  37. data/spec/active_record_dummy/app/models/.gitkeep +0 -0
  38. data/spec/active_record_dummy/app/models/user.rb +3 -0
  39. data/spec/active_record_dummy/app/views/layouts/application.html.erb +14 -0
  40. data/spec/active_record_dummy/config.ru +4 -0
  41. data/spec/active_record_dummy/config/application.rb +68 -0
  42. data/spec/active_record_dummy/config/boot.rb +6 -0
  43. data/spec/active_record_dummy/config/database.yml +54 -0
  44. data/spec/active_record_dummy/config/environment.rb +5 -0
  45. data/spec/active_record_dummy/config/environments/development.rb +37 -0
  46. data/spec/active_record_dummy/config/environments/mysql2.rb +37 -0
  47. data/spec/active_record_dummy/config/environments/postgresql.rb +37 -0
  48. data/spec/active_record_dummy/config/environments/production.rb +67 -0
  49. data/spec/active_record_dummy/config/environments/sqlite3.rb +37 -0
  50. data/spec/active_record_dummy/config/environments/test.rb +37 -0
  51. data/spec/active_record_dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/spec/active_record_dummy/config/initializers/inflections.rb +15 -0
  53. data/spec/active_record_dummy/config/initializers/mime_types.rb +5 -0
  54. data/spec/active_record_dummy/config/initializers/secret_token.rb +7 -0
  55. data/spec/active_record_dummy/config/initializers/session_store.rb +8 -0
  56. data/spec/active_record_dummy/config/initializers/wrap_parameters.rb +14 -0
  57. data/spec/active_record_dummy/config/locales/en.yml +5 -0
  58. data/spec/active_record_dummy/config/routes.rb +58 -0
  59. data/spec/active_record_dummy/db/migrate/20130412154541_create_users.rb +14 -0
  60. data/spec/active_record_dummy/db/schema.rb +27 -0
  61. data/spec/active_record_dummy/db/seeds.rb +7 -0
  62. data/spec/active_record_dummy/lib/assets/.gitkeep +0 -0
  63. data/spec/active_record_dummy/lib/tasks/.gitkeep +0 -0
  64. data/spec/active_record_dummy/log/.gitkeep +0 -0
  65. data/spec/active_record_dummy/public/404.html +26 -0
  66. data/spec/active_record_dummy/public/422.html +26 -0
  67. data/spec/active_record_dummy/public/500.html +25 -0
  68. data/spec/active_record_dummy/public/favicon.ico +0 -0
  69. data/spec/active_record_dummy/public/index.html +241 -0
  70. data/spec/active_record_dummy/public/robots.txt +5 -0
  71. data/spec/active_record_dummy/script/rails +6 -0
  72. data/spec/active_record_dummy/vendor/assets/javascripts/.gitkeep +0 -0
  73. data/spec/active_record_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  74. data/spec/active_record_dummy/vendor/plugins/.gitkeep +0 -0
  75. data/spec/active_record_models/column_types/binary_spec.rb +60 -0
  76. data/spec/active_record_models/column_types/boolean_spec.rb +52 -0
  77. data/spec/active_record_models/column_types/decimal_spec.rb +49 -0
  78. data/spec/active_record_models/column_types/integer_spec.rb +49 -0
  79. data/spec/active_record_models/column_types/string_spec.rb +50 -0
  80. data/spec/active_record_models/model_spec.rb +50 -0
  81. data/spec/dummy_models/test.rb +5 -0
  82. data/spec/mass_insert/adapters/abstract_query_spec.rb +109 -0
  83. data/spec/mass_insert/adapters/adapter_spec.rb +117 -0
  84. data/spec/mass_insert/adapters/column_value_spec.rb +292 -0
  85. data/spec/mass_insert/adapters/helpers/sanitizer_spec.rb +39 -0
  86. data/spec/mass_insert/adapters/helpers/timestamp_spec.rb +79 -0
  87. data/spec/mass_insert/adapters/helpers_spec.rb +16 -0
  88. data/spec/mass_insert/adapters/mysql_adapter_spec.rb +39 -0
  89. data/spec/mass_insert/adapters/postgresql_adapter_spec.rb +29 -0
  90. data/spec/mass_insert/adapters/sqlite3_adapter_spec.rb +90 -0
  91. data/spec/mass_insert/adapters/sqlserver_adapter_spec.rb +56 -0
  92. data/spec/mass_insert/adapters_spec.rb +40 -0
  93. data/spec/mass_insert/base_spec.rb +98 -0
  94. data/spec/mass_insert/process_control_spec.rb +56 -0
  95. data/spec/mass_insert/query_builder_spec.rb +88 -0
  96. data/spec/mass_insert/query_execution_spec.rb +53 -0
  97. data/spec/mass_insert_spec.rb +28 -0
  98. data/spec/spec_helper.rb +6 -0
  99. metadata +254 -0
@@ -0,0 +1,79 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe MassInsert::Adapters::Helpers::Timestamp do
5
+ before :each do
6
+ @adapter = MassInsert::Adapters::Adapter.new([], {})
7
+ end
8
+
9
+ subject{ @adapter }
10
+
11
+ describe "#timestamp?" do
12
+ it "should respond_to timestamp? method" do
13
+ subject.respond_to?(:timestamp?).should be_true
14
+ end
15
+
16
+ context "when respond to timestamp columns" do
17
+ it "should return true" do
18
+ subject.stub(:column_names).and_return([:updated_at, :created_at])
19
+ subject.timestamp?.should be_true
20
+ end
21
+ end
22
+
23
+ context "when not respond to timestamp columns" do
24
+ it "should return false" do
25
+ subject.stub(:column_names).and_return([:created_at])
26
+ subject.timestamp?.should be_false
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "#timestamp_format" do
32
+ it "should respond_to timestamp_format method" do
33
+ subject.respond_to?(:timestamp_format).should be_true
34
+ end
35
+
36
+ it "should return the default timestamp format" do
37
+ subject.timestamp_format.should eq("%Y-%m-%d %H:%M:%S.%6N")
38
+ end
39
+ end
40
+
41
+ describe "#timestamp" do
42
+ it "should respond_to timestamp method" do
43
+ subject.respond_to?(:timestamp).should be_true
44
+ end
45
+
46
+ it "should return the default timestamp value with correct format" do
47
+ subject.stub(:timestamp_format).and_return("%Y-%m-%d %H:%M:%S")
48
+ timestamp_value = Time.now.strftime("%Y-%m-%d %H:%M:%S")
49
+ subject.timestamp.should eq(timestamp_value)
50
+ end
51
+ end
52
+
53
+ describe "#timestamp_values" do
54
+ it "should respond_to timestamp_values method" do
55
+ subject.respond_to?(:timestamp_values).should be_true
56
+ end
57
+
58
+ context "when have high precision" do
59
+ it "should no be equals" do
60
+ timestamp_values = {
61
+ :created_at => subject.timestamp,
62
+ :updated_at => subject.timestamp
63
+ }
64
+ subject.timestamp_values.should_not eq(timestamp_values)
65
+ end
66
+ end
67
+
68
+ context "when do not have precision" do
69
+ it "should be equals" do
70
+ subject.stub(:timestamp_format).and_return("%Y-%m-%d %H:%M:%S")
71
+ timestamp_values = {
72
+ :created_at => subject.timestamp,
73
+ :updated_at => subject.timestamp
74
+ }
75
+ subject.timestamp_values.should eq(timestamp_values)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,16 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe MassInsert::Adapters::Helpers do
5
+ it 'should be defined' do
6
+ should be
7
+ end
8
+
9
+ it 'should define Timestamp module' do
10
+ MassInsert::Adapters::Helpers::Timestamp.should be
11
+ end
12
+
13
+ it 'should define Sanitizer module' do
14
+ MassInsert::Adapters::Helpers::Sanitizer.should be
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe MassInsert::Adapters::Mysql2Adapter do
5
+ before :each do
6
+ @adapter = MassInsert::Adapters::Mysql2Adapter.new([], {})
7
+ end
8
+
9
+ subject{ @adapter }
10
+
11
+ it "should inherit from Adapter class" do
12
+ subject.should be_a(MassInsert::Adapters::Adapter)
13
+ end
14
+
15
+ describe "instance methods" do
16
+ describe "#timestamp_format" do
17
+ it "should respond to timestamp_format method" do
18
+ subject.respond_to?(:timestamp_format).should be_true
19
+ end
20
+
21
+ it "should return the format string" do
22
+ subject.timestamp_format.should eq("%Y-%m-%d %H:%M:%S")
23
+ end
24
+ end
25
+
26
+ describe "#execute" do
27
+ it "should respond to execute method" do
28
+ subject.respond_to?(:execute).should be_true
29
+ end
30
+
31
+ it "call methods and returns their values concatenated" do
32
+ subject.stub(:begin_string).and_return("a")
33
+ subject.stub(:string_columns).and_return("b")
34
+ subject.stub(:string_values).and_return("c")
35
+ subject.execute.should eq("abc")
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,29 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe MassInsert::Adapters::PostgreSQLAdapter do
5
+ before :each do
6
+ @adapter = MassInsert::Adapters::PostgreSQLAdapter.new([], {})
7
+ end
8
+
9
+ subject{ @adapter }
10
+
11
+ it "should inherit from Adapter class" do
12
+ subject.should be_a(MassInsert::Adapters::Adapter)
13
+ end
14
+
15
+ describe "instance methods" do
16
+ describe "#execute" do
17
+ it "should respond to execute method" do
18
+ subject.respond_to?(:execute).should be_true
19
+ end
20
+
21
+ it "call methods and returns their values concatenated" do
22
+ subject.stub(:begin_string).and_return("a")
23
+ subject.stub(:string_columns).and_return("b")
24
+ subject.stub(:string_values).and_return("c")
25
+ subject.execute.should eq("abc")
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,90 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe MassInsert::Adapters::SQLite3Adapter do
5
+ before :each do
6
+ @adapter = MassInsert::Adapters::SQLite3Adapter.new([], {})
7
+ end
8
+
9
+ subject{ @adapter }
10
+
11
+ it "should inherit from Adapter class" do
12
+ subject.should be_a(MassInsert::Adapters::Adapter)
13
+ end
14
+
15
+ describe "instance methods" do
16
+ describe "#string_values" do
17
+ it "should respond to string_values method" do
18
+ subject.respond_to?(:string_values).should be_true
19
+ end
20
+
21
+ it "should returns correct string to values" do
22
+ subject.stub(:string_rows_values).and_return("string_rows_values")
23
+ subject.string_values.should eq("SELECT string_rows_values;")
24
+ end
25
+ end
26
+
27
+ describe "#string_rows_values" do
28
+ it "should respond to string_rows_values method" do
29
+ subject.respond_to?(:string_rows_values).should be_true
30
+ end
31
+
32
+ context "when only have one value hash" do
33
+ it "should returns the correct string" do
34
+ subject.stub(:string_single_row_values).and_return("single_row")
35
+ subject.values = [{}]
36
+ subject.string_rows_values.should eq("single_row")
37
+ end
38
+ end
39
+
40
+ context "when have two or more value hashes" do
41
+ it "should returns the correct string" do
42
+ subject.stub(:string_single_row_values).and_return("single_row")
43
+ subject.values = [{}, {}]
44
+ string = "single_row UNION SELECT single_row"
45
+ subject.string_rows_values.should eq(string)
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#execute" do
51
+ it "should respond to execute method" do
52
+ subject.respond_to?(:execute).should be_true
53
+ end
54
+
55
+ context "when have less than 500 values" do
56
+ it "call methods and returns their values concatenated" do
57
+ subject.values = [{}]
58
+ subject.stub(:begin_string).and_return("a")
59
+ subject.stub(:string_columns).and_return("b")
60
+ subject.stub(:string_values).and_return("c")
61
+ subject.execute.should eq(["abc"])
62
+ end
63
+ end
64
+
65
+ context "when have more than 500 values" do
66
+ it "call methods and returns their values concatenated" do
67
+ subject.values = []
68
+ 800.times{ subject.values << {} }
69
+ subject.stub(:begin_string).and_return("a")
70
+ subject.stub(:string_columns).and_return("b")
71
+ subject.stub(:string_values).and_return("c")
72
+ subject.execute.should eq(["abc", "abc"])
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ describe "MAX_VALUES_PER_INSERTION" do
79
+ it "should respond_to" do
80
+ class_name = MassInsert::Adapters::SQLite3Adapter
81
+ constant = "MAX_VALUES_PER_INSERTION".to_sym
82
+ class_name.const_defined?(constant).should be_true
83
+ end
84
+
85
+ it "should return 1000" do
86
+ class_name = MassInsert::Adapters::SQLite3Adapter
87
+ class_name::MAX_VALUES_PER_INSERTION.should eq(500)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,56 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe MassInsert::Adapters::SQLServerAdapter do
5
+ before :each do
6
+ @adapter = MassInsert::Adapters::SQLServerAdapter.new([], {})
7
+ end
8
+
9
+ subject{ @adapter }
10
+
11
+ it "should inherit from Adapter class" do
12
+ subject.should be_a(MassInsert::Adapters::Adapter)
13
+ end
14
+
15
+ describe "instance methods" do
16
+ describe "#execute" do
17
+ it "should respond to execute method" do
18
+ subject.respond_to?(:execute).should be_true
19
+ end
20
+
21
+ context "when have less than 1000 values" do
22
+ it "call methods and returns their values concatenated" do
23
+ subject.values = [{}]
24
+ subject.stub(:begin_string).and_return("a")
25
+ subject.stub(:string_columns).and_return("b")
26
+ subject.stub(:string_values).and_return("c")
27
+ subject.execute.should eq(["abc"])
28
+ end
29
+ end
30
+
31
+ context "when have more than 1000 values" do
32
+ it "call methods and returns their values concatenated" do
33
+ subject.values = []
34
+ 1500.times{ subject.values << {} }
35
+ subject.stub(:begin_string).and_return("a")
36
+ subject.stub(:string_columns).and_return("b")
37
+ subject.stub(:string_values).and_return("c")
38
+ subject.execute.should eq(["abc", "abc"])
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "MAX_VALUES_PER_INSERTION" do
45
+ it "should respond_to" do
46
+ class_name = MassInsert::Adapters::SQLServerAdapter
47
+ constant = "MAX_VALUES_PER_INSERTION".to_sym
48
+ class_name.const_defined?(constant).should be_true
49
+ end
50
+
51
+ it "should return 1000" do
52
+ class_name = MassInsert::Adapters::SQLServerAdapter
53
+ class_name::MAX_VALUES_PER_INSERTION.should eq(1000)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,40 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe MassInsert::Adapters do
5
+ it 'should be defined' do
6
+ should be
7
+ end
8
+
9
+ it 'should define Adapter class' do
10
+ MassInsert::Adapters::Adapter.should be
11
+ end
12
+
13
+ it 'should define AbstractQuery class' do
14
+ MassInsert::Adapters::AbstractQuery.should be
15
+ end
16
+
17
+ it 'should define ColumnValue class' do
18
+ MassInsert::Adapters::ColumnValue.should be
19
+ end
20
+
21
+ it 'should define ColumnValue class' do
22
+ MassInsert::Adapters::Helpers.should be
23
+ end
24
+
25
+ it 'should define MysqlAdapter class' do
26
+ MassInsert::Adapters::Mysql2Adapter.should be
27
+ end
28
+
29
+ it 'should define PostgreSQLAdapter class' do
30
+ MassInsert::Adapters::PostgreSQLAdapter.should be
31
+ end
32
+
33
+ it 'should define SQLite3Adapter class' do
34
+ MassInsert::Adapters::SQLite3Adapter.should be
35
+ end
36
+
37
+ it 'should define SQLServerAdapter class' do
38
+ MassInsert::Adapters::SQLServerAdapter.should be
39
+ end
40
+ end
@@ -0,0 +1,98 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+ require "./spec/dummy_models/test"
4
+
5
+ describe MassInsert::Base do
6
+ describe "class methods" do
7
+ describe ".mass_insert" do
8
+ before :each do
9
+ MassInsert::ProcessControl.any_instance.stub(:execute => true)
10
+ end
11
+
12
+ it "should respond to mass_insert class method" do
13
+ Test.respond_to?(:mass_insert).should be_true
14
+ end
15
+
16
+ it "should can receive values and many options" do
17
+ values = [{:name => "name"}]
18
+ options = {:option_one => "one", :option_two => "two"}
19
+ Test.mass_insert(values, options).should be_true
20
+ end
21
+
22
+ it "should can receive only values" do
23
+ values = [{:name => "name"}]
24
+ Test.mass_insert(values).should be_true
25
+ end
26
+
27
+ it "should not can called with values" do
28
+ lambda{ Test.mass_insert }.should raise_error
29
+ end
30
+
31
+ it "should call execute ProcessControl method" do
32
+ process = MassInsert::ProcessControl.any_instance
33
+ process.should_receive(:execute).exactly(1).times
34
+ Test.mass_insert([], {})
35
+ end
36
+
37
+ it "should call mass_insert_options method" do
38
+ Test.any_instance.stub(:mass_insert_options)
39
+ Test.should_receive(:mass_insert_options).exactly(1).times
40
+ Test.mass_insert([], {})
41
+ end
42
+ end
43
+
44
+ describe ".mass_insert_options" do
45
+ describe "class_name" do
46
+ it "returns class name that call if that option doesn't exist" do
47
+ options = Test.send(:mass_insert_options)
48
+ options[:class_name].should eq(Test)
49
+ end
50
+
51
+ it "returns class_name option if is in the options" do
52
+ args = {:class_name => "OtherClass"}
53
+ options = Test.send(:mass_insert_options, args)
54
+ options[:class_name].should eq("OtherClass")
55
+ end
56
+ end
57
+
58
+ describe "table_name" do
59
+ it "returns class table_name that call if options doesn't exist" do
60
+ options = Test.send(:mass_insert_options)
61
+ options[:table_name].should eq(Test.table_name)
62
+ end
63
+
64
+ it "returns table_name option if is in the options" do
65
+ args = {:table_name => "OtherTable"}
66
+ options = Test.send(:mass_insert_options, args)
67
+ options[:table_name].should eq("OtherTable")
68
+ end
69
+ end
70
+
71
+ describe "primary_key" do
72
+ it "returns :id if option primary_key doesn't exist" do
73
+ options = Test.send(:mass_insert_options)
74
+ options[:primary_key].should eq(:id)
75
+ end
76
+
77
+ it "returns primary_key option if is in the options" do
78
+ args = {:primary_key => :user_id}
79
+ options = Test.send(:mass_insert_options, args)
80
+ options[:primary_key].should eq(:user_id)
81
+ end
82
+ end
83
+
84
+ describe "primary_key_mode" do
85
+ it "returns :auto if option primary_key_mode doesn't exist" do
86
+ options = Test.send(:mass_insert_options)
87
+ options[:primary_key_mode].should eq(:auto)
88
+ end
89
+
90
+ it "returns primary_key_mode option if is in the options" do
91
+ args = {:primary_key_mode => :manual}
92
+ options = Test.send(:mass_insert_options, args)
93
+ options[:primary_key_mode].should eq(:manual)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end