datashift 0.2.1 → 0.2.2

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 (84) hide show
  1. data/.document +5 -5
  2. data/LICENSE.txt +26 -26
  3. data/README.markdown +326 -305
  4. data/README.rdoc +19 -19
  5. data/Rakefile +86 -93
  6. data/VERSION +1 -1
  7. data/datashift.gemspec +163 -152
  8. data/lib/applications/jruby/jexcel_file.rb +410 -408
  9. data/lib/applications/jruby/word.rb +79 -79
  10. data/lib/datashift.rb +183 -152
  11. data/lib/datashift/exceptions.rb +11 -11
  12. data/lib/datashift/file_definitions.rb +353 -353
  13. data/lib/datashift/mapping_file_definitions.rb +87 -87
  14. data/lib/datashift/method_detail.rb +293 -275
  15. data/lib/datashift/method_dictionary.rb +208 -209
  16. data/lib/datashift/method_mapper.rb +90 -90
  17. data/lib/datashift/model_mapper.rb +27 -0
  18. data/lib/exporters/csv_exporter.rb +36 -0
  19. data/lib/exporters/excel_exporter.rb +116 -0
  20. data/lib/exporters/exporter_base.rb +15 -0
  21. data/lib/generators/csv_generator.rb +36 -36
  22. data/lib/generators/excel_generator.rb +106 -122
  23. data/lib/generators/generator_base.rb +13 -13
  24. data/lib/helpers/core_ext/to_b.rb +24 -24
  25. data/lib/helpers/rake_utils.rb +42 -0
  26. data/lib/helpers/spree_helper.rb +194 -153
  27. data/lib/java/poi-3.7/LICENSE +507 -507
  28. data/lib/java/poi-3.7/NOTICE +21 -21
  29. data/lib/java/poi-3.7/RELEASE_NOTES.txt +115 -115
  30. data/lib/loaders/csv_loader.rb +98 -98
  31. data/lib/loaders/excel_loader.rb +155 -155
  32. data/lib/loaders/loader_base.rb +420 -420
  33. data/lib/loaders/spreadsheet_loader.rb +136 -136
  34. data/lib/loaders/spree/image_loader.rb +67 -63
  35. data/lib/loaders/spree/product_loader.rb +289 -248
  36. data/lib/thor/generate_excel.thor +54 -0
  37. data/sandbox/app/controllers/application_controller.rb +3 -0
  38. data/sandbox/config/application.rb +43 -0
  39. data/sandbox/config/database.yml +34 -0
  40. data/sandbox/config/environment.rb +7 -0
  41. data/sandbox/config/environments/development.rb +30 -0
  42. data/spec/csv_loader_spec.rb +30 -30
  43. data/spec/datashift_spec.rb +26 -26
  44. data/spec/db/migrate/20110803201325_create_test_bed.rb +85 -85
  45. data/spec/excel_exporter_spec.rb +78 -78
  46. data/spec/excel_generator_spec.rb +78 -78
  47. data/spec/excel_loader_spec.rb +223 -223
  48. data/spec/file_definitions.rb +141 -141
  49. data/spec/fixtures/ProjectsDefaults.yml +29 -29
  50. data/spec/fixtures/config/database.yml +27 -27
  51. data/spec/fixtures/datashift_Spree_db.sqlite +0 -0
  52. data/spec/fixtures/datashift_test_models_db.sqlite +0 -0
  53. data/spec/fixtures/negative/SpreeProdMiss1Mandatory.csv +4 -4
  54. data/spec/fixtures/negative/SpreeProdMissManyMandatory.csv +4 -4
  55. data/spec/fixtures/spree/SpreeProducts.csv +4 -4
  56. data/spec/fixtures/spree/SpreeProducts.xls +0 -0
  57. data/spec/fixtures/spree/SpreeProductsMultiColumn.csv +4 -4
  58. data/spec/fixtures/spree/SpreeProductsMultiColumn.xls +0 -0
  59. data/spec/fixtures/spree/SpreeProductsSimple.csv +4 -4
  60. data/spec/fixtures/spree/SpreeProductsWithImages.csv +4 -4
  61. data/spec/fixtures/spree/SpreeZoneExample.csv +5 -5
  62. data/spec/fixtures/test_model_defs.rb +57 -57
  63. data/spec/loader_spec.rb +120 -120
  64. data/spec/method_dictionary_spec.rb +242 -242
  65. data/spec/method_mapper_spec.rb +41 -41
  66. data/spec/spec_helper.rb +154 -116
  67. data/spec/spree_exporter_spec.rb +67 -0
  68. data/spec/spree_generator_spec.rb +77 -64
  69. data/spec/spree_loader_spec.rb +363 -324
  70. data/spec/spree_method_mapping_spec.rb +218 -214
  71. data/tasks/config/seed_fu_product_template.erb +15 -15
  72. data/tasks/config/tidy_config.txt +12 -12
  73. data/tasks/{excel_generator.rake → export/excel_generator.rake} +101 -78
  74. data/tasks/file_tasks.rake +36 -36
  75. data/tasks/import/csv.rake +50 -49
  76. data/tasks/import/excel.rake +74 -71
  77. data/tasks/spree/image_load.rake +108 -108
  78. data/tasks/spree/product_loader.rake +43 -43
  79. data/tasks/word_to_seedfu.rake +166 -166
  80. data/test/helper.rb +18 -18
  81. data/test/test_interact.rb +7 -7
  82. metadata +16 -8
  83. data/datashift-0.1.0.gem +0 -0
  84. data/tasks/db_tasks.rake +0 -66
@@ -1,79 +1,79 @@
1
- # Copyright:: (c) Autotelik Media Ltd 2011
2
- # Author :: Tom Statter
3
- # Date :: Aug 2011
4
- # License:: MIT
5
- #
6
- # Details:: Specs for Excel aspect of Active Record Loader
7
- #
8
- require File.dirname(__FILE__) + '/spec_helper'
9
-
10
- if(Guards::jruby?)
11
- require 'erb'
12
- require 'excel_generator'
13
-
14
- include DataShift
15
-
16
- describe 'Excel Generator' do
17
-
18
- before(:all) do
19
- db_connect( 'test_file' ) # , test_memory, test_mysql
20
-
21
- # load our test model definitions - Project etc
22
- require File.join($DataShiftFixturePath, 'test_model_defs')
23
-
24
- # handle migration changes or reset of test DB
25
- migrate_up
26
-
27
- db_clear() # todo read up about proper transactional fixtures
28
- results_clear()
29
-
30
- @klazz = Project
31
- @assoc_klazz = Category
32
- end
33
-
34
- before(:each) do
35
- MethodDictionary.clear
36
- MethodDictionary.find_operators( @klazz )
37
- MethodDictionary.find_operators( @assoc_klazz )
38
- end
39
-
40
- it "should be able to create a new excel generator" do
41
- generator = ExcelGenerator.new( 'dummy.xls' )
42
-
43
- generator.should_not be_nil
44
- end
45
-
46
- it "should generate template .xls file from model" do
47
-
48
- expect = result_file('project_template_spec.xls')
49
-
50
- gen = ExcelGenerator.new( expect )
51
-
52
- gen.generate(Project)
53
-
54
- File.exists?(expect).should be_true
55
-
56
- puts "Can manually check file @ #{expect}"
57
- end
58
-
59
- it "should export a simple model to .xls spreedsheet" do
60
-
61
- Project.create( :value_as_string => 'Value as Text', :value_as_boolean => true, :value_as_double => 75.672)
62
- #001 Demo string blah blah 2011-02-14 1.00 320.00
63
-
64
- expect= result_file('simple_export_spec.xls')
65
-
66
- gen = ExcelGenerator.new(expect)
67
-
68
- items = Project.all
69
-
70
- gen.export(items)
71
-
72
- File.exists?(expect).should be_true
73
-
74
- end
75
-
76
- end
77
- else
78
- puts "WARNING: skipped excel_generator_spec : Requires JRUBY - JExcelFile requires JAVA"
1
+ # Copyright:: (c) Autotelik Media Ltd 2011
2
+ # Author :: Tom Statter
3
+ # Date :: Aug 2011
4
+ # License:: MIT
5
+ #
6
+ # Details:: Specs for Excel aspect of Active Record Loader
7
+ #
8
+ require File.dirname(__FILE__) + '/spec_helper'
9
+
10
+ if(Guards::jruby?)
11
+ require 'erb'
12
+ require 'excel_generator'
13
+
14
+ include DataShift
15
+
16
+ describe 'Excel Generator' do
17
+
18
+ before(:all) do
19
+ db_connect( 'test_file' ) # , test_memory, test_mysql
20
+
21
+ # load our test model definitions - Project etc
22
+ require File.join($DataShiftFixturePath, 'test_model_defs')
23
+
24
+ # handle migration changes or reset of test DB
25
+ migrate_up
26
+
27
+ db_clear() # todo read up about proper transactional fixtures
28
+ results_clear()
29
+
30
+ @klazz = Project
31
+ @assoc_klazz = Category
32
+ end
33
+
34
+ before(:each) do
35
+ MethodDictionary.clear
36
+ MethodDictionary.find_operators( @klazz )
37
+ MethodDictionary.find_operators( @assoc_klazz )
38
+ end
39
+
40
+ it "should be able to create a new excel generator" do
41
+ generator = ExcelGenerator.new( 'dummy.xls' )
42
+
43
+ generator.should_not be_nil
44
+ end
45
+
46
+ it "should generate template .xls file from model" do
47
+
48
+ expect = result_file('project_template_spec.xls')
49
+
50
+ gen = ExcelGenerator.new( expect )
51
+
52
+ gen.generate(Project)
53
+
54
+ File.exists?(expect).should be_true
55
+
56
+ puts "Can manually check file @ #{expect}"
57
+ end
58
+
59
+ it "should export a simple model to .xls spreedsheet" do
60
+
61
+ Project.create( :value_as_string => 'Value as Text', :value_as_boolean => true, :value_as_double => 75.672)
62
+ #001 Demo string blah blah 2011-02-14 1.00 320.00
63
+
64
+ expect= result_file('simple_export_spec.xls')
65
+
66
+ gen = ExcelGenerator.new(expect)
67
+
68
+ items = Project.all
69
+
70
+ gen.export(items)
71
+
72
+ File.exists?(expect).should be_true
73
+
74
+ end
75
+
76
+ end
77
+ else
78
+ puts "WARNING: skipped excel_generator_spec : Requires JRUBY - JExcelFile requires JAVA"
79
79
  end # jruby
@@ -1,79 +1,79 @@
1
- # Copyright:: (c) Autotelik Media Ltd 2011
2
- # Author :: Tom Statter
3
- # Date :: Aug 2011
4
- # License:: MIT
5
- #
6
- # Details:: Specs for Excel aspect of Active Record Loader
7
- #
8
- require File.dirname(__FILE__) + '/spec_helper'
9
-
10
- if(Guards::jruby?)
11
- require 'erb'
12
- require 'excel_generator'
13
-
14
- include DataShift
15
-
16
- describe 'Excel Generator' do
17
-
18
- before(:all) do
19
- db_connect( 'test_file' ) # , test_memory, test_mysql
20
-
21
- # load our test model definitions - Project etc
22
- require File.join($DataShiftFixturePath, 'test_model_defs')
23
-
24
- # handle migration changes or reset of test DB
25
- migrate_up
26
-
27
- db_clear() # todo read up about proper transactional fixtures
28
- results_clear()
29
-
30
- @klazz = Project
31
- @assoc_klazz = Category
32
- end
33
-
34
- before(:each) do
35
- MethodDictionary.clear
36
- MethodDictionary.find_operators( @klazz )
37
- MethodDictionary.find_operators( @assoc_klazz )
38
- end
39
-
40
- it "should be able to create a new excel generator" do
41
- generator = ExcelGenerator.new( 'dummy.xls' )
42
-
43
- generator.should_not be_nil
44
- end
45
-
46
- it "should generate template .xls file from model" do
47
-
48
- expect = result_file('project_template_spec.xls')
49
-
50
- gen = ExcelGenerator.new( expect )
51
-
52
- gen.generate(Project)
53
-
54
- File.exists?(expect).should be_true
55
-
56
- puts "Can manually check file @ #{expect}"
57
- end
58
-
59
- it "should export a simple model to .xls spreedsheet" do
60
-
61
- Project.create( :value_as_string => 'Value as Text', :value_as_boolean => true, :value_as_double => 75.672)
62
- #001 Demo string blah blah 2011-02-14 1.00 320.00
63
-
64
- expect= result_file('simple_export_spec.xls')
65
-
66
- gen = ExcelGenerator.new(expect)
67
-
68
- items = Project.all
69
-
70
- gen.export(items)
71
-
72
- File.exists?(expect).should be_true
73
-
74
- end
75
-
76
- end
77
- else
78
- puts "WARNING: skipped excel_generator_spec : Requires JRUBY - JExcelFile requires JAVA"
1
+ # Copyright:: (c) Autotelik Media Ltd 2011
2
+ # Author :: Tom Statter
3
+ # Date :: Aug 2011
4
+ # License:: MIT
5
+ #
6
+ # Details:: Specs for Excel aspect of Active Record Loader
7
+ #
8
+ require File.dirname(__FILE__) + '/spec_helper'
9
+
10
+ if(Guards::jruby?)
11
+ require 'erb'
12
+ require 'excel_generator'
13
+
14
+ include DataShift
15
+
16
+ describe 'Excel Generator' do
17
+
18
+ before(:all) do
19
+ db_connect( 'test_file' ) # , test_memory, test_mysql
20
+
21
+ # load our test model definitions - Project etc
22
+ require File.join($DataShiftFixturePath, 'test_model_defs')
23
+
24
+ # handle migration changes or reset of test DB
25
+ migrate_up
26
+
27
+ db_clear() # todo read up about proper transactional fixtures
28
+ results_clear()
29
+
30
+ @klazz = Project
31
+ @assoc_klazz = Category
32
+ end
33
+
34
+ before(:each) do
35
+ MethodDictionary.clear
36
+ MethodDictionary.find_operators( @klazz )
37
+ MethodDictionary.find_operators( @assoc_klazz )
38
+ end
39
+
40
+ it "should be able to create a new excel generator" do
41
+ generator = ExcelGenerator.new( 'dummy.xls' )
42
+
43
+ generator.should_not be_nil
44
+ end
45
+
46
+ it "should generate template .xls file from model" do
47
+
48
+ expect = result_file('project_template_spec.xls')
49
+
50
+ gen = ExcelGenerator.new( expect )
51
+
52
+ gen.generate(Project)
53
+
54
+ File.exists?(expect).should be_true
55
+
56
+ puts "Can manually check file @ #{expect}"
57
+ end
58
+
59
+ it "should export a simple model to .xls spreedsheet" do
60
+
61
+ Project.create( :value_as_string => 'Value as Text', :value_as_boolean => true, :value_as_double => 75.672)
62
+ #001 Demo string blah blah 2011-02-14 1.00 320.00
63
+
64
+ expect= result_file('simple_export_spec.xls')
65
+
66
+ gen = ExcelGenerator.new(expect)
67
+
68
+ items = Project.all
69
+
70
+ gen.export(items)
71
+
72
+ File.exists?(expect).should be_true
73
+
74
+ end
75
+
76
+ end
77
+ else
78
+ puts "WARNING: skipped excel_generator_spec : Requires JRUBY - JExcelFile requires JAVA"
79
79
  end # jruby
@@ -1,224 +1,224 @@
1
- # Copyright:: (c) Autotelik Media Ltd 2011
2
- # Author :: Tom Statter
3
- # Date :: Aug 2011
4
- # License:: MIT
5
- #
6
- # Details:: Specs for Excel aspect of Active Record Loader
7
- #
8
- require File.dirname(__FILE__) + '/spec_helper'
9
-
10
- if(Guards::jruby?)
11
-
12
- require 'erb'
13
- require 'excel_loader'
14
-
15
- include DataShift
16
-
17
- describe 'Excel Loader' do
18
-
19
- before(:all) do
20
- db_connect( 'test_file' ) # , test_memory, test_mysql
21
-
22
- # load our test model definitions - Project etc
23
- require File.join($DataShiftFixturePath, 'test_model_defs')
24
-
25
- # handle migration changes or reset of test DB
26
- migrate_up
27
-
28
- db_clear() # todo read up about proper transactional fixtures
29
-
30
-
31
- Project = Project
32
- @assoc_klazz = Category
33
- end
34
-
35
- before(:each) do
36
-
37
- Project.delete_all
38
-
39
- %w{category_001 category_002 category_003 category_004 category_005}.each do |cat|
40
- @assoc_klazz.find_or_create_by_reference(cat)
41
- end
42
-
43
-
44
- MethodDictionary.clear
45
- MethodDictionary.find_operators( Project )
46
- MethodDictionary.find_operators( @assoc_klazz )
47
- end
48
-
49
- it "should be able to create a new excel loader and load object" do
50
- loader = ExcelLoader.new( Project)
51
-
52
- loader.load_object.should_not be_nil
53
- loader.load_object.should be_is_a(Project)
54
- loader.load_object.new_record?.should be_true
55
- end
56
-
57
- it "should process a simple .xls spreedsheet" do
58
-
59
- loader = ExcelLoader.new(Project)
60
-
61
- count = Project.count
62
- loader.perform_load( $DataShiftFixturePath + '/SimpleProjects.xls')
63
-
64
- loader.loaded_count.should == (Project.count - count)
65
-
66
- end
67
-
68
- it "should process multiple associationss from single column" do
69
-
70
- Project.find_by_title('001').should be_nil
71
- count = Project.count
72
-
73
- loader = ExcelLoader.new(Project)
74
-
75
- loader.perform_load( $DataShiftFixturePath + '/ProjectsSingleCategories.xls')
76
-
77
- loader.loaded_count.should be > 3
78
- loader.loaded_count.should == (Project.count - count)
79
-
80
- {'001' => 2, '002' => 1, '003' => 3, '099' => 0 }.each do|title, expected|
81
- project = Project.find_by_title(title)
82
-
83
- project.should_not be_nil
84
- puts "#{project.inspect} [#{project.categories.size}]"
85
-
86
- project.should have(expected).categories
87
- end
88
- end
89
-
90
- it "should process multiple associations in excel spreedsheet" do
91
-
92
- loader = ExcelLoader.new(Project)
93
-
94
- count = Project.count
95
- loader.perform_load( $DataShiftFixturePath + '/ProjectsMultiCategories.xls')
96
-
97
- loader.loaded_count.should == (Project.count - count)
98
-
99
- {'004' => 3, '005' => 1, '006' => 0, '007' => 1 }.each do|title, expected|
100
- project = Project.find_by_title(title)
101
-
102
- project.should_not be_nil
103
-
104
- project.should have(expected).categories
105
- end
106
-
107
- end
108
-
109
- it "should process multiple associations with lookup specified in column from excel spreedsheet", :fail => true do
110
-
111
- loader = ExcelLoader.new(Project)
112
-
113
- count = Project.count
114
- loader.perform_load( $DataShiftFixturePath + '/ProjectsMultiCategoriesHeaderLookup.xls')
115
-
116
- loader.loaded_count.should == (Project.count - count)
117
-
118
- {'004' => 4, '005' => 1, '006' => 0, '007' => 1 }.each do|title, expected|
119
- project = Project.find_by_title(title)
120
-
121
- project.should_not be_nil
122
-
123
- project.should have(expected).categories
124
- end
125
-
126
- end
127
-
128
- it "should process excel spreedsheet with extra undefined columns" do
129
- loader = ExcelLoader.new(Project)
130
- lambda {loader.perform_load( ifixture_file('BadAssociationName.xls') ) }.should_not raise_error
131
- end
132
-
133
- it "should NOT process excel spreedsheet with extra undefined columns when strict mode" do
134
- loader = ExcelLoader.new(Project)
135
- expect {loader.perform_load( ifixture_file('BadAssociationName.xls'), :strict => true)}.to raise_error(MappingDefinitionError)
136
- end
137
-
138
- it "should raise an error when mandatory columns missing" do
139
- loader = ExcelLoader.new(Project)
140
- expect {loader.perform_load($DataShiftFixturePath + '/ProjectsMultiCategories.xls', :mandatory => ['not_an_option', 'must_be_there'] )}.to raise_error(DataShift::MissingMandatoryError)
141
- end
142
-
143
- it "should provide facility to set default values", :focus => true do
144
- loader = ExcelLoader.new(Project)
145
-
146
- loader.set_default_value('value_as_string', 'some default text' )
147
- loader.set_default_value('value_as_double', 45.467 )
148
- loader.set_default_value('value_as_boolean', true )
149
-
150
- texpected = Time.now.to_s(:db)
151
-
152
- loader.set_default_value('value_as_datetime', texpected )
153
-
154
- #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
155
-
156
- loader.perform_load($DataShiftFixturePath + '/ProjectsSingleCategories.xls')
157
-
158
- p = Project.find_by_title( '099' )
159
-
160
- p.should_not be_nil
161
-
162
- p.value_as_string.should == 'some default text'
163
- p.value_as_double.should == 45.467
164
- p.value_as_boolean.should == true
165
- p.value_as_datetime.should == texpected
166
-
167
- p_no_defs = Project.first
168
-
169
- p_no_defs.value_as_string.should_not == 'some default text'
170
- p_no_defs.value_as_double.should_not == 45.467
171
- p_no_defs.value_as_datetime.should_not == texpected
172
-
173
- end
174
-
175
- it "should provide facility to set pre and post fix values" do
176
- loader = ExcelLoader.new(Project)
177
-
178
- loader.set_prefix('value_as_string', 'myprefix' )
179
- loader.set_postfix('value_as_string', 'my post fix' )
180
-
181
- #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
182
-
183
- loader.perform_load($DataShiftFixturePath + '/ProjectsSingleCategories.xls')
184
-
185
- p = Project.find_by_title( '001' )
186
-
187
- p.should_not be_nil
188
-
189
- p.value_as_string.should == 'myprefixDemo stringmy post fix'
190
- end
191
-
192
- it "should provide facility to set default values via YAML configuration", :excel => true do
193
- loader = ExcelLoader.new(Project)
194
-
195
- loader.configure_from( File.join($DataShiftFixturePath, 'ProjectsDefaults.yml') )
196
-
197
-
198
- loader.perform_load( File.join($DataShiftFixturePath, 'ProjectsSingleCategories.xls') )
199
-
200
- p = Project.find_by_title( '099' )
201
-
202
- p.should_not be_nil
203
-
204
- p.value_as_string.should == "Default Project Value"
205
- end
206
-
207
-
208
- it "should provide facility to over ride values via YAML configuration", :excel => true do
209
- loader = ExcelLoader.new(Project)
210
-
211
- loader.configure_from( File.join($DataShiftFixturePath, 'ProjectsDefaults.yml') )
212
-
213
-
214
- loader.perform_load( File.join($DataShiftFixturePath, 'ProjectsSingleCategories.xls') )
215
-
216
- Project.all.each {|p| p.value_as_double.should == 99.23546 }
217
- end
218
-
219
-
220
- end
221
-
222
- else
223
- puts "WARNING: skipped excel_loader_spec : Requires JRUBY - JExcelFile requires JAVA"
1
+ # Copyright:: (c) Autotelik Media Ltd 2011
2
+ # Author :: Tom Statter
3
+ # Date :: Aug 2011
4
+ # License:: MIT
5
+ #
6
+ # Details:: Specs for Excel aspect of Active Record Loader
7
+ #
8
+ require File.dirname(__FILE__) + '/spec_helper'
9
+
10
+ if(Guards::jruby?)
11
+
12
+ require 'erb'
13
+ require 'excel_loader'
14
+
15
+ include DataShift
16
+
17
+ describe 'Excel Loader' do
18
+
19
+ before(:all) do
20
+ db_connect( 'test_file' ) # , test_memory, test_mysql
21
+
22
+ # load our test model definitions - Project etc
23
+ require File.join($DataShiftFixturePath, 'test_model_defs')
24
+
25
+ # handle migration changes or reset of test DB
26
+ migrate_up
27
+
28
+ db_clear() # todo read up about proper transactional fixtures
29
+
30
+
31
+ Project = Project
32
+ @assoc_klazz = Category
33
+ end
34
+
35
+ before(:each) do
36
+
37
+ Project.delete_all
38
+
39
+ %w{category_001 category_002 category_003 category_004 category_005}.each do |cat|
40
+ @assoc_klazz.find_or_create_by_reference(cat)
41
+ end
42
+
43
+
44
+ MethodDictionary.clear
45
+ MethodDictionary.find_operators( Project )
46
+ MethodDictionary.find_operators( @assoc_klazz )
47
+ end
48
+
49
+ it "should be able to create a new excel loader and load object" do
50
+ loader = ExcelLoader.new( Project)
51
+
52
+ loader.load_object.should_not be_nil
53
+ loader.load_object.should be_is_a(Project)
54
+ loader.load_object.new_record?.should be_true
55
+ end
56
+
57
+ it "should process a simple .xls spreedsheet" do
58
+
59
+ loader = ExcelLoader.new(Project)
60
+
61
+ count = Project.count
62
+ loader.perform_load( $DataShiftFixturePath + '/SimpleProjects.xls')
63
+
64
+ loader.loaded_count.should == (Project.count - count)
65
+
66
+ end
67
+
68
+ it "should process multiple associationss from single column" do
69
+
70
+ Project.find_by_title('001').should be_nil
71
+ count = Project.count
72
+
73
+ loader = ExcelLoader.new(Project)
74
+
75
+ loader.perform_load( $DataShiftFixturePath + '/ProjectsSingleCategories.xls')
76
+
77
+ loader.loaded_count.should be > 3
78
+ loader.loaded_count.should == (Project.count - count)
79
+
80
+ {'001' => 2, '002' => 1, '003' => 3, '099' => 0 }.each do|title, expected|
81
+ project = Project.find_by_title(title)
82
+
83
+ project.should_not be_nil
84
+ puts "#{project.inspect} [#{project.categories.size}]"
85
+
86
+ project.should have(expected).categories
87
+ end
88
+ end
89
+
90
+ it "should process multiple associations in excel spreedsheet" do
91
+
92
+ loader = ExcelLoader.new(Project)
93
+
94
+ count = Project.count
95
+ loader.perform_load( $DataShiftFixturePath + '/ProjectsMultiCategories.xls')
96
+
97
+ loader.loaded_count.should == (Project.count - count)
98
+
99
+ {'004' => 3, '005' => 1, '006' => 0, '007' => 1 }.each do|title, expected|
100
+ project = Project.find_by_title(title)
101
+
102
+ project.should_not be_nil
103
+
104
+ project.should have(expected).categories
105
+ end
106
+
107
+ end
108
+
109
+ it "should process multiple associations with lookup specified in column from excel spreedsheet", :fail => true do
110
+
111
+ loader = ExcelLoader.new(Project)
112
+
113
+ count = Project.count
114
+ loader.perform_load( $DataShiftFixturePath + '/ProjectsMultiCategoriesHeaderLookup.xls')
115
+
116
+ loader.loaded_count.should == (Project.count - count)
117
+
118
+ {'004' => 4, '005' => 1, '006' => 0, '007' => 1 }.each do|title, expected|
119
+ project = Project.find_by_title(title)
120
+
121
+ project.should_not be_nil
122
+
123
+ project.should have(expected).categories
124
+ end
125
+
126
+ end
127
+
128
+ it "should process excel spreedsheet with extra undefined columns" do
129
+ loader = ExcelLoader.new(Project)
130
+ lambda {loader.perform_load( ifixture_file('BadAssociationName.xls') ) }.should_not raise_error
131
+ end
132
+
133
+ it "should NOT process excel spreedsheet with extra undefined columns when strict mode" do
134
+ loader = ExcelLoader.new(Project)
135
+ expect {loader.perform_load( ifixture_file('BadAssociationName.xls'), :strict => true)}.to raise_error(MappingDefinitionError)
136
+ end
137
+
138
+ it "should raise an error when mandatory columns missing" do
139
+ loader = ExcelLoader.new(Project)
140
+ expect {loader.perform_load($DataShiftFixturePath + '/ProjectsMultiCategories.xls', :mandatory => ['not_an_option', 'must_be_there'] )}.to raise_error(DataShift::MissingMandatoryError)
141
+ end
142
+
143
+ it "should provide facility to set default values", :focus => true do
144
+ loader = ExcelLoader.new(Project)
145
+
146
+ loader.set_default_value('value_as_string', 'some default text' )
147
+ loader.set_default_value('value_as_double', 45.467 )
148
+ loader.set_default_value('value_as_boolean', true )
149
+
150
+ texpected = Time.now.to_s(:db)
151
+
152
+ loader.set_default_value('value_as_datetime', texpected )
153
+
154
+ #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
155
+
156
+ loader.perform_load($DataShiftFixturePath + '/ProjectsSingleCategories.xls')
157
+
158
+ p = Project.find_by_title( '099' )
159
+
160
+ p.should_not be_nil
161
+
162
+ p.value_as_string.should == 'some default text'
163
+ p.value_as_double.should == 45.467
164
+ p.value_as_boolean.should == true
165
+ p.value_as_datetime.should == texpected
166
+
167
+ p_no_defs = Project.first
168
+
169
+ p_no_defs.value_as_string.should_not == 'some default text'
170
+ p_no_defs.value_as_double.should_not == 45.467
171
+ p_no_defs.value_as_datetime.should_not == texpected
172
+
173
+ end
174
+
175
+ it "should provide facility to set pre and post fix values" do
176
+ loader = ExcelLoader.new(Project)
177
+
178
+ loader.set_prefix('value_as_string', 'myprefix' )
179
+ loader.set_postfix('value_as_string', 'my post fix' )
180
+
181
+ #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
182
+
183
+ loader.perform_load($DataShiftFixturePath + '/ProjectsSingleCategories.xls')
184
+
185
+ p = Project.find_by_title( '001' )
186
+
187
+ p.should_not be_nil
188
+
189
+ p.value_as_string.should == 'myprefixDemo stringmy post fix'
190
+ end
191
+
192
+ it "should provide facility to set default values via YAML configuration", :excel => true do
193
+ loader = ExcelLoader.new(Project)
194
+
195
+ loader.configure_from( File.join($DataShiftFixturePath, 'ProjectsDefaults.yml') )
196
+
197
+
198
+ loader.perform_load( File.join($DataShiftFixturePath, 'ProjectsSingleCategories.xls') )
199
+
200
+ p = Project.find_by_title( '099' )
201
+
202
+ p.should_not be_nil
203
+
204
+ p.value_as_string.should == "Default Project Value"
205
+ end
206
+
207
+
208
+ it "should provide facility to over ride values via YAML configuration", :excel => true do
209
+ loader = ExcelLoader.new(Project)
210
+
211
+ loader.configure_from( File.join($DataShiftFixturePath, 'ProjectsDefaults.yml') )
212
+
213
+
214
+ loader.perform_load( File.join($DataShiftFixturePath, 'ProjectsSingleCategories.xls') )
215
+
216
+ Project.all.each {|p| p.value_as_double.should == 99.23546 }
217
+ end
218
+
219
+
220
+ end
221
+
222
+ else
223
+ puts "WARNING: skipped excel_loader_spec : Requires JRUBY - JExcelFile requires JAVA"
224
224
  end # jruby