datashift 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -5
- data/Gemfile +28 -25
- data/LICENSE.txt +26 -26
- data/README.markdown +302 -285
- data/README.rdoc +19 -19
- data/Rakefile +93 -95
- data/VERSION +5 -5
- data/datashift.gemspec +162 -178
- data/lib/applications/jruby/jexcel_file.rb +396 -396
- data/lib/applications/jruby/word.rb +79 -79
- data/lib/datashift.rb +152 -113
- 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 +236 -236
- data/lib/datashift/method_mapper.rb +256 -256
- data/lib/generators/csv_generator.rb +36 -36
- data/lib/generators/excel_generator.rb +121 -121
- data/lib/generators/generator_base.rb +13 -13
- data/lib/helpers/core_ext/to_b.rb +24 -24
- data/lib/helpers/spree_helper.rb +131 -131
- 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 +154 -149
- data/lib/loaders/loader_base.rb +403 -331
- data/lib/loaders/spreadsheet_loader.rb +136 -136
- data/lib/loaders/spree/image_loader.rb +45 -45
- data/lib/loaders/spree/product_loader.rb +224 -224
- 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_generator_spec.rb +78 -78
- data/spec/excel_loader_spec.rb +204 -176
- data/spec/file_definitions.rb +141 -141
- data/spec/fixtures/.~lock.ProjectsSingleCategories.xls# +1 -0
- data/spec/fixtures/ProjectsDefaults.yml +29 -0
- data/spec/fixtures/config/database.yml +24 -24
- data/spec/fixtures/interact_models_db.sqlite +0 -0
- data/spec/fixtures/interact_spree_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/SpreeProductsMultiColumn.csv +4 -4
- data/spec/fixtures/spree/SpreeProductsSimple.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_mapper_spec.rb +237 -237
- data/spec/spec_helper.rb +115 -115
- data/spec/spree_generator_spec.rb +64 -64
- data/spec/spree_loader_spec.rb +310 -310
- data/spec/spree_method_mapping_spec.rb +214 -214
- data/tasks/config/seed_fu_product_template.erb +15 -15
- data/tasks/config/tidy_config.txt +12 -12
- data/tasks/db_tasks.rake +65 -64
- data/tasks/excel_generator.rake +78 -78
- data/tasks/file_tasks.rake +36 -36
- data/tasks/import/csv.rake +49 -49
- data/tasks/import/excel.rake +71 -66
- 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 +7 -38
- data/Gemfile.lock +0 -211
- data/bin/autospec +0 -16
- data/bin/convert_to_should_syntax +0 -16
- data/bin/erubis +0 -16
- data/bin/htmldiff +0 -16
- data/bin/jeweler +0 -16
- data/bin/ldiff +0 -16
- data/bin/nokogiri +0 -16
- data/bin/rackup +0 -16
- data/bin/rails +0 -16
- data/bin/rake +0 -16
- data/bin/rake2thor +0 -16
- data/bin/ri +0 -16
- data/bin/rspec +0 -16
- data/bin/spree +0 -16
- data/bin/thor +0 -16
- data/bin/tilt +0 -16
- data/bin/tt +0 -16
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
|
-
MethodMapper.clear
|
26
|
-
MethodMapper.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
|
+
MethodMapper.clear
|
26
|
+
MethodMapper.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
|
data/spec/method_mapper_spec.rb
CHANGED
@@ -1,238 +1,238 @@
|
|
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
|
-
migrate_up
|
17
|
-
@klazz = Project
|
18
|
-
@assoc_klazz = Milestone
|
19
|
-
end
|
20
|
-
|
21
|
-
before(:each) do
|
22
|
-
MethodMapper.clear
|
23
|
-
MethodMapper.find_operators( @klazz )
|
24
|
-
MethodMapper.find_operators( @assoc_klazz )
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should populate method map for a given AR model" do
|
28
|
-
|
29
|
-
MethodMapper.has_many.should_not be_empty
|
30
|
-
MethodMapper.has_many[Project].should include('milestones')
|
31
|
-
|
32
|
-
MethodMapper.assignments.should_not be_empty
|
33
|
-
MethodMapper.assignments[Project].should include('id')
|
34
|
-
MethodMapper.assignments[Project].should include('value_as_string')
|
35
|
-
MethodMapper.assignments[Project].should include('value_as_text')
|
36
|
-
|
37
|
-
MethodMapper.belongs_to.should_not be_empty
|
38
|
-
MethodMapper.belongs_to[Project].should be_empty
|
39
|
-
|
40
|
-
|
41
|
-
MethodMapper.column_types.should be_is_a(Hash)
|
42
|
-
MethodMapper.column_types.should_not be_empty
|
43
|
-
MethodMapper.column_types[Project].size.should == Project.columns.size
|
44
|
-
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should populate assigment members without the equivalent association names" do
|
49
|
-
|
50
|
-
# we should remove has-many & belongs_to from basic assignment set as they require a DB lookup
|
51
|
-
# or a Model.create call, not a simple assignment
|
52
|
-
|
53
|
-
MethodMapper.assignments_for(@klazz).should_not include( MethodMapper.belongs_to_for(@klazz) )
|
54
|
-
MethodMapper.assignments_for(@klazz).should_not include( MethodMapper.has_many_for(@klazz) )
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
it "should populate assignment operators for method details for different forms of a column name" do
|
59
|
-
|
60
|
-
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
61
|
-
|
62
|
-
method_details = MethodMapper.find_method_detail( @klazz, format )
|
63
|
-
|
64
|
-
method_details.class.should == MethodDetail
|
65
|
-
|
66
|
-
method_details.name.should eq( format.to_s )
|
67
|
-
|
68
|
-
method_details.operator.should == 'value_as_string'
|
69
|
-
method_details.operator_for(:assignment).should == 'value_as_string'
|
70
|
-
|
71
|
-
method_details.operator?('value_as_string').should be_true
|
72
|
-
method_details.operator?('blah_as_string').should be_false
|
73
|
-
|
74
|
-
method_details.operator_for(:belongs_to).should be_nil
|
75
|
-
method_details.operator_for(:has_many).should be_nil
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
|
80
|
-
# Note : Not all assignments will currently have a column type, for example
|
81
|
-
# those that are derived from a delegate_belongs_to
|
82
|
-
|
83
|
-
it "should populate column types for assignment operators in method details" do
|
84
|
-
|
85
|
-
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
86
|
-
|
87
|
-
method_details = MethodMapper.find_method_detail( @klazz, format )
|
88
|
-
|
89
|
-
method_details.class.should == MethodDetail
|
90
|
-
|
91
|
-
method_details.col_type.should_not be_nil
|
92
|
-
method_details.col_type.name.should == 'value_as_string'
|
93
|
-
method_details.col_type.default.should == nil
|
94
|
-
method_details.col_type.sql_type.should include 'varchar(255)' # db specific, sqlite
|
95
|
-
method_details.col_type.type.should == :string
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
it "should populate required Class for assignment operators based on column type" do
|
100
|
-
|
101
|
-
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
102
|
-
|
103
|
-
method_details = MethodMapper.find_method_detail( Project, format )
|
104
|
-
|
105
|
-
method_details.operator_class_name.should == 'String'
|
106
|
-
method_details.operator_class.should be_is_a(Class)
|
107
|
-
method_details.operator_class.should == String
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
|
112
|
-
it "should populate belongs_to operator for method details for different forms of a column name" do
|
113
|
-
|
114
|
-
# milestone.project = project.id
|
115
|
-
[:project, 'project', "PROJECT", "prOJECt"].each do |format|
|
116
|
-
|
117
|
-
method_details = MethodMapper.find_method_detail( Milestone, format )
|
118
|
-
|
119
|
-
method_details.should_not be_nil
|
120
|
-
|
121
|
-
method_details.operator.should == 'project'
|
122
|
-
method_details.operator_for(:belongs_to).should == 'project'
|
123
|
-
|
124
|
-
method_details.operator_for(:assignment).should be_nil
|
125
|
-
method_details.operator_for(:has_many).should be_nil
|
126
|
-
end
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
it "should populate required Class for belongs_to operator method details" do
|
131
|
-
|
132
|
-
MethodMapper.find_operators( LoaderRelease )
|
133
|
-
MethodMapper.find_operators( LongAndComplexTableLinkedToVersion )
|
134
|
-
|
135
|
-
# release.project = project.id
|
136
|
-
[:project, 'project', "PROJECT", "prOJECt"].each do |format|
|
137
|
-
|
138
|
-
method_details = MethodMapper.find_method_detail( LoaderRelease, format )
|
139
|
-
|
140
|
-
method_details.operator_class_name.should == 'Project'
|
141
|
-
method_details.operator_class.should == Project
|
142
|
-
end
|
143
|
-
|
144
|
-
|
145
|
-
#LongAndComplexTableLinkedToVersion.version = version.id
|
146
|
-
|
147
|
-
[:version, "Version", "verSION"].each do |format|
|
148
|
-
method_details = MethodMapper.find_method_detail( LongAndComplexTableLinkedToVersion, format )
|
149
|
-
|
150
|
-
method_details.operator_type.should == :belongs_to
|
151
|
-
|
152
|
-
method_details.operator_class_name.should == 'Version'
|
153
|
-
method_details.operator_class.should == Version
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
it "should populate required Class for has_one operator method details" do
|
158
|
-
|
159
|
-
MethodMapper.find_operators( Version )
|
160
|
-
|
161
|
-
# version.long_and_complex_table_linked_to_version = LongAndComplexTableLinkedToVersion.create()
|
162
|
-
|
163
|
-
[: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|
|
164
|
-
method_details = MethodMapper.find_method_detail( Version, format )
|
165
|
-
|
166
|
-
method_details.should_not be_nil
|
167
|
-
|
168
|
-
method_details.operator.should == 'long_and_complex_table_linked_to_version'
|
169
|
-
|
170
|
-
method_details.operator_type.should == :has_one
|
171
|
-
|
172
|
-
method_details.operator_class_name.should == 'LongAndComplexTableLinkedToVersion'
|
173
|
-
method_details.operator_class.should == LongAndComplexTableLinkedToVersion
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
it "should find has_many operator for method details" do
|
179
|
-
|
180
|
-
[:milestones, "Mile Stones", 'mileSTONES', 'MileStones'].each do |format|
|
181
|
-
|
182
|
-
method_details = MethodMapper.find_method_detail( Project, format )
|
183
|
-
|
184
|
-
method_details.class.should == MethodDetail
|
185
|
-
|
186
|
-
result = 'milestones'
|
187
|
-
method_details.operator.should == result
|
188
|
-
method_details.operator_for(:has_many).should == result
|
189
|
-
|
190
|
-
method_details.operator_for(:belongs_to).should be_nil
|
191
|
-
method_details.operator_for(:assignments).should be_nil
|
192
|
-
end
|
193
|
-
|
194
|
-
end
|
195
|
-
|
196
|
-
|
197
|
-
it "should return nil when non existent column name" do
|
198
|
-
["On sale", 'on_sale'].each do |format|
|
199
|
-
detail = MethodMapper.find_method_detail( @klazz, format )
|
200
|
-
|
201
|
-
detail.should be_nil
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
|
206
|
-
it "should find a set of methods based on a list of column names" do
|
207
|
-
|
208
|
-
mapper = MethodMapper.new
|
209
|
-
|
210
|
-
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |column_name_format|
|
211
|
-
|
212
|
-
method_details = MethodMapper.find_method_detail( @klazz, column_name_format )
|
213
|
-
|
214
|
-
method_details.class.should == MethodDetail
|
215
|
-
|
216
|
-
method_details.col_type.should_not be_nil
|
217
|
-
method_details.col_type.name.should == 'value_as_string'
|
218
|
-
method_details.col_type.default.should == nil
|
219
|
-
method_details.col_type.sql_type.should include 'varchar(255)' # db specific, sqlite
|
220
|
-
method_details.col_type.type.should == :string
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
it "should not by default map setter methods", :fail => true do
|
225
|
-
MethodMapper.assignments[Milestone].should_not include('title')
|
226
|
-
end
|
227
|
-
|
228
|
-
it "should support reload and inclusion of setter methods", :fail => true do
|
229
|
-
|
230
|
-
MethodMapper.assignments[Milestone].should_not include('title')
|
231
|
-
|
232
|
-
MethodMapper.find_operators( Milestone, :reload => true, :instance_methods => true )
|
233
|
-
|
234
|
-
# Milestone delegates :title to Project
|
235
|
-
MethodMapper.assignments[Milestone].should include('title')
|
236
|
-
end
|
237
|
-
|
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
|
+
migrate_up
|
17
|
+
@klazz = Project
|
18
|
+
@assoc_klazz = Milestone
|
19
|
+
end
|
20
|
+
|
21
|
+
before(:each) do
|
22
|
+
MethodMapper.clear
|
23
|
+
MethodMapper.find_operators( @klazz )
|
24
|
+
MethodMapper.find_operators( @assoc_klazz )
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should populate method map for a given AR model" do
|
28
|
+
|
29
|
+
MethodMapper.has_many.should_not be_empty
|
30
|
+
MethodMapper.has_many[Project].should include('milestones')
|
31
|
+
|
32
|
+
MethodMapper.assignments.should_not be_empty
|
33
|
+
MethodMapper.assignments[Project].should include('id')
|
34
|
+
MethodMapper.assignments[Project].should include('value_as_string')
|
35
|
+
MethodMapper.assignments[Project].should include('value_as_text')
|
36
|
+
|
37
|
+
MethodMapper.belongs_to.should_not be_empty
|
38
|
+
MethodMapper.belongs_to[Project].should be_empty
|
39
|
+
|
40
|
+
|
41
|
+
MethodMapper.column_types.should be_is_a(Hash)
|
42
|
+
MethodMapper.column_types.should_not be_empty
|
43
|
+
MethodMapper.column_types[Project].size.should == Project.columns.size
|
44
|
+
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should populate assigment members without the equivalent association names" do
|
49
|
+
|
50
|
+
# we should remove has-many & belongs_to from basic assignment set as they require a DB lookup
|
51
|
+
# or a Model.create call, not a simple assignment
|
52
|
+
|
53
|
+
MethodMapper.assignments_for(@klazz).should_not include( MethodMapper.belongs_to_for(@klazz) )
|
54
|
+
MethodMapper.assignments_for(@klazz).should_not include( MethodMapper.has_many_for(@klazz) )
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
it "should populate assignment operators for method details for different forms of a column name" do
|
59
|
+
|
60
|
+
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
61
|
+
|
62
|
+
method_details = MethodMapper.find_method_detail( @klazz, format )
|
63
|
+
|
64
|
+
method_details.class.should == MethodDetail
|
65
|
+
|
66
|
+
method_details.name.should eq( format.to_s )
|
67
|
+
|
68
|
+
method_details.operator.should == 'value_as_string'
|
69
|
+
method_details.operator_for(:assignment).should == 'value_as_string'
|
70
|
+
|
71
|
+
method_details.operator?('value_as_string').should be_true
|
72
|
+
method_details.operator?('blah_as_string').should be_false
|
73
|
+
|
74
|
+
method_details.operator_for(:belongs_to).should be_nil
|
75
|
+
method_details.operator_for(:has_many).should be_nil
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
# Note : Not all assignments will currently have a column type, for example
|
81
|
+
# those that are derived from a delegate_belongs_to
|
82
|
+
|
83
|
+
it "should populate column types for assignment operators in method details" do
|
84
|
+
|
85
|
+
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
86
|
+
|
87
|
+
method_details = MethodMapper.find_method_detail( @klazz, format )
|
88
|
+
|
89
|
+
method_details.class.should == MethodDetail
|
90
|
+
|
91
|
+
method_details.col_type.should_not be_nil
|
92
|
+
method_details.col_type.name.should == 'value_as_string'
|
93
|
+
method_details.col_type.default.should == nil
|
94
|
+
method_details.col_type.sql_type.should include 'varchar(255)' # db specific, sqlite
|
95
|
+
method_details.col_type.type.should == :string
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should populate required Class for assignment operators based on column type" do
|
100
|
+
|
101
|
+
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |format|
|
102
|
+
|
103
|
+
method_details = MethodMapper.find_method_detail( Project, format )
|
104
|
+
|
105
|
+
method_details.operator_class_name.should == 'String'
|
106
|
+
method_details.operator_class.should be_is_a(Class)
|
107
|
+
method_details.operator_class.should == String
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should populate belongs_to operator for method details for different forms of a column name" do
|
113
|
+
|
114
|
+
# milestone.project = project.id
|
115
|
+
[:project, 'project', "PROJECT", "prOJECt"].each do |format|
|
116
|
+
|
117
|
+
method_details = MethodMapper.find_method_detail( Milestone, format )
|
118
|
+
|
119
|
+
method_details.should_not be_nil
|
120
|
+
|
121
|
+
method_details.operator.should == 'project'
|
122
|
+
method_details.operator_for(:belongs_to).should == 'project'
|
123
|
+
|
124
|
+
method_details.operator_for(:assignment).should be_nil
|
125
|
+
method_details.operator_for(:has_many).should be_nil
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should populate required Class for belongs_to operator method details" do
|
131
|
+
|
132
|
+
MethodMapper.find_operators( LoaderRelease )
|
133
|
+
MethodMapper.find_operators( LongAndComplexTableLinkedToVersion )
|
134
|
+
|
135
|
+
# release.project = project.id
|
136
|
+
[:project, 'project', "PROJECT", "prOJECt"].each do |format|
|
137
|
+
|
138
|
+
method_details = MethodMapper.find_method_detail( LoaderRelease, format )
|
139
|
+
|
140
|
+
method_details.operator_class_name.should == 'Project'
|
141
|
+
method_details.operator_class.should == Project
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
#LongAndComplexTableLinkedToVersion.version = version.id
|
146
|
+
|
147
|
+
[:version, "Version", "verSION"].each do |format|
|
148
|
+
method_details = MethodMapper.find_method_detail( LongAndComplexTableLinkedToVersion, format )
|
149
|
+
|
150
|
+
method_details.operator_type.should == :belongs_to
|
151
|
+
|
152
|
+
method_details.operator_class_name.should == 'Version'
|
153
|
+
method_details.operator_class.should == Version
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should populate required Class for has_one operator method details" do
|
158
|
+
|
159
|
+
MethodMapper.find_operators( Version )
|
160
|
+
|
161
|
+
# version.long_and_complex_table_linked_to_version = LongAndComplexTableLinkedToVersion.create()
|
162
|
+
|
163
|
+
[: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|
|
164
|
+
method_details = MethodMapper.find_method_detail( Version, format )
|
165
|
+
|
166
|
+
method_details.should_not be_nil
|
167
|
+
|
168
|
+
method_details.operator.should == 'long_and_complex_table_linked_to_version'
|
169
|
+
|
170
|
+
method_details.operator_type.should == :has_one
|
171
|
+
|
172
|
+
method_details.operator_class_name.should == 'LongAndComplexTableLinkedToVersion'
|
173
|
+
method_details.operator_class.should == LongAndComplexTableLinkedToVersion
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
it "should find has_many operator for method details" do
|
179
|
+
|
180
|
+
[:milestones, "Mile Stones", 'mileSTONES', 'MileStones'].each do |format|
|
181
|
+
|
182
|
+
method_details = MethodMapper.find_method_detail( Project, format )
|
183
|
+
|
184
|
+
method_details.class.should == MethodDetail
|
185
|
+
|
186
|
+
result = 'milestones'
|
187
|
+
method_details.operator.should == result
|
188
|
+
method_details.operator_for(:has_many).should == result
|
189
|
+
|
190
|
+
method_details.operator_for(:belongs_to).should be_nil
|
191
|
+
method_details.operator_for(:assignments).should be_nil
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
|
197
|
+
it "should return nil when non existent column name" do
|
198
|
+
["On sale", 'on_sale'].each do |format|
|
199
|
+
detail = MethodMapper.find_method_detail( @klazz, format )
|
200
|
+
|
201
|
+
detail.should be_nil
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
|
206
|
+
it "should find a set of methods based on a list of column names" do
|
207
|
+
|
208
|
+
mapper = MethodMapper.new
|
209
|
+
|
210
|
+
[:value_as_string, 'value_as_string', "VALUE as_STRING", "value as string"].each do |column_name_format|
|
211
|
+
|
212
|
+
method_details = MethodMapper.find_method_detail( @klazz, column_name_format )
|
213
|
+
|
214
|
+
method_details.class.should == MethodDetail
|
215
|
+
|
216
|
+
method_details.col_type.should_not be_nil
|
217
|
+
method_details.col_type.name.should == 'value_as_string'
|
218
|
+
method_details.col_type.default.should == nil
|
219
|
+
method_details.col_type.sql_type.should include 'varchar(255)' # db specific, sqlite
|
220
|
+
method_details.col_type.type.should == :string
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should not by default map setter methods", :fail => true do
|
225
|
+
MethodMapper.assignments[Milestone].should_not include('title')
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should support reload and inclusion of setter methods", :fail => true do
|
229
|
+
|
230
|
+
MethodMapper.assignments[Milestone].should_not include('title')
|
231
|
+
|
232
|
+
MethodMapper.find_operators( Milestone, :reload => true, :instance_methods => true )
|
233
|
+
|
234
|
+
# Milestone delegates :title to Project
|
235
|
+
MethodMapper.assignments[Milestone].should include('title')
|
236
|
+
end
|
237
|
+
|
238
238
|
end
|