mass_insert 0.0.1

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 (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,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
File without changes
@@ -0,0 +1,60 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe "Binary" do
5
+
6
+ before :each do
7
+ @values, @options = [{:checked => 1}], {}
8
+ end
9
+
10
+ context "when exist in values hashes" do
11
+ context "when contains binary value" do
12
+ context "when is 1" do
13
+ it "should be saved correctly" do
14
+ User.mass_insert(@values, @options)
15
+ User.last.checked.should eq("1")
16
+ end
17
+ end
18
+
19
+ context "when is 0" do
20
+ it "should be saved correctly" do
21
+ @values.first.merge!(:checked => 0)
22
+ User.mass_insert(@values, @options)
23
+ User.last.checked.should eq("0")
24
+ end
25
+ end
26
+ end
27
+
28
+ context "when contains a string" do
29
+ it "should convert string value to binary" do
30
+ @values.first.merge!(:checked => "string")
31
+ User.mass_insert(@values, @options)
32
+ User.last.checked.should eq("string")
33
+ end
34
+ end
35
+
36
+ context "when contains a decimal" do
37
+ it "should convert decimal value to binary" do
38
+ @values.first.merge!(:checked => 25.34)
39
+ User.mass_insert(@values, @options)
40
+ User.last.checked.should eq("25.34")
41
+ end
42
+ end
43
+
44
+ context "when contains a boolean" do
45
+ it "should convert boolean value to binary" do
46
+ @values.first.merge!(:checked => true)
47
+ User.mass_insert(@values, @options)
48
+ User.last.checked.should eq("true")
49
+ end
50
+ end
51
+ end
52
+
53
+ context "when not exist in values hashes" do
54
+ it "should save the default value" do
55
+ @values.first.delete(:checked)
56
+ User.mass_insert(@values, @options)
57
+ User.last.checked.should eq(nil)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,52 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe "Boolean" do
5
+
6
+ before :each do
7
+ @values, @options = [{:active => true}], {}
8
+ end
9
+
10
+ context "when exist in values hashes" do
11
+ context "when contains boolean value" do
12
+ context "when is true" do
13
+ it "should be saved correctly" do
14
+ User.mass_insert(@values, @options)
15
+ User.last.active.should eq(true)
16
+ end
17
+ end
18
+
19
+ context "when is false" do
20
+ it "should be saved correctly" do
21
+ @values.first.merge!(:active => false)
22
+ User.mass_insert(@values, @options)
23
+ User.last.active.should eq(false)
24
+ end
25
+ end
26
+ end
27
+
28
+ context "when contains a string" do
29
+ it "should convert string value to boolean" do
30
+ @values.first.merge!(:active => "string")
31
+ User.mass_insert(@values, @options)
32
+ User.last.active.should eq(true)
33
+ end
34
+ end
35
+
36
+ context "when contains a decimal" do
37
+ it "should convert decimal value to boolean" do
38
+ @values.first.merge!(:active => 25.34)
39
+ User.mass_insert(@values, @options)
40
+ User.last.active.should eq(true)
41
+ end
42
+ end
43
+ end
44
+
45
+ context "when not exist in values hashes" do
46
+ it "should save the default value" do
47
+ @values.first.delete(:active)
48
+ User.mass_insert(@values, @options)
49
+ User.last.active.should eq(nil)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,49 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe "Decimal" do
5
+
6
+ before :each do
7
+ @values, @options = [{:money => 20.50}], {}
8
+ end
9
+
10
+ context "when exist in values hashes" do
11
+ context "when contains an integer" do
12
+ it "should convert integer value to decimal" do
13
+ @values.first.merge!(:money => 10)
14
+ User.mass_insert(@values, @options)
15
+ User.last.money.should eq(10.0)
16
+ end
17
+ end
18
+
19
+ context "when contains a string" do
20
+ it "should convert string value to decimal" do
21
+ @values.first.merge!(:money => "string")
22
+ User.mass_insert(@values, @options)
23
+ User.last.money.should eq(0.0)
24
+ end
25
+ end
26
+
27
+ context "when contains a decimal" do
28
+ it "should save the correct value" do
29
+ User.mass_insert(@values, @options)
30
+ User.last.money.should eq(20.50)
31
+ end
32
+ end
33
+
34
+ context "when contains a boolean" do
35
+ it "should raise an exception" do
36
+ @values.first.merge!(:money => true)
37
+ lambda{ User.mass_insert(@values, @options) }.should raise_exception
38
+ end
39
+ end
40
+ end
41
+
42
+ context "when not exist in values hashes" do
43
+ it "should save the default value" do
44
+ @values.first.delete(:money)
45
+ User.mass_insert(@values, @options)
46
+ User.last.money.should eq(nil)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe "Integer" do
5
+
6
+ before :each do
7
+ @values, @options = [{:age => 20}], {}
8
+ end
9
+
10
+ context "when exist in values hashes" do
11
+ context "when contains an integer" do
12
+ it "should be saved correctly" do
13
+ User.mass_insert(@values, @options)
14
+ User.last.age.should eq(20)
15
+ end
16
+ end
17
+
18
+ context "when contains a string" do
19
+ it "should convert string value to integer" do
20
+ @values.first.merge!(:age => "string")
21
+ User.mass_insert(@values, @options)
22
+ User.last.age.should eq(0)
23
+ end
24
+ end
25
+
26
+ context "when contains a decimal" do
27
+ it "should convert decimal value to integer" do
28
+ @values.first.merge!(:age => 25.69)
29
+ User.mass_insert(@values, @options)
30
+ User.last.age.should eq(25)
31
+ end
32
+ end
33
+
34
+ context "when contains a boolean" do
35
+ it "should raise an exception" do
36
+ @values.first.merge!(:age => true)
37
+ lambda{ User.mass_insert(@values, @options) }.should raise_exception
38
+ end
39
+ end
40
+ end
41
+
42
+ context "when not exist in values hashes" do
43
+ it "should save the default value" do
44
+ @values.first.delete(:age)
45
+ User.mass_insert(@values, @options)
46
+ User.last.age.should eq(nil)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,50 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe "String" do
5
+
6
+ before :each do
7
+ @values, @options = [{:name => "some name"}], {}
8
+ end
9
+
10
+ context "when exist in values hashes" do
11
+ context "when contains a string" do
12
+ it "should be saved correctly" do
13
+ User.mass_insert(@values, @options)
14
+ User.last.name.should eq("some name")
15
+ end
16
+ end
17
+
18
+ context "when contains a integer" do
19
+ it "should convert integer value to string" do
20
+ @values.first.merge!(:name => 10)
21
+ User.mass_insert(@values, @options)
22
+ User.last.name.should eq("10")
23
+ end
24
+ end
25
+
26
+ context "when contains a decimal" do
27
+ it "should convert decimal value to string" do
28
+ @values.first.merge!(:name => 25.69)
29
+ User.mass_insert(@values, @options)
30
+ User.last.name.should eq("25.69")
31
+ end
32
+ end
33
+
34
+ context "when contains a boolean" do
35
+ it "should convert boolean value to string" do
36
+ @values.first.merge!(:name => true)
37
+ User.mass_insert(@values, @options)
38
+ User.last.name.should eq("true")
39
+ end
40
+ end
41
+ end
42
+
43
+ context "when not exist in values hashes" do
44
+ it "should save the default value" do
45
+ @values.first.delete(:name)
46
+ User.mass_insert(@values, @options)
47
+ User.last.name.should eq(nil)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,50 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+ require "./spec/dummy_models/test"
4
+
5
+ describe "Model" do
6
+
7
+ before :each do
8
+ @values, @options = [], {}
9
+ @value_hash = {
10
+ :name => "some_name",
11
+ :email => "some_email",
12
+ :age => 20,
13
+ :active => true,
14
+ :checked => true
15
+ }
16
+ User.delete_all
17
+ end
18
+
19
+ context "when is used without options" do
20
+ it "should save 5 record into users table" do
21
+ 5.times{ @values << @value_hash }
22
+ User.mass_insert(@values, @options)
23
+ User.count.should eq(5)
24
+ end
25
+
26
+ it "should save if values cointains 1500 records" do
27
+ 1500.times{ @values << @value_hash }
28
+ User.mass_insert(@values, @options)
29
+ User.count.should eq(1500)
30
+ end
31
+ end
32
+
33
+ context "when is used with options" do
34
+ context "when the table name doesn't exit" do
35
+ it "should not save any record" do
36
+ 5.times{ @values << @value_hash }
37
+ @options.merge!(:table_name => "countries")
38
+ lambda{ User.mass_insert(@values, @options) }.should raise_exception
39
+ end
40
+ end
41
+
42
+ context "when the class name not inherit from ActiveRecord" do
43
+ it "should not save any record" do
44
+ 5.times{ @values << @value_hash }
45
+ @options.merge!(:class_name => Test)
46
+ lambda{ User.mass_insert(@values, @options) }.should raise_exception
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ class Test < ActiveRecord::Base
2
+ def self.column_names
3
+ ["id", "name", "email"]
4
+ end
5
+ end
@@ -0,0 +1,109 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe MassInsert::Adapters::AbstractQuery do
5
+ before :each do
6
+ @adapter = MassInsert::Adapters::Adapter.new([], {})
7
+ end
8
+
9
+ subject{ @adapter }
10
+
11
+ describe "#begin_string" do
12
+ it "should respond to begin_string method" do
13
+ subject.respond_to?(:begin_string).should be_true
14
+ end
15
+
16
+ it "should returns the correct string" do
17
+ subject.stub(:table_name).and_return("users")
18
+ string = "INSERT INTO users "
19
+ subject.begin_string.should eq(string)
20
+ end
21
+ end
22
+
23
+ describe "#string_columns" do
24
+ it "should respond to string_columns method" do
25
+ subject.respond_to?(:string_columns).should be_true
26
+ end
27
+
28
+ it "should returns correct string to columns" do
29
+ subject.stub(:column_names).and_return([:name, :email])
30
+ subject.string_columns.should eq("(name, email) ")
31
+ end
32
+ end
33
+
34
+ describe "#string_values" do
35
+ it "should respond to string_values method" do
36
+ subject.respond_to?(:string_values).should be_true
37
+ end
38
+
39
+ 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);")
42
+ end
43
+ end
44
+
45
+ describe "#string_rows_values" do
46
+ it "should respond to string_rows_values method" do
47
+ subject.respond_to?(:string_rows_values).should be_true
48
+ end
49
+
50
+ context "when only have one value hash" do
51
+ it "should returns the correct string" do
52
+ subject.stub(:string_single_row_values).and_return("single_row")
53
+ subject.values = [{}]
54
+ subject.string_rows_values.should eq("single_row")
55
+ end
56
+ end
57
+
58
+ context "when have two or more value hashes" do
59
+ it "should returns the correct string" do
60
+ subject.stub(:string_single_row_values).and_return("single_row")
61
+ subject.values = [{}, {}]
62
+ subject.string_rows_values.should eq("single_row), (single_row")
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "#string_single_row_values" do
68
+ it "should respond to string_single_row_values method" do
69
+ subject.respond_to?(:string_single_row_values).should be_true
70
+ end
71
+
72
+ it "should returns the correct string" do
73
+ subject.stub(:string_single_value).and_return("single_value")
74
+ subject.stub(:column_names).and_return([:name, :email])
75
+ subject.string_single_row_values({}).should eq("single_value, single_value")
76
+ end
77
+
78
+ context "when respond to timestamp attributes" do
79
+ it "should call timestamp_values method" do
80
+ subject.stub(:string_single_value).and_return("single_value")
81
+ subject.stub(:column_names).and_return([:created_at, :updated_at])
82
+ subject.stub(:timestamp_values).and_return(:test => "test")
83
+ subject.should_receive(:timestamp_values).exactly(1).times
84
+ subject.string_single_row_values({})
85
+ end
86
+ end
87
+
88
+ context "when not respond to timestamp attributes" do
89
+ it "should returns the correct string" do
90
+ subject.stub(:string_single_value).and_return("single_value")
91
+ subject.stub(:column_names).and_return([:name, :email])
92
+ subject.should_receive(:timestamp_values).exactly(0).times
93
+ subject.string_single_row_values({})
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "#string_single_value" do
99
+ it "should respond to string_single_value method" do
100
+ subject.respond_to?(:string_single_value).should be_true
101
+ end
102
+
103
+ it "should call build method in ColumnValue class" do
104
+ 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")
107
+ end
108
+ end
109
+ end