-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_render_as_csv_test.go
97 lines (85 loc) · 3.23 KB
/
errors_render_as_csv_test.go
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
package csvlib
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tiendc/gofn"
)
func Test_ErrorRenderAsCSV(t *testing.T) {
// CSV error has 2 row errors
csvErr := NewErrors()
csvErr.totalRow = 200
csvErr.header = []string{"Name", "Age", "Address"}
rowErr1 := NewRowErrors(10, 12)
rowErr2 := NewRowErrors(20, 22)
csvErr.Add(rowErr1, rowErr2)
// First row error has 2 cell errors and an unexpected error
cellErr11 := NewCellError(ErrValidationStrLen, 0, "Name")
cellErr11.SetLocalizationKey("ERR_NAME_TOO_LONG")
cellErr11.value = "David David David"
_ = cellErr11.WithParam("MinLen", 1).WithParam("MaxLen", 10)
cellErr12 := NewCellError(ErrValidationRange, 1, "Age")
cellErr12.SetLocalizationKey("ERR_AGE_OUT_OF_RANGE")
cellErr12.value = "101"
_ = cellErr12.WithParam("MinValue", 1).WithParam("MaxValue", 100)
cellErr13 := NewCellError(ErrDecodeQuoteInvalid, -1, "") // error not relate to any column
rowErr1.Add(cellErr11, cellErr12, cellErr13)
// Second row error has 2 other cell errors
cellErr21 := NewCellError(ErrValidationStrLen, 0, "Name")
cellErr22 := NewCellError(ErrValidationRange, 1, "Age")
rowErr2.Add(cellErr21, cellErr22)
// An unexpected error
csvErr.Add(ErrTypeUnsupported)
t.Run("#1: default rendering", func(t *testing.T) {
r, err := NewCSVRenderer(csvErr)
assert.Nil(t, err)
msg, _, err := r.RenderAsString()
assert.Nil(t, err)
assert.Equal(t, gofn.MultilineString(
`Row,Line,CommonError,Name,Age,Address
10,12,ErrDecodeQuoteInvalid,ERR_NAME_TOO_LONG,ERR_AGE_OUT_OF_RANGE,
20,22,,ErrValidation: StrLen,ErrValidation: Range,
`), msg)
})
t.Run("#2: translate en_US", func(t *testing.T) {
r, err := NewCSVRenderer(csvErr, func(cfg *CSVRenderConfig) {
cfg.LocalizationFunc = localizeEnUs
})
assert.Nil(t, err)
msg, _, err := r.RenderAsString()
assert.Nil(t, err)
// nolint: lll
assert.Equal(t, gofn.MultilineString(
`Row,Line,CommonError,Name,Age,Address
10,12,ErrDecodeQuoteInvalid,'David David David' at column 0 - Name length must be from 1 to 10,'101' at column 1 - Age must be from 1 to 100,
20,22,,ErrValidation: StrLen,ErrValidation: Range,
`), msg)
})
t.Run("#3: translate vi_VN", func(t *testing.T) {
r, err := NewCSVRenderer(csvErr, func(cfg *CSVRenderConfig) {
cfg.LocalizationFunc = localizeViVn
cfg.CellRenderFunc = func(rowErr *RowErrors, cellErr *CellError, params ParameterMap) (string, bool) {
if errors.Is(cellErr, ErrDecodeQuoteInvalid) {
return "nội dung bị bao sai (quote)", true
}
return "", true
}
cfg.RenderRowNumberColumnIndex = 1
cfg.RenderCommonErrorColumnIndex = 0
cfg.RenderLineNumberColumnIndex = -1
cfg.HeaderRenderFunc = func(header []string, params ParameterMap) {
header[cfg.RenderRowNumberColumnIndex] = "ROW"
header[cfg.RenderCommonErrorColumnIndex] = "COMMONERR"
}
})
assert.Nil(t, err)
msg, _, err := r.RenderAsString()
assert.Nil(t, err)
// nolint: lll
assert.Equal(t, gofn.MultilineString(
`COMMONERR,ROW,Name,Age,Address
nội dung bị bao sai (quote),10,'David David David' at column 0 - Tên phải dài từ 1 đến 10 ký tự,'101' at column 1 - Tuổi phải từ 1 đến 100,
,20,ErrValidation: StrLen,ErrValidation: Range,
`), msg)
})
}