oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package xlsx_test
2
3import (
4 "bytes"
5 "testing"
6
7 "github.com/xuri/excelize/v2"
8
9 "amadan.net/oficina/sheets/internal/sheets/xlsx"
10)
11
12// widthFixture is a one-cell workbook with a deliberately wide column A
13// and a tall row 1, and everything else left at the workbook's default.
14func widthFixture(t *testing.T, width float64, height float64) *bytes.Buffer {
15 t.Helper()
16 f := excelize.NewFile()
17 defer f.Close()
18 sheet := f.GetSheetList()[0]
19 if err := f.SetCellValue(sheet, "A1", "Invoice Quarter"); err != nil {
20 t.Fatalf("set cell: %v", err)
21 }
22 if err := f.SetCellValue(sheet, "B1", "x"); err != nil {
23 t.Fatalf("set cell: %v", err)
24 }
25 if err := f.SetColWidth(sheet, "A", "A", width); err != nil {
26 t.Fatalf("set width: %v", err)
27 }
28 if err := f.SetRowHeight(sheet, 1, height); err != nil {
29 t.Fatalf("set height: %v", err)
30 }
31 var buf bytes.Buffer
32 if err := f.Write(&buf); err != nil {
33 t.Fatalf("write fixture: %v", err)
34 }
35 return &buf
36}
37
38func TestImportKeepsColumnWidths(t *testing.T) {
39 h := importReader(t, widthFixture(t, 30, 40))
40 defer h.Release()
41
42 cols, rows, err := h.Sizes()
43 if err != nil {
44 t.Fatalf("sizes: %v", err)
45 }
46 // 30 characters at Excel's 7px max-digit-width plus 5px padding.
47 if got := cols[0]; got != 215 {
48 t.Errorf("column A = %dpx, want 215 — an imported file whose author widened "+
49 "a column should arrive with it widened", got)
50 }
51 // A column the file said nothing about must stay unsized, not be
52 // pinned at whatever the default happened to be on import day.
53 if _, ok := cols[1]; ok {
54 t.Errorf("column B was given a width the file never set: %d", cols[1])
55 }
56 // 40 points at 96/72.
57 if got := rows[0]; got != 53 {
58 t.Errorf("row 1 = %dpx, want 53", got)
59 }
60}
61
62// TestSizesSurviveARoundTrip is the lossless-round-trip obligation,
63// applied to layout. A file that comes back with every column at the
64// default width is a different document from the one that went in.
65func TestSizesSurviveARoundTrip(t *testing.T) {
66 first := importReader(t, widthFixture(t, 30, 40))
67 var out bytes.Buffer
68 if err := xlsx.Export(first, &out); err != nil {
69 t.Fatalf("export: %v", err)
70 }
71 first.Release()
72
73 second := importReader(t, &out)
74 defer second.Release()
75
76 cols, rows, err := second.Sizes()
77 if err != nil {
78 t.Fatalf("sizes: %v", err)
79 }
80 // Within a pixel: the conversion goes through Excel's character
81 // units and back, and the rounding is not required to be exact —
82 // what must survive is the proportion somebody chose.
83 if got := cols[0]; got < 214 || got > 216 {
84 t.Errorf("after a round trip column A = %dpx, want 215±1 — export is dropping "+
85 "the author's layout", got)
86 }
87 if got := rows[0]; got < 52 || got > 54 {
88 t.Errorf("after a round trip row 1 = %dpx, want 53±1", got)
89 }
90}
91