Skip to content

Commit

Permalink
Add metricsdecorator tests (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored Oct 18, 2023
1 parent 4819045 commit 5f049f0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
18 changes: 7 additions & 11 deletions metricsdecorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

type metricsDecorator struct {
addRowsTime []*metrics.Time
addRowTime []*metrics.Time
addColTime []*metrics.Time
addPageTime []*metrics.Time
addPDFTime []*metrics.Time
headerTime *metrics.Time
Expand Down Expand Up @@ -58,7 +58,7 @@ func (m *metricsDecorator) AddRows(rows ...core.Row) {
m.inner.AddRows(rows...)
})

m.addRowTime = append(m.addRowTime, timeSpent)
m.addRowsTime = append(m.addRowsTime, timeSpent)
}

func (m *metricsDecorator) AddRow(rowHeight float64, cols ...core.Col) core.Row {
Expand All @@ -67,7 +67,7 @@ func (m *metricsDecorator) AddRow(rowHeight float64, cols ...core.Col) core.Row
r = m.inner.AddRow(rowHeight, cols...)
})

m.addColTime = append(m.addColTime, timeSpent)
m.addRowTime = append(m.addRowTime, timeSpent)
return r
}

Expand Down Expand Up @@ -151,11 +151,11 @@ func (m *metricsDecorator) buildMetrics(bytesSize int) *metrics.Report {
})
}

if len(m.addColTime) > 0 {
if len(m.addRowsTime) > 0 {
timeMetrics = append(timeMetrics, metrics.TimeMetric{
Key: "add_cols",
Times: m.addColTime,
Avg: m.getAVG(m.addColTime),
Key: "add_rows",
Times: m.addRowsTime,
Avg: m.getAVG(m.addRowsTime),
})
}

Expand All @@ -180,10 +180,6 @@ func (m *metricsDecorator) buildMetrics(bytesSize int) *metrics.Report {
}

func (m *metricsDecorator) getAVG(times []*metrics.Time) *metrics.Time {
if len(times) == 0 {
return nil
}

var sum float64
for _, time := range times {
sum += time.Value
Expand Down
48 changes: 48 additions & 0 deletions metricsdecorator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package maroto

import (
"fmt"
"testing"

"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.Document{}
docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3})
inner := &mocks.Maroto{}
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))
}

0 comments on commit 5f049f0

Please sign in to comment.