Skip to content

feat: Implement comprehensive test suite for Activity API (004-activity-api-tests)#84

Merged
willvelida merged 2 commits intomainfrom
004-activity-api-tests
Oct 29, 2025
Merged

feat: Implement comprehensive test suite for Activity API (004-activity-api-tests)#84
willvelida merged 2 commits intomainfrom
004-activity-api-tests

Conversation

@willvelida
Copy link
Owner

Overview

Implements comprehensive test coverage for Activity API achieving 79.3% code coverage with 104 passing tests.

Resolves #4-activity-api-tests

Test Coverage Summary

Test Type Count Status
Unit Tests 74 ✅ All Passing
Contract Tests 18 ✅ All Passing
E2E Tests 12 ✅ All Passing
Total 104 100% Success

Code Coverage: 79.3% (excludes Program.cs entry point per decision record)

Changes

Test Infrastructure

Unit Tests (74 tests):

  • ActivityHandlersShould.cs - 16 tests for endpoint handler logic including date validation
  • CosmosRepositoryShould.cs - 17 tests for database operations and error handling
  • ActivityShould.cs - 15 tests for Fitbit entity mappings
  • EndpointRouteBuilderExtensionsShould.cs - 16 tests for endpoint registration
  • SettingsShould.cs - 10 tests for configuration validation

Integration Tests (30 tests):

  • Contract Tests (18 tests):
    • ProgramStartupTests.cs - Verify application startup and DI container
    • ApiSmokeTests.cs - Validate HTTP pipeline and health checks
  • E2E Tests (12 tests):
    • ActivityEndpointsTests.cs - Full integration with Cosmos DB Emulator
    • Tests GET /activities, GET /activities/{date}, GET /activities/range endpoints
    • Includes performance and data persistence validation

Test Fixtures:

  • IntegrationTestFixture - Cosmos DB Emulator connection management
  • ActivityApiWebApplicationFactory - WebApplicationFactory with Gateway mode for E2E tests
  • ContractTestFixture - Fast contract test fixture for DI validation
  • TestDataHelper - Generate consistent test data

API Enhancements

  • ✅ Add date format validation in GetActivityByDate handler (returns 400 Bad Request for invalid dates)
  • ✅ Fix GetAllActivities to return 200 OK with empty array when no data exists (was incorrectly returning 404)

Cosmos DB Emulator Infrastructure

Local Development:

  • docker-compose.cosmos.yml - Container configuration for local emulator
  • cosmos-emulator.ps1 - PowerShell helper script with start/stop/status/cert commands
  • docs/cosmos-emulator-setup.md - Comprehensive setup guide with troubleshooting

Key Configuration:

  • Uses Gateway mode (HTTPS port 8081) instead of Direct mode to avoid SSL negotiation issues
  • Configured with 10 partitions, no data persistence for fast test execution
  • Certificate validation bypass for local development

CI/CD Integration

GitHub Actions Workflow Updates (deploy-activity-api.yml):

  • ✅ Added run-e2e-tests job using template-dotnet-run-e2e-tests.yml
  • ✅ E2E tests run in parallel with deployment using Cosmos DB Emulator service container
  • ✅ Test filter: FullyQualifiedName~E2E to match E2E namespace pattern
  • ✅ All 104 tests will now execute in CI/CD pipeline

Workflow Execution:

  1. Unit tests (70% coverage threshold)
  2. Contract tests (parallel with unit tests)
  3. E2E tests (parallel with deployment, uses Cosmos DB Emulator)
  4. Build container image
  5. Deploy to dev environment

Documentation

  • ✅ README files for test projects with setup and execution instructions
  • ✅ Updated common-resolutions.md with Gateway mode fix for SSL negotiation issues
  • ✅ Added decision record for coverlet extension method coverage anomaly
  • ✅ Updated copilot-instructions.md with test patterns and technologies
  • ✅ Complete spec documentation in specs/004-activity-api-tests/

Testing Instructions

Run All Tests Locally

# Start Cosmos DB Emulator
.\cosmos-emulator.ps1 start

# Wait for emulator to be healthy (takes ~2 minutes)
.\cosmos-emulator.ps1 status

# Run all tests
cd src/Biotrackr.Activity.Api
dotnet test --verbosity minimal

# Stop emulator when done
.\cosmos-emulator.ps1 stop

Run Specific Test Types

# Unit tests only
dotnet test --filter "FullyQualifiedName!~IntegrationTests"

# Contract tests only
dotnet test --filter "FullyQualifiedName~Contract"

# E2E tests only (requires Cosmos DB Emulator)
dotnet test --filter "FullyQualifiedName~E2E"

Key Technical Decisions

  1. Gateway Mode for Cosmos DB: Uses Gateway mode (HTTPS) instead of Direct mode (TCP+SSL) to avoid SSL certificate negotiation issues with the emulator's self-signed certificate (documented in common-resolutions.md)

  2. Test Organization: Separated contract tests (fast, no dependencies) from E2E tests (requires Cosmos DB) using namespace-based organization (decision record)

  3. Parallel CI/CD Execution: E2E tests run in parallel with deployment since they use the emulator, not the deployed environment, reducing pipeline duration

  4. Date Validation: Added input validation at the handler level to return 400 Bad Request before making repository calls, improving API correctness

Checklist

  • All tests passing locally (104/104)
  • Code coverage meets threshold (79.3% > 70%)
  • Unit tests implemented for all handlers, repository, models, extensions, configuration
  • Contract tests validate DI and startup behavior
  • E2E tests validate endpoints with Cosmos DB integration
  • GitHub Actions workflow updated with E2E test job
  • Cosmos DB Emulator setup documented and automated
  • README files added for test projects
  • Decision records created for key technical choices
  • Common resolutions documented for troubleshooting

Screenshots

Local Test Execution:

Test summary: total: 104, failed: 0, succeeded: 104, skipped: 0, duration: 8.9s
Build succeeded

Coverage Report:

| Module                 | Line   | Branch | Method |
|------------------------|--------|--------|--------|
| Biotrackr.Activity.Api | 79.3%  | 76.3%  | 79.6%  |

Related Issues

…ty-api-tests)

Implement 104 tests achieving 79.3% code coverage for Activity API:
- 74 unit tests (handlers, repository, models, configuration, extensions)
- 18 contract tests (startup validation, service registration)
- 12 E2E tests (endpoint integration with Cosmos DB Emulator)

## Core Changes

### Test Infrastructure
- Add IntegrationTestFixture with Cosmos DB Emulator support
- Add ActivityApiWebApplicationFactory with Gateway mode configuration
- Add ContractTestFixture for dependency injection validation
- Add TestDataHelper for generating test data
- Configure appsettings.Test.json with emulator connection details

### Unit Tests (74 tests)
- ActivityHandlers: 16 tests covering all endpoint logic including date validation
- CosmosRepository: 17 tests for database operations and error handling
- Activity model: 15 tests for Fitbit entity mappings
- EndpointRouteBuilderExtensions: 16 tests for endpoint registration
- Settings: 10 tests for configuration validation

### Integration Tests (30 tests)
Contract Tests (18 tests):
- ProgramStartupTests: Verify application startup and DI container
- ApiSmokeTests: Validate HTTP pipeline and health checks

E2E Tests (12 tests):
- ActivityEndpointsTests: Full integration with Cosmos DB Emulator
- Validates GET /activities, GET /activities/{date}, GET /activities/range endpoints
- Includes performance and data persistence validation

### API Enhancements
- Add date format validation in GetActivityByDate handler (returns 400 for invalid dates)
- Fix GetAllActivities to return 200 OK with empty array when no data exists

### Cosmos DB Emulator Setup
- Add docker-compose.cosmos.yml for local emulator container
- Add cosmos-emulator.ps1 PowerShell helper (start/stop/status/cert commands)
- Add docs/cosmos-emulator-setup.md with comprehensive setup guide
- Configure Gateway mode (HTTPS port 8081) to avoid SSL negotiation issues with Direct mode

### CI/CD Integration
- Add run-e2e-tests job to deploy-activity-api.yml workflow
- Configure E2E tests to run in parallel with deployment
- Use template-dotnet-run-e2e-tests.yml with Cosmos DB Emulator service

### Documentation
- Add README files for test projects with setup instructions
- Document Gateway mode fix in common-resolutions.md
- Add decision record for coverlet extension method coverage anomaly
- Update copilot-instructions.md with test patterns

## Test Results
Local execution: 104/104 tests passing (100%)
- Unit tests: 74/74 passing
- Contract tests: 18/18 passing
- E2E tests: 12/12 passing (with Cosmos DB Emulator)

Coverage: 79.3% (excludes Program.cs entry point per decision record)

Resolves #4-activity-api-tests
- Change E2E tests to run after deployment (needs: [deploy-dev, env-setup])
  to match Weight API workflow structure
- Update job name from 'Run E2E Tests with Cosmos DB Emulator' to
  'Run E2E Tests Against Dev' for consistency
- Update Activity API badges in README.md:
  - Unit test coverage: 43% → 79.3% (yellow badge)
  - Integration tests: N/A → 30 Passing (brightgreen badge)
@github-actions
Copy link

Code Coverage

Package Line Rate Branch Rate Health
Biotrackr.Activity.Api 80% 86%
Summary 80% (214 / 269) 86% (24 / 28)

Minimum allowed line rate is 70%

@willvelida willvelida merged commit 792e48a into main Oct 29, 2025
13 checks passed
@willvelida willvelida deleted the 004-activity-api-tests branch October 29, 2025 04: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.

1 participant