174 lines
5.6 KiB
Go
174 lines
5.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// ========== GetAgentSummary ==========
|
|
|
|
func TestSummaryReportRepo_GetAgentSummary(t *testing.T) {
|
|
db := setupTestDB(t, &model.ReportingEventsRollup{})
|
|
repo := NewSummaryReportRepo(db)
|
|
|
|
accountID := uint(1)
|
|
since := time.Date(2026, 5, 1, 0, 0, 0, 0, time.UTC)
|
|
until := time.Date(2026, 5, 31, 0, 0, 0, 0, time.UTC)
|
|
|
|
// Seed rollup data for agent dimension
|
|
date := time.Date(2026, 5, 15, 0, 0, 0, 0, time.UTC)
|
|
agentRollup := &model.ReportingEventsRollup{
|
|
AccountID: accountID,
|
|
Date: date,
|
|
DimensionType: model.DimensionAgent,
|
|
DimensionID: 10,
|
|
Metric: model.MetricFirstResponse,
|
|
Count: 5,
|
|
SumValue: 250.0,
|
|
SumValueBusinessHours: 125.0,
|
|
}
|
|
require.NoError(t, db.Create(agentRollup).Error)
|
|
|
|
// Also seed an inbox rollup (should NOT appear in agent results)
|
|
inboxRollup := &model.ReportingEventsRollup{
|
|
AccountID: accountID,
|
|
Date: date,
|
|
DimensionType: model.DimensionInbox,
|
|
DimensionID: 20,
|
|
Metric: model.MetricFirstResponse,
|
|
Count: 3,
|
|
SumValue: 150.0,
|
|
SumValueBusinessHours: 75.0,
|
|
}
|
|
require.NoError(t, db.Create(inboxRollup).Error)
|
|
|
|
results, err := repo.GetAgentSummary(context.Background(), accountID, since, until)
|
|
require.NoError(t, err)
|
|
assert.Len(t, results, 1, "only agent-dimension rollups should be returned")
|
|
assert.Equal(t, model.DimensionAgent, results[0].DimensionType)
|
|
assert.Equal(t, uint(10), results[0].DimensionID)
|
|
assert.Equal(t, model.MetricFirstResponse, results[0].Metric)
|
|
}
|
|
|
|
// ========== GetTeamSummary ==========
|
|
|
|
func TestSummaryReportRepo_GetTeamSummary(t *testing.T) {
|
|
db := setupTestDB(t, &model.ReportingEventsRollup{})
|
|
repo := NewSummaryReportRepo(db)
|
|
|
|
accountID := uint(1)
|
|
since := time.Date(2026, 5, 1, 0, 0, 0, 0, time.UTC)
|
|
until := time.Date(2026, 5, 31, 0, 0, 0, 0, time.UTC)
|
|
|
|
date := time.Date(2026, 5, 10, 0, 0, 0, 0, time.UTC)
|
|
teamRollup := &model.ReportingEventsRollup{
|
|
AccountID: accountID,
|
|
Date: date,
|
|
DimensionType: model.DimensionTeam,
|
|
DimensionID: 30,
|
|
Metric: model.MetricResolutionTime,
|
|
Count: 8,
|
|
SumValue: 400.0,
|
|
SumValueBusinessHours: 200.0,
|
|
}
|
|
require.NoError(t, db.Create(teamRollup).Error)
|
|
|
|
results, err := repo.GetTeamSummary(context.Background(), accountID, since, until)
|
|
require.NoError(t, err)
|
|
assert.Len(t, results, 1)
|
|
assert.Equal(t, model.DimensionTeam, results[0].DimensionType)
|
|
assert.Equal(t, uint(30), results[0].DimensionID)
|
|
}
|
|
|
|
// ========== GetInboxSummary ==========
|
|
|
|
func TestSummaryReportRepo_GetInboxSummary(t *testing.T) {
|
|
db := setupTestDB(t, &model.ReportingEventsRollup{})
|
|
repo := NewSummaryReportRepo(db)
|
|
|
|
accountID := uint(1)
|
|
since := time.Date(2026, 5, 1, 0, 0, 0, 0, time.UTC)
|
|
until := time.Date(2026, 5, 31, 0, 0, 0, 0, time.UTC)
|
|
|
|
date := time.Date(2026, 5, 12, 0, 0, 0, 0, time.UTC)
|
|
inboxRollup := &model.ReportingEventsRollup{
|
|
AccountID: accountID,
|
|
Date: date,
|
|
DimensionType: model.DimensionInbox,
|
|
DimensionID: 40,
|
|
Metric: model.MetricReplyTime,
|
|
Count: 12,
|
|
SumValue: 600.0,
|
|
SumValueBusinessHours: 300.0,
|
|
}
|
|
require.NoError(t, db.Create(inboxRollup).Error)
|
|
|
|
results, err := repo.GetInboxSummary(context.Background(), accountID, since, until)
|
|
require.NoError(t, err)
|
|
assert.Len(t, results, 1)
|
|
assert.Equal(t, model.DimensionInbox, results[0].DimensionType)
|
|
assert.Equal(t, uint(40), results[0].DimensionID)
|
|
}
|
|
|
|
// ========== GetLabelSummary ==========
|
|
|
|
func TestSummaryReportRepo_GetLabelSummary(t *testing.T) {
|
|
db := setupTestDB(t, &model.ReportingEventsRollup{})
|
|
repo := NewSummaryReportRepo(db)
|
|
|
|
accountID := uint(1)
|
|
since := time.Date(2026, 5, 1, 0, 0, 0, 0, time.UTC)
|
|
until := time.Date(2026, 5, 31, 0, 0, 0, 0, time.UTC)
|
|
|
|
date := time.Date(2026, 5, 18, 0, 0, 0, 0, time.UTC)
|
|
labelRollup := &model.ReportingEventsRollup{
|
|
AccountID: accountID,
|
|
Date: date,
|
|
DimensionType: model.DimensionLabel,
|
|
DimensionID: 50,
|
|
Metric: model.MetricResolutionsCount,
|
|
Count: 7,
|
|
SumValue: 350.0,
|
|
SumValueBusinessHours: 175.0,
|
|
}
|
|
require.NoError(t, db.Create(labelRollup).Error)
|
|
|
|
results, err := repo.GetLabelSummary(context.Background(), accountID, since, until)
|
|
require.NoError(t, err)
|
|
assert.Len(t, results, 1)
|
|
assert.Equal(t, model.DimensionLabel, results[0].DimensionType)
|
|
assert.Equal(t, uint(50), results[0].DimensionID)
|
|
}
|
|
|
|
// ========== Empty results ==========
|
|
|
|
func TestSummaryReportRepo_EmptyResults(t *testing.T) {
|
|
db := setupTestDB(t, &model.ReportingEventsRollup{})
|
|
repo := NewSummaryReportRepo(db)
|
|
|
|
accountID := uint(999)
|
|
since := time.Date(2026, 5, 1, 0, 0, 0, 0, time.UTC)
|
|
until := time.Date(2026, 5, 31, 0, 0, 0, 0, time.UTC)
|
|
|
|
results, err := repo.GetAgentSummary(context.Background(), accountID, since, until)
|
|
require.NoError(t, err)
|
|
assert.Len(t, results, 0)
|
|
|
|
results, err = repo.GetTeamSummary(context.Background(), accountID, since, until)
|
|
require.NoError(t, err)
|
|
assert.Len(t, results, 0)
|
|
|
|
results, err = repo.GetInboxSummary(context.Background(), accountID, since, until)
|
|
require.NoError(t, err)
|
|
assert.Len(t, results, 0)
|
|
|
|
results, err = repo.GetLabelSummary(context.Background(), accountID, since, until)
|
|
require.NoError(t, err)
|
|
assert.Len(t, results, 0)
|
|
} |