-
Notifications
You must be signed in to change notification settings - Fork 44
/
general_test.rb
121 lines (94 loc) · 3.28 KB
/
general_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require "test_helper"
class OdsGeneralTest < ActiveSupport::TestCase
test "kitchen sink" do
options = {
headers: [
['Latest Posts'],
['Title','Category','Author','Posted on','Posted At','Earnings']
],
data: 50.times.map{|i| [i, "foobar-#{i}", 5.4*i, true, Date.today, Time.now]},
header_style: {background_color: "000000", color: "FFFFFF", align: :center, font_size: 12, bold: true},
row_style: {background_color: nil, color: "000000", align: :left, font_size: 12},
sheet_name: 'Kitchen Sink',
freeze_headers: true,
column_types: [
:string,
:float,
:float,
:boolean,
:date,
:time,
nil
],
}
# Using Array Data
file_data = SpreadsheetArchitect.to_ods(options)
save_file("ods/kitchen_sink.ods", file_data)
end
test "multisheet" do
test_data = [[1,2,3], [4,5,6], [7,8,9]]
spreadsheet = Post.to_rodf_spreadsheet
spreadsheet = CustomPost.to_rodf_spreadsheet({sheet_name: 'Latest Projects'}, spreadsheet)
spreadsheet = SpreadsheetArchitect.to_rodf_spreadsheet({data: test_data, sheet_name: 'Another Sheet'}, spreadsheet)
save_file("ods/multi_sheet.ods", spreadsheet.bytes)
end
describe "column_types" do
test "validates provided types" do
assert_raise SpreadsheetArchitect::Exceptions::ArgumentError do
SpreadsheetArchitect.to_ods(
data: [],
column_types: [""]
)
end
SpreadsheetArchitect.to_ods(
data: [],
column_types: [:string, :float, :date, :time, :boolean, :hyperlink, nil, ->(x){ :foo }]
)
end
test "works with Proc types" do
url = "https://github.com/westonganger/spreadsheet_architect"
data = [
[1, 2],
[1, url],
]
ss = SpreadsheetArchitect.to_rodf_spreadsheet(
data: data,
column_types: [
:string,
->(x){ x.to_s.start_with?("http") ? :hyperlink : :string }
]
)
doc = parse_ods_spreadsheet(ss)
cells = doc.xpath("//table:table-cell")
hyperlinks = doc.xpath("//text:a")
assert_equal 4, cells.size
assert_equal 1, hyperlinks.size
cell = cells[3]
assert_equal url, cell.text
assert_equal url, cell.at_xpath(".//text:a").attributes["href"].value
save_file("ods/hyperlinks.ods", ss)
end
describe "hyperlinks" do
test "shows the text and have the correct attributes" do
url = "https://github.com/westonganger/spreadsheet_architect"
data = [
[1,2,3],
[1, url, "https://github.com/caxlsx/caxlsx"],
]
ss = SpreadsheetArchitect.to_rodf_spreadsheet(data: data, column_types: [:string, :hyperlink, :string])
doc = parse_ods_spreadsheet(ss)
cells = doc.xpath("//table:table-cell")
hyperlinks = doc.xpath("//text:a")
assert_equal 6, cells.size
assert_equal 2, hyperlinks.size
cell = cells[1]
assert_equal "2", cell.text
assert_equal "2", cell.at_xpath(".//text:a").attributes["href"].value
cell = cells[4]
assert_equal url, cell.text
assert_equal url, cell.at_xpath(".//text:a").attributes["href"].value
save_file("ods/hyperlinks.ods", ss)
end
end
end
end