ndr_import 8.5.0 → 8.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/Gemfile +0 -3
- data/README.md +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/code_safety.yml +27 -11
- data/exe/pdf_acro_form_to_yaml +23 -0
- data/exe/pdf_to_text +28 -0
- data/exe/word_to_text +26 -0
- data/gemfiles/Gemfile.rails52 +0 -3
- data/gemfiles/Gemfile.rails60 +5 -0
- data/lib/ndr_import/version.rb +1 -1
- data/ndr_import.gemspec +9 -7
- metadata +23 -164
- data/gemfiles/Gemfile.rails50 +0 -8
- data/gemfiles/Gemfile.rails51 +0 -9
- data/test/file/acro_form_test.rb +0 -39
- data/test/file/base_test.rb +0 -54
- data/test/file/delimited_test.rb +0 -233
- data/test/file/docx_test.rb +0 -53
- data/test/file/excel_test.rb +0 -124
- data/test/file/pdf_test.rb +0 -36
- data/test/file/registry_test.rb +0 -62
- data/test/file/seven_zip_test.rb +0 -59
- data/test/file/text_test.rb +0 -92
- data/test/file/word_test.rb +0 -35
- data/test/file/xml_test.rb +0 -21
- data/test/file/zip_test.rb +0 -47
- data/test/fixed_width/table_test.rb +0 -35
- data/test/helpers/file/delimited_test.rb +0 -105
- data/test/helpers/file/excel_test.rb +0 -82
- data/test/helpers/file/pdf_test.rb +0 -27
- data/test/helpers/file/word_test.rb +0 -26
- data/test/helpers/file/xml_test.rb +0 -131
- data/test/helpers/file/zip_test.rb +0 -75
- data/test/mapper_test.rb +0 -676
- data/test/non_tabular/mapping_test.rb +0 -36
- data/test/non_tabular/table_test.rb +0 -590
- data/test/non_tabular_file_helper_test.rb +0 -501
- data/test/pdf_form/table_test.rb +0 -119
- data/test/readme_test.rb +0 -53
- data/test/resources/acro_form.pdf +0 -0
- data/test/resources/blank_tab_test.xlsx +0 -0
- data/test/resources/bomd.csv +0 -3
- data/test/resources/broken.csv +0 -3
- data/test/resources/filesystem_paths.yml +0 -26
- data/test/resources/flat_file.pdf +0 -0
- data/test/resources/flat_file.txt +0 -27
- data/test/resources/flat_file.yml +0 -20
- data/test/resources/hello_utf16be.txt +0 -0
- data/test/resources/hello_utf16le.txt +0 -0
- data/test/resources/hello_utf8.txt +0 -2
- data/test/resources/hello_windows.txt +0 -2
- data/test/resources/hello_world.doc +0 -0
- data/test/resources/hello_world.docx +0 -0
- data/test/resources/hello_world.pdf +0 -0
- data/test/resources/hello_world.txt +0 -2
- data/test/resources/high_ascii_delimited.txt +0 -2
- data/test/resources/high_ascii_delimited_example_two.txt +0 -3
- data/test/resources/malformed.csv +0 -3
- data/test/resources/malformed.xml +0 -6
- data/test/resources/malformed_pipe.csv +0 -3
- data/test/resources/normal.7z +0 -0
- data/test/resources/normal.csv +0 -3
- data/test/resources/normal.csv.zip +0 -0
- data/test/resources/normal_pipe.csv +0 -3
- data/test/resources/normal_thorn.csv +0 -3
- data/test/resources/not_a_pdf.pdf +0 -0
- data/test/resources/not_a_word_file.doc +0 -0
- data/test/resources/not_a_word_file.docx +0 -0
- data/test/resources/not_sign_delimited.txt +0 -3
- data/test/resources/password_protected_hello_world.docx +0 -0
- data/test/resources/password_protected_sample_xlsx.xlsx +0 -0
- data/test/resources/sample.xml +0 -34
- data/test/resources/sample_xls.xls +0 -0
- data/test/resources/sample_xlsx.xlsx +0 -0
- data/test/resources/sheet_streaming.xls +0 -0
- data/test/resources/sheet_streaming.xlsx +0 -0
- data/test/resources/standard_mappings.yml +0 -39
- data/test/resources/txt_file_xls_extension.xls +0 -1
- data/test/resources/txt_file_xlsx_extension.xlsx +0 -1
- data/test/resources/utf-16be_xml.xml +0 -0
- data/test/resources/utf-16be_xml_with_declaration.xml +0 -0
- data/test/resources/utf-16le_xml.xml +0 -0
- data/test/resources/utf-8_xml.xml +0 -9
- data/test/resources/windows-1252_xml.xml +0 -9
- data/test/resources/windows.csv +0 -5
- data/test/resources/xlsx_file_xls_extension.xls +0 -0
- data/test/standard_mappings_test.rb +0 -22
- data/test/table_test.rb +0 -545
- data/test/test_helper.rb +0 -35
- data/test/universal_importer_helper_test.rb +0 -86
- data/test/xml/table_test.rb +0 -90
data/gemfiles/Gemfile.rails50
DELETED
data/gemfiles/Gemfile.rails51
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# ooxml_decrypt is not published to rubygems.org so we need to specify where it is
|
4
|
-
gem 'ooxml_decrypt', git: 'https://github.com/timgentry/ooxml_decrypt'
|
5
|
-
|
6
|
-
gemspec path: '..'
|
7
|
-
|
8
|
-
# Minitest 5.10.2 doesn't work with Rails version
|
9
|
-
gem 'minitest', '< 5.10.2'
|
data/test/file/acro_form_test.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'ndr_import/file/acro_form'
|
3
|
-
|
4
|
-
module NdrImport
|
5
|
-
module File
|
6
|
-
# Acro Form file handler tests
|
7
|
-
class AcroFormTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
@permanent_test_files = SafePath.new('permanent_test_files')
|
10
|
-
end
|
11
|
-
|
12
|
-
test 'should read pdf correctly' do
|
13
|
-
file_path = @permanent_test_files.join('acro_form.pdf')
|
14
|
-
handler = NdrImport::File::AcroForm.new(file_path, nil)
|
15
|
-
|
16
|
-
expected_row = { 'Group3' => nil,
|
17
|
-
'Textbox1_required' => nil,
|
18
|
-
'Textbox2_required' => nil,
|
19
|
-
'List Box_required' => '3',
|
20
|
-
'Dropdown_required' => '3',
|
21
|
-
'Textbox1_optional' => nil,
|
22
|
-
'Textbox2_optional' => nil,
|
23
|
-
'List Box_optional' => '3',
|
24
|
-
'Dropdown_optional' => '3',
|
25
|
-
'Date_required' => nil,
|
26
|
-
'Date__optional' => nil,
|
27
|
-
'Textbox3_numerical_required' => nil,
|
28
|
-
'Textbox3_numerical_optional' => nil }
|
29
|
-
|
30
|
-
handler.tables.each do |tablename, sheet|
|
31
|
-
assert_nil tablename
|
32
|
-
assert_instance_of Enumerator, sheet
|
33
|
-
assert_equal 1, sheet.to_a.size
|
34
|
-
assert_equal expected_row, sheet.to_a.first
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
data/test/file/base_test.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'ndr_import/file/registry'
|
3
|
-
|
4
|
-
module NdrImport
|
5
|
-
module File
|
6
|
-
# Base file handler tests
|
7
|
-
class BaseTest < ActiveSupport::TestCase
|
8
|
-
# Handles a single table file, but for test purposes,
|
9
|
-
# I could be bothered to implement it fully
|
10
|
-
class SingleTableLazyDeveloper < ::NdrImport::File::Base
|
11
|
-
end
|
12
|
-
|
13
|
-
def setup
|
14
|
-
@permanent_test_files = SafePath.new('permanent_test_files')
|
15
|
-
end
|
16
|
-
|
17
|
-
test 'should fail on not implementing rows' do
|
18
|
-
begin
|
19
|
-
Registry.register(SingleTableLazyDeveloper, 'lazy_dev')
|
20
|
-
|
21
|
-
exception = assert_raises(RuntimeError) do
|
22
|
-
file_path = @permanent_test_files.join('normal.csv')
|
23
|
-
handler = SingleTableLazyDeveloper.new(file_path, 'lazy_dev')
|
24
|
-
|
25
|
-
handler.tables.each do |tablename, sheet|
|
26
|
-
assert_nil tablename
|
27
|
-
assert_instance_of Enumerator, sheet
|
28
|
-
sheet.to_a
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
msg = 'Implement NdrImport::File::BaseTest::SingleTableLazyDeveloper#rows'
|
33
|
-
assert_equal msg, exception.message
|
34
|
-
ensure
|
35
|
-
Registry.unregister('lazy_dev')
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
test 'should not fail when set up with an readable safepath' do
|
40
|
-
assert Base.new(SafePath.new('test_space_r'), nil)
|
41
|
-
assert Base.new(SafePath.new('test_space_rw'), nil)
|
42
|
-
end
|
43
|
-
|
44
|
-
test 'should fail when set up with an unreadable safepath' do
|
45
|
-
assert_raises(SecurityError) { Base.new(SafePath.new('test_space_w'), nil) }
|
46
|
-
end
|
47
|
-
|
48
|
-
test 'should fail when set up with a non-safepath' do
|
49
|
-
exception = assert_raises(ArgumentError) { Base.new(NdrImport.root, nil) }
|
50
|
-
assert exception.message =~ /file_name should be of type SafePath, but it is String/
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
data/test/file/delimited_test.rb
DELETED
@@ -1,233 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'ndr_import/file/delimited'
|
3
|
-
|
4
|
-
module NdrImport
|
5
|
-
module File
|
6
|
-
# Delimited file handler tests
|
7
|
-
class DelimitedTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
@permanent_test_files = SafePath.new('permanent_test_files')
|
10
|
-
end
|
11
|
-
|
12
|
-
test 'should read csv correctly' do
|
13
|
-
file_path = @permanent_test_files.join('normal.csv')
|
14
|
-
handler = NdrImport::File::Delimited.new(file_path, 'csv', 'col_sep' => nil)
|
15
|
-
handler.tables.each do |tablename, sheet|
|
16
|
-
assert_nil tablename
|
17
|
-
sheet = sheet.to_a
|
18
|
-
assert_equal(('A'..'Z').to_a, sheet[0])
|
19
|
-
assert_equal ['1'] * 26, sheet[1]
|
20
|
-
assert_equal ['2'] * 26, sheet[2]
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
test 'should read pipe correctly' do
|
25
|
-
file_path = @permanent_test_files.join('normal_pipe.csv')
|
26
|
-
handler = NdrImport::File::Delimited.new(file_path, 'delimited', 'col_sep' => '|')
|
27
|
-
handler.tables.each do |tablename, sheet|
|
28
|
-
assert_nil tablename
|
29
|
-
sheet = sheet.to_a
|
30
|
-
assert_equal(('A'..'Z').to_a, sheet[0])
|
31
|
-
assert_equal ['1'] * 26, sheet[1]
|
32
|
-
assert_equal ['2'] * 26, sheet[2]
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
test 'should read malformed pipe correctly' do
|
37
|
-
file_path = @permanent_test_files.join('malformed_pipe.csv')
|
38
|
-
handler = NdrImport::File::Delimited.new(file_path, 'delimited', 'col_sep' => '|',
|
39
|
-
'liberal_parsing' => 'true')
|
40
|
-
handler.tables.each do |tablename, sheet|
|
41
|
-
assert_nil tablename
|
42
|
-
sheet = sheet.to_a
|
43
|
-
assert_equal(('A'..'Z').to_a, sheet[0])
|
44
|
-
assert_equal ['1'] * 26, sheet[1]
|
45
|
-
expected_row = ['2'] * 25
|
46
|
-
expected_row << '2"malformed"'
|
47
|
-
assert_equal expected_row, sheet[2].sort
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
test 'should read thorn correctly' do
|
52
|
-
file_path = @permanent_test_files.join('normal_thorn.csv')
|
53
|
-
handler = NdrImport::File::Delimited.new(file_path, 'delimited', 'col_sep' => "\xfe")
|
54
|
-
handler.tables.each do |tablename, sheet|
|
55
|
-
assert_nil tablename
|
56
|
-
sheet = sheet.to_a
|
57
|
-
assert_equal(('A'..'Z').to_a, sheet[0])
|
58
|
-
assert_equal ['1'] * 26, sheet[1]
|
59
|
-
assert_equal ['2'] * 26, sheet[2]
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
test 'should read csv with a BOM' do
|
64
|
-
file_path = @permanent_test_files.join('bomd.csv')
|
65
|
-
handler = NdrImport::File::Delimited.new(file_path, 'csv', 'col_sep' => nil)
|
66
|
-
handler.tables.each do |tablename, sheet|
|
67
|
-
assert_nil tablename
|
68
|
-
assert_instance_of Enumerator, sheet
|
69
|
-
sheet = sheet.to_a
|
70
|
-
assert_equal(('A'..'Z').to_a, sheet[0])
|
71
|
-
assert_equal ['1'] * 26, sheet[1]
|
72
|
-
assert_equal ['2'] * 26, sheet[2]
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
test 'should read windows-1252 csv' do
|
77
|
-
file_path = @permanent_test_files.join('windows.csv')
|
78
|
-
handler = NdrImport::File::Delimited.new(file_path, 'csv', 'col_sep' => nil)
|
79
|
-
handler.tables.each do |tablename, sheet|
|
80
|
-
assert_nil tablename
|
81
|
-
assert_instance_of Enumerator, sheet
|
82
|
-
sheet = sheet.to_a
|
83
|
-
assert_equal 1, sheet.length
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
test 'should read acsii-delimited csv' do
|
88
|
-
file_path = @permanent_test_files.join('high_ascii_delimited.txt')
|
89
|
-
handler = NdrImport::File::Delimited.new(file_path, 'delimited', 'col_sep' => "\xfe")
|
90
|
-
handler.tables.each do |tablename, sheet|
|
91
|
-
assert_nil tablename
|
92
|
-
assert_instance_of Enumerator, sheet
|
93
|
-
sheet = sheet.to_a
|
94
|
-
assert_equal 2, sheet.length
|
95
|
-
assert_equal '1234567890', sheet[0][1]
|
96
|
-
assert_equal '1234567890', sheet[1][1]
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
test 'should read acsii-delimited txt' do
|
101
|
-
rows = []
|
102
|
-
file_path = @permanent_test_files.join('high_ascii_delimited_example_two.txt')
|
103
|
-
handler = NdrImport::File::Delimited.new(file_path, 'delimited', 'col_sep' => "\xfd")
|
104
|
-
handler.tables.each do |tablename, sheet|
|
105
|
-
assert_nil tablename
|
106
|
-
assert_instance_of Enumerator, sheet
|
107
|
-
sheet.each do |row|
|
108
|
-
rows << row
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
assert_equal(('A'..'Z').to_a, rows[0])
|
113
|
-
assert_equal ['1'] * 26, rows[1]
|
114
|
-
assert_equal ['2'] * 26, rows[2]
|
115
|
-
end
|
116
|
-
|
117
|
-
test 'should read malformed delimited txt' do
|
118
|
-
rows = []
|
119
|
-
file_path = @permanent_test_files.join('malformed.csv')
|
120
|
-
handler = NdrImport::File::Delimited.new(file_path, 'csv', 'col_sep' => nil,
|
121
|
-
'liberal_parsing' => 'true')
|
122
|
-
handler.tables.each do |tablename, sheet|
|
123
|
-
assert_nil tablename
|
124
|
-
assert_instance_of Enumerator, sheet
|
125
|
-
sheet.each do |row|
|
126
|
-
rows << row
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
assert_equal(('A'..'Z').to_a, rows[0])
|
131
|
-
assert_equal ['1'] * 26, rows[1]
|
132
|
-
expected_row = ['2'] * 25
|
133
|
-
expected_row << '2"malformed"'
|
134
|
-
assert_equal expected_row, rows[2].sort
|
135
|
-
end
|
136
|
-
|
137
|
-
test 'should fail to read malformed delimited txt without liberal_parsing' do
|
138
|
-
rows_yielded = []
|
139
|
-
exception = assert_raises(CSVLibrary::MalformedCSVError) do
|
140
|
-
file_path = @permanent_test_files.join('malformed.csv')
|
141
|
-
handler = NdrImport::File::Delimited.new(file_path, 'csv')
|
142
|
-
|
143
|
-
handler.tables.each do |tablename, sheet|
|
144
|
-
assert_nil tablename
|
145
|
-
assert_instance_of Enumerator, sheet
|
146
|
-
sheet.each do |row|
|
147
|
-
rows_yielded << row
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
assert rows_yielded.empty?, 'no rows should have been yielded'
|
153
|
-
|
154
|
-
msg = 'Invalid CSV format on row 3 of malformed.csv. Original: Illegal quoting in line 3.'
|
155
|
-
assert_equal msg, exception.message
|
156
|
-
end
|
157
|
-
|
158
|
-
test 'should read line-by-line' do
|
159
|
-
rows = []
|
160
|
-
file_path = @permanent_test_files.join('normal.csv')
|
161
|
-
handler = NdrImport::File::Delimited.new(file_path, 'csv')
|
162
|
-
|
163
|
-
handler.tables.each do |tablename, sheet|
|
164
|
-
assert_nil tablename
|
165
|
-
assert_instance_of Enumerator, sheet
|
166
|
-
sheet.each do |row|
|
167
|
-
rows << row
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
assert_equal(('A'..'Z').to_a, rows[0])
|
172
|
-
assert_equal ['1'] * 26, rows[1]
|
173
|
-
assert_equal ['2'] * 26, rows[2]
|
174
|
-
end
|
175
|
-
|
176
|
-
test 'should report addition details upon failure to slurp csv' do
|
177
|
-
exception = assert_raises(CSVLibrary::MalformedCSVError) do
|
178
|
-
file_path = @permanent_test_files.join('broken.csv')
|
179
|
-
handler = NdrImport::File::Delimited.new(file_path, 'csv', 'col_sep' => nil)
|
180
|
-
|
181
|
-
handler.tables.each do |tablename, sheet|
|
182
|
-
assert_nil tablename
|
183
|
-
assert_instance_of Enumerator, sheet
|
184
|
-
sheet.to_a
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
assert_match(/Invalid CSV format on row 2 of broken\.csv\./, exception.message)
|
189
|
-
assert_match(CORRUPTED_QUOTES_MESSAGE_PATTERN, exception.message)
|
190
|
-
assert_match(/in line 2/, exception.message)
|
191
|
-
end
|
192
|
-
|
193
|
-
test 'should report addition details upon failure to read csv line-by-line' do
|
194
|
-
rows_yielded = []
|
195
|
-
exception = assert_raises(CSVLibrary::MalformedCSVError) do
|
196
|
-
file_path = @permanent_test_files.join('broken.csv')
|
197
|
-
handler = NdrImport::File::Delimited.new(file_path, 'csv')
|
198
|
-
|
199
|
-
handler.tables.each do |tablename, sheet|
|
200
|
-
assert_nil tablename
|
201
|
-
assert_instance_of Enumerator, sheet
|
202
|
-
sheet.each do |row|
|
203
|
-
rows_yielded << row
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
assert rows_yielded.empty?, 'no rows should have been yielded'
|
209
|
-
|
210
|
-
assert_match(/Invalid CSV format on row 2 of broken\.csv\./, exception.message)
|
211
|
-
assert_match(CORRUPTED_QUOTES_MESSAGE_PATTERN, exception.message)
|
212
|
-
assert_match(/in line 2/, exception.message)
|
213
|
-
end
|
214
|
-
|
215
|
-
test 'should only determine encodings once' do
|
216
|
-
file_path = @permanent_test_files.join('normal.csv')
|
217
|
-
handler = NdrImport::File::Delimited.new(file_path, 'csv', 'col_sep' => nil)
|
218
|
-
|
219
|
-
handler.expects(try_each_encoding: { mode: 'r:bom|utf-8', col_sep: ',' }).once
|
220
|
-
|
221
|
-
2.times do
|
222
|
-
handler.tables.each do |tablename, sheet|
|
223
|
-
assert_nil tablename
|
224
|
-
sheet = sheet.to_a
|
225
|
-
assert_equal(('A'..'Z').to_a, sheet[0])
|
226
|
-
assert_equal ['1'] * 26, sheet[1]
|
227
|
-
assert_equal ['2'] * 26, sheet[2]
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
233
|
-
end
|
data/test/file/docx_test.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'ndr_import/file/docx'
|
3
|
-
|
4
|
-
module NdrImport
|
5
|
-
module File
|
6
|
-
# Word .docx document file handler tests
|
7
|
-
class DocxTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
@permanent_test_files = SafePath.new('permanent_test_files')
|
10
|
-
end
|
11
|
-
|
12
|
-
test 'should read word file' do
|
13
|
-
file_path = @permanent_test_files.join('hello_world.docx')
|
14
|
-
handler = NdrImport::File::Docx.new(file_path, nil)
|
15
|
-
handler.tables.each do |tablename, sheet|
|
16
|
-
assert_nil tablename
|
17
|
-
assert_instance_of Enumerator, sheet
|
18
|
-
assert_equal [
|
19
|
-
'Hello world, this is a modern word document',
|
20
|
-
'With more than one line of text',
|
21
|
-
'Three in fact'
|
22
|
-
], sheet.to_a
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
test 'should read password protected word file' do
|
27
|
-
file_path = @permanent_test_files.join('password_protected_hello_world.docx')
|
28
|
-
handler = NdrImport::File::Docx.new(file_path, nil, file_password: 'salad')
|
29
|
-
handler.tables.each do |tablename, sheet|
|
30
|
-
assert_nil tablename
|
31
|
-
assert_instance_of Enumerator, sheet
|
32
|
-
assert_equal [
|
33
|
-
'Hello world, this is a modern word document',
|
34
|
-
'With more than one line of text',
|
35
|
-
'Three in fact'
|
36
|
-
], sheet.to_a
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
test 'should raise exception on invalid word file' do
|
41
|
-
assert_raises RuntimeError do
|
42
|
-
file_path = @permanent_test_files.join('not_a_word_file.docx')
|
43
|
-
handler = NdrImport::File::Docx.new(file_path, nil)
|
44
|
-
handler.tables.each do |tablename, sheet|
|
45
|
-
assert_nil tablename
|
46
|
-
assert_instance_of Enumerator, sheet
|
47
|
-
sheet.to_a
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
data/test/file/excel_test.rb
DELETED
@@ -1,124 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'ndr_import/file/excel'
|
3
|
-
|
4
|
-
module NdrImport
|
5
|
-
module File
|
6
|
-
# Excel file handler tests
|
7
|
-
class ExcelTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
@permanent_test_files = SafePath.new('permanent_test_files')
|
10
|
-
end
|
11
|
-
|
12
|
-
test 'should read xls file' do
|
13
|
-
file_path = @permanent_test_files.join('sample_xls.xls')
|
14
|
-
handler = NdrImport::File::Excel.new(file_path, nil)
|
15
|
-
handler.tables.each do |tablename, sheet|
|
16
|
-
assert_equal 'Sheet1', tablename
|
17
|
-
assert_instance_of Enumerator, sheet
|
18
|
-
assert_equal %w(1A 1B), sheet.first
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
test 'should read xlsx file' do
|
23
|
-
file_path = @permanent_test_files.join('sample_xlsx.xlsx')
|
24
|
-
handler = NdrImport::File::Excel.new(file_path, nil)
|
25
|
-
handler.tables.each do |tablename, sheet|
|
26
|
-
assert_equal 'Sheet1', tablename
|
27
|
-
assert_instance_of Enumerator, sheet
|
28
|
-
assert_equal %w(1A 1B), sheet.first
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
test 'should read xlsx file with null sheet' do
|
33
|
-
file_path = @permanent_test_files.join('blank_tab_test.xlsx')
|
34
|
-
handler = NdrImport::File::Excel.new(file_path, nil)
|
35
|
-
|
36
|
-
sheets = handler.tables.map { |_tablename, sheet| sheet }
|
37
|
-
|
38
|
-
assert_equal %w(column_a column_b column_c column_d), sheets[0].next
|
39
|
-
assert_equal %w(11111 ABC123 8888888888 2), sheets[0].next
|
40
|
-
assert_equal %w(column_a column_b column_c column_d), sheets[1].next
|
41
|
-
assert_equal %w(11111 ABC123 8888888888 3), sheets[1].next
|
42
|
-
assert_raises(StopIteration) { sheets[2].next }
|
43
|
-
end
|
44
|
-
|
45
|
-
%w(sheet_streaming.xlsx sheet_streaming.xls).each do |filename|
|
46
|
-
test "should be able to stream from multiple sheets at once from #{filename}" do
|
47
|
-
file_path = @permanent_test_files.join(filename)
|
48
|
-
handler = NdrImport::File::Excel.new(file_path, nil)
|
49
|
-
|
50
|
-
sheets = handler.tables.map { |_tablename, sheet| sheet }
|
51
|
-
|
52
|
-
assert_equal %w(1A1 1B1), sheets[0].next
|
53
|
-
assert_raises(StopIteration) { sheets[2].next }
|
54
|
-
assert_equal %w(2A1 2B1), sheets[1].next
|
55
|
-
assert_equal %w(2A2 2B2), sheets[1].next
|
56
|
-
assert_equal %w(1A2 1B2), sheets[0].next
|
57
|
-
assert_raises(StopIteration) { sheets[2].next }
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
test 'should read xlsx file with the incorrect xls extension' do
|
62
|
-
file_path = @permanent_test_files.join('xlsx_file_xls_extension.xls')
|
63
|
-
handler = NdrImport::File::Excel.new(file_path, nil)
|
64
|
-
handler.tables.each do |tablename, sheet|
|
65
|
-
assert_equal 'Sheet1', tablename
|
66
|
-
assert_instance_of Enumerator, sheet
|
67
|
-
assert_equal %w(1A 1B), sheet.first
|
68
|
-
end
|
69
|
-
|
70
|
-
SafeFile.delete @permanent_test_files.join('xlsx_file_xls_extension_amend.xlsx')
|
71
|
-
end
|
72
|
-
|
73
|
-
test 'should read password protected xlsx file' do
|
74
|
-
file_path = @permanent_test_files.join('password_protected_sample_xlsx.xlsx')
|
75
|
-
handler = NdrImport::File::Excel.new(file_path, nil, file_password: 'carrot')
|
76
|
-
handler.tables.each do |tablename, sheet|
|
77
|
-
assert_equal 'Sheet1', tablename
|
78
|
-
assert_instance_of Enumerator, sheet
|
79
|
-
assert_equal %w(1A 1B), sheet.first
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
test 'read_excel_file helper should handle exceptions' do
|
84
|
-
# txt file
|
85
|
-
assert_raises RuntimeError do
|
86
|
-
file_path = @permanent_test_files.join('flat_file.txt')
|
87
|
-
handler = NdrImport::File::Excel.new(file_path, nil)
|
88
|
-
|
89
|
-
handler.tables.each do |tablename, sheet|
|
90
|
-
assert_nil tablename
|
91
|
-
assert_instance_of Enumerator, sheet
|
92
|
-
sheet.to_a
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
# .txt file in .xls extension
|
97
|
-
assert_raises RuntimeError do
|
98
|
-
file_path = @permanent_test_files.join('txt_file_xls_extension.xls')
|
99
|
-
handler = NdrImport::File::Excel.new(file_path, 'txt')
|
100
|
-
|
101
|
-
handler.tables.each do |tablename, sheet|
|
102
|
-
assert_nil tablename
|
103
|
-
assert_instance_of Enumerator, sheet
|
104
|
-
sheet.to_a
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
# .txt file in .xlsx extension
|
109
|
-
assert_raises RuntimeError do
|
110
|
-
file_path = @permanent_test_files.join('txt_file_xlsx_extension.xlsx')
|
111
|
-
handler = NdrImport::File::Excel.new(file_path, 'txt')
|
112
|
-
|
113
|
-
handler.tables.each do |tablename, sheet|
|
114
|
-
assert_nil tablename
|
115
|
-
assert_instance_of Enumerator, sheet
|
116
|
-
sheet.to_a
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
SafeFile.delete @permanent_test_files.join('txt_file_xls_extension_amend.xlsx')
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|