From 81f3b843ce0780988981c9ae732e179278ee7a47 Mon Sep 17 00:00:00 2001 From: Drew Dara-Abrams <> Date: Mon, 23 Dec 2024 20:32:26 -0800 Subject: [PATCH] maybe improve tests --- meters/stripe/stripe.go | 27 +++++++++++---------------- meters/stripe/stripe_test.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/meters/stripe/stripe.go b/meters/stripe/stripe.go index eb7cc5a..3ee7dd9 100644 --- a/meters/stripe/stripe.go +++ b/meters/stripe/stripe.go @@ -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, } } @@ -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 diff --git a/meters/stripe/stripe_test.go b/meters/stripe/stripe_test.go index 14a9178..c03a24d 100644 --- a/meters/stripe/stripe_test.go +++ b/meters/stripe/stripe_test.go @@ -3,6 +3,7 @@ package stripe import ( "encoding/json" "errors" + "fmt" "net/http" "os" "testing" @@ -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(), @@ -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"},