Permalink
Browse files

Tests refactoring

Primary motivation: Avoid statefull tests with not ignorable git file tree changes.
Multiple tests reads and overwrites signle file for won needs.
Multiple tests reads and overwrites file under version control.

Secondary motivation: Minimal tests logic aligment, separate error expectation
and not error expectation tests. Introduce sub-test over test data sets and so far.

This commit is not ideal but necessary (IMHO)
  • Loading branch information...
albenik committed Dec 27, 2018
1 parent 9a6f66a commit 35426ed5dc5196374332498c82b4d480b05e5ac5
Showing with 810 additions and 642 deletions.
  1. +1 −0 .gitignore
  2. +9 −11 cell_test.go
  3. +22 −18 chart_test.go
  4. +34 −8 datavalidation_test.go
  5. +11 −8 date_test.go
  6. +622 −477 excelize_test.go
  7. +1 −1 go.mod
  8. +2 −2 go.sum
  9. +29 −28 lib_test.go
  10. +69 −64 sheetpr_test.go
  11. +10 −25 sheetview_test.go
  12. BIN test/{badWorkbook.xlsx → BadWorkbook.xlsx}
@@ -0,0 +1 @@
test/Test*.xlsx
@@ -1,6 +1,10 @@
package excelize

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCheckCellInArea(t *testing.T) {
expectedTrueCellInAreaList := [][2]string{
@@ -14,11 +18,8 @@ func TestCheckCellInArea(t *testing.T) {
cell := expectedTrueCellInArea[0]
area := expectedTrueCellInArea[1]

cellInArea := checkCellInArea(cell, area)

if !cellInArea {
t.Fatalf("Expected cell %v to be in area %v, got false\n", cell, area)
}
assert.True(t, checkCellInArea(cell, area),
"Expected cell %v to be in area %v, got false\n", cell, area)
}

expectedFalseCellInAreaList := [][2]string{
@@ -31,10 +32,7 @@ func TestCheckCellInArea(t *testing.T) {
cell := expectedFalseCellInArea[0]
area := expectedFalseCellInArea[1]

cellInArea := checkCellInArea(cell, area)

if cellInArea {
t.Fatalf("Expected cell %v not to be inside of area %v, but got true\n", cell, area)
}
assert.False(t, checkCellInArea(cell, area),
"Expected cell %v not to be inside of area %v, but got true\n", cell, area)
}
}
@@ -4,6 +4,8 @@ import (
"bytes"
"encoding/xml"
"testing"

"github.com/stretchr/testify/assert"
)

func TestChartSize(t *testing.T) {
@@ -22,18 +24,18 @@ func TestChartSize(t *testing.T) {
xlsx.AddChart("Sheet1", "E4", `{"type":"col3DClustered","dimension":{"width":640, "height":480},"series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`)
// Save xlsx file by the given path.
err := xlsx.Write(&buffer)
if err != nil {
t.Fatal(err)
if !assert.NoError(t, err) {
t.FailNow()
}

newFile, err := OpenReader(&buffer)
if err != nil {
t.Fatal(err)
if !assert.NoError(t, err) {
t.FailNow()
}

chartsNum := newFile.countCharts()
if chartsNum != 1 {
t.Fatalf("Expected 1 chart, actual %d", chartsNum)
if !assert.Equal(t, 1, chartsNum, "Expected 1 chart, actual %d", chartsNum) {
t.FailNow()
}

var (
@@ -42,25 +44,27 @@ func TestChartSize(t *testing.T) {
)

content, ok := newFile.XLSX["xl/drawings/drawing1.xml"]
if !ok {
t.Fatal("Can't open the chart")
}
assert.True(t, ok, "Can't open the chart")

err = xml.Unmarshal([]byte(content), &workdir)
if err != nil {
t.Fatal(err)
if !assert.NoError(t, err) {
t.FailNow()
}

err = xml.Unmarshal([]byte("<decodeTwoCellAnchor>"+workdir.TwoCellAnchor[0].Content+"</decodeTwoCellAnchor>"), &anchor)
if err != nil {
t.Fatal(err)
if !assert.NoError(t, err) {
t.FailNow()
}

if anchor.From.Col != 4 || anchor.From.Row != 3 {
t.Fatalf("From: Expected column 4, row 3, actual column %d, row %d", anchor.From.Col, anchor.From.Row)
}
if anchor.To.Col != 14 || anchor.To.Row != 27 {
t.Fatalf("To: Expected column 14, row 27, actual column %d, row %d", anchor.To.Col, anchor.To.Row)
if !assert.Equal(t, 4, anchor.From.Col, "Expected 'from' column 4") ||
!assert.Equal(t, 3, anchor.From.Row, "Expected 'from' row 3") {

t.FailNow()
}

if !assert.Equal(t, 14, anchor.To.Col, "Expected 'to' column 14") ||
!assert.Equal(t, 27, anchor.To.Row, "Expected 'to' row 27") {

t.FailNow()
}
}
@@ -9,9 +9,15 @@

package excelize

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDataValidation(t *testing.T) {
const resultFile = "./test/TestDataValidation.xlsx"

xlsx := NewFile()

dvRange := NewDataValidation(true)
@@ -21,37 +27,57 @@ func TestDataValidation(t *testing.T) {
dvRange.SetError(DataValidationErrorStyleWarning, "error title", "error body")
dvRange.SetError(DataValidationErrorStyleInformation, "error title", "error body")
xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}

dvRange = NewDataValidation(true)
dvRange.Sqref = "A3:B4"
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
dvRange.SetInput("input title", "input body")
xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}

dvRange = NewDataValidation(true)
dvRange.Sqref = "A5:B6"
dvRange.SetDropList([]string{"1", "2", "3"})
xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}
}

func TestDataValidationError(t *testing.T) {
const resultFile = "./test/TestDataValidationError.xlsx"

xlsx := NewFile()
xlsx.SetCellStr("Sheet1", "E1", "E1")
xlsx.SetCellStr("Sheet1", "E2", "E2")
xlsx.SetCellStr("Sheet1", "E3", "E3")
dvRange = NewDataValidation(true)

dvRange := NewDataValidation(true)
dvRange.SetSqref("A7:B8")
dvRange.SetSqref("A7:B8")
dvRange.SetSqrefDropList("$E$1:$E$3", true)

err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
t.Log(err)
assert.EqualError(t, err, "cross-sheet sqref cell are not supported")

xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}

dvRange = NewDataValidation(true)
dvRange.SetDropList(make([]string, 258))

err = dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
t.Log(err)
assert.EqualError(t, err, "data validation must be 0-255 characters")

// Test write file to given path.
err = xlsx.SaveAs("./test/Book_data_validation.xlsx")
if err != nil {
t.Error(err)
xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}
}
@@ -1,8 +1,11 @@
package excelize

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

type dateTest struct {
@@ -18,10 +21,10 @@ func TestTimeToExcelTime(t *testing.T) {
{401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
}

for _, test := range trueExpectedInputList {
if test.ExcelValue != timeToExcelTime(test.GoValue) {
t.Fatalf("Expected %v from %v = true, got %v\n", test.ExcelValue, test.GoValue, timeToExcelTime(test.GoValue))
}
for i, test := range trueExpectedInputList {
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
assert.Equal(t, test.ExcelValue, timeToExcelTime(test.GoValue))
})
}
}

@@ -34,9 +37,9 @@ func TestTimeFromExcelTime(t *testing.T) {
{401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
}

for _, test := range trueExpectedInputList {
if test.GoValue != timeFromExcelTime(test.ExcelValue, false) {
t.Fatalf("Expected %v from %v = true, got %v\n", test.GoValue, test.ExcelValue, timeFromExcelTime(test.ExcelValue, false))
}
for i, test := range trueExpectedInputList {
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
assert.Equal(t, test.GoValue, timeFromExcelTime(test.ExcelValue, false))
})
}
}
Oops, something went wrong.

0 comments on commit 35426ed

Please sign in to comment.