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] - CompletedDifferenceCount #2230

Closed
tomsmith8 opened this issue Dec 19, 2024 · 5 comments · Fixed by #2270
Closed

[Unit Tests] - CompletedDifferenceCount #2230

tomsmith8 opened this issue Dec 19, 2024 · 5 comments · Fixed by #2270
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "CompletedDifferenceCount"


Stakwork Run


Unit Test Code


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


package db

import (
  "testing"

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

// Mocking the database and CompletedDifference function
type MockDatabase struct {
  mock.Mock
}

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

// Test cases for CompletedDifferenceCount function
func TestCompletedDifferenceCount(t *testing.T) {
  mockDB := new(MockDatabase)

  tests := []struct {
  	name          string
  	r             PaymentDateRange
  	workspace     string
  	mockReturn    []DateDifference
  	expectedCount int64
  }{
  	{
  		name:          "Standard Input with Results",
  		r:             PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:     "workspace1",
  		mockReturn:    []DateDifference{{}, {}, {}}, // Mocking 3 results
  		expectedCount: 3,
  	},
  	{
  		name:          "Standard Input with No Results",
  		r:             PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:     "workspace2",
  		mockReturn:    []DateDifference{}, // Mocking no results
  		expectedCount: 0,
  	},
  	{
  		name:          "Empty Workspace",
  		r:             PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:     "",
  		mockReturn:    []DateDifference{{}, {}}, // Mocking 2 results for all workspaces
  		expectedCount: 2,
  	},
  	{
  		name:          "Date Range with No Overlap",
  		r:             PaymentDateRange{StartDate: "1900-01-01", EndDate: "1900-12-31"},
  		workspace:     "workspace1",
  		mockReturn:    []DateDifference{}, // Mocking no results
  		expectedCount: 0,
  	},
  	{
  		name:          "Invalid Date Range",
  		r:             PaymentDateRange{StartDate: "2023-12-31", EndDate: "2023-01-01"},
  		workspace:     "workspace1",
  		mockReturn:    []DateDifference{}, // Mocking no results due to invalid range
  		expectedCount: 0,
  	},
  	{
  		name:          "Null Date Range",
  		r:             PaymentDateRange{}, // Assuming nil is represented as an empty struct
  		workspace:     "workspace1",
  		mockReturn:    []DateDifference{}, // Mocking no results
  		expectedCount: 0,
  	},
  	{
  		name:          "Large Date Range",
  		r:             PaymentDateRange{StartDate: "2000-01-01", EndDate: "2023-12-31"},
  		workspace:     "workspace1",
  		mockReturn:    make([]DateDifference, 1000), // Mocking 1000 results
  		expectedCount: 1000,
  	},
  	{
  		name:          "High Volume of Workspaces",
  		r:             PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:     "",
  		mockReturn:    make([]DateDifference, 500), // Mocking 500 results across all workspaces
  		expectedCount: 500,
  	},
  	{
  		name:          "Single Day Date Range",
  		r:             PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-01-01"},
  		workspace:     "workspace1",
  		mockReturn:    []DateDifference{{}}, // Mocking 1 result for the single day
  		expectedCount: 1,
  	},
  	{
  		name:          "Non-Existent Workspace",
  		r:             PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-12-31"},
  		workspace:     "nonexistent",
  		mockReturn:    []DateDifference{}, // Mocking no results
  		expectedCount: 0,
  	},
  	{
  		name:          "Boundary Date Range",
  		r:             PaymentDateRange{StartDate: "2023-01-01", EndDate: "2023-01-01"},
  		workspace:     "workspace1",
  		mockReturn:    []DateDifference{{}}, // Mocking 1 result at the boundary
  		expectedCount: 1,
  	},
  	{
  		name:          "Overlapping Date Ranges",
  		r:             PaymentDateRange{StartDate: "2023-06-01", EndDate: "2023-06-30"},
  		workspace:     "workspace1",
  		mockReturn:    []DateDifference{{}, {}, {}}, // Mocking 3 results in the overlapping range
  		expectedCount: 3,
  	},
  }

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

  		result := mockDB.CompletedDifferenceCount(tt.r, tt.workspace)
  		assert.Equal(t, tt.expectedCount, result)

  		mockDB.AssertExpectations(t)
  	})
  }
}
@Shoaibdev7
Copy link
Contributor

Shoaibdev7 commented Dec 19, 2024

@tomsmith8 assign me?

@MahtabBukhari
Copy link
Contributor

MahtabBukhari commented Dec 19, 2024

@tomsmith8 please assign

@MirzaHanan
Copy link
Contributor

@tomsmith8 Please assign me?

@MuhammadUmer44
Copy link
Contributor

@tomsmith8 assign me?

@AhsanFarooqDev
Copy link
Contributor

@tomsmith8 can I help?

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.

6 participants