Skip to content

feat(tests): implement unit tests and DB mocking for Hub REST API - #33

Merged
Vaishnav88sk merged 6 commits into
masterfrom
feat/hub-api-tests
Jun 15, 2026
Merged

Vaishnav88sk merged 6 commits into
masterfrom
feat/hub-api-tests

Conversation

@Vaishnav88sk

Copy link
Copy Markdown
Owner

📖 Description

This PR addresses Issue #19 by introducing comprehensive unit tests for the SRE Hub REST API.

To ensure the CI tests can run quickly and securely without requiring a live PostgreSQL instance, we integrated go-sqlmock to intercept and mock all database queries at the database/sql layer.

✅ Changes Made

  • Dependency Addition: Added github.com/DATA-DOG/go-sqlmock to the sre-agent/hub module.
  • Database Refactor: Introduced a new NewWithDB(db *sql.DB) constructor in internal/db/store.go to safely inject mocked connections without triggering schema migrations.
  • API Tests: Created internal/api/handlers_test.go utilizing net/http/httptest to test our endpoints.

🧪 Testing Scope

The new test suite fully covers the acceptance criteria defined in #19:

  • TestReceiveIncident_Valid: Verifies a 201 Created response when a valid incident payload is submitted and database expectations are met.
  • TestReceiveIncident_InvalidJSON: Verifies a 400 Bad Request error is returned when malformed JSON is posted.
  • TestReceiveIncident_Unauthorized: Verifies a 401 Unauthorized error when the X-Claritty-Key header is missing.
  • TestGetStats: Verifies that the metrics calculations return a 200 OK JSON response.

Fixes #19

@Vaishnav88sk Vaishnav88sk self-assigned this Jun 15, 2026
@Vaishnav88sk Vaishnav88sk added this to the v1.1 Release milestone Jun 15, 2026
@codecov

codecov Bot commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Vaishnav88sk
Vaishnav88sk merged commit e1cf464 into master Jun 15, 2026
9 checks passed
@Vaishnav88sk
Vaishnav88sk deleted the feat/hub-api-tests branch June 15, 2026 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Tests] Implement Unit Tests for SRE Hub REST API & Database

1 participant