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] - GenerateBudgetInvoice #2232

Closed
tomsmith8 opened this issue Dec 19, 2024 · 4 comments · Fixed by #2263
Closed

[Unit Tests] - GenerateBudgetInvoice #2232

tomsmith8 opened this issue Dec 19, 2024 · 4 comments · Fixed by #2263
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "GenerateBudgetInvoice"


Stakwork Run


Unit Test Code


File: /tmp/stakwork/sphinx-tribes/handlers/tribes.go


package tribes

import (
  "bytes"
  "errors"
  "net/http"
  "net/http/httptest"
  "testing"

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

// Mocking the tribeHandler to test GenerateBudgetInvoice
type MockTribeHandler struct {
  mock.Mock
}

func (m *MockTribeHandler) GenerateV1BudgetInvoice(w http.ResponseWriter, r *http.Request) {
  m.Called(w, r)
}

func (m *MockTribeHandler) GenerateV2BudgetInvoice(w http.ResponseWriter, r *http.Request) {
  m.Called(w, r)
}

// Mocking the config package
var mockConfig = struct {
  IsV2Payment bool
}{
  IsV2Payment: false,
}

func TestGenerateBudgetInvoice(t *testing.T) {
  tests := []struct {
  	name           string
  	isV2Payment    bool
  	requestBody    []byte
  	expectedMethod string
  	expectedStatus int
  }{
  	{
  		name:           "V1 Payment Path",
  		isV2Payment:    false,
  		expectedMethod: "GenerateV1BudgetInvoice",
  	},
  	{
  		name:           "V2 Payment Path",
  		isV2Payment:    true,
  		expectedMethod: "GenerateV2BudgetInvoice",
  	},
  	{
  		name:           "Empty Request Body",
  		isV2Payment:    false,
  		requestBody:    []byte(""),
  		expectedStatus: http.StatusBadRequest,
  	},
  	{
  		name:           "Invalid JSON in Request Body",
  		isV2Payment:    false,
  		requestBody:    []byte("{invalid json}"),
  		expectedStatus: http.StatusBadRequest,
  	},
  	// Additional test cases for error conditions, performance, and special cases can be added here
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		mockConfig.IsV2Payment = tt.isV2Payment

  		mockHandler := new(MockTribeHandler)
  		mockHandler.On("GenerateV1BudgetInvoice", mock.Anything, mock.Anything).Return()
  		mockHandler.On("GenerateV2BudgetInvoice", mock.Anything, mock.Anything).Return()

  		req := httptest.NewRequest(http.MethodPost, "/budgetinvoices", bytes.NewBuffer(tt.requestBody))
  		w := httptest.NewRecorder()

  		// Call the function under test
  		mockHandler.GenerateBudgetInvoice(w, req)

  		// Assert the expected method was called
  		if tt.expectedMethod != "" {
  			mockHandler.AssertCalled(t, tt.expectedMethod, w, req)
  		}

  		// Assert the expected status code for error cases
  		if tt.expectedStatus != 0 {
  			assert.Equal(t, tt.expectedStatus, w.Code)
  		}
  	})
  }
}

// Mocking HTTP client for network failure simulation
type MockHTTPClient struct {
  mock.Mock
}

func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) {
  args := m.Called(req)
  return args.Get(0).(*http.Response), args.Error(1)
}

func TestNetworkFailureOnExternalCall(t *testing.T) {
  mockHandler := new(MockTribeHandler)
  mockHandler.On("GenerateV1BudgetInvoice", mock.Anything, mock.Anything).Return()

  req := httptest.NewRequest(http.MethodPost, "/budgetinvoices", nil)
  w := httptest.NewRecorder()

  // Simulate network failure
  mockClient := new(MockHTTPClient)
  mockClient.On("Do", mock.Anything).Return(nil, errors.New("network error"))

  // Call the function under test
  mockHandler.GenerateBudgetInvoice(w, req)

  // Assert the expected status code
  assert.Equal(t, http.StatusServiceUnavailable, w.Code)
}
@sophieturner0
Copy link
Contributor

sophieturner0 commented Dec 19, 2024

@tomsmith8 I can help?

@AhsanFarooqDev
Copy link
Contributor

AhsanFarooqDev commented Dec 19, 2024

@tomsmith8 can I help?

@MuhammadUmer44
Copy link
Contributor

@tomsmith8 assign me?

@aliraza556
Copy link
Contributor

@tomsmith8 Please 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.

5 participants