feat: Implement comprehensive test suite for Activity API (004-activity-api-tests)#84
Merged
willvelida merged 2 commits intomainfrom Oct 29, 2025
Merged
feat: Implement comprehensive test suite for Activity API (004-activity-api-tests)#84willvelida merged 2 commits intomainfrom
willvelida merged 2 commits intomainfrom
Conversation
…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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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 validationCosmosRepositoryShould.cs- 17 tests for database operations and error handlingActivityShould.cs- 15 tests for Fitbit entity mappingsEndpointRouteBuilderExtensionsShould.cs- 16 tests for endpoint registrationSettingsShould.cs- 10 tests for configuration validationIntegration Tests (30 tests):
ProgramStartupTests.cs- Verify application startup and DI containerApiSmokeTests.cs- Validate HTTP pipeline and health checksActivityEndpointsTests.cs- Full integration with Cosmos DB EmulatorTest Fixtures:
IntegrationTestFixture- Cosmos DB Emulator connection managementActivityApiWebApplicationFactory- WebApplicationFactory with Gateway mode for E2E testsContractTestFixture- Fast contract test fixture for DI validationTestDataHelper- Generate consistent test dataAPI Enhancements
GetActivityByDatehandler (returns 400 Bad Request for invalid dates)GetAllActivitiesto 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 emulatorcosmos-emulator.ps1- PowerShell helper script with start/stop/status/cert commandsdocs/cosmos-emulator-setup.md- Comprehensive setup guide with troubleshootingKey Configuration:
CI/CD Integration
GitHub Actions Workflow Updates (
deploy-activity-api.yml):run-e2e-testsjob usingtemplate-dotnet-run-e2e-tests.ymlFullyQualifiedName~E2Eto match E2E namespace patternWorkflow Execution:
Documentation
common-resolutions.mdwith Gateway mode fix for SSL negotiation issuescopilot-instructions.mdwith test patterns and technologiesspecs/004-activity-api-tests/Testing Instructions
Run All Tests Locally
Run Specific Test Types
Key Technical Decisions
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)
Test Organization: Separated contract tests (fast, no dependencies) from E2E tests (requires Cosmos DB) using namespace-based organization (decision record)
Parallel CI/CD Execution: E2E tests run in parallel with deployment since they use the emulator, not the deployed environment, reducing pipeline duration
Date Validation: Added input validation at the handler level to return 400 Bad Request before making repository calls, improving API correctness
Checklist
Screenshots
Local Test Execution:
Coverage Report:
Related Issues