-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
metricsdecorator_test.go
233 lines (181 loc) · 5.75 KB
/
metricsdecorator_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package maroto
import (
"fmt"
"testing"
"github.com/johnfercher/maroto/v2/pkg/core/entity"
"github.com/johnfercher/maroto/v2/pkg/components/text"
"github.com/johnfercher/go-tree/node"
"github.com/johnfercher/maroto/v2/pkg/components/col"
"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/core"
"github.com/johnfercher/maroto/v2/mocks"
"github.com/johnfercher/maroto/v2/pkg/components/page"
"github.com/stretchr/testify/assert"
)
func TestNewMetricsDecorator(t *testing.T) {
// Act
sut := NewMetricsDecorator(nil)
// Assert
assert.NotNil(t, sut)
assert.Equal(t, "*maroto.MetricsDecorator", fmt.Sprintf("%T", sut))
}
func TestMetricsDecorator_AddPages(t *testing.T) {
// Arrange
pg := page.New()
docToReturn := mocks.NewDocument(t)
docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3})
inner := mocks.NewMaroto(t)
inner.EXPECT().AddPages(pg)
inner.EXPECT().Generate().Return(docToReturn, nil)
sut := NewMetricsDecorator(inner)
// Act
sut.AddPages(pg)
sut.AddPages(pg)
// Assert
doc, err := sut.Generate()
assert.Nil(t, err)
assert.NotNil(t, doc)
report := doc.GetReport()
assert.NotNil(t, report)
assert.Equal(t, 2, len(report.TimeMetrics))
assert.Equal(t, "generate", report.TimeMetrics[0].Key)
assert.Equal(t, "add_page", report.TimeMetrics[1].Key)
assert.Equal(t, 2, len(report.TimeMetrics[1].Times))
inner.AssertNumberOfCalls(t, "AddPages", 2)
}
func TestMetricsDecorator_AddRow(t *testing.T) {
// Arrange
col := col.New(12)
docToReturn := mocks.NewDocument(t)
docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3})
inner := mocks.NewMaroto(t)
inner.EXPECT().AddRow(10.0, col).Return(nil)
inner.EXPECT().Generate().Return(docToReturn, nil)
sut := NewMetricsDecorator(inner)
// Act
sut.AddRow(10, col)
sut.AddRow(10, col)
// Assert
doc, err := sut.Generate()
assert.Nil(t, err)
assert.NotNil(t, doc)
report := doc.GetReport()
assert.NotNil(t, report)
assert.Equal(t, 2, len(report.TimeMetrics))
assert.Equal(t, "generate", report.TimeMetrics[0].Key)
assert.Equal(t, "add_row", report.TimeMetrics[1].Key)
assert.Equal(t, 2, len(report.TimeMetrics[1].Times))
inner.AssertNumberOfCalls(t, "AddRow", 2)
}
func TestMetricsDecorator_AddRows(t *testing.T) {
// Arrange
row := row.New(10).Add(col.New(12))
docToReturn := mocks.NewDocument(t)
docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3})
inner := mocks.NewMaroto(t)
inner.EXPECT().AddRows(row)
inner.EXPECT().Generate().Return(docToReturn, nil)
sut := NewMetricsDecorator(inner)
// Act
sut.AddRows(row)
sut.AddRows(row)
// Assert
doc, err := sut.Generate()
assert.Nil(t, err)
assert.NotNil(t, doc)
report := doc.GetReport()
assert.NotNil(t, report)
assert.Equal(t, 2, len(report.TimeMetrics))
assert.Equal(t, "generate", report.TimeMetrics[0].Key)
assert.Equal(t, "add_rows", report.TimeMetrics[1].Key)
assert.Equal(t, 2, len(report.TimeMetrics[1].Times))
inner.AssertNumberOfCalls(t, "AddRows", 2)
}
func TestMetricsDecorator_GetStructure(t *testing.T) {
// Arrange
row := row.New(10).Add(col.New(12))
docToReturn := mocks.NewDocument(t)
docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3})
inner := mocks.NewMaroto(t)
inner.EXPECT().AddRows(row)
inner.EXPECT().GetStructure().Return(&node.Node[core.Structure]{})
inner.EXPECT().Generate().Return(docToReturn, nil)
sut := NewMetricsDecorator(inner)
sut.AddRows(row)
// Act
_ = sut.GetStructure()
// Assert
doc, err := sut.Generate()
assert.Nil(t, err)
assert.NotNil(t, doc)
report := doc.GetReport()
assert.NotNil(t, report)
assert.Equal(t, 3, len(report.TimeMetrics))
assert.Equal(t, "get_tree_structure", report.TimeMetrics[0].Key)
assert.Equal(t, "generate", report.TimeMetrics[1].Key)
assert.Equal(t, "add_rows", report.TimeMetrics[2].Key)
assert.Equal(t, 1, len(report.TimeMetrics[1].Times))
inner.AssertNumberOfCalls(t, "AddRows", 1)
inner.AssertNumberOfCalls(t, "GetStructure", 1)
}
func TestMetricsDecorator_FitlnCurrentPage(t *testing.T) {
// Arrange
inner := mocks.NewMaroto(t)
inner.EXPECT().FitlnCurrentPage(10.0).Return(true)
inner.EXPECT().FitlnCurrentPage(20.0).Return(false)
sut := NewMetricsDecorator(inner)
// Act & Assert
assert.True(t, sut.FitlnCurrentPage(10))
assert.False(t, sut.FitlnCurrentPage(20))
}
func TestMetricsDecorator_GetCurrentConfig(t *testing.T) {
// Arrange
cfgToReturn := &entity.Config{
MaxGridSize: 15,
}
inner := mocks.NewMaroto(t)
inner.EXPECT().GetCurrentConfig().Return(cfgToReturn)
sut := NewMetricsDecorator(inner)
// Act
cfg := sut.GetCurrentConfig()
// Assert
assert.Equal(t, cfgToReturn.MaxGridSize, cfg.MaxGridSize)
}
func TestMetricsDecorator_RegisterHeader(t *testing.T) {
// Arrange
row := text.NewRow(10, "text")
inner := mocks.NewMaroto(t)
inner.EXPECT().RegisterHeader(row).Return(nil)
inner.EXPECT().Generate().Return(&core.Pdf{}, nil)
sut := NewMetricsDecorator(inner)
// Act
err := sut.RegisterHeader(row)
// Assert
assert.Nil(t, err)
doc, err := sut.Generate()
assert.Nil(t, err)
report := doc.GetReport()
assert.NotNil(t, report)
assert.Equal(t, 2, len(report.TimeMetrics))
assert.Equal(t, "generate", report.TimeMetrics[0].Key)
assert.Equal(t, "header", report.TimeMetrics[1].Key)
}
func TestMetricsDecorator_RegisterFooter(t *testing.T) {
// Arrange
row := text.NewRow(10, "text")
inner := mocks.NewMaroto(t)
inner.EXPECT().RegisterFooter(row).Return(nil)
inner.EXPECT().Generate().Return(&core.Pdf{}, nil)
sut := NewMetricsDecorator(inner)
// Act
err := sut.RegisterFooter(row)
// Assert
assert.Nil(t, err)
doc, err := sut.Generate()
assert.Nil(t, err)
report := doc.GetReport()
assert.NotNil(t, report)
assert.Equal(t, 2, len(report.TimeMetrics))
assert.Equal(t, "generate", report.TimeMetrics[0].Key)
assert.Equal(t, "footer", report.TimeMetrics[1].Key)
}