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

[Unit Tests] - AverageCompletedTime #2287

Merged
Merged
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
171 changes: 171 additions & 0 deletions db/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ func (m *mockDatabase) CompletedDifferenceCount(r PaymentDateRange, workspace st
return int64(len(list))
}

func (m *mockDatabase) AverageCompletedTime(r PaymentDateRange, workspace string) uint {
paidList := m.CompletedDifference(r, workspace)
paidCount := int64(len(paidList))
var paidSum uint
for _, diff := range paidList {
paidSum += uint(math.Round(diff.Diff))
}
return CalculateAverageDays(paidCount, paidSum)
}

func TestCompletedDifferenceCount(t *testing.T) {

tests := []struct {
Expand Down Expand Up @@ -223,3 +233,164 @@ func TestCompletedDifferenceCount(t *testing.T) {
})
}
}


func TestAverageCompletedTime(t *testing.T) {

tests := []struct {
name string
dateRange PaymentDateRange
workspace string
mockData []DateDifference
expected uint
}{
{
name: "Standard Case - Multiple Entries",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace1",
mockData: []DateDifference{{Diff: 86400}, {Diff: 172800}},
expected: 2,
},
{
name: "Empty Result",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace2",
mockData: []DateDifference{},
expected: 0,
},
{
name: "Single Entry",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace3",
mockData: []DateDifference{{Diff: 86400}},
expected: 1,
},
{
name: "Very Small Time Difference",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace4",
mockData: []DateDifference{{Diff: 3600}},
expected: 0,
},
{
name: "Large Time Difference",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace5",
mockData: []DateDifference{{Diff: 864000}},
expected: 10,
},
{
name: "Mixed Time Differences",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace6",
mockData: []DateDifference{
{Diff: 86400},
{Diff: 172800},
{Diff: 259200},
},
expected: 2,
},
{
name: "No Workspace Specified",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "",
mockData: []DateDifference{{Diff: 86400}},
expected: 1,
},
{
name: "Boundary Values - Zero",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace7",
mockData: []DateDifference{{Diff: 0}},
expected: 0,
},
{
name: "Boundary Values - Very Large",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace8",
mockData: []DateDifference{{Diff: 8640000}},
expected: 100,
},
{
name: "Invalid Date Range",
dateRange: PaymentDateRange{
StartDate: "2023-12-31",
EndDate: "2023-01-01",
},
workspace: "workspace9",
mockData: []DateDifference{},
expected: 0,
},
{
name: "Large Dataset",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace10",
mockData: func() []DateDifference {
diffs := make([]DateDifference, 1000)
for i := range diffs {
diffs[i] = DateDifference{Diff: 86400}
}
return diffs
}(),
expected: 1,
},
{
name: "Fractional Days",
dateRange: PaymentDateRange{
StartDate: "2023-01-01",
EndDate: "2023-12-31",
},
workspace: "workspace11",
mockData: []DateDifference{
{Diff: 129600},
{Diff: 172800},
},
expected: 2,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockDB := &mockDatabase{
completedDifferenceFn: func(r PaymentDateRange, workspace string) []DateDifference {
assert.Equal(t, tt.dateRange, r)
assert.Equal(t, tt.workspace, workspace)
return tt.mockData
},
}

result := mockDB.AverageCompletedTime(tt.dateRange, tt.workspace)
assert.Equal(t, tt.expected, result)
})
}
}
Loading