ndr_import 3.0.0
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.
- checksums.yaml +15 -0
- data/.gitignore +14 -0
- data/.rubocop.yml +27 -0
- data/.ruby-version +1 -0
- data/.travis.yml +22 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/Guardfile +16 -0
- data/LICENSE.txt +21 -0
- data/README.md +69 -0
- data/Rakefile +13 -0
- data/code_safety.yml +374 -0
- data/gemfiles/Gemfile.rails32 +5 -0
- data/gemfiles/Gemfile.rails32.lock +142 -0
- data/gemfiles/Gemfile.rails41 +5 -0
- data/gemfiles/Gemfile.rails41.lock +145 -0
- data/gemfiles/Gemfile.rails42 +5 -0
- data/gemfiles/Gemfile.rails42.lock +145 -0
- data/lib/ndr_import.rb +13 -0
- data/lib/ndr_import/csv_library.rb +40 -0
- data/lib/ndr_import/file/all.rb +8 -0
- data/lib/ndr_import/file/base.rb +76 -0
- data/lib/ndr_import/file/delimited.rb +86 -0
- data/lib/ndr_import/file/excel.rb +131 -0
- data/lib/ndr_import/file/pdf.rb +38 -0
- data/lib/ndr_import/file/registry.rb +50 -0
- data/lib/ndr_import/file/text.rb +52 -0
- data/lib/ndr_import/file/word.rb +30 -0
- data/lib/ndr_import/file/zip.rb +67 -0
- data/lib/ndr_import/helpers/file/delimited.rb +105 -0
- data/lib/ndr_import/helpers/file/excel.rb +181 -0
- data/lib/ndr_import/helpers/file/pdf.rb +29 -0
- data/lib/ndr_import/helpers/file/word.rb +27 -0
- data/lib/ndr_import/helpers/file/xml.rb +45 -0
- data/lib/ndr_import/helpers/file/zip.rb +44 -0
- data/lib/ndr_import/mapper.rb +220 -0
- data/lib/ndr_import/mapping_error.rb +5 -0
- data/lib/ndr_import/non_tabular/column_mapping.rb +73 -0
- data/lib/ndr_import/non_tabular/line.rb +46 -0
- data/lib/ndr_import/non_tabular/mapping.rb +35 -0
- data/lib/ndr_import/non_tabular/record.rb +99 -0
- data/lib/ndr_import/non_tabular/table.rb +193 -0
- data/lib/ndr_import/non_tabular_file_helper.rb +160 -0
- data/lib/ndr_import/standard_mappings.rb +23 -0
- data/lib/ndr_import/table.rb +179 -0
- data/lib/ndr_import/version.rb +4 -0
- data/ndr_import.gemspec +44 -0
- data/test/file/base_test.rb +54 -0
- data/test/file/delimited_test.rb +143 -0
- data/test/file/excel_test.rb +85 -0
- data/test/file/pdf_test.rb +35 -0
- data/test/file/registry_test.rb +60 -0
- data/test/file/text_test.rb +92 -0
- data/test/file/word_test.rb +35 -0
- data/test/file/zip_test.rb +47 -0
- data/test/helpers/file/delimited_test.rb +113 -0
- data/test/helpers/file/excel_test.rb +97 -0
- data/test/helpers/file/pdf_test.rb +26 -0
- data/test/helpers/file/word_test.rb +26 -0
- data/test/helpers/file/xml_test.rb +131 -0
- data/test/helpers/file/zip_test.rb +75 -0
- data/test/mapper_test.rb +551 -0
- data/test/non_tabular/mapping_test.rb +36 -0
- data/test/non_tabular/table_test.rb +510 -0
- data/test/non_tabular_file_helper_test.rb +501 -0
- data/test/readme_test.rb +53 -0
- data/test/resources/bomd.csv +3 -0
- data/test/resources/broken.csv +3 -0
- data/test/resources/filesystem_paths.yml +26 -0
- data/test/resources/flat_file.pdf +0 -0
- data/test/resources/flat_file.txt +27 -0
- data/test/resources/flat_file.yml +20 -0
- data/test/resources/hello_utf16be.txt +0 -0
- data/test/resources/hello_utf16le.txt +0 -0
- data/test/resources/hello_utf8.txt +2 -0
- data/test/resources/hello_windows.txt +2 -0
- data/test/resources/hello_world.doc +0 -0
- data/test/resources/hello_world.pdf +0 -0
- data/test/resources/hello_world.txt +2 -0
- data/test/resources/high_ascii_delimited.txt +2 -0
- data/test/resources/malformed.xml +6 -0
- data/test/resources/normal.csv +3 -0
- data/test/resources/normal.csv.zip +0 -0
- data/test/resources/normal_pipe.csv +3 -0
- data/test/resources/normal_thorn.csv +3 -0
- data/test/resources/not_a_pdf.pdf +0 -0
- data/test/resources/not_a_word_file.doc +0 -0
- data/test/resources/sample_xls.xls +0 -0
- data/test/resources/sample_xlsx.xlsx +0 -0
- data/test/resources/standard_mappings.yml +39 -0
- data/test/resources/txt_file_xls_extension.xls +1 -0
- data/test/resources/txt_file_xlsx_extension.xlsx +1 -0
- 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 +9 -0
- data/test/resources/windows-1252_xml.xml +9 -0
- data/test/resources/windows.csv +5 -0
- data/test/resources/xlsx_file_xls_extension.xls +0 -0
- data/test/standard_mappings_test.rb +22 -0
- data/test/table_test.rb +288 -0
- data/test/test_helper.rb +13 -0
- metadata +443 -0
|
@@ -0,0 +1,85 @@
|
|
|
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 the incorrect xls extension' do
|
|
33
|
+
file_path = @permanent_test_files.join('xlsx_file_xls_extension.xls')
|
|
34
|
+
handler = NdrImport::File::Excel.new(file_path, nil)
|
|
35
|
+
handler.tables.each do |tablename, sheet|
|
|
36
|
+
assert_equal 'Sheet1', tablename
|
|
37
|
+
assert_instance_of Enumerator, sheet
|
|
38
|
+
assert_equal %w(1A 1B), sheet.first
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
SafeFile.delete @permanent_test_files.join('xlsx_file_xls_extension_amend.xlsx')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
test 'read_excel_file helper should handle exceptions' do
|
|
45
|
+
# txt file
|
|
46
|
+
assert_raises RuntimeError do
|
|
47
|
+
file_path = @permanent_test_files.join('flat_file.txt')
|
|
48
|
+
handler = NdrImport::File::Excel.new(file_path, nil)
|
|
49
|
+
|
|
50
|
+
handler.tables.each do |tablename, sheet|
|
|
51
|
+
assert_nil tablename
|
|
52
|
+
assert_instance_of Enumerator, sheet
|
|
53
|
+
sheet.to_a
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# .txt file in .xls extension
|
|
58
|
+
assert_raises RuntimeError do
|
|
59
|
+
file_path = @permanent_test_files.join('txt_file_xls_extension.xls')
|
|
60
|
+
handler = NdrImport::File::Excel.new(file_path, 'txt')
|
|
61
|
+
|
|
62
|
+
handler.tables.each do |tablename, sheet|
|
|
63
|
+
assert_nil tablename
|
|
64
|
+
assert_instance_of Enumerator, sheet
|
|
65
|
+
sheet.to_a
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# .txt file in .xlsx extension
|
|
70
|
+
assert_raises RuntimeError do
|
|
71
|
+
file_path = @permanent_test_files.join('txt_file_xlsx_extension.xlsx')
|
|
72
|
+
handler = NdrImport::File::Excel.new(file_path, 'txt')
|
|
73
|
+
|
|
74
|
+
handler.tables.each do |tablename, sheet|
|
|
75
|
+
assert_nil tablename
|
|
76
|
+
assert_instance_of Enumerator, sheet
|
|
77
|
+
sheet.to_a
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
SafeFile.delete @permanent_test_files.join('txt_file_xls_extension_amend.xlsx')
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'ndr_import/file/pdf'
|
|
3
|
+
|
|
4
|
+
module NdrImport
|
|
5
|
+
module File
|
|
6
|
+
# PDF file handler tests
|
|
7
|
+
class PdfTest < 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('hello_world.pdf')
|
|
14
|
+
handler = NdrImport::File::Pdf.new(file_path, nil)
|
|
15
|
+
handler.tables.each do |tablename, sheet|
|
|
16
|
+
assert_nil tablename
|
|
17
|
+
assert_instance_of Enumerator, sheet
|
|
18
|
+
assert_equal ['Hello World'], sheet.to_a
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
test 'should raise exception on invalid pdf file' do
|
|
23
|
+
assert_raises RuntimeError do
|
|
24
|
+
file_path = @permanent_test_files.join('not_a_pdf.pdf')
|
|
25
|
+
handler = NdrImport::File::Pdf.new(file_path, nil)
|
|
26
|
+
handler.tables.each do |tablename, sheet|
|
|
27
|
+
assert_nil tablename
|
|
28
|
+
assert_instance_of Enumerator, sheet
|
|
29
|
+
sheet.to_a
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'ndr_import/file/registry'
|
|
3
|
+
|
|
4
|
+
module NdrImport
|
|
5
|
+
module File
|
|
6
|
+
# Registry file handler tests
|
|
7
|
+
class RegistryTest < ActiveSupport::TestCase
|
|
8
|
+
def setup
|
|
9
|
+
@home = SafePath.new('test_space_rw')
|
|
10
|
+
@permanent_test_files = SafePath.new('permanent_test_files')
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
test 'Registry.handlers' do
|
|
14
|
+
assert_instance_of Hash, NdrImport::File::Registry.handlers
|
|
15
|
+
assert_equal %w(csv doc pdf pipe thorn txt xls xlsx zip),
|
|
16
|
+
NdrImport::File::Registry.handlers.keys.sort
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
test 'should fail to enumerate unknown format' do
|
|
20
|
+
exception = assert_raises(RuntimeError) do
|
|
21
|
+
file_path = @permanent_test_files.join('normal.csv')
|
|
22
|
+
tables = NdrImport::File::Registry.tables(file_path, 'mp3')
|
|
23
|
+
tables.each do |tablename, sheet|
|
|
24
|
+
assert_nil tablename
|
|
25
|
+
sheet.to_a
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
assert_equal 'Error: Unknown file format "mp3"', exception.message
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
test 'should enumerate pdf file table' do
|
|
33
|
+
file_path = @permanent_test_files.join('hello_world.pdf')
|
|
34
|
+
tables = NdrImport::File::Registry.tables(file_path, nil)
|
|
35
|
+
tables.each do |tablename, sheet|
|
|
36
|
+
assert_nil tablename
|
|
37
|
+
assert_instance_of Enumerator, sheet
|
|
38
|
+
assert_equal ['Hello World'], sheet.to_a
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
test 'should enumerate zip file tables' do
|
|
43
|
+
options = { 'unzip_path' => @home }
|
|
44
|
+
file_path = @permanent_test_files.join('normal.csv.zip')
|
|
45
|
+
files = NdrImport::File::Registry.files(file_path, options)
|
|
46
|
+
files.each do |filename|
|
|
47
|
+
tables = NdrImport::File::Registry.tables(filename, nil, options)
|
|
48
|
+
|
|
49
|
+
tables.each do |tablename, sheet|
|
|
50
|
+
assert_nil tablename
|
|
51
|
+
sheet = sheet.to_a
|
|
52
|
+
assert_equal(('A'..'Z').to_a, sheet[0])
|
|
53
|
+
assert_equal ['1'] * 26, sheet[1]
|
|
54
|
+
assert_equal ['2'] * 26, sheet[2]
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
require 'test_helper'
|
|
3
|
+
require 'ndr_import/file/text'
|
|
4
|
+
|
|
5
|
+
module NdrImport
|
|
6
|
+
module File
|
|
7
|
+
# Text file handler tests
|
|
8
|
+
class TextTest < ActiveSupport::TestCase
|
|
9
|
+
def setup
|
|
10
|
+
@permanent_test_files = SafePath.new('permanent_test_files')
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
test 'should read text file' do
|
|
14
|
+
file_path = @permanent_test_files.join('hello_world.txt')
|
|
15
|
+
handler = NdrImport::File::Text.new(file_path, nil)
|
|
16
|
+
handler.tables.each do |tablename, sheet|
|
|
17
|
+
assert_nil tablename
|
|
18
|
+
assert_instance_of Enumerator, sheet
|
|
19
|
+
assert_equal ['Hello world,', 'this is a text document'], sheet.to_a
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test 'should read text file with UTF-8 encoding' do
|
|
24
|
+
file_path = @permanent_test_files.join('hello_utf8.txt')
|
|
25
|
+
handler = NdrImport::File::Text.new(file_path, nil)
|
|
26
|
+
handler.tables.each do |tablename, sheet|
|
|
27
|
+
assert_nil tablename
|
|
28
|
+
assert_instance_of Enumerator, sheet
|
|
29
|
+
|
|
30
|
+
lines = sheet.to_a
|
|
31
|
+
|
|
32
|
+
assert_equal ['Hello world', 'This is a thorny þ issue!'], lines
|
|
33
|
+
assert lines.all? { |line| line.encoding.name == 'UTF-8' && line.valid_encoding? }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
test 'should read text file with UTF-16 [BE] encoding' do
|
|
38
|
+
file_path = @permanent_test_files.join('hello_utf16be.txt')
|
|
39
|
+
handler = NdrImport::File::Text.new(file_path, nil)
|
|
40
|
+
handler.tables.each do |tablename, sheet|
|
|
41
|
+
assert_nil tablename
|
|
42
|
+
assert_instance_of Enumerator, sheet
|
|
43
|
+
|
|
44
|
+
lines = sheet.to_a
|
|
45
|
+
|
|
46
|
+
assert_equal ['Hello world', 'This is a thorny þ issue!'], lines
|
|
47
|
+
assert lines.all? { |line| line.encoding.name == 'UTF-8' && line.valid_encoding? }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
test 'should read text file with UTF-16 [LE] encoding' do
|
|
52
|
+
file_path = @permanent_test_files.join('hello_utf16le.txt')
|
|
53
|
+
handler = NdrImport::File::Text.new(file_path, nil)
|
|
54
|
+
handler.tables.each do |tablename, sheet|
|
|
55
|
+
assert_nil tablename
|
|
56
|
+
assert_instance_of Enumerator, sheet
|
|
57
|
+
|
|
58
|
+
lines = sheet.to_a
|
|
59
|
+
|
|
60
|
+
assert_equal ['Hello world', 'This is a thorny þ issue!'], lines
|
|
61
|
+
assert lines.all? { |line| line.encoding.name == 'UTF-8' && line.valid_encoding? }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
test 'should read text file with Windows-1252 encoding' do
|
|
66
|
+
file_path = @permanent_test_files.join('hello_windows.txt')
|
|
67
|
+
handler = NdrImport::File::Text.new(file_path, nil)
|
|
68
|
+
handler.tables.each do |tablename, sheet|
|
|
69
|
+
assert_nil tablename
|
|
70
|
+
assert_instance_of Enumerator, sheet
|
|
71
|
+
|
|
72
|
+
lines = sheet.to_a
|
|
73
|
+
|
|
74
|
+
assert_equal ['Hello windows world', 'This is a thorny þ issue!'], lines
|
|
75
|
+
assert lines.all? { |line| line.encoding.name == 'UTF-8' && line.valid_encoding? }
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
test 'should raise exception on invalid text file' do
|
|
80
|
+
assert_raises RuntimeError do
|
|
81
|
+
file_path = @permanent_test_files.join('hello_world.pdf')
|
|
82
|
+
handler = NdrImport::File::Text.new(file_path, nil)
|
|
83
|
+
handler.tables.each do |tablename, sheet|
|
|
84
|
+
assert_nil tablename
|
|
85
|
+
assert_instance_of Enumerator, sheet
|
|
86
|
+
sheet.to_a
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'ndr_import/file/pdf'
|
|
3
|
+
|
|
4
|
+
module NdrImport
|
|
5
|
+
module File
|
|
6
|
+
# Word document file handler tests
|
|
7
|
+
class WordTest < 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.doc')
|
|
14
|
+
handler = NdrImport::File::Word.new(file_path, nil)
|
|
15
|
+
handler.tables.each do |tablename, sheet|
|
|
16
|
+
assert_nil tablename
|
|
17
|
+
assert_instance_of Enumerator, sheet
|
|
18
|
+
assert_equal ['Hello world, this is a word document'], sheet.to_a
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
test 'should raise exception on invalid word file' do
|
|
23
|
+
assert_raises RuntimeError do
|
|
24
|
+
file_path = @permanent_test_files.join('not_a_word_file.doc')
|
|
25
|
+
handler = NdrImport::File::Word.new(file_path, nil)
|
|
26
|
+
handler.tables.each do |tablename, sheet|
|
|
27
|
+
assert_nil tablename
|
|
28
|
+
assert_instance_of Enumerator, sheet
|
|
29
|
+
sheet.to_a
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'ndr_import/file/zip'
|
|
3
|
+
require 'zip'
|
|
4
|
+
|
|
5
|
+
module NdrImport
|
|
6
|
+
module File
|
|
7
|
+
# Zip file handler tests
|
|
8
|
+
class ZipTest < ActiveSupport::TestCase
|
|
9
|
+
def setup
|
|
10
|
+
@home = SafePath.new('test_space_rw')
|
|
11
|
+
@permanent_test_files = SafePath.new('permanent_test_files')
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'should reject non SafePath arguments' do
|
|
15
|
+
file_path = @home.join('imaginary.zip')
|
|
16
|
+
|
|
17
|
+
assert_raises ArgumentError do
|
|
18
|
+
NdrImport::File::Zip.new(file_path.to_s, nil, 'unzip_path' => @home.to_s)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
assert_raises ArgumentError do
|
|
22
|
+
NdrImport::File::Zip.new(file_path.to_s, nil, 'unzip_path' => @home)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
assert_raises ArgumentError do
|
|
26
|
+
NdrImport::File::Zip.new(file_path, nil, 'unzip_path' => @home.to_s)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
test 'should read table correctly' do
|
|
31
|
+
options = { 'unzip_path' => @home }
|
|
32
|
+
file_path = @permanent_test_files.join('normal.csv.zip')
|
|
33
|
+
|
|
34
|
+
handler = NdrImport::File::Zip.new(file_path, nil, options)
|
|
35
|
+
handler.files.each do |filename|
|
|
36
|
+
assert_instance_of SafePath, filename
|
|
37
|
+
assert_equal 'normal.csv', ::File.basename(filename)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
exception = assert_raises RuntimeError do
|
|
41
|
+
handler.tables
|
|
42
|
+
end
|
|
43
|
+
assert_equal 'Zip#tables should never be called', exception.message
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'ndr_import/helpers/file/delimited'
|
|
3
|
+
|
|
4
|
+
# Delimited file helper tests
|
|
5
|
+
class DelimitedTest < ActiveSupport::TestCase
|
|
6
|
+
# This is a test importer class to test the Delimited file helper mixin
|
|
7
|
+
class TestImporter
|
|
8
|
+
include NdrImport::Helpers::File::Delimited
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def setup
|
|
12
|
+
@permanent_test_files = SafePath.new('permanent_test_files')
|
|
13
|
+
@importer = TestImporter.new
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
test 'should read csv correctly' do
|
|
17
|
+
rows = @importer.read_delimited_file(@permanent_test_files.join('normal.csv'), nil)
|
|
18
|
+
assert_equal(('A'..'Z').to_a, rows[0])
|
|
19
|
+
assert_equal ['1'] * 26, rows[1]
|
|
20
|
+
assert_equal ['2'] * 26, rows[2]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test 'should read csv with a BOM' do
|
|
24
|
+
rows = @importer.read_delimited_file(@permanent_test_files.join('bomd.csv'), nil)
|
|
25
|
+
assert_equal(('A'..'Z').to_a, rows[0])
|
|
26
|
+
assert_equal ['1'] * 26, rows[1]
|
|
27
|
+
assert_equal ['2'] * 26, rows[2]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
test 'should read windows-1252 csv' do
|
|
31
|
+
rows = @importer.read_delimited_file(@permanent_test_files.join('windows.csv'), nil)
|
|
32
|
+
assert_equal 1, rows.length
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
test 'should read acsii-delimited csv' do
|
|
36
|
+
rows = @importer.read_delimited_file(@permanent_test_files.join('high_ascii_delimited.txt'),
|
|
37
|
+
"\xfe")
|
|
38
|
+
assert_equal 2, rows.length
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
test 'should read line-by-line' do
|
|
42
|
+
rows = []
|
|
43
|
+
@importer.delimited_rows(@permanent_test_files.join('normal.csv')) { |row| rows << row }
|
|
44
|
+
assert_equal(('A'..'Z').to_a, rows[0])
|
|
45
|
+
assert_equal ['1'] * 26, rows[1]
|
|
46
|
+
assert_equal ['2'] * 26, rows[2]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
test 'should read line-by-line with custom delimiter' do
|
|
50
|
+
count = 0
|
|
51
|
+
file = @permanent_test_files.join('high_ascii_delimited.txt')
|
|
52
|
+
|
|
53
|
+
@importer.delimited_rows(file, "\xfe") { count += 1 }
|
|
54
|
+
assert_equal 2, count
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
test 'should report addition details upon failure to slurp csv' do
|
|
58
|
+
exception = assert_raises(CSVLibrary::MalformedCSVError) do
|
|
59
|
+
@importer.read_delimited_file(@permanent_test_files.join('broken.csv'), nil)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
msg = 'Invalid CSV format on row 2 of broken.csv. Original: Missing or stray quote in line 2'
|
|
63
|
+
assert_equal msg, exception.message
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
test 'should report addition details upon failure to read csv line-by-line' do
|
|
67
|
+
rows_yielded = []
|
|
68
|
+
exception = assert_raises(CSVLibrary::MalformedCSVError) do
|
|
69
|
+
@importer.delimited_rows(@permanent_test_files.join('broken.csv')) do |row|
|
|
70
|
+
rows_yielded << row
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
assert rows_yielded.empty?, 'no rows should have been yielded'
|
|
75
|
+
|
|
76
|
+
msg = 'Invalid CSV format on row 2 of broken.csv. Original: Missing or stray quote in line 2'
|
|
77
|
+
assert_equal msg, exception.message
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
test 'delimited_tables should read table correctly' do
|
|
81
|
+
table = @importer.send(:delimited_tables, @permanent_test_files.join('normal.csv'))
|
|
82
|
+
table.each do |tablename, sheet|
|
|
83
|
+
assert_nil tablename
|
|
84
|
+
sheet = sheet.to_a
|
|
85
|
+
assert_equal(('A'..'Z').to_a, sheet[0])
|
|
86
|
+
assert_equal ['1'] * 26, sheet[1]
|
|
87
|
+
assert_equal ['2'] * 26, sheet[2]
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def test_each_delimited_table_should_be_deprecated
|
|
92
|
+
original_stderr = $stderr
|
|
93
|
+
$stderr = StringIO.new
|
|
94
|
+
|
|
95
|
+
table = @importer.send(:each_delimited_table, @permanent_test_files.join('normal.csv'))
|
|
96
|
+
table.each do |tablename, sheet|
|
|
97
|
+
assert_nil tablename
|
|
98
|
+
sheet = sheet.to_a
|
|
99
|
+
assert_equal(('A'..'Z').to_a, sheet[0])
|
|
100
|
+
assert_equal ['1'] * 26, sheet[1]
|
|
101
|
+
assert_equal ['2'] * 26, sheet[2]
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
assert_match(/\A\[warning\] each_delimited_table will be deprecated/, $stderr.string)
|
|
105
|
+
ensure
|
|
106
|
+
$stderr = original_stderr
|
|
107
|
+
end if Gem::Requirement.new('< 3.0.0').satisfied_by?(Gem::Version.new(NdrImport::VERSION))
|
|
108
|
+
|
|
109
|
+
def test_deprecated_methods_removed_in_v3
|
|
110
|
+
refute @importer.public_methods.include?(:each_delimited_table), 'should be removed in v3.0.0'
|
|
111
|
+
refute @importer.public_methods.include?(:each_delimited_row), 'should be removed in v3.0.0'
|
|
112
|
+
end if Gem::Requirement.new('>= 3.0.0').satisfied_by?(Gem::Version.new(NdrImport::VERSION))
|
|
113
|
+
end
|