Debian Codegraph @

test/formatters/test_csv.rb

raw
1 require "test_helper"
2
3 class TestRooFormatterCSV < Minitest::Test
4 def test_date_time_to_csv
5 with_each_spreadsheet(name: "time-test") do |workbook|
6 Dir.mktmpdir do |tempdir|
7 csv_output = File.join(tempdir, "time_test.csv")
8 assert workbook.to_csv(csv_output)
9 assert File.exist?(csv_output)
10 assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/time-test.csv #{csv_output}`
11 # --strip-trailing-cr is needed because the test-file use 0A and
12 # the test on an windows box generates 0D 0A as line endings
13 end
14 end
15 end
16
17 def test_boolean_to_csv
18 with_each_spreadsheet(name: "boolean") do |workbook|
19 Dir.mktmpdir do |tempdir|
20 csv_output = File.join(tempdir,"boolean.csv")
21 assert workbook.to_csv(csv_output)
22 assert File.exist?(csv_output)
23 assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/boolean.csv #{csv_output}`
24 # --strip-trailing-cr is needed because the test-file use 0A and
25 # the test on an windows box generates 0D 0A as line endings
26 end
27 end
28 end
29
30 def test_link_to_csv
31 with_each_spreadsheet(name: "link", format: :excelx) do |workbook|
32 Dir.mktmpdir do |tempdir|
33 csv_output = File.join(tempdir, "link.csv")
34 assert workbook.to_csv(csv_output)
35 assert File.exist?(csv_output)
36 assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/link.csv #{csv_output}`
37 # --strip-trailing-cr is needed because the test-file use 0A and
38 # the test on an windows box generates 0D 0A as line endings
39 end
40 end
41 end
42
43 # "/tmp/xxxx" darf man unter Windows nicht verwenden, weil das nicht erkannt
44 # wird.
45 # Besser: Methode um temporaeres Dir. portabel zu bestimmen
46 def test_huge_document_to_csv
47 skip_long_test
48
49 original_csv_path = File.join(TESTDIR, "Bibelbund.csv")
50 with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook|
51 Dir.mktmpdir do |tempdir|
52 new_csv_path = File.join(tempdir, "Bibelbund.csv")
53 assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", workbook.cell(45, "A")
54 assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", workbook.cell(46, "A")
55 assert_equal "Tagebuch aus Chile Juli 1977", workbook.cell(55, "A")
56 assert workbook.to_csv(new_csv_path)
57 assert File.exist?(new_csv_path)
58 assert FileUtils.identical?(original_csv_path, new_csv_path), "error in class #{workbook.class}"
59 end
60 end
61 end
62
63 def test_bug_empty_sheet
64 with_each_spreadsheet(name: "formula", format: [:openoffice, :excelx]) do |workbook|
65 workbook.default_sheet = "Sheet3" # is an empty sheet
66 Dir.mktmpdir do |tempdir|
67 workbook.to_csv(File.join(tempdir, "emptysheet.csv"))
68 assert_equal "", `cat #{File.join(tempdir, "emptysheet.csv")}`
69 end
70 end
71 end
72
73 def test_bug_quotes_excelx
74 skip_long_test
75 # TODO: run this test with a much smaller document
76 with_each_spreadsheet(name: "Bibelbund", format: [:openoffice, :excelx]) do |workbook|
77 workbook.default_sheet = workbook.sheets.first
78 assert_equal(
79 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
80 workbook.cell("A", 76)
81 )
82 workbook.to_csv("csv#{$$}")
83 assert_equal(
84 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
85 workbook.cell("A", 78)
86 )
87 File.delete_if_exist("csv#{$$}")
88 end
89 end
90
91 def test_bug_datetime_to_csv
92 with_each_spreadsheet(name: "datetime") do |workbook|
93 Dir.mktmpdir do |tempdir|
94 datetime_csv_file = File.join(tempdir, "datetime.csv")
95
96 assert workbook.to_csv(datetime_csv_file)
97 assert File.exist?(datetime_csv_file)
98 assert_equal "", file_diff("#{TESTDIR}/so_datetime.csv", datetime_csv_file)
99 end
100 end
101 end
102
103 def test_bug_datetime_offset_change
104 # DO NOT REMOVE Asia/Calcutta
105 [nil, "US/Eastern", "US/Pacific", "Asia/Calcutta"].each do |zone|
106 with_timezone(zone) do
107 with_each_spreadsheet(name: "datetime_timezone_ist_offset_change", format: %i[excelx openoffice libreoffice]) do |workbook|
108 Dir.mktmpdir do |tempdir|
109 datetime_csv_file = File.join(tempdir, "datetime_timezone_ist_offset_change.csv")
110
111 assert workbook.to_csv(datetime_csv_file)
112 assert File.exist?(datetime_csv_file)
113 assert_equal "", file_diff("#{TESTDIR}/so_datetime_timezone_ist_offset_change.csv", datetime_csv_file)
114 end
115 end
116 end
117 end
118 end
119
120 def test_true_class
121 assert_equal "true", cell_to_csv(1, 1)
122 end
123
124 def test_false_class
125 assert_equal "false", cell_to_csv(2, 1)
126 end
127
128 def test_date_class
129 assert_equal "2017-01-01", cell_to_csv(3, 1)
130 end
131
132 def cell_to_csv(row, col)
133 filename = File.join(TESTDIR, "formula_cell_types.xlsx")
134 Roo::Spreadsheet.open(filename).send("cell_to_csv", row, col, "Sheet1")
135 end
136 end