datashift 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,7 +25,6 @@ describe 'Excel Exporter' do
25
25
  # handle migration changes or reset of test DB
26
26
  migrate_up
27
27
 
28
- db_clear() # todo read up about proper transactional fixtures
29
28
  results_clear()
30
29
 
31
30
  @klazz = Project
@@ -36,6 +35,13 @@ describe 'Excel Exporter' do
36
35
  MethodDictionary.clear
37
36
  MethodDictionary.find_operators( @klazz )
38
37
  MethodDictionary.find_operators( @assoc_klazz )
38
+
39
+ db_clear() # todo read up about proper transactional fixtures
40
+
41
+ Project.create( :value_as_string => 'Value as String', :value_as_boolean => true, :value_as_double => 75.672)
42
+ Project.create( :value_as_string => 'Another Value as String', :value_as_boolean => false, :value_as_double => 12)
43
+
44
+
39
45
  end
40
46
 
41
47
  it "should be able to create a new excel exporter" do
@@ -44,7 +50,33 @@ describe 'Excel Exporter' do
44
50
  generator.should_not be_nil
45
51
  end
46
52
 
47
- it "should export a model to .xls file" do
53
+ it "should handle bad params to export" do
54
+
55
+ expect = result_file('project_first_export_spec.csv')
56
+
57
+ exporter = DataShift::ExcelExporter.new( expect )
58
+
59
+ expect{ exporter.export(nil) }.not_to raise_error
60
+
61
+ expect{ exporter.export([]) }.not_to raise_error
62
+
63
+ puts "Can manually check file @ #{expect}"
64
+ end
65
+
66
+ it "should export model object to .xls file" do
67
+
68
+ expect = result_file('project_first_export_spec.xls')
69
+
70
+ gen = ExcelExporter.new( expect )
71
+
72
+ gen.export(Project.all.first)
73
+
74
+ File.exists?(expect).should be_true
75
+
76
+ puts "Can manually check file @ #{expect}"
77
+ end
78
+
79
+ it "should export collection of model objects to .xls file" do
48
80
 
49
81
  expect = result_file('project_export_spec.xls')
50
82
 
@@ -56,7 +88,7 @@ describe 'Excel Exporter' do
56
88
 
57
89
  puts "Can manually check file @ #{expect}"
58
90
  end
59
-
91
+
60
92
  it "should export a model and associations to .xls file" do
61
93
 
62
94
  Project.create( :value_as_string => 'Value as Text', :value_as_boolean => true, :value_as_double => 75.672)
@@ -28,7 +28,7 @@ describe 'Excel Loader' do
28
28
  before(:each) do
29
29
 
30
30
  %w{category_001 category_002 category_003 category_004 category_005}.each do |cat|
31
- Category.find_or_create_by_reference(cat)
31
+ Category.find_or_create_by(reference: cat)
32
32
  end
33
33
 
34
34
 
@@ -136,13 +136,15 @@ describe 'Excel Loader' do
136
136
  it "should provide facility to set default values", :focus => true do
137
137
  loader = ExcelLoader.new(Project)
138
138
 
139
- loader.set_default_value('value_as_string', 'some default text' )
140
- loader.set_default_value('value_as_double', 45.467 )
141
- loader.set_default_value('value_as_boolean', true )
139
+ populator = loader.populator
140
+
141
+ populator.set_default_value('value_as_string', 'some default text' )
142
+ populator.set_default_value('value_as_double', 45.467 )
143
+ populator.set_default_value('value_as_boolean', true )
142
144
 
143
145
  texpected = Time.now.to_s(:db)
144
146
 
145
- loader.set_default_value('value_as_datetime', texpected )
147
+ populator.set_default_value('value_as_datetime', texpected )
146
148
 
147
149
  #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
148
150
 
@@ -171,8 +173,8 @@ describe 'Excel Loader' do
171
173
  it "should provide facility to set pre and post fix values" do
172
174
  loader = ExcelLoader.new(Project)
173
175
 
174
- loader.set_prefix('value_as_string', 'myprefix' )
175
- loader.set_postfix('value_as_string', 'my post fix' )
176
+ loader.populator.set_prefix('value_as_string', 'myprefix' )
177
+ loader.populator.set_postfix('value_as_string', 'my post fix' )
176
178
 
177
179
  #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
178
180
 
data/spec/excel_spec.rb CHANGED
@@ -39,13 +39,34 @@ describe 'Excel Proxy' do
39
39
  sheet2 = @excel.create_worksheet( :name => "underworld")
40
40
  sheet2.name.should == "underworld"
41
41
  end
42
+
43
+ it "can sanitize worksheet names as per Excel spec" do
44
+
45
+ include ExcelBase
46
+ extend ExcelBase
47
+
48
+ # name.gsub(/[\[\]:\*\/\\\?]/, '
49
+
50
+ sanitize_sheet_name("aute?chre").should == "autechre"
51
+ sanitize_sheet_name("?autechre").should == "autechre"
52
+ sanitize_sheet_name("aute?chre?").should == "autechre"
53
+
54
+ sanitize_sheet_name("daft: ?punk").should == "daft punk"
55
+ sanitize_sheet_name("guy call[]ed *Gerald").should == "guy called Gerald"
56
+
57
+ sanitize_sheet_name("guy called */Gerald").should == "guy called Gerald"
58
+ end
42
59
 
43
- it "ensures name of worksheet sanitized" do
44
- sheet = @excel.create_worksheet( :name => "daft: ?punk")
45
- sheet.name.should == "daft punk"
60
+ # Pending - inject create_worksheet method into Spreadsheet gem
61
+ if(DataShift::Guards.jruby?)
62
+ it "ensures name of worksheet sanitized" do
46
63
 
47
- sheet = @excel.create_worksheet( :name => "under[]world")
48
- sheet.name.should == "underworld"
64
+ sheet = @excel.create_worksheet( :name => "daft: ?punk")
65
+ sheet.name.should == "daft punk"
66
+
67
+ sheet = @excel.create_worksheet( :name => "under[]world")
68
+ sheet.name.should == "underworld"
69
+ end
49
70
  end
50
71
 
51
72
  it "can create multiple named worksheets" do
@@ -9,7 +9,7 @@ require File.dirname(__FILE__) + '/spec_helper'
9
9
 
10
10
  require 'erb'
11
11
 
12
- describe 'Basic Loader' do
12
+ describe 'LoaderBase' do
13
13
 
14
14
  include_context "ActiveRecordTestModelsConnected"
15
15
 
@@ -111,4 +111,16 @@ describe 'Basic Loader' do
111
111
 
112
112
  end
113
113
 
114
+ it "should be able to mark a load attempt as a failure" do
115
+
116
+ failed_count = @loader.failed_count
117
+ @loader.load_object.new_record?.should be_true
118
+
119
+ @loader.load_object.save!
120
+
121
+ @loader.failure
122
+
123
+ @loader.failed_count.should == failed_count + 1
124
+ end
125
+
114
126
  end
@@ -13,72 +13,69 @@ require 'method_dictionary'
13
13
 
14
14
  describe 'Method Dictionary' do
15
15
 
16
-
17
16
  include_context "ActiveRecordTestModelsConnected"
18
17
 
19
- include DataShift
20
-
21
18
  before(:each) do
22
- MethodDictionary.clear
19
+ DataShift::MethodDictionary.clear
23
20
  end
24
21
 
25
22
  it "should store dictionary for multiple AR models" do
26
23
 
27
- MethodDictionary.find_operators( Project )
28
- MethodDictionary.find_operators( Milestone )
24
+ DataShift::MethodDictionary.find_operators( Project )
25
+ DataShift::MethodDictionary.find_operators( Milestone )
29
26
 
30
- MethodDictionary.assignments.size.should == 2
31
- MethodDictionary.has_many.size.should == 2
27
+ DataShift::MethodDictionary.assignments.size.should == 2
28
+ DataShift::MethodDictionary.has_many.size.should == 2
32
29
  end
33
30
 
34
31
  it "should populate method dictionary for a given AR model" do
35
32
 
36
- MethodDictionary.find_operators( Project )
37
- MethodDictionary.find_operators( Milestone )
33
+ DataShift::MethodDictionary.find_operators( Project )
34
+ DataShift::MethodDictionary.find_operators( Milestone )
38
35
 
39
- MethodDictionary.has_many.should_not be_empty
40
- MethodDictionary.has_many[Project].should include('milestones')
36
+ DataShift::MethodDictionary.has_many.should_not be_empty
37
+ DataShift::MethodDictionary.has_many[Project].should include('milestones')
41
38
 
42
- MethodDictionary.assignments.should_not be_empty
43
- MethodDictionary.assignments[Project].should include('id')
44
- MethodDictionary.assignments[Project].should include('value_as_string')
45
- MethodDictionary.assignments[Project].should include('value_as_text')
39
+ DataShift::MethodDictionary.assignments.should_not be_empty
40
+ DataShift::MethodDictionary.assignments[Project].should include('id')
41
+ DataShift::MethodDictionary.assignments[Project].should include('value_as_string')
42
+ DataShift::MethodDictionary.assignments[Project].should include('value_as_text')
46
43
 
47
- MethodDictionary.belongs_to.should_not be_empty
48
- MethodDictionary.belongs_to[Project].should be_empty
44
+ DataShift::MethodDictionary.belongs_to.should_not be_empty
45
+ DataShift::MethodDictionary.belongs_to[Project].should be_empty
49
46
 
50
47
 
51
- MethodDictionary.column_types.should be_is_a(Hash)
52
- MethodDictionary.column_types.should_not be_empty
53
- MethodDictionary.column_types[Project].size.should == Project.columns.size
48
+ DataShift::MethodDictionary.column_types.should be_is_a(Hash)
49
+ DataShift::MethodDictionary.column_types.should_not be_empty
50
+ DataShift::MethodDictionary.column_types[Project].size.should == Project.columns.size
54
51
 
55
52
 
56
53
  end
57
54
 
58
55
  it "should populate assigment members without the equivalent association names" do
59
56
 
60
- MethodDictionary.find_operators( Project )
57
+ DataShift::MethodDictionary.find_operators( Project )
61
58
 
62
59
  # we should remove has-many & belongs_to from basic assignment set as they require a DB lookup
63
60
  # or a Model.create call, not a simple assignment
64
61
 
65
- MethodDictionary.assignments_for(Project).should_not include( MethodDictionary.belongs_to_for(Project) )
66
- MethodDictionary.assignments_for(Project).should_not include( MethodDictionary.has_many_for(Project) )
62
+ DataShift::MethodDictionary.assignments_for(Project).should_not include( DataShift::MethodDictionary.belongs_to_for(Project) )
63
+ DataShift::MethodDictionary.assignments_for(Project).should_not include( DataShift::MethodDictionary.has_many_for(Project) )
67
64
  end
68
65
 
69
66
 
70
67
  it "should populate assignment operators for method details for different forms of a column name" do
71
68
 
72
- MethodDictionary.find_operators( Project )
73
- MethodDictionary.find_operators( Milestone )
69
+ DataShift::MethodDictionary.find_operators( Project )
70
+ DataShift::MethodDictionary.find_operators( Milestone )
74
71
 
75
- MethodDictionary.build_method_details( Project )
72
+ DataShift::MethodDictionary.build_method_details( Project )
76
73
 
77
74
  [:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
78
75
 
79
- method_details = MethodDictionary.find_method_detail( Project, format )
76
+ method_details = DataShift::MethodDictionary.find_method_detail( Project, format )
80
77
 
81
- method_details.class.should == MethodDetail
78
+ method_details.class.should == DataShift::MethodDetail
82
79
 
83
80
  method_details.operator.should == 'value_as_string'
84
81
  method_details.operator_for(:assignment).should == 'value_as_string'
@@ -97,15 +94,15 @@ describe 'Method Dictionary' do
97
94
 
98
95
  it "should populate column types for assignment operators in method details" do
99
96
 
100
- MethodDictionary.find_operators( Project )
97
+ DataShift::MethodDictionary.find_operators( Project )
101
98
 
102
- MethodDictionary.build_method_details( Project )
99
+ DataShift::MethodDictionary.build_method_details( Project )
103
100
 
104
101
  [:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
105
102
 
106
- method_details = MethodDictionary.find_method_detail( Project, format )
103
+ method_details = DataShift::MethodDictionary.find_method_detail( Project, format )
107
104
 
108
- method_details.class.should == MethodDetail
105
+ method_details.class.should == DataShift::MethodDetail
109
106
 
110
107
  method_details.col_type.should_not be_nil
111
108
  method_details.col_type.name.should == 'value_as_string'
@@ -117,13 +114,13 @@ describe 'Method Dictionary' do
117
114
 
118
115
  it "should populate required Class for assignment operators based on column type" do
119
116
 
120
- MethodDictionary.find_operators( Project )
117
+ DataShift::MethodDictionary.find_operators( Project )
121
118
 
122
- MethodDictionary.build_method_details( Project )
119
+ DataShift::MethodDictionary.build_method_details( Project )
123
120
 
124
121
  [:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
125
122
 
126
- method_details = MethodDictionary.find_method_detail( Project, format )
123
+ method_details = DataShift::MethodDictionary.find_method_detail( Project, format )
127
124
 
128
125
  method_details.operator_class_name.should == 'String'
129
126
  method_details.operator_class.should be_is_a(Class)
@@ -134,16 +131,16 @@ describe 'Method Dictionary' do
134
131
 
135
132
  it "should populate belongs_to operator for method details for different forms of a column name" do
136
133
 
137
- MethodDictionary.find_operators( Project )
138
- MethodDictionary.find_operators( Milestone )
134
+ DataShift::MethodDictionary.find_operators( Project )
135
+ DataShift::MethodDictionary.find_operators( Milestone )
139
136
 
140
- MethodDictionary.build_method_details( Project )
141
- MethodDictionary.build_method_details( Milestone )
137
+ DataShift::MethodDictionary.build_method_details( Project )
138
+ DataShift::MethodDictionary.build_method_details( Milestone )
142
139
 
143
140
  # milestone.project = project.id
144
141
  [:project, 'project', "PROJECT", "prOJECt"].each do |format|
145
142
 
146
- method_details = MethodDictionary.find_method_detail( Milestone, format )
143
+ method_details = DataShift::MethodDictionary.find_method_detail( Milestone, format )
147
144
 
148
145
  method_details.should_not be_nil
149
146
 
@@ -158,17 +155,17 @@ describe 'Method Dictionary' do
158
155
 
159
156
  it "should populate required Class for belongs_to operator method details" do
160
157
 
161
- MethodDictionary.find_operators( LoaderRelease )
162
- MethodDictionary.find_operators( LongAndComplexTableLinkedToVersion )
158
+ DataShift::MethodDictionary.find_operators( LoaderRelease )
159
+ DataShift::MethodDictionary.find_operators( LongAndComplexTableLinkedToVersion )
163
160
 
164
- MethodDictionary.build_method_details( LoaderRelease )
165
- MethodDictionary.build_method_details( LongAndComplexTableLinkedToVersion )
161
+ DataShift::MethodDictionary.build_method_details( LoaderRelease )
162
+ DataShift::MethodDictionary.build_method_details( LongAndComplexTableLinkedToVersion )
166
163
 
167
164
 
168
165
  # release.project = project.id
169
166
  [:project, 'project', "PROJECT", "prOJECt"].each do |format|
170
167
 
171
- method_details = MethodDictionary.find_method_detail( LoaderRelease, format )
168
+ method_details = DataShift::MethodDictionary.find_method_detail( LoaderRelease, format )
172
169
 
173
170
  method_details.operator_class_name.should == 'Project'
174
171
  method_details.operator_class.should == Project
@@ -176,7 +173,7 @@ describe 'Method Dictionary' do
176
173
 
177
174
 
178
175
  [:version, "Version", "verSION"].each do |format|
179
- method_details = MethodDictionary.find_method_detail( LongAndComplexTableLinkedToVersion, format )
176
+ method_details = DataShift::MethodDictionary.find_method_detail( LongAndComplexTableLinkedToVersion, format )
180
177
 
181
178
  method_details.operator_type.should == :belongs_to
182
179
 
@@ -187,13 +184,13 @@ describe 'Method Dictionary' do
187
184
 
188
185
  it "should populate required Class for has_one operator method details" do
189
186
 
190
- MethodDictionary.find_operators( Version )
191
- MethodDictionary.build_method_details( Version )
187
+ DataShift::MethodDictionary.find_operators( Version )
188
+ DataShift::MethodDictionary.build_method_details( Version )
192
189
 
193
190
  # version.long_and_complex_table_linked_to_version = LongAndComplexTableLinkedToVersion.create()
194
191
 
195
192
  [:long_and_complex_table_linked_to_version, 'LongAndComplexTableLinkedToVersion', "Long And Complex_Table_Linked To Version", "Long_And_Complex_Table_Linked_To_Version"].each do |format|
196
- method_details = MethodDictionary.find_method_detail( Version, format )
193
+ method_details = DataShift::MethodDictionary.find_method_detail( Version, format )
197
194
 
198
195
  method_details.should_not be_nil
199
196
 
@@ -207,29 +204,29 @@ describe 'Method Dictionary' do
207
204
  end
208
205
 
209
206
  it "should return false on for?(klass) if find_operators hasn't been called for klass" do
210
- MethodDictionary.clear
211
- MethodDictionary::for?(Project).should == false
212
- MethodDictionary::find_operators(Project)
213
- MethodDictionary::for?(Project).should == true
207
+ DataShift::MethodDictionary.clear
208
+ DataShift::MethodDictionary::for?(Project).should == false
209
+ DataShift::MethodDictionary::find_operators(Project)
210
+ DataShift::MethodDictionary::for?(Project).should == true
214
211
 
215
212
  end
216
213
 
217
214
  it "should return false on for?(klass) if find_operators hasn't been called for klass" do
218
- MethodDictionary.clear
219
- MethodDictionary::for?(Project).should == false
215
+ DataShift::MethodDictionary.clear
216
+ DataShift::MethodDictionary::for?(Project).should == false
220
217
  end
221
218
 
222
219
  it "should find has_many operator for method details" do
223
220
 
224
- MethodDictionary.find_operators( Project )
221
+ DataShift::MethodDictionary.find_operators( Project )
225
222
 
226
- MethodDictionary.build_method_details( Project )
223
+ DataShift::MethodDictionary.build_method_details( Project )
227
224
 
228
225
  [:milestones, "Mile Stones", 'mileSTONES', 'MileStones'].each do |format|
229
226
 
230
- method_details = MethodDictionary.find_method_detail( Project, format )
227
+ method_details = DataShift::MethodDictionary.find_method_detail( Project, format )
231
228
 
232
- method_details.class.should == MethodDetail
229
+ method_details.class.should == DataShift::MethodDetail
233
230
 
234
231
  result = 'milestones'
235
232
  method_details.operator.should == result
@@ -244,10 +241,10 @@ describe 'Method Dictionary' do
244
241
 
245
242
  it "should return nil when non existent column name" do
246
243
 
247
- MethodDictionary.find_operators( Project )
244
+ DataShift::MethodDictionary.find_operators( Project )
248
245
 
249
246
  ["On sale", 'on_sale'].each do |format|
250
- detail = MethodDictionary.find_method_detail( Project, format )
247
+ detail = DataShift::MethodDictionary.find_method_detail( Project, format )
251
248
 
252
249
  detail.should be_nil
253
250
  end
@@ -259,22 +256,32 @@ describe 'Method Dictionary' do
259
256
  end
260
257
 
261
258
  it "should not by default map setter methods" do
262
- MethodDictionary.find_operators( Milestone )
259
+ DataShift::MethodDictionary.find_operators( Milestone )
260
+
261
+ DataShift::MethodDictionary.assignments[Milestone].should_not include('title')
262
+ end
263
+
264
+ it "should support inclusion of setter methods" do
265
+
266
+ Milestone.new.respond_to?('title=').should == true
267
+ Milestone.new.respond_to?('milestone_setter=').should == true
263
268
 
264
- MethodDictionary.assignments[Milestone].should_not include('title')
269
+ DataShift::MethodDictionary.setters( Milestone ).should include('title=') # delegate
270
+ DataShift::MethodDictionary.setters( Milestone ).should include('milestone_setter=') # normal method
265
271
  end
266
272
 
267
273
  it "should support reload and inclusion of setter methods" do
268
274
 
269
- MethodDictionary.find_operators( Project )
270
- MethodDictionary.find_operators( Milestone )
275
+ DataShift::MethodDictionary.find_operators( Project )
276
+ DataShift::MethodDictionary.find_operators( Milestone )
271
277
 
272
- MethodDictionary.assignments[Milestone].should_not include('title')
273
-
274
- MethodDictionary.find_operators( Milestone, :reload => true, :instance_methods => true )
278
+ DataShift::MethodDictionary.assignments[Milestone].should_not include('title')
279
+
280
+ DataShift::MethodDictionary.find_operators( Milestone, :reload => true, :instance_methods => true )
275
281
 
276
282
  # Milestone delegates :title to Project
277
- MethodDictionary.assignments[Milestone].should include('title')
283
+ DataShift::MethodDictionary.assignments[Milestone].should include('title')
284
+ DataShift::MethodDictionary.assignments[Milestone].should include('milestone_setter')
278
285
  end
279
286
 
280
287
  end