Skip to content

Commit

Permalink
maybe improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew Dara-Abrams committed Dec 24, 2024
1 parent 63023e2 commit 81f3b84
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
27 changes: 11 additions & 16 deletions meters/stripe/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,19 @@ func (m *StripeMeterProvider) Flush() error {
// Convert events to API payload
eventPayloads := make([]interface{}, len(events))
for i, evt := range events {
// Build payload with dimensions
payload := map[string]interface{}{
"stripe_customer_id": evt.CustomerId,
"value": fmt.Sprintf("%f", evt.Value),
}
// I think we can add extra "dimensions" to payload, but not sure
for _, dim := range evt.Dimensions {
payload[dim.Key] = dim.Value
}

eventPayloads[i] = map[string]interface{}{
"event_name": evt.EventName,
"payload": map[string]interface{}{
"stripe_customer_id": evt.CustomerId,
"value": fmt.Sprintf("%f", evt.Value),
"metadata": buildMetadataFromDimensions(nil, evt.Dimensions),
},
"payload": payload,
}
}

Expand Down Expand Up @@ -171,17 +177,6 @@ func (m *StripeMeterProvider) sendMeter(user meters.MeterUser, meterName string,
return nil
}

func buildMetadataFromDimensions(cfgDims, extraDims meters.Dimensions) map[string]string {
metadata := make(map[string]string)
for _, d := range cfgDims {
metadata[d.Key] = d.Value
}
for _, d := range extraDims {
metadata[d.Key] = d.Value
}
return metadata
}

func (m *StripeMeterProvider) GetValue(user meters.MeterUser, meterName string, startTime time.Time, endTime time.Time, dims meters.Dimensions) (float64, bool) {
// TODO: Implement GetValue using https://docs.stripe.com/api/billing/meter-event-summary
// Not needed
Expand Down
30 changes: 29 additions & 1 deletion meters/stripe/stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stripe
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"testing"
Expand Down Expand Up @@ -216,6 +217,7 @@ func TestStripeMeterWithMock(t *testing.T) {
return nil
}).Times(1)

// Verify the correct payload structure with dimensions
mockBackend.EXPECT().
CallRaw(
gomock.Any(),
Expand All @@ -224,7 +226,33 @@ func TestStripeMeterWithMock(t *testing.T) {
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(nil).Times(1)
).DoAndReturn(func(_ interface{}, _ interface{}, _ interface{}, body interface{}, _ interface{}, _ interface{}) error {
var payload struct {
Events []struct {
EventName string `json:"event_name"`
Payload map[string]interface{} `json:"payload"`
} `json:"events"`
}
if err := json.Unmarshal([]byte(body.(string)), &payload); err != nil {
return fmt.Errorf("failed to unmarshal payload: %w", err)
}
if len(payload.Events) != 1 {
return fmt.Errorf("expected 1 event, got %d", len(payload.Events))
}
evt := payload.Events[0]
// Verify event name
if evt.EventName != "test_meter" {
return fmt.Errorf("expected event_name 'test_meter', got '%s'", evt.EventName)
}
// Verify payload structure
p := evt.Payload
if p["stripe_customer_id"] != "customer-123" ||
p["value"] != "100.000000" ||
p["2"] != "extra_dimension" {
return fmt.Errorf("invalid payload: %v", p)
}
return nil
}).Times(1)

err := mp.sendMeter(testUser, "test_meter", 100, meters.Dimensions{
{Key: "2", Value: "extra_dimension"},
Expand Down

0 comments on commit 81f3b84

Please sign in to comment.