| 1 | package xlsx_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "image" |
| 7 | "image/png" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/xuri/excelize/v2" |
| 12 | |
| 13 | "amadan.net/oficina/sheets/internal/sheets/xlsx" |
| 14 | ) |
| 15 | |
| 16 | // featureBook is a workbook carrying one of each feature the import |
| 17 | // leaves behind, built with excelize so each is written the way a real |
| 18 | // spreadsheet writes it. |
| 19 | func featureBook(t *testing.T) *bytes.Buffer { |
| 20 | t.Helper() |
| 21 | f := excelize.NewFile() |
| 22 | s := f.GetSheetList()[0] |
| 23 | must := func(err error) { |
| 24 | t.Helper() |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | } |
| 29 | for i, v := range []float64{1, 2, 3} { |
| 30 | cell, _ := excelize.CoordinatesToCellName(1, i+1) |
| 31 | must(f.SetCellFloat(s, cell, v, -1, 64)) |
| 32 | } |
| 33 | must(f.SetCellStr(s, "B1", canary)) |
| 34 | must(f.MergeCell(s, "B1", "C1")) |
| 35 | must(f.AddComment(s, excelize.Comment{Cell: "A1", Author: "a", Text: canary})) |
| 36 | dv := excelize.NewDataValidation(true) |
| 37 | dv.Sqref = "A1:A3" |
| 38 | must(dv.SetDropList([]string{"1", "2"})) |
| 39 | must(f.AddDataValidation(s, dv)) |
| 40 | style, err := f.NewConditionalStyle(&excelize.Style{Font: &excelize.Font{Bold: true}}) |
| 41 | must(err) |
| 42 | must(f.SetConditionalFormat(s, "A1:A3", []excelize.ConditionalFormatOptions{ |
| 43 | {Type: "cell", Criteria: ">", Format: &style, Value: "1"}})) |
| 44 | must(f.SetCellHyperLink(s, "A2", "https://example.com/"+canary, "External")) |
| 45 | must(f.SetDefinedName(&excelize.DefinedName{Name: "TaxRate", RefersTo: s + "!$A$1"})) |
| 46 | must(f.SetRowVisible(s, 3, false)) |
| 47 | must(f.SetColVisible(s, "D", false)) |
| 48 | must(f.SetPanes(s, &excelize.Panes{Freeze: true, YSplit: 1, TopLeftCell: "A2", ActivePane: "bottomLeft"})) |
| 49 | must(f.AutoFilter(s, "A1:A3", nil)) |
| 50 | var img bytes.Buffer |
| 51 | must(png.Encode(&img, image.NewRGBA(image.Rect(0, 0, 2, 2)))) |
| 52 | must(f.AddPictureFromBytes(s, "E1", &excelize.Picture{Extension: ".png", File: img.Bytes()})) |
| 53 | must(f.AddChart(s, "G1", &excelize.Chart{Type: excelize.Col, Series: []excelize.ChartSeries{ |
| 54 | {Name: s + "!$A$1", Values: s + "!$A$1:$A$3"}}})) |
| 55 | border, err := f.NewStyle(&excelize.Style{Border: []excelize.Border{{Type: "left", Color: "000000", Style: 1}}}) |
| 56 | must(err) |
| 57 | must(f.SetCellStyle(s, "A3", "A3", border)) |
| 58 | for _, name := range []string{"Second", "Third"} { |
| 59 | if _, err := f.NewSheet(name); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | } |
| 63 | var buf bytes.Buffer |
| 64 | must(f.Write(&buf)) |
| 65 | return &buf |
| 66 | } |
| 67 | |
| 68 | func TestImportReportsWhatItLeavesBehind(t *testing.T) { |
| 69 | h := newTab(t, openTestDB(t), testSealer(t)) |
| 70 | rep, err := xlsx.Import(featureBook(t), h) |
| 71 | if err != nil { |
| 72 | t.Fatalf("Import: %v", err) |
| 73 | } |
| 74 | if rep.OmittedWorksheets() != 2 { |
| 75 | t.Errorf("omitted worksheets = %d, want 2", rep.OmittedWorksheets()) |
| 76 | } |
| 77 | want := map[xlsx.Code]int{ |
| 78 | xlsx.OmittedMerges: 1, xlsx.OmittedComments: 1, xlsx.OmittedValidation: 1, |
| 79 | xlsx.OmittedConditional: 1, xlsx.OmittedHyperlinks: 1, xlsx.OmittedDefinedNames: 1, |
| 80 | xlsx.OmittedHiddenRows: 1, xlsx.OmittedHiddenCols: 1, xlsx.OmittedFrozenPanes: 1, |
| 81 | xlsx.OmittedFilter: 1, xlsx.OmittedPictures: 1, xlsx.OmittedWorkbookChart: 1, |
| 82 | xlsx.OmittedWorkbookImage: 1, xlsx.FormatPartial: 1, |
| 83 | } |
| 84 | for code, n := range want { |
| 85 | if got := entry(rep, code); got.Count != n { |
| 86 | t.Logf("%s cells %v", code, got.Cells) |
| 87 | t.Errorf("%s = %d, want %d", code, got.Count, n) |
| 88 | } |
| 89 | } |
| 90 | if got := entry(rep, xlsx.OmittedUndetected).Count; got != 0 { |
| 91 | t.Errorf("undetected = %d, want every detector to have read its part", got) |
| 92 | } |
| 93 | |
| 94 | // The report is codes, coordinates and counts. No text from the |
| 95 | // workbook reaches it — not a cell, a comment, a link or a name. |
| 96 | body, err := json.Marshal(rep) |
| 97 | if err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | for _, leak := range []string{canary, "TaxRate", "example.com", "Second", "Sheet1"} { |
| 101 | if strings.Contains(string(body), leak) { |
| 102 | t.Errorf("report carries %q: %s", leak, body) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |