Skip to content

Commit

Permalink
sc-14371 updated test file
Browse files Browse the repository at this point in the history
  • Loading branch information
Gyan-Gupta-Rtsl committed Jan 17, 2025
1 parent aaaebc8 commit a686a65
Showing 1 changed file with 0 additions and 14 deletions.
14 changes: 0 additions & 14 deletions sendgrid/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@ import (
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/simpledotorg/rtsl_exporter/sendgrid"
)

// MockTransport mocks HTTP responses for testing purposes.
type MockTransport struct {
Response *http.Response
Error error
}

// RoundTrip simulates the HTTP transport layer for testing.
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Simulate network errors or responses
return m.Response, m.Error
}

// TestContext holds the shared state and setup for the tests.
type TestContext struct {
client *sendgrid.Client
originalTransport http.RoundTripper
mockTransport *MockTransport
}

// Initialize sets up the TestContext with the provided mock responses and status codes.
func (tc *TestContext) Initialize(apiKeys map[string]string, mockResponse *sendgrid.SendgridCreditsResponse, statusCode int, err error) {
mockResponseBody, _ := json.Marshal(mockResponse)
Expand All @@ -45,12 +40,10 @@ func (tc *TestContext) Initialize(apiKeys map[string]string, mockResponse *sendg
tc.originalTransport = http.DefaultTransport
http.DefaultTransport = tc.mockTransport
}

// Cleanup restores the original HTTP transport after tests.
func (tc *TestContext) Cleanup() {
http.DefaultTransport = tc.originalTransport
}

// TestFetchMetrics_Success tests successful fetching of metrics.
func TestFetchMetrics_Success(t *testing.T) {
tc := &TestContext{}
Expand All @@ -65,43 +58,37 @@ func TestFetchMetrics_Success(t *testing.T) {
}
tc.Initialize(map[string]string{mockAccountName: mockAPIKey}, &mockResponse, http.StatusOK, nil)
defer tc.Cleanup()

metrics, statusCode, _, err := tc.client.FetchMetrics(mockAccountName)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, statusCode)
assert.InDelta(t, 1000, metrics.Total, 0.001)
assert.InDelta(t, 800, metrics.Remaining, 0.001)
assert.InDelta(t, 200, metrics.Used, 0.001)
}

// TestFetchMetrics_AccountNotFound tests handling of an account not found error.
func TestFetchMetrics_AccountNotFound(t *testing.T) {
tc := &TestContext{}
mockAPIKey := "mockAPIKey"
mockAccountName := "mockAccount"
tc.Initialize(map[string]string{mockAccountName: mockAPIKey}, nil, http.StatusNotFound, nil)
defer tc.Cleanup()

metrics, statusCode, _, err := tc.client.FetchMetrics(mockAccountName)
assert.Error(t, err)
assert.Nil(t, metrics)
assert.Equal(t, http.StatusNotFound, statusCode)
}

// TestFetchMetrics_HTTPError tests handling of HTTP errors.
func TestFetchMetrics_HTTPError(t *testing.T) {
tc := &TestContext{}
mockAPIKey := "mockAPIKey"
mockAccountName := "mockAccount"
tc.Initialize(map[string]string{mockAccountName: mockAPIKey}, nil, 0, http.ErrHandlerTimeout)
defer tc.Cleanup()

metrics, statusCode, _, err := tc.client.FetchMetrics(mockAccountName)
assert.Error(t, err)
assert.Nil(t, metrics)
assert.Equal(t, 0, statusCode)
}

// TestFetchMetrics_Timeout tests handling of timeouts.
func TestFetchMetrics_Timeout(t *testing.T) {
tc := &TestContext{}
Expand All @@ -116,7 +103,6 @@ func TestFetchMetrics_Timeout(t *testing.T) {
originalTransport := http.DefaultTransport
defer func() { http.DefaultTransport = originalTransport }()
http.DefaultTransport = tc.mockTransport

metrics, statusCode, _, err := tc.client.FetchMetrics(mockAccountName)
assert.Error(t, err)
assert.Nil(t, metrics)
Expand Down

0 comments on commit a686a65

Please sign in to comment.