datashift 0.15.0 → 0.16.0

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 (58) hide show
  1. checksums.yaml +7 -0
  2. data/README.markdown +91 -55
  3. data/VERSION +1 -1
  4. data/datashift.gemspec +8 -23
  5. data/lib/applications/jexcel_file.rb +1 -2
  6. data/lib/datashift.rb +34 -15
  7. data/lib/datashift/column_packer.rb +98 -34
  8. data/lib/datashift/data_transforms.rb +83 -0
  9. data/lib/datashift/delimiters.rb +58 -10
  10. data/lib/datashift/excel_base.rb +123 -0
  11. data/lib/datashift/exceptions.rb +45 -7
  12. data/lib/datashift/load_object.rb +25 -0
  13. data/lib/datashift/mapping_service.rb +91 -0
  14. data/lib/datashift/method_detail.rb +40 -62
  15. data/lib/datashift/method_details_manager.rb +18 -2
  16. data/lib/datashift/method_dictionary.rb +27 -10
  17. data/lib/datashift/method_mapper.rb +49 -41
  18. data/lib/datashift/model_mapper.rb +42 -22
  19. data/lib/datashift/populator.rb +258 -143
  20. data/lib/datashift/thor_base.rb +38 -0
  21. data/lib/exporters/csv_exporter.rb +57 -145
  22. data/lib/exporters/excel_exporter.rb +73 -60
  23. data/lib/generators/csv_generator.rb +65 -5
  24. data/lib/generators/generator_base.rb +69 -3
  25. data/lib/generators/mapping_generator.rb +112 -0
  26. data/lib/helpers/core_ext/csv_file.rb +33 -0
  27. data/lib/loaders/csv_loader.rb +41 -39
  28. data/lib/loaders/excel_loader.rb +130 -116
  29. data/lib/loaders/loader_base.rb +190 -146
  30. data/lib/loaders/paperclip/attachment_loader.rb +4 -4
  31. data/lib/loaders/paperclip/datashift_paperclip.rb +5 -3
  32. data/lib/loaders/paperclip/image_loading.rb +9 -7
  33. data/lib/loaders/reporter.rb +17 -8
  34. data/lib/thor/export.thor +12 -13
  35. data/lib/thor/generate.thor +1 -9
  36. data/lib/thor/import.thor +13 -24
  37. data/lib/thor/mapping.thor +65 -0
  38. data/spec/Gemfile +13 -11
  39. data/spec/Gemfile.lock +98 -93
  40. data/spec/csv_exporter_spec.rb +104 -99
  41. data/spec/csv_generator_spec.rb +159 -0
  42. data/spec/csv_loader_spec.rb +197 -16
  43. data/spec/datashift_spec.rb +9 -0
  44. data/spec/excel_exporter_spec.rb +149 -58
  45. data/spec/excel_generator_spec.rb +35 -44
  46. data/spec/excel_loader_spec.rb +196 -178
  47. data/spec/excel_spec.rb +8 -5
  48. data/spec/loader_base_spec.rb +47 -7
  49. data/spec/mapping_spec.rb +117 -0
  50. data/spec/method_dictionary_spec.rb +24 -11
  51. data/spec/method_mapper_spec.rb +5 -7
  52. data/spec/model_mapper_spec.rb +41 -0
  53. data/spec/paperclip_loader_spec.rb +3 -6
  54. data/spec/populator_spec.rb +48 -14
  55. data/spec/spec_helper.rb +85 -73
  56. data/spec/thor_spec.rb +40 -5
  57. metadata +93 -86
  58. data/lib/applications/excel_base.rb +0 -63
@@ -42,5 +42,14 @@ describe 'DataShift' do
42
42
 
43
43
 
44
44
  end
45
+
46
+ it "should provide quick way to create exception class" do
47
+ DataShift::DataShiftException.generate( "BadRspecError")
48
+
49
+ e = DataShift::BadRspecError.new("my new exception class")
50
+
51
+ expect(e).to be
52
+ expect(e.message).to eq "my new exception class"
53
+ end
45
54
 
46
55
  end
@@ -7,7 +7,7 @@
7
7
  #
8
8
  require File.dirname(__FILE__) + '/spec_helper'
9
9
 
10
-
10
+
11
11
  require 'erb'
12
12
  require 'excel_exporter'
13
13
 
@@ -16,92 +16,183 @@ include DataShift
16
16
  describe 'Excel Exporter' do
17
17
 
18
18
  before(:all) do
19
-
20
- # load our test model definitions - Project etc
21
- require ifixture_file('test_model_defs')
22
-
23
- db_connect( 'test_file' ) # , test_memory, test_mysql
24
-
25
- # handle migration changes or reset of test DB
26
- migrate_up
27
19
 
28
- results_clear()
20
+ results_clear( "exp_*.xls" )
29
21
 
30
22
  @klazz = Project
31
23
  @assoc_klazz = Category
32
24
  end
33
-
25
+
34
26
  before(:each) do
35
27
  MethodDictionary.clear
36
28
  MethodDictionary.find_operators( @klazz )
37
29
  MethodDictionary.find_operators( @assoc_klazz )
38
-
30
+
39
31
  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
-
45
- end
46
-
47
- it "should be able to create a new excel exporter" do
48
- generator = ExcelExporter.new( 'dummy.xls' )
49
-
50
- generator.should_not be_nil
32
+
51
33
  end
52
-
53
- it "should handle bad params to export" do
54
34
 
55
- expect = result_file('project_first_export_spec.csv')
35
+ context 'simple project' do
56
36
 
57
- exporter = DataShift::ExcelExporter.new( expect )
58
-
59
- expect{ exporter.export(nil) }.not_to raise_error
37
+ before(:each) do
38
+ create( :project )
39
+ end
40
+
41
+ it "should be able to create a new excel exporter" do
42
+ generator = ExcelExporter.new( 'exp_dummy.xls' )
43
+
44
+ generator.should_not be_nil
45
+ end
46
+
47
+ it "should handle bad params to export" do
48
+
49
+ expect = result_file('project_first_export_spec.csv')
50
+
51
+ exporter = DataShift::ExcelExporter.new( expect )
52
+
53
+ expect{ exporter.export(nil) }.not_to raise_error
54
+
55
+ expect{ exporter.export([]) }.not_to raise_error
56
+
57
+ puts "Can manually check file @ #{expect}"
58
+ end
59
+
60
+ it "should export model object to .xls file" do
61
+
62
+ expected = result_file('exp_project_first_export_spec.xls')
63
+
64
+ gen = ExcelExporter.new( expected )
65
+
66
+ gen.export(Project.all.first)
67
+
68
+ expect(File.exists?(expected)).to eq true
69
+
70
+ puts "Can manually check file @ #{expected}"
71
+ end
60
72
 
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
73
  end
78
74
 
79
75
  it "should export collection of model objects to .xls file" do
80
76
 
81
- expect = result_file('project_export_spec.xls')
77
+ create_list(:project, 7)
78
+
79
+ expected = result_file('exp_project_export_spec.xls')
80
+
81
+ gen = ExcelExporter.new( expected )
82
82
 
83
- gen = ExcelExporter.new( expect )
84
-
85
83
  gen.export(Project.all)
86
-
87
- File.exists?(expect).should be_true
88
-
89
- puts "Can manually check file @ #{expect}"
84
+
85
+ expect( File.exists?(expected)).to eq true
86
+
87
+ excel = Excel.new
88
+ excel.open(expected)
89
+
90
+ expect(excel.num_rows).to eq 8
91
+
92
+ end
93
+
94
+
95
+ it "should export a model and associations to .xls file" do
96
+
97
+ create( :project_user )
98
+ create_list(:project, 7)
99
+
100
+ expected = result_file('exp_project_plus_assoc.xls')
101
+
102
+ gen = ExcelExporter.new(expected)
103
+
104
+ items = Project.all
105
+
106
+ gen.export_with_associations(Project, items)
107
+
108
+ expect(File.exists?(expected)).to eq true
109
+
110
+ excel = Excel.new
111
+ excel.open(expected)
112
+
113
+ expect(excel.row(0)).to include 'owner'
114
+ expect(excel.row(0)).to include 'user'
115
+
116
+ expect(excel.num_rows).to eq Project.count + 1
117
+
118
+ user_inx = excel.row(0).index 'user'
119
+
120
+ expect(user_inx).to be > -1
121
+
122
+ expect( excel[1, user_inx] ).to include 'mr'
123
+
124
+ inx = excel.row(0).index 'owner'
125
+
126
+ expect(inx).to be > -1
127
+
128
+ expect( excel[1, inx] ).to include '10000.23'
90
129
  end
91
-
92
- it "should export a model and associations to .xls file" do
93
130
 
94
- Project.create( :value_as_string => 'Value as Text', :value_as_boolean => true, :value_as_double => 75.672)
131
+ it "should export a model and has_many assocs to .xls file" do
132
+
133
+ create( :project_user )
134
+ create( :project_with_milestones )
135
+ #create( :project_with_milestones, milestones_count: 4 )
136
+ create_list(:project, 7)
95
137
 
96
- expect= result_file('project_plus_assoc_export_spec.xls')
138
+ expected = result_file('exp_project_plus_has_many_assoc.xls')
97
139
 
98
- gen = ExcelExporter.new(expect)
140
+ gen = ExcelExporter.new(expected)
99
141
 
100
142
  items = Project.all
101
143
 
102
144
  gen.export_with_associations(Project, items)
103
145
 
104
- File.exists?(expect).should be_true
146
+ expect(File.exists?(expected)).to eq true
147
+
148
+ excel = Excel.new
149
+ excel.open(expected)
150
+
151
+ expect(excel.row(0)).to include 'owner'
152
+ expect(excel.row(0)).to include 'user'
153
+
154
+ expect(excel.num_rows).to eq Project.count + 1
155
+
156
+ milestone_inx = excel.row(0).index 'milestones'
157
+
158
+ expect(milestone_inx).to be > -1
159
+
160
+ puts excel[2, milestone_inx].inspect
161
+
162
+ expect( excel[2, milestone_inx].to_s ).to include Delimiters::multi_assoc_delim
163
+ expect( excel[2, milestone_inx].to_s ).to include 'milestone 1'
164
+
165
+ end
166
+
167
+
168
+ it "should export a model and assocs in json to .xls file" do
169
+
170
+ create( :project_user )
171
+ create( :project_with_milestones )
172
+ #create( :project_with_milestones, milestones_count: 4 )
173
+ create_list(:project, 7)
174
+
175
+ expected = result_file('exp_project_plus_has_many_assoc.xls')
176
+
177
+ gen = ExcelExporter.new(expected)
178
+
179
+ items = Project.all
180
+
181
+ gen.export_with_associations(Project, items, json: true)
182
+
183
+ expect(File.exists?(expected)).to eq true
184
+
185
+ excel = Excel.new
186
+ excel.open(expected)
187
+
188
+ expect(excel.num_rows).to eq Project.count + 1
189
+
190
+ milestone_inx = excel.row(0).index 'milestones'
191
+
192
+ puts excel[2, milestone_inx].inspect
193
+
194
+ expect( excel[2, milestone_inx].to_s ).to include '['
195
+ expect( excel[2, milestone_inx].to_s ).to include '"name":"milestone 1"'
105
196
 
106
197
  end
107
198
 
@@ -15,17 +15,8 @@ include DataShift
15
15
  describe 'Excel Generator' do
16
16
 
17
17
  before(:all) do
18
-
19
- # load our test model definitions - Project etc
20
- require ifixture_file('test_model_defs')
21
-
22
- db_connect( 'test_file' ) # , test_memory, test_mysql
23
-
24
- # handle migration changes or reset of test DB
25
- migrate_up
26
18
 
27
- db_clear() # todo read up about proper transactional fixtures
28
- results_clear()
19
+ results_clear("gen_*.xls")
29
20
 
30
21
  @klazz = Project
31
22
  @assoc_klazz = Category
@@ -38,27 +29,27 @@ describe 'Excel Generator' do
38
29
  end
39
30
 
40
31
  it "should be able to create a new excel generator" do
41
- generator = ExcelGenerator.new( 'dummy.xls' )
32
+ generator = ExcelGenerator.new( 'gen_dummy.xls' )
42
33
 
43
34
  generator.should_not be_nil
44
35
  end
45
36
 
46
37
  it "should generate template .xls file from model" do
47
38
 
48
- expect = result_file('project_template_spec.xls')
39
+ expected = result_file('gen_project_template_spec.xls')
49
40
 
50
- gen = ExcelGenerator.new( expect )
41
+ gen = ExcelGenerator.new( expected )
51
42
 
52
43
  gen.generate(Project)
53
44
 
54
- File.exists?(expect).should be_true
45
+ expect(File.exists?(expected)).to eq true
55
46
 
56
- puts "Can manually check file @ #{expect}"
47
+ puts "Can manually check file @ #{expected}"
57
48
 
58
49
  excel = Excel.new
59
- excel.open(expect)
50
+ excel.open(expected)
60
51
 
61
- excel.worksheets.should have(1).items
52
+ expect(excel.worksheets.size).to eq 1
62
53
 
63
54
  excel.worksheet(0).name.should == 'Project'
64
55
 
@@ -77,18 +68,18 @@ describe 'Excel Generator' do
77
68
 
78
69
  it "should include all associations in template .xls file from model" do
79
70
 
80
- expect= result_file('project_plus_assoc_template_spec.xls')
71
+ expected = result_file('gen_project_plus_assoc_template_spec.xls')
81
72
 
82
- gen = ExcelGenerator.new(expect)
73
+ gen = ExcelGenerator.new(expected)
83
74
 
84
75
  gen.generate_with_associations(Project)
85
76
 
86
- File.exists?(expect).should be_true
77
+ expect( File.exists?(expected)).to eq true
87
78
 
88
79
  excel = Excel.new
89
- excel.open(expect)
80
+ excel.open(expected)
90
81
 
91
- excel.worksheets.should have(1).items
82
+ expect(excel.worksheets.size).to eq 1
92
83
 
93
84
  excel.worksheet(0).name.should == 'Project'
94
85
 
@@ -100,22 +91,22 @@ describe 'Excel Generator' do
100
91
  end
101
92
 
102
93
 
103
- it "should enable us to exclude associations by type in template .xls file", :fail => true do
94
+ it "should enable us to exclude associations by type in template .xls file" do
104
95
 
105
- expect= result_file('project_plus_some_assoc_template_spec.xls')
96
+ expected = result_file('gen_project_plus_some_assoc_template_spec.xls')
106
97
 
107
- gen = ExcelGenerator.new(expect)
98
+ gen = ExcelGenerator.new(expected)
108
99
 
109
100
  options = {:exclude => :has_many }
110
101
 
111
102
  gen.generate_with_associations(Project, options)
112
103
 
113
- File.exists?(expect).should be_true, "Failed to find expected result file #{expect}"
104
+ expect(File.exists?(expected)).to eq true #, "Failed to find expected result file #{expected}"
114
105
 
115
106
  excel = Excel.new
116
- excel.open(expect)
107
+ excel.open(expected)
117
108
 
118
- excel.worksheets.should have(1).items
109
+ expect(excel.worksheets.size).to eq 1
119
110
 
120
111
  excel.worksheet(0).name.should == 'Project'
121
112
 
@@ -131,22 +122,22 @@ describe 'Excel Generator' do
131
122
  end
132
123
 
133
124
 
134
- it "should enable us to exclude certain associations in template .xls file ", :fail => true do
125
+ it "should enable us to exclude certain associations in template .xls file " do
135
126
 
136
- expect= result_file('project_plus_some_assoc_template_spec.xls')
127
+ expected = result_file('gen_project_plus_some_assoc_template_spec.xls')
137
128
 
138
- gen = ExcelGenerator.new(expect)
129
+ gen = ExcelGenerator.new(expected)
139
130
 
140
131
  options = {:remove => [:milestones, :versions] }
141
132
 
142
133
  gen.generate_with_associations(Project, options)
143
134
 
144
- File.exists?(expect).should be_true, "Failed to find expected result file #{expect}"
135
+ expect(File.exists?(expected)).to eq true#, "Failed to find expected result file #{expected}"
145
136
 
146
137
  excel = Excel.new
147
- excel.open(expect)
138
+ excel.open(expected)
148
139
 
149
- excel.worksheets.should have(1).items
140
+ expect(excel.worksheets.size).to eq 1
150
141
 
151
142
  excel.worksheet(0).name.should == 'Project'
152
143
 
@@ -164,22 +155,22 @@ describe 'Excel Generator' do
164
155
  end
165
156
 
166
157
 
167
- it "should enable us to remove standard rails feilds from template .xls file ", :fail => true do
158
+ it "should enable us to remove standard rails feilds from template .xls file " do
168
159
 
169
- expect= result_file('project_plus_some_assoc_template_spec.xls')
160
+ expected = result_file('gen_project_plus_some_assoc_template_spec.xls')
170
161
 
171
- gen = ExcelGenerator.new(expect)
162
+ gen = ExcelGenerator.new(expected)
172
163
 
173
164
  options = {:remove_rails => true}
174
165
 
175
166
  gen.generate_with_associations(Project, options)
176
167
 
177
- File.exists?(expect).should be_true, "Failed to find expected result file #{expect}"
168
+ expect(File.exists?(expected)).to eq true#, "Failed to find expected result file #{expected}"
178
169
 
179
170
  excel = Excel.new
180
- excel.open(expect)
171
+ excel.open(expected)
181
172
 
182
- excel.worksheets.should have(1).items
173
+ expect(excel.worksheets.size).to eq 1
183
174
 
184
175
  excel.worksheet(0).name.should == 'Project'
185
176
 
@@ -194,18 +185,18 @@ describe 'Excel Generator' do
194
185
 
195
186
  it "should enable us to autosize columns in the .xls file" do
196
187
 
197
- expect= result_file('project_autosized_template_spec.xls')
188
+ expected = result_file('gen_project_autosized_template_spec.xls')
198
189
 
199
- gen = ExcelGenerator.new(expect)
190
+ gen = ExcelGenerator.new(expected)
200
191
 
201
192
  options = {:autosize => true, :exclude => :milestones }
202
193
 
203
194
  gen.generate_with_associations(Project, options)
204
195
 
205
- File.exists?(expect).should be_true
196
+ expect( File.exists?(expected)).to eq true
206
197
 
207
198
  excel = Excel.new
208
- excel.open(expect)
199
+ excel.open(expected)
209
200
 
210
201
  end
211
202
 
@@ -14,206 +14,224 @@ include DataShift
14
14
 
15
15
  describe 'Excel Loader' do
16
16
 
17
- include_context "ActiveRecordTestModelsConnected"
18
-
17
+ # include_context "ActiveRecordTestModelsConnected"
18
+
19
19
  before(:each) do
20
- DataShift::MethodDictionary.clear
21
-
22
- @method_mapper = DataShift::MethodMapper.new
23
20
  end
24
-
25
-
26
- include_context "ClearAndPopulateProject"
27
-
21
+
22
+
28
23
  before(:each) do
24
+ create_list(:category, 5)
25
+ end
26
+
27
+ context 'loader creates new records' do
28
+
29
+ it "should be able to create a new excel loader and load object" do
30
+ loader = ExcelLoader.new( Project)
29
31
 
30
- %w{category_001 category_002 category_003 category_004 category_005}.each do |cat|
31
- Category.find_or_create_by(reference: cat)
32
+ loader.load_object.should_not be_nil
33
+ loader.load_object.should be_is_a(Project)
34
+ expect(loader.load_object.new_record?).to eq true
32
35
  end
33
36
 
34
-
35
- end
36
-
37
- it "should be able to create a new excel loader and load object" do
38
- loader = ExcelLoader.new( Project)
39
-
40
- loader.load_object.should_not be_nil
41
- loader.load_object.should be_is_a(Project)
42
- loader.load_object.new_record?.should be_true
43
- end
44
-
45
- it "should process a simple .xls spreedsheet" do
46
-
47
- loader = ExcelLoader.new(Project)
48
-
49
- count = Project.count
50
- loader.perform_load ifixture_file('SimpleProjects.xls')
51
-
52
- loader.loaded_count.should == (Project.count - count)
53
- end
37
+ it "should process a simple .xls spreedsheet" do
38
+
39
+ loader = ExcelLoader.new(Project)
40
+
41
+ count = Project.count
42
+ loader.perform_load ifixture_file('SimpleProjects.xls')
43
+
44
+ loader.loaded_count.should == (Project.count - count)
45
+ end
46
+
47
+ it "should process multiple associations from single column" do
48
+
49
+ DataShift::MethodDictionary.find_operators( Category )
50
+
51
+ DataShift::MethodDictionary.build_method_details( Category )
52
+
53
+ expect(Project.find_by_title('001')).to be_nil
54
+
55
+ count = Project.count
54
56
 
55
- it "should process multiple associationss from single column", :fail => true do
56
-
57
-
58
- DataShift::MethodDictionary.find_operators( Category )
59
-
60
- DataShift::MethodDictionary.build_method_details( Category )
61
-
62
- Project.find_by_title('001').should be_nil
63
- count = Project.count
57
+ loader = ExcelLoader.new(Project)
64
58
 
65
- loader = ExcelLoader.new(Project)
66
-
59
+ loader.perform_load( ifixture_file('ProjectsSingleCategories.xls') )
67
60
 
68
- loader.perform_load( ifixture_file('ProjectsSingleCategories.xls') )
61
+ expect(loader.loaded_count).to eq 4
69
62
 
70
- loader.loaded_count.should be > 3
71
- loader.loaded_count.should == (Project.count - count)
63
+ loader.loaded_count.should == (Project.count - count)
72
64
 
73
- {'001' => 2, '002' => 1, '003' => 3, '099' => 0 }.each do|title, expected|
74
- project = Project.find_by_title(title)
65
+ {'001' => 2, '002' => 1, '003' => 3, '099' => 0 }.each do|title, expected|
66
+ project = Project.find_by_title(title)
75
67
 
76
- project.should_not be_nil
77
- #puts "#{project.inspect} [#{project.categories.size}]"
78
-
79
- project.should have(expected).categories
68
+ expect(project).to_not be_nil
69
+
70
+ expect(project.categories.size).to eq expected
71
+ end
80
72
  end
81
- end
82
73
 
83
- it "should process multiple associations in excel spreedsheet" do
84
-
85
- loader = ExcelLoader.new(Project)
86
-
87
- count = Project.count
88
- loader.perform_load( ifixture_file('ProjectsMultiCategories.xls' ))
89
-
90
- loader.loaded_count.should == (Project.count - count)
91
-
92
- {'004' => 3, '005' => 1, '006' => 0, '007' => 1 }.each do|title, expected|
93
- project = Project.find_by_title(title)
94
-
95
- project.should_not be_nil
96
-
97
- project.should have(expected).categories
74
+ it "should process multiple associations in excel spreedsheet" do
75
+
76
+ loader = ExcelLoader.new(Project)
77
+
78
+ count = Project.count
79
+ loader.perform_load( ifixture_file('ProjectsMultiCategories.xls' ))
80
+
81
+ loader.loaded_count.should == (Project.count - count)
82
+
83
+ {'004' => 3, '005' => 1, '006' => 0, '007' => 1 }.each do|title, expected|
84
+ project = Project.find_by_title(title)
85
+
86
+ project.should_not be_nil
87
+
88
+ expect(project.categories.size).to eq expected
89
+ end
90
+
98
91
  end
99
-
100
- end
101
-
102
- it "should process multiple associations with lookup specified in column from excel spreedsheet" do
103
-
104
- loader = ExcelLoader.new(Project)
105
-
106
- count = Project.count
107
- loader.perform_load( ifixture_file('ProjectsMultiCategoriesHeaderLookup.xls'))
108
-
109
- loader.loaded_count.should == (Project.count - count)
110
- loader.loaded_count.should > 3
111
-
112
- {'004' => 4, '005' => 1, '006' => 0, '007' => 1 }.each do|title, expected|
113
- project = Project.find_by_title(title)
114
-
115
- project.should_not be_nil
116
-
117
- project.should have(expected).categories
92
+
93
+ it "should process multiple associations with lookup specified in column from excel spreedsheet" do
94
+
95
+ loader = ExcelLoader.new(Project)
96
+
97
+ count = Project.count
98
+ loader.perform_load( ifixture_file('ProjectsMultiCategoriesHeaderLookup.xls'))
99
+
100
+ loader.loaded_count.should == (Project.count - count)
101
+ loader.loaded_count.should > 3
102
+
103
+ {'004' => 4, '005' => 1, '006' => 0, '007' => 1 }.each do|title, expected|
104
+ project = Project.find_by_title(title)
105
+
106
+ project.should_not be_nil
107
+
108
+ expect(project.categories.size).to eq expected
109
+ end
110
+
118
111
  end
119
-
120
- end
121
-
122
- it "should process excel spreedsheet with extra undefined columns" do
123
- loader = ExcelLoader.new(Project)
124
- lambda {loader.perform_load( ifixture_file('BadAssociationName.xls') ) }.should_not raise_error
125
- end
126
112
 
127
- it "should NOT process excel spreedsheet with extra undefined columns when strict mode" do
128
- loader = ExcelLoader.new(Project)
129
- expect {loader.perform_load( ifixture_file('BadAssociationName.xls'), :strict => true)}.to raise_error(MappingDefinitionError)
130
- end
113
+ it "should process excel spreedsheet with extra undefined columns" do
114
+ loader = ExcelLoader.new(Project)
115
+ lambda {loader.perform_load( ifixture_file('BadAssociationName.xls') ) }.should_not raise_error
116
+ end
131
117
 
132
- it "should raise an error when mandatory columns missing" do
133
- loader = ExcelLoader.new(Project)
134
- expect {loader.perform_load(ifixture_file('ProjectsMultiCategories.xls'), :mandatory => ['not_an_option', 'must_be_there'])}.to raise_error(DataShift::MissingMandatoryError)
135
- end
118
+ it "should NOT process excel spreedsheet with extra undefined columns when strict mode" do
119
+ loader = ExcelLoader.new(Project)
120
+ expect {loader.perform_load( ifixture_file('BadAssociationName.xls'), :strict => true)}.to raise_error(MappingDefinitionError)
121
+ end
122
+
123
+ it "should raise an error when mandatory columns missing" do
124
+ loader = ExcelLoader.new(Project)
125
+ expect {loader.perform_load(ifixture_file('ProjectsMultiCategories.xls'), :mandatory => ['not_an_option', 'must_be_there'])}.to raise_error(DataShift::MissingMandatoryError)
126
+ end
136
127
 
137
- it "should provide facility to set default values", :focus => true do
138
- loader = ExcelLoader.new(Project)
139
-
140
- populator = loader.populator
141
-
142
- populator.set_default_value('value_as_string', 'some default text' )
143
- populator.set_default_value('value_as_double', 45.467 )
144
- populator.set_default_value('value_as_boolean', true )
145
-
146
- texpected = Time.now.to_s(:db)
147
-
148
- populator.set_default_value('value_as_datetime', texpected )
149
-
150
- #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
151
-
152
- loader.perform_load(ifixture_file('ProjectsSingleCategories.xls'))
153
-
154
- p = Project.find_by_title( '099' )
155
-
156
- p.should_not be_nil
157
-
158
- p.value_as_string.should == 'some default text'
159
- p.value_as_double.should == 45.467
160
- p.value_as_boolean.should == true
161
- p.value_as_datetime.to_s(:db).should == texpected
162
-
163
- # expected: "2012-09-17 10:00:52"
164
- # got: Mon Sep 17 10:00:52 +0100 2012 (using ==)
165
-
166
- p_no_defs = Project.first
167
-
168
- p_no_defs.value_as_string.should_not == 'some default text'
169
- p_no_defs.value_as_double.should_not == 45.467
170
- p_no_defs.value_as_datetime.should_not == texpected
171
-
172
- end
173
-
174
- it "should provide facility to set pre and post fix values" do
175
- loader = ExcelLoader.new(Project)
176
-
177
- loader.populator.set_prefix('value_as_string', 'myprefix' )
178
- loader.populator.set_postfix('value_as_string', 'my post fix' )
179
-
180
- #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
181
-
182
- loader.perform_load( ifixture_file('ProjectsSingleCategories.xls'))
183
-
184
- p = Project.find_by_title( '001' )
185
-
186
- p.should_not be_nil
187
-
188
- p.value_as_string.should == 'myprefixDemo stringmy post fix'
189
128
  end
190
-
191
- it "should provide facility to set default values via YAML configuration", :excel => true do
192
- loader = ExcelLoader.new(Project)
193
-
194
- loader.configure_from( ifixture_file('ProjectsDefaults.yml') )
195
-
196
-
197
- loader.perform_load( ifixture_file('ProjectsSingleCategories.xls') )
198
-
199
- p = Project.find_by_title( '099' )
200
-
201
- p.should_not be_nil
202
-
203
- p.value_as_string.should == "Default Project Value"
129
+
130
+ context 'update existing records' do
204
131
  end
205
132
 
133
+ context 'external configuration of loader' do
134
+
135
+ it "should provide facility to set default values", :focus => true do
136
+ loader = ExcelLoader.new(Project)
137
+
138
+ populator = loader.populator
139
+
140
+ populator.set_default_value('value_as_string', 'some default text' )
141
+ populator.set_default_value('value_as_double', 45.467 )
142
+ populator.set_default_value('value_as_boolean', true )
143
+
144
+ texpected = Time.now.to_s(:db)
145
+
146
+ populator.set_default_value('value_as_datetime', texpected )
147
+
148
+ #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
149
+
150
+ loader.perform_load(ifixture_file('ProjectsSingleCategories.xls'))
151
+
152
+ p = Project.find_by_title( '099' )
153
+
154
+ p.should_not be_nil
155
+
156
+ p.value_as_string.should == 'some default text'
157
+ p.value_as_double.should == 45.467
158
+ p.value_as_boolean.should == true
159
+ p.value_as_datetime.to_s(:db).should == texpected
160
+
161
+ # expected: "2012-09-17 10:00:52"
162
+ # got: Mon Sep 17 10:00:52 +0100 2012 (using ==)
163
+
164
+ p_no_defs = Project.first
165
+
166
+ p_no_defs.value_as_string.should_not == 'some default text'
167
+ p_no_defs.value_as_double.should_not == 45.467
168
+ p_no_defs.value_as_datetime.should_not == texpected
169
+
170
+ end
171
+
172
+ it "should provide facility to set pre and post fix values" do
173
+ loader = ExcelLoader.new(Project)
174
+
175
+ loader.populator.set_prefix('value_as_string', 'myprefix' )
176
+ loader.populator.set_postfix('value_as_string', 'my post fix' )
177
+
178
+ #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
179
+
180
+ loader.perform_load( ifixture_file('ProjectsSingleCategories.xls'))
181
+
182
+ p = Project.find_by_title( '001' )
183
+
184
+ p.should_not be_nil
185
+
186
+ p.value_as_string.should == 'myprefixDemo stringmy post fix'
187
+ end
188
+
189
+ it "should provide facility to set default values via YAML configuration", :excel => true do
190
+ loader = ExcelLoader.new(Project)
191
+
192
+ loader.configure_from( ifixture_file('ProjectsDefaults.yml') )
193
+
194
+
195
+ loader.perform_load( ifixture_file('ProjectsSingleCategories.xls') )
196
+
197
+ p = Project.find_by_title( '099' )
198
+
199
+ p.should_not be_nil
200
+
201
+ p.value_as_string.should == "Default Project Value"
202
+ end
203
+
204
+
205
+ it "should provide facility to over ride values via YAML configuration", :excel => true do
206
+ loader = ExcelLoader.new(Project)
207
+
208
+ loader.configure_from( ifixture_file('ProjectsDefaults.yml') )
209
+
210
+
211
+ loader.perform_load( ifixture_file('ProjectsSingleCategories.xls') )
212
+
213
+ Project.all.each {|p| p.value_as_double.should == 99.23546 }
214
+ end
215
+
216
+
217
+
218
+ it "should provide facility to over ride values via YAML configuration", :yaml => true do
219
+ loader = ExcelLoader.new(Project)
220
+
221
+ expect(Project.count).to eq 0
222
+
223
+ loader.configure_from( ifixture_file('ProjectsDefaults.yml') )
224
+
225
+
226
+ loader.perform_load( ifixture_file('ProjectsSingleCategories.xls') )
227
+
228
+ Project.all.each do |p|
229
+ expect(p.value_as_double).to be_a BigDecimal
230
+ expect(p.value_as_double).to eq 99.23546
231
+ end
232
+ end
233
+
206
234
 
207
- it "should provide facility to over ride values via YAML configuration", :excel => true do
208
- loader = ExcelLoader.new(Project)
209
-
210
- loader.configure_from( ifixture_file('ProjectsDefaults.yml') )
211
-
212
-
213
- loader.perform_load( ifixture_file('ProjectsSingleCategories.xls') )
214
-
215
- Project.all.each {|p| p.value_as_double.should == 99.23546 }
216
235
  end
217
-
218
-
236
+
219
237
  end