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 #2229

Closed
tomsmith8 opened this issue Dec 19, 2024 · 3 comments · Fixed by #2287
Closed

[Unit Tests] - AverageCompletedTime #2229

tomsmith8 opened this issue Dec 19, 2024 · 3 comments · Fixed by #2287
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "AverageCompletedTime"


Stakwork Run


Unit Test Code


File: /tmp/stakwork/sphinx-tribes/db/metrics.go


package db

import (
  "math"
  "testing"

  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/mock"
)

// Mocking the database and its methods
type MockDatabase struct {
  mock.Mock
}

func (m *MockDatabase) CompletedDifference(r PaymentDateRange, workspace string) []DateDifference {
  args := m.Called(r, workspace)
  return args.Get(0).([]DateDifference)
}

func (m *MockDatabase) CompletedDifferenceCount(r PaymentDateRange, workspace string) int64 {
  args := m.Called(r, workspace)
  return args.Get(0).(int64)
}

// Mocking the CalculateAverageDays function
func CalculateAverageDays(paidCount int64, paidSum uint) uint {
  if paidCount != 0 {
  	avg := float64(paidSum) / float64(paidCount)
  	avgDays := math.Round(avg / 86400) // Assuming 86400 seconds in a day
  	return uint(avgDays)
  }
  return 0
}

func TestAverageCompletedTime(t *testing.T) {
  mockDB := new(MockDatabase)

  tests := []struct {
  	name             string
  	r                PaymentDateRange
  	workspace        string
  	mockedDiffs      []DateDifference
  	mockedCount      int64
  	expectedOutcome  uint
  }{
  	{
  		name:            "Standard Input with Multiple Entries",
  		r:               PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:       "workspace1",
  		mockedDiffs:     []DateDifference{{Diff: 86400}, {Diff: 172800}},
  		mockedCount:     2,
  		expectedOutcome: 1,
  	},
  	{
  		name:            "Single Entry",
  		r:               PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-01-31"},
  		workspace:       "workspace2",
  		mockedDiffs:     []DateDifference{{Diff: 86400}},
  		mockedCount:     1,
  		expectedOutcome: 1,
  	},
  	{
  		name:            "No Entries",
  		r:               PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-01-31"},
  		workspace:       "workspace3",
  		mockedDiffs:     []DateDifference{},
  		mockedCount:     0,
  		expectedOutcome: 0,
  	},
  	{
  		name:            "Zero Difference",
  		r:               PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-01-31"},
  		workspace:       "workspace4",
  		mockedDiffs:     []DateDifference{{Diff: 0}},
  		mockedCount:     1,
  		expectedOutcome: 0,
  	},
  	{
  		name:            "Invalid Date Range",
  		r:               PaymentDateRange{StartDate: "2023-12-31", EndDate: "2023-01-01"},
  		workspace:       "workspace5",
  		mockedDiffs:     []DateDifference{},
  		mockedCount:     0,
  		expectedOutcome: 0,
  	},
  	{
  		name:            "Null or Empty Workspace",
  		r:               PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:       "",
  		mockedDiffs:     []DateDifference{},
  		mockedCount:     0,
  		expectedOutcome: 0,
  	},
  	{
  		name:            "Large Number of Entries",
  		r:               PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:       "workspace6",
  		mockedDiffs:     make([]DateDifference, 1000),
  		mockedCount:     1000,
  		expectedOutcome: 1,
  	},
  	{
  		name:            "Mixed Differences",
  		r:               PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:       "workspace7",
  		mockedDiffs:     []DateDifference{{Diff: 86400}, {Diff: 259200}},
  		mockedCount:     2,
  		expectedOutcome: 2,
  	},
  	{
  		name:            "Negative Differences",
  		r:               PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:       "workspace8",
  		mockedDiffs:     []DateDifference{{Diff: -86400}},
  		mockedCount:     1,
  		expectedOutcome: 0,
  	},
  	{
  		name:            "Floating Point Differences",
  		r:               PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:       "workspace9",
  		mockedDiffs:     []DateDifference{{Diff: 129600}, {Diff: 172800}},
  		mockedCount:     2,
  		expectedOutcome: 2,
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		mockDB.On("CompletedDifference", tt.r, tt.workspace).Return(tt.mockedDiffs)
  		mockDB.On("CompletedDifferenceCount", tt.r, tt.workspace).Return(tt.mockedCount)

  		result := mockDB.AverageCompletedTime(tt.r, tt.workspace)
  		assert.Equal(t, tt.expectedOutcome, result)
  	})
  }
}
@aliraza556
Copy link
Contributor

aliraza556 commented Dec 19, 2024

@tomsmith8 assign me?

@saithsab877
Copy link
Contributor

saithsab877 commented Dec 19, 2024

@tomsmith8 I can help?

@MuhammadUmer44
Copy link
Contributor

@tomsmith8 assign me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants