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,117 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+ require "./spec/dummy_models/test"
4
+
5
+ describe MassInsert::Adapters::Adapter do
6
+ before :each do
7
+ @adapter = MassInsert::Adapters::Adapter.new([], {})
8
+ end
9
+
10
+ subject{ @adapter }
11
+
12
+ describe "instance methods" do
13
+ describe "#initialize" do
14
+
15
+ before :each do
16
+ @values = [{:name => "name"}]
17
+ @options = {:option_one => 10}
18
+ @adapter = MassInsert::Adapters::Adapter.new(@values, @options)
19
+ end
20
+
21
+ it "should initialize the values" do
22
+ @adapter.values.should eq(@values)
23
+ end
24
+
25
+ it "should initialize the options" do
26
+ @adapter.options.should eq(@options)
27
+ end
28
+ end
29
+
30
+ describe "#class_name" do
31
+ it "should respond to class name method" do
32
+ subject.respond_to?(:class_name).should be_true
33
+ end
34
+
35
+ it "should returns the class_name in options" do
36
+ subject.options = {:class_name => Test}
37
+ subject.class_name.should eq(Test)
38
+ end
39
+ end
40
+
41
+ describe "#table_name" do
42
+ it "should respond to table_name method" do
43
+ subject.respond_to?(:table_name).should be_true
44
+ end
45
+
46
+ it "should returns the table_name in options" do
47
+ subject.options = {:table_name => "users"}
48
+ subject.table_name.should eq("users")
49
+ end
50
+ end
51
+
52
+ describe "#table_columns" do
53
+ it "should respond to table_columns method" do
54
+ subject.respond_to?(:table_columns).should be_true
55
+ end
56
+
57
+ it "should returns the table_columns in ActiveRecord class" do
58
+ subject.options = {:class_name => Test}
59
+ columns = [:id, :name, :email]
60
+ subject.table_columns.should eq(columns)
61
+ end
62
+ end
63
+
64
+ describe "#column_names" do
65
+ it "should respond to column_names method" do
66
+ subject.respond_to?(:column_names).should be_true
67
+ end
68
+
69
+ context "when primary_key is auto" do
70
+ it "should return an array without primary key column" do
71
+ subject.options.merge!({
72
+ :class_name => Test,
73
+ :primary_key => :id,
74
+ :primary_key_mode => :auto
75
+ })
76
+ column_names = [:name, :email]
77
+ subject.column_names.should eq(column_names)
78
+ end
79
+ end
80
+
81
+ context "when primary key is manual" do
82
+ it "should return an array with primary key column" do
83
+ subject.options.merge!({
84
+ :class_name => Test,
85
+ :primary_key => :id,
86
+ :primary_key_mode => :manual
87
+ })
88
+ column_names = [:id, :name, :email]
89
+ subject.column_names.should eq(column_names)
90
+ end
91
+ end
92
+ end
93
+
94
+ describe "#primary_key" do
95
+ it "should respond to primary_key method" do
96
+ subject.respond_to?(:primary_key).should be_true
97
+ end
98
+
99
+ it "should returns the primary_key in options" do
100
+ subject.options = {:primary_key => :user_id}
101
+ subject.primary_key.should eq(:user_id)
102
+ end
103
+ end
104
+
105
+ describe "#primary_key_mode" do
106
+ it "should respond to primary_key_mode method" do
107
+ subject.respond_to?(:primary_key_mode).should be_true
108
+ end
109
+
110
+ it "should returns the primary_key_mode in options" do
111
+ subject.options = {:primary_key_mode => :auto}
112
+ subject.primary_key_mode.should eq(:auto)
113
+ end
114
+ end
115
+
116
+ end
117
+ end
@@ -0,0 +1,292 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
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 }
21
+
22
+ describe "#initialize" do
23
+ it "should assign options param to option attribute" do
24
+ subject.options.should eq(@options)
25
+ end
26
+
27
+ it "should assign column param to column attribute" do
28
+ subject.column.should eq(:name)
29
+ end
30
+
31
+ it "should assign row param to row attribute" do
32
+ subject.row.should eq(@row)
33
+ end
34
+ end
35
+
36
+ describe "#class_name" do
37
+ it "should respond to class_name method" do
38
+ subject.respond_to?(:class_name).should be_true
39
+ end
40
+
41
+ it "should return the class_name in options" do
42
+ subject.class_name.should eq(User)
43
+ end
44
+ end
45
+
46
+ describe "#column_type" do
47
+ it "should respond to column_type method" do
48
+ subject.respond_to?(:column_type).should be_true
49
+ end
50
+
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
63
+ end
64
+ end
65
+
66
+ describe "#colum_value" do
67
+ it "should respond to column_value method" do
68
+ subject.respond_to?(:column_value).should be_true
69
+ end
70
+
71
+ it "should return symbol :string" do
72
+ subject.column = :age
73
+ subject.column_value.should eq(10)
74
+ end
75
+ end
76
+
77
+ describe "#adapter" do
78
+ it "should respond to adapter method" do
79
+ subject.respond_to?(:adapter).should be_true
80
+ end
81
+
82
+ it "should return the adapter type" do
83
+ config = {"config" => {:adapter => "sql"}}
84
+ ActiveRecord::Base.connection.stub(:instance_values).and_return(config)
85
+ subject.adapter.should eq("sql")
86
+ end
87
+ end
88
+
89
+ describe "#default_value" do
90
+ it "should respond to default_value method" do
91
+ subject.respond_to?(:default_value).should be_true
92
+ end
93
+
94
+ context "when default_db_value is nil" do
95
+ it "should return 'null' string" do
96
+ subject.stub(:default_db_value).and_return(nil)
97
+ subject.default_value.should eq("null")
98
+ end
99
+ end
100
+
101
+ context "when default_db_value is not nil" do
102
+ it "should return the correct value" do
103
+ subject.stub(:default_db_value).and_return("default_value")
104
+ subject.default_value.should eq("default_value")
105
+ end
106
+ end
107
+ end
108
+
109
+ describe "#default_db_value" do
110
+ it "should respond to default_db_value method" do
111
+ subject.respond_to?(:default_db_value).should be_true
112
+ end
113
+
114
+ it "should return the default database value" do
115
+ subject.column = :name
116
+ subject.default_db_value.should eq(nil)
117
+ end
118
+ end
119
+
120
+ describe "#build" do
121
+ it "should respond to build method" do
122
+ subject.respond_to?(:build).should be_true
123
+ end
124
+
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")
129
+ end
130
+ end
131
+
132
+ [
133
+ :string,
134
+ :text,
135
+ :date,
136
+ :time,
137
+ :datetime,
138
+ :timestamp,
139
+ :binary
140
+ ].each do |column_type|
141
+ method = "column_value_#{column_type}".to_sym
142
+
143
+ describe "#column_value_#{method.to_s}" do
144
+ it "should respond to #{method.to_s} method" do
145
+ subject.respond_to?(method).should be_true
146
+ end
147
+
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
161
+ end
162
+ end
163
+ end
164
+
165
+ describe "#column_value_integer" do
166
+ it "should respond to column_value_integer method" do
167
+ subject.respond_to?(:column_value_integer).should be_true
168
+ end
169
+
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")
175
+ end
176
+ end
177
+
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
191
+ end
192
+ end
193
+ end
194
+
195
+ [:decimal, :float].each do |column_type|
196
+ method = "column_value_#{column_type}".to_sym
197
+
198
+ describe "#column_value_#{method.to_s}" do
199
+ it "should respond to #{method.to_s} method" do
200
+ subject.respond_to?(method).should be_true
201
+ end
202
+
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")
208
+ end
209
+ end
210
+
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
224
+ end
225
+ end
226
+ end
227
+ end
228
+
229
+ describe "#column_value_boolean" do
230
+ it "should respond to column_value_boolean method" do
231
+ subject.respond_to?(:column_value_boolean).should be_true
232
+ end
233
+
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
242
+ end
243
+
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
251
+ end
252
+
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
259
+ end
260
+ end
261
+ end
262
+
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
272
+
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
281
+
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
289
+ end
290
+ end
291
+ end
292
+ end
@@ -0,0 +1,39 @@
1
+ require './spec/spec_helper'
2
+ require "./lib/mass_insert"
3
+
4
+ describe MassInsert::Adapters::Helpers::Sanitizer do
5
+
6
+ before :each do
7
+ @adapter = MassInsert::Adapters::Adapter.new([], {})
8
+ end
9
+
10
+ subject{ @adapter }
11
+
12
+ describe "#sanitized_columns" do
13
+ before :each do
14
+ options = {
15
+ :primary_key => :id,
16
+ :primary_key_mode => :auto,
17
+ }
18
+ subject.options.merge!(options)
19
+ subject.stub(:table_columns).and_return([:id, :name])
20
+ end
21
+
22
+ it "should respond to sanitized_columns" do
23
+ subject.respond_to?(:sanitized_columns).should be_true
24
+ end
25
+
26
+ context "when primary_key_mode is automatic" do
27
+ it "should returns the column without primary_key" do
28
+ subject.sanitized_columns.should eq([:name])
29
+ end
30
+ end
31
+
32
+ context "when primary_key_mode is not automatic" do
33
+ it "should returns the columns including primary_key" do
34
+ subject.options.merge!(:primary_key_mode => :manual)
35
+ subject.sanitized_columns.should eq([:id, :name])
36
+ end
37
+ end
38
+ end
39
+ end