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.
- data/.document +5 -5
- data/LICENSE.txt +26 -26
- data/README.markdown +326 -305
- data/README.rdoc +19 -19
- data/Rakefile +86 -93
- data/VERSION +1 -1
- data/datashift.gemspec +163 -152
- data/lib/applications/jruby/jexcel_file.rb +410 -408
- data/lib/applications/jruby/word.rb +79 -79
- data/lib/datashift.rb +183 -152
- data/lib/datashift/exceptions.rb +11 -11
- data/lib/datashift/file_definitions.rb +353 -353
- data/lib/datashift/mapping_file_definitions.rb +87 -87
- data/lib/datashift/method_detail.rb +293 -275
- data/lib/datashift/method_dictionary.rb +208 -209
- data/lib/datashift/method_mapper.rb +90 -90
- data/lib/datashift/model_mapper.rb +27 -0
- data/lib/exporters/csv_exporter.rb +36 -0
- data/lib/exporters/excel_exporter.rb +116 -0
- data/lib/exporters/exporter_base.rb +15 -0
- data/lib/generators/csv_generator.rb +36 -36
- data/lib/generators/excel_generator.rb +106 -122
- data/lib/generators/generator_base.rb +13 -13
- data/lib/helpers/core_ext/to_b.rb +24 -24
- data/lib/helpers/rake_utils.rb +42 -0
- data/lib/helpers/spree_helper.rb +194 -153
- data/lib/java/poi-3.7/LICENSE +507 -507
- data/lib/java/poi-3.7/NOTICE +21 -21
- data/lib/java/poi-3.7/RELEASE_NOTES.txt +115 -115
- data/lib/loaders/csv_loader.rb +98 -98
- data/lib/loaders/excel_loader.rb +155 -155
- data/lib/loaders/loader_base.rb +420 -420
- data/lib/loaders/spreadsheet_loader.rb +136 -136
- data/lib/loaders/spree/image_loader.rb +67 -63
- data/lib/loaders/spree/product_loader.rb +289 -248
- data/lib/thor/generate_excel.thor +54 -0
- data/sandbox/app/controllers/application_controller.rb +3 -0
- data/sandbox/config/application.rb +43 -0
- data/sandbox/config/database.yml +34 -0
- data/sandbox/config/environment.rb +7 -0
- data/sandbox/config/environments/development.rb +30 -0
- data/spec/csv_loader_spec.rb +30 -30
- data/spec/datashift_spec.rb +26 -26
- data/spec/db/migrate/20110803201325_create_test_bed.rb +85 -85
- data/spec/excel_exporter_spec.rb +78 -78
- data/spec/excel_generator_spec.rb +78 -78
- data/spec/excel_loader_spec.rb +223 -223
- data/spec/file_definitions.rb +141 -141
- data/spec/fixtures/ProjectsDefaults.yml +29 -29
- data/spec/fixtures/config/database.yml +27 -27
- data/spec/fixtures/datashift_Spree_db.sqlite +0 -0
- data/spec/fixtures/datashift_test_models_db.sqlite +0 -0
- data/spec/fixtures/negative/SpreeProdMiss1Mandatory.csv +4 -4
- data/spec/fixtures/negative/SpreeProdMissManyMandatory.csv +4 -4
- data/spec/fixtures/spree/SpreeProducts.csv +4 -4
- data/spec/fixtures/spree/SpreeProducts.xls +0 -0
- data/spec/fixtures/spree/SpreeProductsMultiColumn.csv +4 -4
- data/spec/fixtures/spree/SpreeProductsMultiColumn.xls +0 -0
- data/spec/fixtures/spree/SpreeProductsSimple.csv +4 -4
- data/spec/fixtures/spree/SpreeProductsWithImages.csv +4 -4
- data/spec/fixtures/spree/SpreeZoneExample.csv +5 -5
- data/spec/fixtures/test_model_defs.rb +57 -57
- data/spec/loader_spec.rb +120 -120
- data/spec/method_dictionary_spec.rb +242 -242
- data/spec/method_mapper_spec.rb +41 -41
- data/spec/spec_helper.rb +154 -116
- data/spec/spree_exporter_spec.rb +67 -0
- data/spec/spree_generator_spec.rb +77 -64
- data/spec/spree_loader_spec.rb +363 -324
- data/spec/spree_method_mapping_spec.rb +218 -214
- data/tasks/config/seed_fu_product_template.erb +15 -15
- data/tasks/config/tidy_config.txt +12 -12
- data/tasks/{excel_generator.rake → export/excel_generator.rake} +101 -78
- data/tasks/file_tasks.rake +36 -36
- data/tasks/import/csv.rake +50 -49
- data/tasks/import/excel.rake +74 -71
- data/tasks/spree/image_load.rake +108 -108
- data/tasks/spree/product_loader.rake +43 -43
- data/tasks/word_to_seedfu.rake +166 -166
- data/test/helper.rb +18 -18
- data/test/test_interact.rb +7 -7
- metadata +16 -8
- data/datashift-0.1.0.gem +0 -0
- data/tasks/db_tasks.rake +0 -66
@@ -1,4 +1,4 @@
|
|
1
|
-
SKU,Name,Description,Available On, Price,CostPrice,product_properties,Taxons,Variants,count_on_hand,Images
|
2
|
-
DEMO_001,Demo Product for AR Loader,blah blah,14/02/2011,399.99,320,test_pp_001,Paintings|WaterColour,"mime_type:jpeg,PDF,PNG",12|6|7,spec/fixtures/images/DEMO_001_ror_bag.jpeg
|
3
|
-
DEMO_002,Demo Excel Load via Jruby,less blah,14/05/2011,100,30,test_pp_003,Paintings|Landscape|Oils,"mime_type:jpeg, PDF,PNG",5|4|2,spec/fixtures/images/DEMO_002_Powerstation.jpg
|
4
|
-
DEMO_003,Demo third row in future,more blah blah,01/07/2012,50.34,23.34,test_pp_002|test_pp_003:Example free value|yet_another_property,Drawings,"mime_type:jpeg,PDF,PNG|print_type:black_white,colour",12|4|7|12|18,spec/fixtures/images/DEMO_003_ror_mug.jpeg
|
1
|
+
SKU,Name,Description,Available On, Price,CostPrice,product_properties,Taxons,Variants,count_on_hand,Images
|
2
|
+
DEMO_001,Demo Product for AR Loader,blah blah,14/02/2011,399.99,320,test_pp_001,Paintings|WaterColour,"mime_type:jpeg,PDF,PNG",12|6|7,spec/fixtures/images/DEMO_001_ror_bag.jpeg
|
3
|
+
DEMO_002,Demo Excel Load via Jruby,less blah,14/05/2011,100,30,test_pp_003,Paintings|Landscape|Oils,"mime_type:jpeg, PDF,PNG",5|4|2,spec/fixtures/images/DEMO_002_Powerstation.jpg
|
4
|
+
DEMO_003,Demo third row in future,more blah blah,01/07/2012,50.34,23.34,test_pp_002|test_pp_003:Example free value|yet_another_property,Drawings,"mime_type:jpeg,PDF,PNG|print_type:black_white,colour",12|4|7|12|18,spec/fixtures/images/DEMO_003_ror_mug.jpeg
|
@@ -1,5 +1,5 @@
|
|
1
|
-
"Name","Description"
|
2
|
-
"ROW","Rest of World"
|
3
|
-
"UK","United Kingdom"
|
4
|
-
"EUR","Europe"
|
5
|
-
"NA","North America"
|
1
|
+
"Name","Description"
|
2
|
+
"ROW","Rest of World"
|
3
|
+
"UK","United Kingdom"
|
4
|
+
"EUR","Europe"
|
5
|
+
"NA","North America"
|
@@ -1,57 +1,57 @@
|
|
1
|
-
|
2
|
-
class Project < ActiveRecord::Base
|
3
|
-
|
4
|
-
has_one :owner
|
5
|
-
|
6
|
-
has_many :milestones
|
7
|
-
|
8
|
-
has_many :loader_releases
|
9
|
-
has_many :versions, :through => :loader_releases
|
10
|
-
|
11
|
-
|
12
|
-
#has_many :release_versions, :through => :loader_releases, :source => :versions
|
13
|
-
|
14
|
-
has_and_belongs_to_many :categories
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
class Owner < ActiveRecord::Base
|
19
|
-
belongs_to :project
|
20
|
-
end
|
21
|
-
|
22
|
-
class Milestone < ActiveRecord::Base
|
23
|
-
belongs_to :project
|
24
|
-
#validate the name, cost
|
25
|
-
|
26
|
-
delegate :title, :title=, :to => :project
|
27
|
-
end
|
28
|
-
|
29
|
-
# had_and_belongs to join table
|
30
|
-
class Category < ActiveRecord::Base
|
31
|
-
has_and_belongs_to_many :projects
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
class Version < ActiveRecord::Base
|
36
|
-
has_many :releases
|
37
|
-
|
38
|
-
has_one :long_and_complex_table_linked_to_version
|
39
|
-
end
|
40
|
-
|
41
|
-
# Join Table with additional columns
|
42
|
-
class LoaderRelease < ActiveRecord::Base
|
43
|
-
|
44
|
-
belongs_to :project
|
45
|
-
belongs_to :version
|
46
|
-
|
47
|
-
#validate the name
|
48
|
-
end
|
49
|
-
|
50
|
-
class Empty < ActiveRecord::Base
|
51
|
-
end
|
52
|
-
|
53
|
-
# Join Table with additional columns
|
54
|
-
class LongAndComplexTableLinkedToVersion < ActiveRecord::Base
|
55
|
-
|
56
|
-
belongs_to :version
|
57
|
-
end
|
1
|
+
|
2
|
+
class Project < ActiveRecord::Base
|
3
|
+
|
4
|
+
has_one :owner
|
5
|
+
|
6
|
+
has_many :milestones
|
7
|
+
|
8
|
+
has_many :loader_releases
|
9
|
+
has_many :versions, :through => :loader_releases
|
10
|
+
|
11
|
+
|
12
|
+
#has_many :release_versions, :through => :loader_releases, :source => :versions
|
13
|
+
|
14
|
+
has_and_belongs_to_many :categories
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class Owner < ActiveRecord::Base
|
19
|
+
belongs_to :project
|
20
|
+
end
|
21
|
+
|
22
|
+
class Milestone < ActiveRecord::Base
|
23
|
+
belongs_to :project
|
24
|
+
#validate the name, cost
|
25
|
+
|
26
|
+
delegate :title, :title=, :to => :project
|
27
|
+
end
|
28
|
+
|
29
|
+
# had_and_belongs to join table
|
30
|
+
class Category < ActiveRecord::Base
|
31
|
+
has_and_belongs_to_many :projects
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
class Version < ActiveRecord::Base
|
36
|
+
has_many :releases
|
37
|
+
|
38
|
+
has_one :long_and_complex_table_linked_to_version
|
39
|
+
end
|
40
|
+
|
41
|
+
# Join Table with additional columns
|
42
|
+
class LoaderRelease < ActiveRecord::Base
|
43
|
+
|
44
|
+
belongs_to :project
|
45
|
+
belongs_to :version
|
46
|
+
|
47
|
+
#validate the name
|
48
|
+
end
|
49
|
+
|
50
|
+
class Empty < ActiveRecord::Base
|
51
|
+
end
|
52
|
+
|
53
|
+
# Join Table with additional columns
|
54
|
+
class LongAndComplexTableLinkedToVersion < ActiveRecord::Base
|
55
|
+
|
56
|
+
belongs_to :version
|
57
|
+
end
|
data/spec/loader_spec.rb
CHANGED
@@ -1,121 +1,121 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Aug 2011
|
4
|
-
# License:: MIT
|
5
|
-
#
|
6
|
-
# Details:: Specs for base class Loader
|
7
|
-
#
|
8
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
9
|
-
|
10
|
-
require 'erb'
|
11
|
-
|
12
|
-
describe 'Basic Loader' do
|
13
|
-
|
14
|
-
before(:all) do
|
15
|
-
db_connect( 'test_file' ) # , test_memory, test_mysql
|
16
|
-
|
17
|
-
# load our test model definitions - Project etc
|
18
|
-
require File.join($DataShiftFixturePath, 'test_model_defs')
|
19
|
-
|
20
|
-
migrate_up
|
21
|
-
@klazz = Project
|
22
|
-
end
|
23
|
-
|
24
|
-
before(:each) do
|
25
|
-
MethodDictionary.clear
|
26
|
-
MethodDictionary.find_operators( @klazz )
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should be able to create a new loader and load object" do
|
30
|
-
loader = LoaderBase.new( @klazz )
|
31
|
-
|
32
|
-
loader.load_object.should_not be_nil
|
33
|
-
loader.load_object.should be_is_a(@klazz)
|
34
|
-
loader.load_object.new_record?.should be_true
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should process a string field against an assigment method detail" do
|
38
|
-
|
39
|
-
loader = LoaderBase.new(Project)
|
40
|
-
|
41
|
-
column = 'Value As String'
|
42
|
-
row = 'Another Lazy fox '
|
43
|
-
|
44
|
-
loader.find_and_process(column, row)
|
45
|
-
|
46
|
-
loader.load_object.value_as_string.should == row
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should process a text field against an assigment method detail" do
|
50
|
-
|
51
|
-
loader = LoaderBase.new(Project)
|
52
|
-
|
53
|
-
column = :value_as_text
|
54
|
-
row = "Another Lazy fox\nJumped over something and bumped,\nHis head"
|
55
|
-
|
56
|
-
loader.find_and_process(column, row)
|
57
|
-
|
58
|
-
loader.load_object.value_as_text.should == row
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
it "should process a boolean field against an assigment method detail" do
|
63
|
-
|
64
|
-
loader = LoaderBase.new(Project)
|
65
|
-
|
66
|
-
column = :value_as_boolean
|
67
|
-
row = true
|
68
|
-
|
69
|
-
loader.find_and_process(column, row)
|
70
|
-
|
71
|
-
loader.load_object.value_as_boolean.should == row
|
72
|
-
|
73
|
-
row = 'false'
|
74
|
-
|
75
|
-
loader.find_and_process(column, row)
|
76
|
-
|
77
|
-
loader.load_object.value_as_boolean.should == false
|
78
|
-
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
it "should process a double field against an assigment operator" do
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should process various date formats against a date assigment operator" do
|
86
|
-
|
87
|
-
loader = LoaderBase.new(Project)
|
88
|
-
|
89
|
-
column = :value_as_datetime
|
90
|
-
|
91
|
-
loader.find_and_process(column, Time.now)
|
92
|
-
loader.load_object.value_as_datetime.should_not be_nil
|
93
|
-
|
94
|
-
loader.find_and_process(column, "2011-07-23")
|
95
|
-
loader.load_object.value_as_datetime.should_not be_nil
|
96
|
-
|
97
|
-
loader.find_and_process(column, "Sat Jul 23 09:01:56 +0100 2011")
|
98
|
-
loader.load_object.value_as_datetime.should_not be_nil
|
99
|
-
|
100
|
-
loader.find_and_process(column, Time.now.to_s(:db))
|
101
|
-
loader.load_object.value_as_datetime.should_not be_nil
|
102
|
-
|
103
|
-
loader.find_and_process(column, "Jul 23 2011 23:02:59")
|
104
|
-
loader.load_object.value_as_datetime.should_not be_nil
|
105
|
-
|
106
|
-
if(Guards.jruby?)
|
107
|
-
loader.find_and_process(column, "07/23/2011") # dd/mm/YYYY
|
108
|
-
loader.load_object.value_as_datetime.should_not be_nil
|
109
|
-
end
|
110
|
-
|
111
|
-
# bad casts - TODO - is this really an error needs raising ?
|
112
|
-
loader.find_and_process(column, "2011 07 23")
|
113
|
-
loader.load_object.value_as_datetime.should be_nil
|
114
|
-
|
115
|
-
|
116
|
-
loader.find_and_process(column, "2011-23-07")
|
117
|
-
loader.load_object.value_as_datetime.should be_nil
|
118
|
-
|
119
|
-
end
|
120
|
-
|
1
|
+
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
+
# Author :: Tom Statter
|
3
|
+
# Date :: Aug 2011
|
4
|
+
# License:: MIT
|
5
|
+
#
|
6
|
+
# Details:: Specs for base class Loader
|
7
|
+
#
|
8
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
9
|
+
|
10
|
+
require 'erb'
|
11
|
+
|
12
|
+
describe 'Basic Loader' do
|
13
|
+
|
14
|
+
before(:all) do
|
15
|
+
db_connect( 'test_file' ) # , test_memory, test_mysql
|
16
|
+
|
17
|
+
# load our test model definitions - Project etc
|
18
|
+
require File.join($DataShiftFixturePath, 'test_model_defs')
|
19
|
+
|
20
|
+
migrate_up
|
21
|
+
@klazz = Project
|
22
|
+
end
|
23
|
+
|
24
|
+
before(:each) do
|
25
|
+
MethodDictionary.clear
|
26
|
+
MethodDictionary.find_operators( @klazz )
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be able to create a new loader and load object" do
|
30
|
+
loader = LoaderBase.new( @klazz )
|
31
|
+
|
32
|
+
loader.load_object.should_not be_nil
|
33
|
+
loader.load_object.should be_is_a(@klazz)
|
34
|
+
loader.load_object.new_record?.should be_true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should process a string field against an assigment method detail" do
|
38
|
+
|
39
|
+
loader = LoaderBase.new(Project)
|
40
|
+
|
41
|
+
column = 'Value As String'
|
42
|
+
row = 'Another Lazy fox '
|
43
|
+
|
44
|
+
loader.find_and_process(column, row)
|
45
|
+
|
46
|
+
loader.load_object.value_as_string.should == row
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should process a text field against an assigment method detail" do
|
50
|
+
|
51
|
+
loader = LoaderBase.new(Project)
|
52
|
+
|
53
|
+
column = :value_as_text
|
54
|
+
row = "Another Lazy fox\nJumped over something and bumped,\nHis head"
|
55
|
+
|
56
|
+
loader.find_and_process(column, row)
|
57
|
+
|
58
|
+
loader.load_object.value_as_text.should == row
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should process a boolean field against an assigment method detail" do
|
63
|
+
|
64
|
+
loader = LoaderBase.new(Project)
|
65
|
+
|
66
|
+
column = :value_as_boolean
|
67
|
+
row = true
|
68
|
+
|
69
|
+
loader.find_and_process(column, row)
|
70
|
+
|
71
|
+
loader.load_object.value_as_boolean.should == row
|
72
|
+
|
73
|
+
row = 'false'
|
74
|
+
|
75
|
+
loader.find_and_process(column, row)
|
76
|
+
|
77
|
+
loader.load_object.value_as_boolean.should == false
|
78
|
+
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should process a double field against an assigment operator" do
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should process various date formats against a date assigment operator" do
|
86
|
+
|
87
|
+
loader = LoaderBase.new(Project)
|
88
|
+
|
89
|
+
column = :value_as_datetime
|
90
|
+
|
91
|
+
loader.find_and_process(column, Time.now)
|
92
|
+
loader.load_object.value_as_datetime.should_not be_nil
|
93
|
+
|
94
|
+
loader.find_and_process(column, "2011-07-23")
|
95
|
+
loader.load_object.value_as_datetime.should_not be_nil
|
96
|
+
|
97
|
+
loader.find_and_process(column, "Sat Jul 23 09:01:56 +0100 2011")
|
98
|
+
loader.load_object.value_as_datetime.should_not be_nil
|
99
|
+
|
100
|
+
loader.find_and_process(column, Time.now.to_s(:db))
|
101
|
+
loader.load_object.value_as_datetime.should_not be_nil
|
102
|
+
|
103
|
+
loader.find_and_process(column, "Jul 23 2011 23:02:59")
|
104
|
+
loader.load_object.value_as_datetime.should_not be_nil
|
105
|
+
|
106
|
+
if(Guards.jruby?)
|
107
|
+
loader.find_and_process(column, "07/23/2011") # dd/mm/YYYY
|
108
|
+
loader.load_object.value_as_datetime.should_not be_nil
|
109
|
+
end
|
110
|
+
|
111
|
+
# bad casts - TODO - is this really an error needs raising ?
|
112
|
+
loader.find_and_process(column, "2011 07 23")
|
113
|
+
loader.load_object.value_as_datetime.should be_nil
|
114
|
+
|
115
|
+
|
116
|
+
loader.find_and_process(column, "2011-23-07")
|
117
|
+
loader.load_object.value_as_datetime.should be_nil
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
121
|
end
|
@@ -1,243 +1,243 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Aug 2011
|
4
|
-
# License:: MIT
|
5
|
-
#
|
6
|
-
# Details:: Specs for MethodMapper aspect of Active Record Loader
|
7
|
-
# MethodMapper provides the bridge between 'strings' e.g column headings
|
8
|
-
# and a classes different types of assignment operators
|
9
|
-
#
|
10
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
11
|
-
|
12
|
-
describe 'Method Mapping' do
|
13
|
-
|
14
|
-
before(:all) do
|
15
|
-
db_connect( 'test_file' ) # , test_memory, test_mysql
|
16
|
-
|
17
|
-
# load our test model definitions - Project etc
|
18
|
-
require ifixture_file('test_model_defs')
|
19
|
-
|
20
|
-
migrate_up
|
21
|
-
end
|
22
|
-
|
23
|
-
before(:each) do
|
24
|
-
MethodDictionary.clear
|
25
|
-
MethodDictionary.find_operators( Project )
|
26
|
-
MethodDictionary.find_operators( Milestone )
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should store dictionary for multiple AR models" do
|
30
|
-
MethodDictionary.assignments.size.should == 2
|
31
|
-
MethodDictionary.has_many.size.should == 2
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should populate method dictionary for a given AR model" do
|
35
|
-
|
36
|
-
MethodDictionary.has_many.should_not be_empty
|
37
|
-
MethodDictionary.has_many[Project].should include('milestones')
|
38
|
-
|
39
|
-
MethodDictionary.assignments.should_not be_empty
|
40
|
-
MethodDictionary.assignments[Project].should include('id')
|
41
|
-
MethodDictionary.assignments[Project].should include('value_as_string')
|
42
|
-
MethodDictionary.assignments[Project].should include('value_as_text')
|
43
|
-
|
44
|
-
MethodDictionary.belongs_to.should_not be_empty
|
45
|
-
MethodDictionary.belongs_to[Project].should be_empty
|
46
|
-
|
47
|
-
|
48
|
-
MethodDictionary.column_types.should be_is_a(Hash)
|
49
|
-
MethodDictionary.column_types.should_not be_empty
|
50
|
-
MethodDictionary.column_types[Project].size.should == Project.columns.size
|
51
|
-
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should populate assigment members without the equivalent association names" do
|
56
|
-
|
57
|
-
# we should remove has-many & belongs_to from basic assignment set as they require a DB lookup
|
58
|
-
# or a Model.create call, not a simple assignment
|
59
|
-
|
60
|
-
MethodDictionary.assignments_for(Project).should_not include( MethodDictionary.belongs_to_for(Project) )
|
61
|
-
MethodDictionary.assignments_for(Project).should_not include( MethodDictionary.has_many_for(Project) )
|
62
|
-
end
|
63
|
-
|
64
|
-
|
65
|
-
it "should populate assignment operators for method details for different forms of a column name" do
|
66
|
-
|
67
|
-
MethodDictionary.build_method_details( Project )
|
68
|
-
|
69
|
-
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
70
|
-
|
71
|
-
method_details = MethodDictionary.find_method_detail( Project, format )
|
72
|
-
|
73
|
-
method_details.class.should == MethodDetail
|
74
|
-
|
75
|
-
method_details.operator.should == 'value_as_string'
|
76
|
-
method_details.operator_for(:assignment).should == 'value_as_string'
|
77
|
-
|
78
|
-
method_details.operator?('value_as_string').should be_true
|
79
|
-
method_details.operator?('blah_as_string').should be_false
|
80
|
-
|
81
|
-
method_details.operator_for(:belongs_to).should be_nil
|
82
|
-
method_details.operator_for(:has_many).should be_nil
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
# Note : Not all assignments will currently have a column type, for example
|
88
|
-
# those that are derived from a delegate_belongs_to
|
89
|
-
|
90
|
-
it "should populate column types for assignment operators in method details" do
|
91
|
-
|
92
|
-
MethodDictionary.build_method_details( Project )
|
93
|
-
|
94
|
-
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
95
|
-
|
96
|
-
method_details = MethodDictionary.find_method_detail( Project, format )
|
97
|
-
|
98
|
-
method_details.class.should == MethodDetail
|
99
|
-
|
100
|
-
method_details.col_type.should_not be_nil
|
101
|
-
method_details.col_type.name.should == 'value_as_string'
|
102
|
-
method_details.col_type.default.should == nil
|
103
|
-
method_details.col_type.sql_type.should include 'varchar(255)' # db specific, sqlite
|
104
|
-
method_details.col_type.type.should == :string
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should populate required Class for assignment operators based on column type" do
|
109
|
-
|
110
|
-
MethodDictionary.build_method_details( Project )
|
111
|
-
|
112
|
-
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
113
|
-
|
114
|
-
method_details = MethodDictionary.find_method_detail( Project, format )
|
115
|
-
|
116
|
-
method_details.operator_class_name.should == 'String'
|
117
|
-
method_details.operator_class.should be_is_a(Class)
|
118
|
-
method_details.operator_class.should == String
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should populate belongs_to operator for method details for different forms of a column name" do
|
124
|
-
|
125
|
-
MethodDictionary.build_method_details( Project )
|
126
|
-
MethodDictionary.build_method_details( Milestone )
|
127
|
-
|
128
|
-
# milestone.project = project.id
|
129
|
-
[:project, 'project', "PROJECT", "prOJECt"].each do |format|
|
130
|
-
|
131
|
-
method_details = MethodDictionary.find_method_detail( Milestone, format )
|
132
|
-
|
133
|
-
method_details.should_not be_nil
|
134
|
-
|
135
|
-
method_details.operator.should == 'project'
|
136
|
-
method_details.operator_for(:belongs_to).should == 'project'
|
137
|
-
|
138
|
-
method_details.operator_for(:assignment).should be_nil
|
139
|
-
method_details.operator_for(:has_many).should be_nil
|
140
|
-
end
|
141
|
-
|
142
|
-
end
|
143
|
-
|
144
|
-
it "should populate required Class for belongs_to operator method details" do
|
145
|
-
|
146
|
-
MethodDictionary.find_operators( LoaderRelease )
|
147
|
-
MethodDictionary.find_operators( LongAndComplexTableLinkedToVersion )
|
148
|
-
|
149
|
-
MethodDictionary.build_method_details( LoaderRelease )
|
150
|
-
MethodDictionary.build_method_details( LongAndComplexTableLinkedToVersion )
|
151
|
-
|
152
|
-
|
153
|
-
# release.project = project.id
|
154
|
-
[:project, 'project', "PROJECT", "prOJECt"].each do |format|
|
155
|
-
|
156
|
-
method_details = MethodDictionary.find_method_detail( LoaderRelease, format )
|
157
|
-
|
158
|
-
method_details.operator_class_name.should == 'Project'
|
159
|
-
method_details.operator_class.should == Project
|
160
|
-
end
|
161
|
-
|
162
|
-
|
163
|
-
[:version, "Version", "verSION"].each do |format|
|
164
|
-
method_details = MethodDictionary.find_method_detail( LongAndComplexTableLinkedToVersion, format )
|
165
|
-
|
166
|
-
method_details.operator_type.should == :belongs_to
|
167
|
-
|
168
|
-
method_details.operator_class_name.should == 'Version'
|
169
|
-
method_details.operator_class.should == Version
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
it "should populate required Class for has_one operator method details" do
|
174
|
-
|
175
|
-
MethodDictionary.find_operators( Version )
|
176
|
-
MethodDictionary.build_method_details( Version )
|
177
|
-
|
178
|
-
# version.long_and_complex_table_linked_to_version = LongAndComplexTableLinkedToVersion.create()
|
179
|
-
|
180
|
-
[: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|
|
181
|
-
method_details = MethodDictionary.find_method_detail( Version, format )
|
182
|
-
|
183
|
-
method_details.should_not be_nil
|
184
|
-
|
185
|
-
method_details.operator.should == 'long_and_complex_table_linked_to_version'
|
186
|
-
|
187
|
-
method_details.operator_type.should == :has_one
|
188
|
-
|
189
|
-
method_details.operator_class_name.should == 'LongAndComplexTableLinkedToVersion'
|
190
|
-
method_details.operator_class.should == LongAndComplexTableLinkedToVersion
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
|
195
|
-
it "should find has_many operator for method details" do
|
196
|
-
|
197
|
-
MethodDictionary.build_method_details( Project )
|
198
|
-
|
199
|
-
[:milestones, "Mile Stones", 'mileSTONES', 'MileStones'].each do |format|
|
200
|
-
|
201
|
-
method_details = MethodDictionary.find_method_detail( Project, format )
|
202
|
-
|
203
|
-
method_details.class.should == MethodDetail
|
204
|
-
|
205
|
-
result = 'milestones'
|
206
|
-
method_details.operator.should == result
|
207
|
-
method_details.operator_for(:has_many).should == result
|
208
|
-
|
209
|
-
method_details.operator_for(:belongs_to).should be_nil
|
210
|
-
method_details.operator_for(:assignments).should be_nil
|
211
|
-
end
|
212
|
-
|
213
|
-
end
|
214
|
-
|
215
|
-
|
216
|
-
it "should return nil when non existent column name" do
|
217
|
-
["On sale", 'on_sale'].each do |format|
|
218
|
-
detail = MethodDictionary.find_method_detail( Project, format )
|
219
|
-
|
220
|
-
detail.should be_nil
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
|
225
|
-
it "should find a set of methods based on a list of column names" do
|
226
|
-
pending("key API - map column headers to set of methods")
|
227
|
-
end
|
228
|
-
|
229
|
-
it "should not by default map setter methods", :fail => true do
|
230
|
-
MethodDictionary.assignments[Milestone].should_not include('title')
|
231
|
-
end
|
232
|
-
|
233
|
-
it "should support reload and inclusion of setter methods", :fail => true do
|
234
|
-
|
235
|
-
MethodDictionary.assignments[Milestone].should_not include('title')
|
236
|
-
|
237
|
-
MethodDictionary.find_operators( Milestone, :reload => true, :instance_methods => true )
|
238
|
-
|
239
|
-
# Milestone delegates :title to Project
|
240
|
-
MethodDictionary.assignments[Milestone].should include('title')
|
241
|
-
end
|
242
|
-
|
1
|
+
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
+
# Author :: Tom Statter
|
3
|
+
# Date :: Aug 2011
|
4
|
+
# License:: MIT
|
5
|
+
#
|
6
|
+
# Details:: Specs for MethodMapper aspect of Active Record Loader
|
7
|
+
# MethodMapper provides the bridge between 'strings' e.g column headings
|
8
|
+
# and a classes different types of assignment operators
|
9
|
+
#
|
10
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
11
|
+
|
12
|
+
describe 'Method Mapping' do
|
13
|
+
|
14
|
+
before(:all) do
|
15
|
+
db_connect( 'test_file' ) # , test_memory, test_mysql
|
16
|
+
|
17
|
+
# load our test model definitions - Project etc
|
18
|
+
require ifixture_file('test_model_defs')
|
19
|
+
|
20
|
+
migrate_up
|
21
|
+
end
|
22
|
+
|
23
|
+
before(:each) do
|
24
|
+
MethodDictionary.clear
|
25
|
+
MethodDictionary.find_operators( Project )
|
26
|
+
MethodDictionary.find_operators( Milestone )
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should store dictionary for multiple AR models" do
|
30
|
+
MethodDictionary.assignments.size.should == 2
|
31
|
+
MethodDictionary.has_many.size.should == 2
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should populate method dictionary for a given AR model" do
|
35
|
+
|
36
|
+
MethodDictionary.has_many.should_not be_empty
|
37
|
+
MethodDictionary.has_many[Project].should include('milestones')
|
38
|
+
|
39
|
+
MethodDictionary.assignments.should_not be_empty
|
40
|
+
MethodDictionary.assignments[Project].should include('id')
|
41
|
+
MethodDictionary.assignments[Project].should include('value_as_string')
|
42
|
+
MethodDictionary.assignments[Project].should include('value_as_text')
|
43
|
+
|
44
|
+
MethodDictionary.belongs_to.should_not be_empty
|
45
|
+
MethodDictionary.belongs_to[Project].should be_empty
|
46
|
+
|
47
|
+
|
48
|
+
MethodDictionary.column_types.should be_is_a(Hash)
|
49
|
+
MethodDictionary.column_types.should_not be_empty
|
50
|
+
MethodDictionary.column_types[Project].size.should == Project.columns.size
|
51
|
+
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should populate assigment members without the equivalent association names" do
|
56
|
+
|
57
|
+
# we should remove has-many & belongs_to from basic assignment set as they require a DB lookup
|
58
|
+
# or a Model.create call, not a simple assignment
|
59
|
+
|
60
|
+
MethodDictionary.assignments_for(Project).should_not include( MethodDictionary.belongs_to_for(Project) )
|
61
|
+
MethodDictionary.assignments_for(Project).should_not include( MethodDictionary.has_many_for(Project) )
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
it "should populate assignment operators for method details for different forms of a column name" do
|
66
|
+
|
67
|
+
MethodDictionary.build_method_details( Project )
|
68
|
+
|
69
|
+
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
70
|
+
|
71
|
+
method_details = MethodDictionary.find_method_detail( Project, format )
|
72
|
+
|
73
|
+
method_details.class.should == MethodDetail
|
74
|
+
|
75
|
+
method_details.operator.should == 'value_as_string'
|
76
|
+
method_details.operator_for(:assignment).should == 'value_as_string'
|
77
|
+
|
78
|
+
method_details.operator?('value_as_string').should be_true
|
79
|
+
method_details.operator?('blah_as_string').should be_false
|
80
|
+
|
81
|
+
method_details.operator_for(:belongs_to).should be_nil
|
82
|
+
method_details.operator_for(:has_many).should be_nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
# Note : Not all assignments will currently have a column type, for example
|
88
|
+
# those that are derived from a delegate_belongs_to
|
89
|
+
|
90
|
+
it "should populate column types for assignment operators in method details" do
|
91
|
+
|
92
|
+
MethodDictionary.build_method_details( Project )
|
93
|
+
|
94
|
+
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
95
|
+
|
96
|
+
method_details = MethodDictionary.find_method_detail( Project, format )
|
97
|
+
|
98
|
+
method_details.class.should == MethodDetail
|
99
|
+
|
100
|
+
method_details.col_type.should_not be_nil
|
101
|
+
method_details.col_type.name.should == 'value_as_string'
|
102
|
+
method_details.col_type.default.should == nil
|
103
|
+
method_details.col_type.sql_type.should include 'varchar(255)' # db specific, sqlite
|
104
|
+
method_details.col_type.type.should == :string
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should populate required Class for assignment operators based on column type" do
|
109
|
+
|
110
|
+
MethodDictionary.build_method_details( Project )
|
111
|
+
|
112
|
+
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
113
|
+
|
114
|
+
method_details = MethodDictionary.find_method_detail( Project, format )
|
115
|
+
|
116
|
+
method_details.operator_class_name.should == 'String'
|
117
|
+
method_details.operator_class.should be_is_a(Class)
|
118
|
+
method_details.operator_class.should == String
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should populate belongs_to operator for method details for different forms of a column name" do
|
124
|
+
|
125
|
+
MethodDictionary.build_method_details( Project )
|
126
|
+
MethodDictionary.build_method_details( Milestone )
|
127
|
+
|
128
|
+
# milestone.project = project.id
|
129
|
+
[:project, 'project', "PROJECT", "prOJECt"].each do |format|
|
130
|
+
|
131
|
+
method_details = MethodDictionary.find_method_detail( Milestone, format )
|
132
|
+
|
133
|
+
method_details.should_not be_nil
|
134
|
+
|
135
|
+
method_details.operator.should == 'project'
|
136
|
+
method_details.operator_for(:belongs_to).should == 'project'
|
137
|
+
|
138
|
+
method_details.operator_for(:assignment).should be_nil
|
139
|
+
method_details.operator_for(:has_many).should be_nil
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should populate required Class for belongs_to operator method details" do
|
145
|
+
|
146
|
+
MethodDictionary.find_operators( LoaderRelease )
|
147
|
+
MethodDictionary.find_operators( LongAndComplexTableLinkedToVersion )
|
148
|
+
|
149
|
+
MethodDictionary.build_method_details( LoaderRelease )
|
150
|
+
MethodDictionary.build_method_details( LongAndComplexTableLinkedToVersion )
|
151
|
+
|
152
|
+
|
153
|
+
# release.project = project.id
|
154
|
+
[:project, 'project', "PROJECT", "prOJECt"].each do |format|
|
155
|
+
|
156
|
+
method_details = MethodDictionary.find_method_detail( LoaderRelease, format )
|
157
|
+
|
158
|
+
method_details.operator_class_name.should == 'Project'
|
159
|
+
method_details.operator_class.should == Project
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
[:version, "Version", "verSION"].each do |format|
|
164
|
+
method_details = MethodDictionary.find_method_detail( LongAndComplexTableLinkedToVersion, format )
|
165
|
+
|
166
|
+
method_details.operator_type.should == :belongs_to
|
167
|
+
|
168
|
+
method_details.operator_class_name.should == 'Version'
|
169
|
+
method_details.operator_class.should == Version
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should populate required Class for has_one operator method details" do
|
174
|
+
|
175
|
+
MethodDictionary.find_operators( Version )
|
176
|
+
MethodDictionary.build_method_details( Version )
|
177
|
+
|
178
|
+
# version.long_and_complex_table_linked_to_version = LongAndComplexTableLinkedToVersion.create()
|
179
|
+
|
180
|
+
[: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|
|
181
|
+
method_details = MethodDictionary.find_method_detail( Version, format )
|
182
|
+
|
183
|
+
method_details.should_not be_nil
|
184
|
+
|
185
|
+
method_details.operator.should == 'long_and_complex_table_linked_to_version'
|
186
|
+
|
187
|
+
method_details.operator_type.should == :has_one
|
188
|
+
|
189
|
+
method_details.operator_class_name.should == 'LongAndComplexTableLinkedToVersion'
|
190
|
+
method_details.operator_class.should == LongAndComplexTableLinkedToVersion
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
|
195
|
+
it "should find has_many operator for method details" do
|
196
|
+
|
197
|
+
MethodDictionary.build_method_details( Project )
|
198
|
+
|
199
|
+
[:milestones, "Mile Stones", 'mileSTONES', 'MileStones'].each do |format|
|
200
|
+
|
201
|
+
method_details = MethodDictionary.find_method_detail( Project, format )
|
202
|
+
|
203
|
+
method_details.class.should == MethodDetail
|
204
|
+
|
205
|
+
result = 'milestones'
|
206
|
+
method_details.operator.should == result
|
207
|
+
method_details.operator_for(:has_many).should == result
|
208
|
+
|
209
|
+
method_details.operator_for(:belongs_to).should be_nil
|
210
|
+
method_details.operator_for(:assignments).should be_nil
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
it "should return nil when non existent column name" do
|
217
|
+
["On sale", 'on_sale'].each do |format|
|
218
|
+
detail = MethodDictionary.find_method_detail( Project, format )
|
219
|
+
|
220
|
+
detail.should be_nil
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
|
225
|
+
it "should find a set of methods based on a list of column names" do
|
226
|
+
pending("key API - map column headers to set of methods")
|
227
|
+
end
|
228
|
+
|
229
|
+
it "should not by default map setter methods", :fail => true do
|
230
|
+
MethodDictionary.assignments[Milestone].should_not include('title')
|
231
|
+
end
|
232
|
+
|
233
|
+
it "should support reload and inclusion of setter methods", :fail => true do
|
234
|
+
|
235
|
+
MethodDictionary.assignments[Milestone].should_not include('title')
|
236
|
+
|
237
|
+
MethodDictionary.find_operators( Milestone, :reload => true, :instance_methods => true )
|
238
|
+
|
239
|
+
# Milestone delegates :title to Project
|
240
|
+
MethodDictionary.assignments[Milestone].should include('title')
|
241
|
+
end
|
242
|
+
|
243
243
|
end
|