datashift 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/.document +5 -5
  2. data/Gemfile +28 -25
  3. data/LICENSE.txt +26 -26
  4. data/README.markdown +302 -285
  5. data/README.rdoc +19 -19
  6. data/Rakefile +93 -95
  7. data/VERSION +5 -5
  8. data/datashift.gemspec +162 -178
  9. data/lib/applications/jruby/jexcel_file.rb +396 -396
  10. data/lib/applications/jruby/word.rb +79 -79
  11. data/lib/datashift.rb +152 -113
  12. data/lib/datashift/exceptions.rb +11 -11
  13. data/lib/datashift/file_definitions.rb +353 -353
  14. data/lib/datashift/mapping_file_definitions.rb +87 -87
  15. data/lib/datashift/method_detail.rb +236 -236
  16. data/lib/datashift/method_mapper.rb +256 -256
  17. data/lib/generators/csv_generator.rb +36 -36
  18. data/lib/generators/excel_generator.rb +121 -121
  19. data/lib/generators/generator_base.rb +13 -13
  20. data/lib/helpers/core_ext/to_b.rb +24 -24
  21. data/lib/helpers/spree_helper.rb +131 -131
  22. data/lib/java/poi-3.7/LICENSE +507 -507
  23. data/lib/java/poi-3.7/NOTICE +21 -21
  24. data/lib/java/poi-3.7/RELEASE_NOTES.txt +115 -115
  25. data/lib/loaders/csv_loader.rb +98 -98
  26. data/lib/loaders/excel_loader.rb +154 -149
  27. data/lib/loaders/loader_base.rb +403 -331
  28. data/lib/loaders/spreadsheet_loader.rb +136 -136
  29. data/lib/loaders/spree/image_loader.rb +45 -45
  30. data/lib/loaders/spree/product_loader.rb +224 -224
  31. data/spec/csv_loader_spec.rb +30 -30
  32. data/spec/datashift_spec.rb +26 -26
  33. data/spec/db/migrate/20110803201325_create_test_bed.rb +85 -85
  34. data/spec/excel_generator_spec.rb +78 -78
  35. data/spec/excel_loader_spec.rb +204 -176
  36. data/spec/file_definitions.rb +141 -141
  37. data/spec/fixtures/.~lock.ProjectsSingleCategories.xls# +1 -0
  38. data/spec/fixtures/ProjectsDefaults.yml +29 -0
  39. data/spec/fixtures/config/database.yml +24 -24
  40. data/spec/fixtures/interact_models_db.sqlite +0 -0
  41. data/spec/fixtures/interact_spree_db.sqlite +0 -0
  42. data/spec/fixtures/negative/SpreeProdMiss1Mandatory.csv +4 -4
  43. data/spec/fixtures/negative/SpreeProdMissManyMandatory.csv +4 -4
  44. data/spec/fixtures/spree/SpreeProducts.csv +4 -4
  45. data/spec/fixtures/spree/SpreeProductsMultiColumn.csv +4 -4
  46. data/spec/fixtures/spree/SpreeProductsSimple.csv +4 -4
  47. data/spec/fixtures/spree/SpreeZoneExample.csv +5 -5
  48. data/spec/fixtures/test_model_defs.rb +57 -57
  49. data/spec/loader_spec.rb +120 -120
  50. data/spec/method_mapper_spec.rb +237 -237
  51. data/spec/spec_helper.rb +115 -115
  52. data/spec/spree_generator_spec.rb +64 -64
  53. data/spec/spree_loader_spec.rb +310 -310
  54. data/spec/spree_method_mapping_spec.rb +214 -214
  55. data/tasks/config/seed_fu_product_template.erb +15 -15
  56. data/tasks/config/tidy_config.txt +12 -12
  57. data/tasks/db_tasks.rake +65 -64
  58. data/tasks/excel_generator.rake +78 -78
  59. data/tasks/file_tasks.rake +36 -36
  60. data/tasks/import/csv.rake +49 -49
  61. data/tasks/import/excel.rake +71 -66
  62. data/tasks/spree/image_load.rake +108 -108
  63. data/tasks/spree/product_loader.rake +43 -43
  64. data/tasks/word_to_seedfu.rake +166 -166
  65. data/test/helper.rb +18 -18
  66. data/test/test_interact.rb +7 -7
  67. metadata +7 -38
  68. data/Gemfile.lock +0 -211
  69. data/bin/autospec +0 -16
  70. data/bin/convert_to_should_syntax +0 -16
  71. data/bin/erubis +0 -16
  72. data/bin/htmldiff +0 -16
  73. data/bin/jeweler +0 -16
  74. data/bin/ldiff +0 -16
  75. data/bin/nokogiri +0 -16
  76. data/bin/rackup +0 -16
  77. data/bin/rails +0 -16
  78. data/bin/rake +0 -16
  79. data/bin/rake2thor +0 -16
  80. data/bin/ri +0 -16
  81. data/bin/rspec +0 -16
  82. data/bin/spree +0 -16
  83. data/bin/thor +0 -16
  84. data/bin/tilt +0 -16
  85. data/bin/tt +0 -16
@@ -1,31 +1,31 @@
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
- require 'erb'
11
- require 'excel_loader'
12
-
13
- describe 'CSV Loader' do
14
-
15
- before(:all) do
16
-
17
- # load our test model definitions - Project etc
18
- require File.join($DataShiftFixturePath, 'test_model_defs')
19
-
20
- db_connect( 'test_file' ) # , test_memory, test_mysql
21
- migrate_up
22
- @klazz = Project
23
- end
24
-
25
- before(:each) do
26
- MethodMapper.clear
27
- MethodMapper.find_operators( @klazz )
28
- MethodMapper.find_operators( @assoc_klazz )
29
- end
30
-
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
+ require 'erb'
11
+ require 'excel_loader'
12
+
13
+ describe 'CSV Loader' do
14
+
15
+ before(:all) do
16
+
17
+ # load our test model definitions - Project etc
18
+ require File.join($DataShiftFixturePath, 'test_model_defs')
19
+
20
+ db_connect( 'test_file' ) # , test_memory, test_mysql
21
+ migrate_up
22
+ @klazz = Project
23
+ end
24
+
25
+ before(:each) do
26
+ MethodMapper.clear
27
+ MethodMapper.find_operators( @klazz )
28
+ MethodMapper.find_operators( @assoc_klazz )
29
+ end
30
+
31
31
  end
@@ -1,27 +1,27 @@
1
- # Copyright:: (c) Autotelik Media Ltd 2011
2
- # Author :: Tom Statter
3
- # Date :: Oct 2011
4
- # License:: MIT
5
- #
6
- # Details:: Specs for high level apsects of DataShift library
7
- #
8
- require File.dirname(__FILE__) + '/spec_helper'
9
-
10
- describe 'DataShift' do
11
-
12
- before(:each) do
13
- end
14
-
15
- it "should provide gem version" do
16
- DataShift::gem_version
17
- end
18
-
19
- it "should provide gem name" do
20
- DataShift::gem_name.should == 'datashift'
21
- end
22
-
23
- it "should provide root_path" do
24
- DataShift.root_path.should_not be_empty
25
- end
26
-
1
+ # Copyright:: (c) Autotelik Media Ltd 2011
2
+ # Author :: Tom Statter
3
+ # Date :: Oct 2011
4
+ # License:: MIT
5
+ #
6
+ # Details:: Specs for high level apsects of DataShift library
7
+ #
8
+ require File.dirname(__FILE__) + '/spec_helper'
9
+
10
+ describe 'DataShift' do
11
+
12
+ before(:each) do
13
+ end
14
+
15
+ it "should provide gem version" do
16
+ DataShift::gem_version
17
+ end
18
+
19
+ it "should provide gem name" do
20
+ DataShift::gem_name.should == 'datashift'
21
+ end
22
+
23
+ it "should provide root_path" do
24
+ DataShift.root_path.should_not be_empty
25
+ end
26
+
27
27
  end
@@ -1,85 +1,85 @@
1
- # Author :: Tom Statter
2
- # Date :: Aug 2011
3
- # License:: MIT
4
- #
5
- # Details:: Migration to create a test database that exercises all aspects of Active Record models
6
- #
7
-
8
- class CreateTestBed < ActiveRecord::Migration
9
-
10
- def self.up
11
-
12
- # has many :milestones
13
- create_table :projects do |t|
14
- t.string :title
15
- t.string :value_as_string
16
- t.text :value_as_text
17
- t.boolean :value_as_boolean, :default => false
18
- t.datetime :value_as_datetime, :default => nil
19
- t.integer :value_as_integer, :default => 0
20
- t.decimal :value_as_double, :precision => 8, :scale => 2, :default => 0.0
21
- t.timestamps
22
- end
23
-
24
- # belongs_to :project, project => has_many
25
- create_table :milestones do |t|
26
- t.string :name
27
- t.datetime :datetime, :default => nil
28
- t.decimal :cost, :precision => 8, :scale => 2, :default => 0.0
29
- t.references :project
30
- t.timestamps
31
- end
32
-
33
- # belongs_to :project, project => has_one
34
- create_table :owners do |t|
35
- t.string :name
36
- t.references :project
37
- t.timestamps
38
- end
39
-
40
- # has_belongs_to_many :project
41
- create_table :categories do |t|
42
- t.string :reference
43
- t.timestamps
44
- end
45
-
46
- # testing has_belongs_to_many (hence no id)
47
- create_table :categories_projects, :id => false do |t|
48
- t.references :category
49
- t.references :project
50
- end
51
-
52
- create_table :versions do |t|
53
- t.string :name
54
- t.timestamps
55
- end
56
-
57
- # testing project has_many release + versions :through
58
- create_table :loader_releases do |t|
59
- t.string :name
60
- t.references :project
61
- t.references :version
62
- t.timestamps
63
- end
64
-
65
-
66
- create_table :long_and_complex_table_linked_to_versions do |t|
67
- t.references :version
68
- end
69
-
70
- create_table :empties do |t|
71
- end
72
-
73
- end
74
-
75
- def self.down
76
- drop_table :projects
77
- drop_table :categories
78
- drop_table :loader_releases
79
- drop_table :versions
80
- drop_table :categories_projectss
81
- drop_table :milestones
82
- drop_table :long_and_complex_table_linked_to_versions
83
- drop_table :empties
84
- end
85
- end
1
+ # Author :: Tom Statter
2
+ # Date :: Aug 2011
3
+ # License:: MIT
4
+ #
5
+ # Details:: Migration to create a test database that exercises all aspects of Active Record models
6
+ #
7
+
8
+ class CreateTestBed < ActiveRecord::Migration
9
+
10
+ def self.up
11
+
12
+ # has many :milestones
13
+ create_table :projects do |t|
14
+ t.string :title
15
+ t.string :value_as_string
16
+ t.text :value_as_text
17
+ t.boolean :value_as_boolean, :default => false
18
+ t.datetime :value_as_datetime, :default => nil
19
+ t.integer :value_as_integer, :default => 0
20
+ t.decimal :value_as_double, :precision => 8, :scale => 2, :default => 0.0
21
+ t.timestamps
22
+ end
23
+
24
+ # belongs_to :project, project => has_many
25
+ create_table :milestones do |t|
26
+ t.string :name
27
+ t.datetime :datetime, :default => nil
28
+ t.decimal :cost, :precision => 8, :scale => 2, :default => 0.0
29
+ t.references :project
30
+ t.timestamps
31
+ end
32
+
33
+ # belongs_to :project, project => has_one
34
+ create_table :owners do |t|
35
+ t.string :name
36
+ t.references :project
37
+ t.timestamps
38
+ end
39
+
40
+ # has_belongs_to_many :project
41
+ create_table :categories do |t|
42
+ t.string :reference
43
+ t.timestamps
44
+ end
45
+
46
+ # testing has_belongs_to_many (hence no id)
47
+ create_table :categories_projects, :id => false do |t|
48
+ t.references :category
49
+ t.references :project
50
+ end
51
+
52
+ create_table :versions do |t|
53
+ t.string :name
54
+ t.timestamps
55
+ end
56
+
57
+ # testing project has_many release + versions :through
58
+ create_table :loader_releases do |t|
59
+ t.string :name
60
+ t.references :project
61
+ t.references :version
62
+ t.timestamps
63
+ end
64
+
65
+
66
+ create_table :long_and_complex_table_linked_to_versions do |t|
67
+ t.references :version
68
+ end
69
+
70
+ create_table :empties do |t|
71
+ end
72
+
73
+ end
74
+
75
+ def self.down
76
+ drop_table :projects
77
+ drop_table :categories
78
+ drop_table :loader_releases
79
+ drop_table :versions
80
+ drop_table :categories_projectss
81
+ drop_table :milestones
82
+ drop_table :long_and_complex_table_linked_to_versions
83
+ drop_table :empties
84
+ end
85
+ end
@@ -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
- MethodMapper.clear
36
- MethodMapper.find_operators( @klazz )
37
- MethodMapper.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
+ MethodMapper.clear
36
+ MethodMapper.find_operators( @klazz )
37
+ MethodMapper.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,177 +1,205 @@
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
- @klazz = 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}.each do |cat|
40
- @assoc_klazz.find_or_create_by_reference(cat)
41
- end
42
-
43
-
44
- MethodMapper.clear
45
- MethodMapper.find_operators( @klazz )
46
- MethodMapper.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( @klazz)
51
-
52
- loader.load_object.should_not be_nil
53
- loader.load_object.should be_is_a(@klazz)
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(@klazz)
60
-
61
- count = @klazz.count
62
- loader.perform_load( $DataShiftFixturePath + '/SimpleProjects.xls')
63
-
64
- loader.loaded_count.should == (@klazz.count - count)
65
-
66
- end
67
-
68
- it "should process multiple associationss from single column" do
69
-
70
- @klazz.find_by_title('001').should be_nil
71
- count = @klazz.count
72
-
73
- loader = ExcelLoader.new(@klazz)
74
-
75
- loader.perform_load( $DataShiftFixturePath + '/ProjectsSingleCategories.xls')
76
-
77
- loader.loaded_count.should be > 3
78
- loader.loaded_count.should == (@klazz.count - count)
79
-
80
- {'001' => 2, '002' => 1, '003' => 3, '099' => 0 }.each do|title, expected|
81
- project = @klazz.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 = @klazz.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 excel spreedsheet with extra undefined columns" do
110
- loader = ExcelLoader.new(Project)
111
- lambda {loader.perform_load( ifixture_file('BadAssociationName.xls') ) }.should_not raise_error
112
- end
113
-
114
- it "should NOT process excel spreedsheet with extra undefined columns when strict mode" do
115
- loader = ExcelLoader.new(Project)
116
- expect {loader.perform_load( ifixture_file('BadAssociationName.xls'), :strict => true)}.to raise_error(MappingDefinitionError)
117
- end
118
-
119
- it "should raise an error when mandatory columns missing" do
120
- loader = ExcelLoader.new(Project)
121
- expect {loader.perform_load($DataShiftFixturePath + '/ProjectsMultiCategories.xls', :mandatory => ['not_an_option', 'must_be_there'] )}.to raise_error(DataShift::MissingMandatoryError)
122
- end
123
-
124
- it "should provide facility to set default values", :focus => true do
125
- loader = ExcelLoader.new(Project)
126
-
127
- loader.set_default_value('value_as_string', 'some default text' )
128
- loader.set_default_value('value_as_double', 45.467 )
129
- loader.set_default_value('value_as_boolean', true )
130
-
131
- texpected = Time.now.to_s(:db)
132
-
133
- loader.set_default_value('value_as_datetime', texpected )
134
-
135
- #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
136
-
137
- loader.perform_load($DataShiftFixturePath + '/ProjectsSingleCategories.xls')
138
-
139
- p = Project.find_by_title( '099' )
140
-
141
- p.should_not be_nil
142
-
143
- p.value_as_string.should == 'some default text'
144
- p.value_as_double.should == 45.467
145
- p.value_as_boolean.should == true
146
- p.value_as_datetime.should == texpected
147
-
148
- p_no_defs = Project.first
149
-
150
- p_no_defs.value_as_string.should_not == 'some default text'
151
- p_no_defs.value_as_double.should_not == 45.467
152
- p_no_defs.value_as_datetime.should_not == texpected
153
-
154
- end
155
-
156
- it "should provide facility to set pre and post fix values", :focus => true do
157
- loader = ExcelLoader.new(Project)
158
-
159
- loader.set_prefix('value_as_string', 'myprefix' )
160
- loader.set_postfix('value_as_string', 'my post fix' )
161
-
162
- #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
163
-
164
- loader.perform_load($DataShiftFixturePath + '/ProjectsSingleCategories.xls')
165
-
166
- p = Project.find_by_title( '001' )
167
-
168
- p.should_not be_nil
169
-
170
- p.value_as_string.should == 'myprefixDemo stringmy post fix'
171
- end
172
-
173
- end
174
-
175
- else
176
- 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
+ @klazz = 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}.each do |cat|
40
+ @assoc_klazz.find_or_create_by_reference(cat)
41
+ end
42
+
43
+
44
+ MethodMapper.clear
45
+ MethodMapper.find_operators( @klazz )
46
+ MethodMapper.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( @klazz)
51
+
52
+ loader.load_object.should_not be_nil
53
+ loader.load_object.should be_is_a(@klazz)
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(@klazz)
60
+
61
+ count = @klazz.count
62
+ loader.perform_load( $DataShiftFixturePath + '/SimpleProjects.xls')
63
+
64
+ loader.loaded_count.should == (@klazz.count - count)
65
+
66
+ end
67
+
68
+ it "should process multiple associationss from single column" do
69
+
70
+ @klazz.find_by_title('001').should be_nil
71
+ count = @klazz.count
72
+
73
+ loader = ExcelLoader.new(@klazz)
74
+
75
+ loader.perform_load( $DataShiftFixturePath + '/ProjectsSingleCategories.xls')
76
+
77
+ loader.loaded_count.should be > 3
78
+ loader.loaded_count.should == (@klazz.count - count)
79
+
80
+ {'001' => 2, '002' => 1, '003' => 3, '099' => 0 }.each do|title, expected|
81
+ project = @klazz.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 = @klazz.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 excel spreedsheet with extra undefined columns" do
110
+ loader = ExcelLoader.new(Project)
111
+ lambda {loader.perform_load( ifixture_file('BadAssociationName.xls') ) }.should_not raise_error
112
+ end
113
+
114
+ it "should NOT process excel spreedsheet with extra undefined columns when strict mode" do
115
+ loader = ExcelLoader.new(Project)
116
+ expect {loader.perform_load( ifixture_file('BadAssociationName.xls'), :strict => true)}.to raise_error(MappingDefinitionError)
117
+ end
118
+
119
+ it "should raise an error when mandatory columns missing" do
120
+ loader = ExcelLoader.new(Project)
121
+ expect {loader.perform_load($DataShiftFixturePath + '/ProjectsMultiCategories.xls', :mandatory => ['not_an_option', 'must_be_there'] )}.to raise_error(DataShift::MissingMandatoryError)
122
+ end
123
+
124
+ it "should provide facility to set default values", :focus => true do
125
+ loader = ExcelLoader.new(Project)
126
+
127
+ loader.set_default_value('value_as_string', 'some default text' )
128
+ loader.set_default_value('value_as_double', 45.467 )
129
+ loader.set_default_value('value_as_boolean', true )
130
+
131
+ texpected = Time.now.to_s(:db)
132
+
133
+ loader.set_default_value('value_as_datetime', texpected )
134
+
135
+ #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
136
+
137
+ loader.perform_load($DataShiftFixturePath + '/ProjectsSingleCategories.xls')
138
+
139
+ p = Project.find_by_title( '099' )
140
+
141
+ p.should_not be_nil
142
+
143
+ p.value_as_string.should == 'some default text'
144
+ p.value_as_double.should == 45.467
145
+ p.value_as_boolean.should == true
146
+ p.value_as_datetime.should == texpected
147
+
148
+ p_no_defs = Project.first
149
+
150
+ p_no_defs.value_as_string.should_not == 'some default text'
151
+ p_no_defs.value_as_double.should_not == 45.467
152
+ p_no_defs.value_as_datetime.should_not == texpected
153
+
154
+ end
155
+
156
+ it "should provide facility to set pre and post fix values" do
157
+ loader = ExcelLoader.new(Project)
158
+
159
+ loader.set_prefix('value_as_string', 'myprefix' )
160
+ loader.set_postfix('value_as_string', 'my post fix' )
161
+
162
+ #value_as_string Value as Text value as datetime value_as_boolean value_as_double category
163
+
164
+ loader.perform_load($DataShiftFixturePath + '/ProjectsSingleCategories.xls')
165
+
166
+ p = Project.find_by_title( '001' )
167
+
168
+ p.should_not be_nil
169
+
170
+ p.value_as_string.should == 'myprefixDemo stringmy post fix'
171
+ end
172
+
173
+ it "should provide facility to set default values via YAML configuration", :excel => true do
174
+ loader = ExcelLoader.new(Project)
175
+
176
+ loader.configure_from( File.join($DataShiftFixturePath, 'ProjectsDefaults.yml') )
177
+
178
+
179
+ loader.perform_load( File.join($DataShiftFixturePath, 'ProjectsSingleCategories.xls') )
180
+
181
+ p = Project.find_by_title( '099' )
182
+
183
+ p.should_not be_nil
184
+
185
+ p.value_as_string.should == "Default Project Value"
186
+ end
187
+
188
+
189
+ it "should provide facility to over ride values via YAML configuration", :excel => true do
190
+ loader = ExcelLoader.new(Project)
191
+
192
+ loader.configure_from( File.join($DataShiftFixturePath, 'ProjectsDefaults.yml') )
193
+
194
+
195
+ loader.perform_load( File.join($DataShiftFixturePath, 'ProjectsSingleCategories.xls') )
196
+
197
+ Project.all.each {|p| p.value_as_double.should == 99.23546 }
198
+ end
199
+
200
+
201
+ end
202
+
203
+ else
204
+ puts "WARNING: skipped excel_loader_spec : Requires JRUBY - JExcelFile requires JAVA"
177
205
  end # jruby