Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metricsdecorator tests #348

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}