Skip to content

Commit eff674b

Browse files
add test independently
1 parent fd743cd commit eff674b

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

slicer_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package excelize
22

33
import (
4+
"encoding/xml"
45
"fmt"
56
"math/rand"
67
"os"
@@ -719,3 +720,90 @@ func TestAddPivotCacheSlicer(t *testing.T) {
719720
})
720721
assert.NoError(t, err)
721722
}
723+
724+
func TestBuildSlicerItems(t *testing.T) {
725+
f := NewFile()
726+
727+
months := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun"}
728+
years := []int{2017, 2018, 2019}
729+
types := []string{"Meat", "Dairy", "Beverages"}
730+
731+
assert.NoError(t, f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "Year", "Type", "Sales"}))
732+
733+
for row := 2; row <= 20; row++ {
734+
i := (row - 2) % len(months)
735+
assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), months[i]))
736+
assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), years[i%len(years)]))
737+
assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), types[i%len(types)]))
738+
assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), row*100))
739+
}
740+
741+
pivotOpts := &PivotTableOptions{
742+
DataRange: "Sheet1!A1:D20",
743+
PivotTableRange: "Sheet1!F2:J15",
744+
Name: "TestPivotTable",
745+
Rows: []PivotTableField{{Data: "Month"}},
746+
Columns: []PivotTableField{{Data: "Year"}},
747+
Data: []PivotTableField{{Data: "Sales", Subtotal: "Sum"}},
748+
}
749+
assert.NoError(t, f.AddPivotTable(pivotOpts))
750+
751+
pc, err := f.pivotCacheReader(pivotOpts.pivotCacheXML)
752+
assert.NoError(t, err)
753+
754+
if pc.CacheFields != nil && len(pc.CacheFields.CacheField) > 0 {
755+
pc.CacheFields.CacheField[0].SharedItems = &xlsxSharedItems{
756+
Count: len(months),
757+
S: make([]xlsxString, len(months)),
758+
}
759+
for i, month := range months {
760+
pc.CacheFields.CacheField[0].SharedItems.S[i] = xlsxString{V: month}
761+
}
762+
763+
pivotCache, err := xml.Marshal(pc)
764+
assert.NoError(t, err)
765+
f.saveFileList(pivotOpts.pivotCacheXML, pivotCache)
766+
}
767+
768+
items, err := f.buildSlicerItems(pivotOpts, "Month", []string{})
769+
assert.NoError(t, err)
770+
assert.Equal(t, len(months), len(items))
771+
772+
for i, item := range items {
773+
assert.True(t, item.S, "Item %d should be selected", i)
774+
assert.Equal(t, i, item.X, "Item %d should have correct index", i)
775+
}
776+
777+
selectedItems := []string{"Jan", "Mar", "May"}
778+
items, err = f.buildSlicerItems(pivotOpts, "Month", selectedItems)
779+
assert.NoError(t, err)
780+
assert.Equal(t, len(months), len(items))
781+
782+
for i, item := range items {
783+
expectedValue := months[i]
784+
expectedSelected := false
785+
for _, sel := range selectedItems {
786+
if sel == expectedValue {
787+
expectedSelected = true
788+
break
789+
}
790+
}
791+
assert.Equal(t, expectedSelected, item.S, "Item %d should have correct selection state", i)
792+
assert.Equal(t, i, item.X, "Item %d should have correct index", i)
793+
}
794+
795+
items, err = f.buildSlicerItems(pivotOpts, "NonExistent", []string{})
796+
assert.NoError(t, err)
797+
assert.Equal(t, 1, len(items))
798+
assert.True(t, items[0].S)
799+
800+
invalidPivotCacheXML := "xl/pivotCache/invalidCache.xml"
801+
f.Pkg.Store(invalidPivotCacheXML, MacintoshCyrillicCharset)
802+
invalidPivotOpts := &PivotTableOptions{
803+
pivotCacheXML: invalidPivotCacheXML,
804+
}
805+
_, err = f.buildSlicerItems(invalidPivotOpts, "Month", []string{})
806+
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
807+
808+
assert.NoError(t, f.Close())
809+
}

0 commit comments

Comments
 (0)