MCPcopy Create free account
hub / github.com/PostHog/duckgres / TestBillingUsageWindowAndRows

Function TestBillingUsageWindowAndRows

controlplane/compute_billing_api_test.go:121–162  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

119 r.ServeHTTP(rec, req)
120 if rec.Code != http.StatusForbidden {
121 t.Fatalf("%s %s = %d, want 403 from the admin gate", req.Method, req.URL.Path, rec.Code)
122 }
123 }
124 if gateHits != 2 {
125 t.Fatalf("admin gate ran %d times, want 2 (every billing route must be gated)", gateHits)
126 }
127}
128
129type usageResponse struct {
130 WatermarkLow time.Time `json:"watermark_low"`
131 WatermarkHigh time.Time `json:"watermark_high"`
132 Usage []configstore.ComputeUsageRow `json:"usage"`
133 Storage []configstore.StorageUsageRow `json:"storage"`
134}
135
136func TestBillingUsageWindowAndRows(t *testing.T) {
137 now := time.Date(2026, 7, 1, 12, 40, 47, 0, time.UTC)
138 cursor := time.Date(2026, 7, 1, 12, 30, 0, 0, time.UTC)
139 store := &fakeBillingStore{
140 cursor: cursor, hasAck: true,
141 rows: []configstore.ComputeUsageRow{{
142 Date: "2026-07-01", OrgID: "org_abc", TeamID: 12345, QuerySource: "standard",
143 CPU: "8", MemGiB: "16", CPUSeconds: 4800, MemorySeconds: 9600,
144 }},
145 storageRows: []configstore.StorageUsageRow{{
146 Date: "2026-07-01", OrgID: "org_abc", TeamID: 12345, GiBSeconds: "18000000.5",
147 }},
148 }
149 router := newBillingTestRouter(store, now)
150
151 rec := httptest.NewRecorder()
152 router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/v1/billing/usage", nil))
153 if rec.Code != http.StatusOK {
154 t.Fatalf("status = %d: %s", rec.Code, rec.Body.String())
155 }
156 var resp usageResponse
157 if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
158 t.Fatalf("decode: %v", err)
159 }
160 if !resp.WatermarkLow.Equal(cursor) {
161 t.Fatalf("watermark_low = %v, want cursor %v", resp.WatermarkLow, cursor)
162 }
163 // Latest closed bucket at 12:40:47 with 60s width + 30s grace:
164 // 12:40:47 − 90s = 12:39:17 → truncated to 12:39:00.
165 wantHigh := time.Date(2026, 7, 1, 12, 39, 0, 0, time.UTC)

Callers

nothing calls this directly

Calls 3

newBillingTestRouterFunction · 0.85
EqualMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected