Skip to main content

Analytics Service — Testing Strategy

Status: populated Owner: Platform Engineering + QA Last updated: 2026-04-18

1. Coverage Targets

LayerTargetTool
Use cases (upsert logic, rollup)90% (branch)Vitest
Value objects (HourBucket, DeliveryRate)100%Vitest
Query builders85%Vitest
Integration (NATS → PG → REST)All critical pathsTestcontainers

2. Unit Tests

  • RollUpHourlyToDailyUseCase: correct aggregation of hourly rows into daily; idempotent (run twice → same result).
  • Idempotency: same eventId processed twice → processed_events dedup → counter unchanged.
  • HourBucket VO: truncation correctness at hour boundary.
  • DeliveryRate: 0/0 = null, 10/10 = 1.0, 7/10 = 0.7.
  • ProcessBillingEventUseCase: BILLED increments totalMessages + totalCost; REFUNDED decrements.
  • ProcessDlrEventUseCase: DELIVERED increments deliveredMessages; FAILED increments failedMessages; latency accumulates.

3. Integration Tests (Mandatory)

  • test/integration/billing-event-upsert.spec.ts — consume billing event → PG upsert in all 3 scopes (platform, operator, account).
  • test/integration/dlr-event-upsert.spec.ts — consume DLR event → operator_performance upserted correctly.
  • test/integration/idempotency.spec.ts — same billing event published twice → counter unchanged after second process.
  • test/integration/rollup.spec.ts — insert hourly rows → run rollup → daily row matches sum.
  • test/integration/summary-api.spec.ts — REST GET /summary returns correct aggregated values from seeded data.
  • test/integration/account-scope.spec.ts/accounts/:id/usage with mismatched account header returns 403.
  • test/integration/backfill.spec.ts — replay events with eventId already in processed_events → no change.

4. Contract Tests

  • Schema conformance: sms.billed.v1 and sms.dlr.inbound.v1 consumed schemas validated against platform schema registry in CI.
  • If upstream changes schema, analytics CI detects incompatibility before deployment.

5. Data Accuracy Tests

  • End-to-end accuracy test (staging): inject 10,000 synthetic billing events → verify summary totals match sum of injected data exactly.
  • Latency distribution test: inject DLR events with known latency values → verify avg and P95 within 5% of expected.

6. Performance Tests

  • Upsert throughput: 5,000 billing events/sec sustained, P95 upsert ≤ 10 ms.
  • REST API: 100 concurrent dashboard requests, P95 ≤ 500 ms for summary.
  • k6 scenarios in loadtests/anlyt/.