rexport 1.2.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,75 +0,0 @@
1
- require 'test_helper'
2
-
3
- class DataFieldsTest < ActiveSupport::TestCase
4
-
5
- class RexportClassMethodsTest < DataFieldsTest
6
- test 'should get klass from assocations' do
7
- assert_equal Family, Enrollment.get_klass_from_associations('student', 'family')
8
- end
9
-
10
- test 'should raise no method error for missing associations' do
11
- assert_raise NoMethodError do
12
- Enrollment.get_klass_from_associations('not_an_association')
13
- end
14
- end
15
- end
16
-
17
- class RexportInstanceMethodsTest < DataFieldsTest
18
- test 'should exoport value of data attribute' do
19
- assert_equal %w(1), build(:enrollment).export('grade')
20
- end
21
-
22
- test 'should export value returned from method' do
23
- assert_equal %w(bar), build(:enrollment).export('foo')
24
- end
25
-
26
- test 'should return empty string for undefined method' do
27
- assert_equal [''], build(:enrollment).export('bad_method')
28
- end
29
-
30
- test 'should format date for export' do
31
- assert_equal [Time.now.strftime("%m/%d/%y")], build(:enrollment).export('updated_at')
32
- end
33
-
34
- test 'should export Y for true' do
35
- assert_equal %w(Y), Enrollment.new(active: true).export('active')
36
- end
37
-
38
- test 'should export N for false' do
39
- assert_equal %w(N), Enrollment.new(active: false).export('active')
40
- end
41
-
42
- test 'should handle missing associations' do
43
- assert_equal [''], Enrollment.new.export('status.name')
44
- end
45
-
46
- test 'should handle undefined export field' do
47
- assert_equal ['UNDEFINED EXPORT FIELD'], Enrollment.new.export('undefined_rexport_field')
48
- end
49
-
50
- test 'should export value of associated data attribute' do
51
- assert_equal ['The Sample Family'], build(:enrollment).export('student.family.name')
52
- end
53
-
54
- test 'should export value returned from associated method' do
55
- assert_equal %w(bar), build(:enrollment).export('student.family.foo')
56
- end
57
-
58
- test 'should export field from non rexported model' do
59
- assert_equal %w(active), build(:enrollment).export('status.name')
60
- end
61
-
62
- test 'should export local, associated, and non rexported fields in order' do
63
- assert_equal(
64
- ['The Sample Family', 'Sammy Sample', '1', 'bar', 'active'],
65
- build(:enrollment).export(
66
- 'student.family.name',
67
- 'student.name',
68
- 'grade',
69
- 'student.family.foo',
70
- 'status.name'
71
- )
72
- )
73
- end
74
- end
75
- end
@@ -1,37 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ExportFilterMethodsTest < ActiveSupport::TestCase
4
- test 'should return associated object name' do
5
- assert_equal '1', FactoryBot.create(:grade_filter).display_value
6
- end
7
-
8
- test 'should return chained associated object value' do
9
- assert_equal 'active', FactoryBot.create(:status_filter).display_value
10
- end
11
-
12
- test 'should return chained associated object' do
13
- family = FactoryBot.create(:family)
14
- assert_equal(
15
- family.name,
16
- export_filter('student.family_id', value: family.id).display_value
17
- )
18
- end
19
-
20
- test 'should return undefined association' do
21
- assert_equal('UNDEFINED ASSOCIATION', export_filter('bogus_id').display_value)
22
- end
23
-
24
- test 'should return associated object not found' do
25
- assert_equal 'ASSOCIATED OBJECT NOT FOUND', export_filter('status_id').display_value
26
- end
27
-
28
- private
29
-
30
- def export_filter(filter_field, value: 1)
31
- ExportFilter.new(
32
- export: FactoryBot.create(:export),
33
- filter_field: filter_field,
34
- value: value
35
- )
36
- end
37
- end
@@ -1,21 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ExportItemMethodsTest < ActiveSupport::TestCase
4
- test 'should resort export items' do
5
- export = FactoryBot.create(:export)
6
- ExportItem.resort(export.export_items.ordered.reverse.map(&:id).map(&:to_s))
7
- assert_equal 'Bogus Item', export.export_items.ordered.first.name
8
- end
9
-
10
- test 'should return attributes_for_copy' do
11
- assert FactoryBot.create(:family_name_export_item).attributes_for_copy
12
- end
13
-
14
- test 'should set name when blank' do
15
- assert_equal 'Grade', FactoryBot.create(:grade_export_item).name
16
- end
17
-
18
- test 'should set name for chained field to last two items' do
19
- assert_equal 'Name - Test', ExportItem.create(rexport_field: 'this.is.a.name.test').name
20
- end
21
- end
@@ -1,226 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ExportMethodsTest < ActiveSupport::TestCase
4
- test 'should return models' do
5
- assert Export.models
6
- end
7
-
8
- test 'should return full name' do
9
- assert_equal 'Enrollments - Enrollment Export', build(:export).full_name
10
- end
11
-
12
- test 'should return header' do
13
- assert_equal ['Family Name', 'Grade', 'Status', 'Bogus Item'], create(:export).header
14
- end
15
-
16
- test 'should return rexport_fields' do
17
- assert_equal(
18
- %w(student.family.name grade status_name bogus_field),
19
- build(:export).send(:rexport_fields)
20
- )
21
- end
22
-
23
- test 'should return rexport_methods' do
24
- assert_equal(
25
- %w(student.family.name grade status.name undefined_rexport_field),
26
- create(:export).send(:rexport_methods)
27
- )
28
- end
29
-
30
- test 'should return records' do
31
- create(:enrollment)
32
- assert_equal(
33
- ['The Sample Family', '1', 'active', 'UNDEFINED EXPORT FIELD'],
34
- create(:export).records.first
35
- )
36
- end
37
-
38
- test 'should return records that match filters' do
39
- create(:enrollment)
40
- create(:second_grade_enrollment)
41
- assert_equal 2, Enrollment.count
42
- assert_equal [['1', 'active']], create(:filtered_export).records
43
- end
44
-
45
- test 'should return error message for records when export has an invalid filter' do
46
- assert_equal(
47
- [["SQLite3::SQLException: no such column: enrollments.invalid"]],
48
- create(:invalid_filtered_export).records
49
- )
50
- end
51
-
52
- test 'should call get_records' do
53
- export = build(:export)
54
- export.expects(:get_records).with(Rexport::SAMPLE_SIZE).returns(true)
55
- assert export.sample_records
56
- end
57
-
58
- test 'should return to_s with no records' do
59
- assert_equal "Family Name|Grade|Status|Bogus Item\n", create(:export).to_s
60
- end
61
-
62
- test 'should return to_s with record' do
63
- create(:enrollment)
64
- assert_equal(
65
- "Family Name|Grade|Status|Bogus Item\nThe Sample Family|1|active|UNDEFINED EXPORT FIELD\n",
66
- create(:export).to_s
67
- )
68
- end
69
-
70
- test 'should return to_csv with no records' do
71
- assert_equal "Family Name,Grade,Status,Bogus Item\n", create(:export).to_csv
72
- end
73
-
74
- test 'should return to_csv with record' do
75
- FactoryBot.create(:enrollment)
76
- assert_equal(
77
- "Family Name,Grade,Status,Bogus Item\nThe Sample Family,1,active,UNDEFINED EXPORT FIELD\n",
78
- create(:export).to_csv
79
- )
80
- end
81
-
82
- test 'should return to_csv with passed objects' do
83
- assert_equal(
84
- "Family Name,Grade,Status,Bogus Item\n\"\",99,\"\",UNDEFINED EXPORT FIELD\n",
85
- create(:export).to_csv([Enrollment.new(grade: 99)])
86
- )
87
- end
88
-
89
- test 'should return build_conditions' do
90
- assert_equal(
91
- {'statuses.name' => 'active', 'enrollments.grade' => '1'},
92
- create(:filtered_export).send(:build_conditions)
93
- )
94
- end
95
-
96
- test 'should return build_include' do
97
- assert_equal [{student: [:family]}, :status], create(:export).send(:build_include)
98
- end
99
-
100
- test 'should return rexport_models' do
101
- export = create(:export)
102
- assert_equal(
103
- %w(Enrollment Family SelfReferentialCheck Student),
104
- export.rexport_models.map(&:name).sort
105
- )
106
- assert_equal(
107
- ['', 'self_referential_check', 'student', 'student.family'],
108
- export.rexport_models.map(&:path).map(&:to_s).sort
109
- )
110
- end
111
-
112
- test 'should return true for has_rexport_field?' do
113
- assert build(:export).has_rexport_field?('student.family.name')
114
- end
115
-
116
- test 'should return false for has_rexport_field?' do
117
- refute build(:export).has_rexport_field?('student.family.number')
118
- end
119
-
120
- test 'should save export_items from a hash' do
121
- assert_equal %w(a b c), rexport_fields_for(create_export(fields: {a: 1, b: 1, c: 1}))
122
- end
123
-
124
- test 'should save export_items from an array' do
125
- assert_equal %w(a b c), rexport_fields_for(create_export(fields: %w(a b c)))
126
- end
127
-
128
- test 'should add export_item to exising export on update' do
129
- export = create_export(fields: %w(a c))
130
- assert_difference 'ExportItem.count' do
131
- export.update_attribute(:rexport_fields, %w(a b c))
132
- end
133
- assert_equal %w(a b c), rexport_fields_for(export)
134
- end
135
-
136
- test 'should delete export_item that is not in rexport_fields on update' do
137
- export = create_export(fields: %w(a b c))
138
- assert_difference 'ExportItem.count', -1 do
139
- export.update_attribute(:rexport_fields, %w(a c))
140
- end
141
- assert_equal %w(a c), rexport_fields_for(export)
142
- end
143
-
144
- test 'should re-order export_items when passed an array of export_fields on update' do
145
- export = create_export(fields: %w(a b c))
146
- export.update_attribute(:rexport_fields, %w(c b a))
147
- assert_equal %w(c b a), rexport_fields_for(export)
148
- end
149
-
150
- test 'should not re-order export_items when passed a hash of export_fields on update' do
151
- export = create_export(fields: %w(a b c))
152
- export.update_attribute(:rexport_fields, {c: 1, b: 1, a: 1})
153
- assert_equal %w(a b c), rexport_fields_for(export)
154
- end
155
-
156
- test 'should not modify export_items on update when no export_fields are passed' do
157
- export = create_export(fields: %w(a b c))
158
- assert_no_difference 'ExportItem.count' do
159
- export.update_attribute(:name, 'New Name')
160
- end
161
- assert_equal %w(a b c), rexport_fields_for(export)
162
- end
163
-
164
- test 'should create export with an export_filter' do
165
- assert_difference 'ExportFilter.count' do
166
- create_export(filters: {status_id: 1})
167
- end
168
- end
169
-
170
- test 'should add an export_filter to an existing export' do
171
- export = create_export
172
- assert_difference 'ExportFilter.count' do
173
- export.update_attribute(:export_filter_attributes, {status_id: 1})
174
- end
175
- end
176
-
177
- test 'should delete an export_filter from an export' do
178
- export = create_export(filters: {status_id: 1})
179
- assert_difference 'ExportFilter.count', -1 do
180
- export.update_attribute(:export_filter_attributes, {status_id: ''})
181
- end
182
- end
183
-
184
- test 'should return filter_value for filter_field' do
185
- assert_equal 'active', create(:filtered_export).filter_value('status.name')
186
- end
187
-
188
- test 'should return nil for filter_value when no matching export_filters' do
189
- assert_nil create(:filtered_export).filter_value('does_not_exist')
190
- end
191
-
192
- test 'should create copy with unique name' do
193
- assert_equal 'Enrollment Export Copy', create(:export).copy.name
194
- assert_equal 'Enrollment Export Copy [1]', create(:export).copy.name
195
- assert_equal 'Enrollment Export Copy [2]', create(:export).copy.name
196
- end
197
-
198
- test 'should copy export with export_items' do
199
- export = create(:export)
200
- assert_difference 'ExportItem.count', export.export_items.count do
201
- export.copy.export_items.count
202
- end
203
- end
204
-
205
- test 'should copy export with export_filters' do
206
- export = create(:filtered_export)
207
- assert_difference 'ExportFilter.count', export.export_filters.count do
208
- export.copy
209
- end
210
- end
211
-
212
- private
213
-
214
- def create_export(fields: {}, filters: {})
215
- Export.create(
216
- name: 'Test',
217
- model_class_name: 'Enrollment',
218
- rexport_fields: fields,
219
- export_filter_attributes: filters
220
- )
221
- end
222
-
223
- def rexport_fields_for(export)
224
- export.export_items.ordered.map(&:rexport_field)
225
- end
226
- end
@@ -1,137 +0,0 @@
1
- require 'test_helper'
2
-
3
- class RexportModel < ActiveSupport::TestCase
4
- test 'should initialize rexport_fields' do
5
- assert_equal(
6
- %w(active bad_method created_at foo_method grade ilp_status_name status_name updated_at),
7
- rexport_model.rexport_fields.keys.sort
8
- )
9
- end
10
-
11
- test 'should initialize data atributes' do
12
- assert_equal 'grade', rexport_model.rexport_fields[:grade].method
13
- assert_equal :integer, rexport_model.rexport_fields[:grade].type
14
- end
15
-
16
- test 'should initialize method' do
17
- assert_equal 'foo', rexport_model.rexport_fields[:foo_method].method
18
- assert_nil rexport_model.rexport_fields[:foo_method].type
19
- end
20
-
21
- test 'should add single association method to rexport_fields' do
22
- assert_fields_length do |rexport|
23
- rexport.add_association_methods(associations: 'test_association')
24
- assert_equal 'test_association_name', rexport.rexport_fields[:test_association_name].name
25
- assert_equal 'test_association.name', rexport.rexport_fields[:test_association_name].method
26
- end
27
- end
28
-
29
- test 'should add name methods for multiple associations' do
30
- assert_fields_length(change: 3) do |rexport|
31
- rexport.add_association_methods(associations: %w(a b c))
32
- end
33
- end
34
-
35
- test 'should add multiple methods for multiple associations' do
36
- assert_fields_length(change: 9) do |rexport|
37
- rexport.add_association_methods(associations: %w(a1 a2 a3), methods: %w(m1 m2 m3))
38
- end
39
- end
40
-
41
- test 'should remove single rexport field' do
42
- rexport_model.tap do |rexport|
43
- assert rexport.rexport_fields[:grade]
44
- assert rexport.remove_rexport_fields(:grade)
45
- assert_nil rexport.rexport_fields[:grade]
46
- end
47
- end
48
-
49
- test 'should remove multiple rexport fields' do
50
- fields = %w(grade status_name foo_method)
51
-
52
- rexport_model.tap do |rexport|
53
- fields.each { |field| assert(rexport.rexport_fields[field]) }
54
- assert rexport.remove_rexport_fields(fields)
55
- fields.each { |field| assert_nil(rexport.rexport_fields[field]) }
56
- end
57
- end
58
-
59
- test 'should get rexport methods' do
60
- assert_equal(
61
- [
62
- 'student.family.foo',
63
- 'student.name',
64
- 'undefined_rexport_field',
65
- 'undefined_rexport_field',
66
- 'status.name',
67
- 'grade'
68
- ],
69
- rexport_model.get_rexport_methods(
70
- 'student.family.foo_method',
71
- 'student.name',
72
- 'student.bad_method',
73
- 'bad_association.test',
74
- 'status_name',
75
- 'grade'
76
- )
77
- )
78
- end
79
-
80
- test 'should use cached rexport_model when getting multiple fields from one model' do
81
- rexport_model.tap do |rexport|
82
- assert_equal ['student.name'], rexport.get_rexport_methods('student.name')
83
- Student.expects(:get_klass_from_associations).times(0)
84
- assert_equal ['student.date_of_birth'], rexport.get_rexport_methods('student.date_of_birth')
85
- end
86
- end
87
-
88
- test 'should return default foreign_key' do
89
- assert_equal 'status_id', rexport_model.filter_column(data_field('status_name'))
90
- end
91
-
92
- test 'should return custom foreign_key' do
93
- assert_equal 'ilp_status_id', rexport_model.filter_column(data_field('ilp_status_name'))
94
- end
95
-
96
- test 'should call rexport_fields_array' do
97
- assert_equal(
98
- %w(active bad_method created_at foo_method grade ilp_status_name status_name updated_at),
99
- rexport_model.rexport_fields_array.map(&:name)
100
- )
101
- end
102
-
103
- test 'should return field_path' do
104
- assert_equal 'foo.bar', rexport_model(path: 'foo').field_path('bar')
105
- end
106
-
107
- test 'should return collection_from_association' do
108
- Status.expects(:all).returns(true)
109
- assert rexport_model.collection_from_association('status')
110
- end
111
-
112
- test 'should return custom find method for collection_from_association' do
113
- Student.expects(:find_family_for_rexport).returns(true)
114
- assert rexport_model(model: Student).collection_from_association('family')
115
- end
116
-
117
-
118
- test 'should return class name' do
119
- assert_equal 'Enrollment', rexport_model.name
120
- end
121
-
122
- private
123
-
124
- def rexport_model(model: Enrollment, path: nil)
125
- Rexport::RexportModel.new(model, path: path)
126
- end
127
-
128
- def data_field(name)
129
- rexport_model.rexport_fields[name]
130
- end
131
-
132
- def assert_fields_length(rexport: rexport_model, change: 1, &block)
133
- assert_difference('rexport.rexport_fields.length', change) do
134
- block.call(rexport)
135
- end
136
- end
137
- end
@@ -1,43 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TreeNodeTest < ActiveSupport::TestCase
4
- test 'should initialize with name' do
5
- t = Rexport::TreeNode.new('test')
6
- assert_equal 'test', t.name
7
- assert_equal [], t.children
8
- end
9
-
10
- test 'should initialize with name and children' do
11
- t = Rexport::TreeNode.new('test', %w(one two three))
12
- assert_equal 'test', t.name
13
- assert_equal ["test", [["one", [["two", [["three", []]]]]]]], t.to_a
14
- end
15
-
16
- test 'should add children' do
17
- root = Rexport::TreeNode.new('root')
18
- root.add_child('a')
19
- assert_equal ['root', [['a', []]]], root.to_a
20
- root.add_child('a')
21
- assert_equal ['root', [['a', []]]], root.to_a
22
- root.add_child('a', [1 , 2])
23
- assert_equal ['root', [['a', [[1, [[2, []]]]]]]], root.to_a
24
- end
25
-
26
- test 'should return empty include' do
27
- root = Rexport::TreeNode.new('root')
28
- assert_equal [], root.to_include
29
- end
30
-
31
- test 'should return single level array include' do
32
- root = Rexport::TreeNode.new('root')
33
- %w(a b c).each { |l| root.add_child(l) }
34
- assert_equal [:a, :b, :c], root.to_include
35
- end
36
-
37
- test 'should return nested hash include' do
38
- root = Rexport::TreeNode.new('root', %w(a b c))
39
- assert_equal [{a: [{b: [:c]}]}], root.to_include
40
- root.add_child 'b', 'one', 'two'
41
- assert_equal [{a: [{b: [:c]}]}, {b: [{one:[:two]}]}], root.to_include
42
- end
43
- end