Skip to content

Latest commit

 

History

History
295 lines (223 loc) · 7.41 KB

File metadata and controls

295 lines (223 loc) · 7.41 KB

Test Coverage Summary

Overview

This document provides a summary of the testing implementation for the LinkKeeper project.

Test Statistics

Coverage by Package

Package Coverage Test Files
internal/user-service/usecase 100.0% user_test.go
internal/user-service/repository 86.4% user_test.go
internal/api-service/usecase 52.2% link_test.go
internal/user-service/transport/http 50.7% http_test.go
tests (integration) integration_test.go

Total Test Count

  • Unit Tests: 35 tests
    • User Service Repository: 8 tests
    • User Service Usecase: 8 tests
    • User Service HTTP Transport: 8 tests
    • API Service Usecase: 11 tests
  • Integration Tests: 3 tests
  • Total: 38 tests

Test Categories

1. User Service Tests

Repository Layer (internal/user-service/repository/user_test.go)

  • TestUserRepo_Create - User creation
  • TestUserRepo_Create_DuplicateTelegramID - Duplicate prevention
  • TestUserRepo_GetByID - Retrieval by UUID
  • TestUserRepo_GetByID_NotFound - Not found handling
  • TestUserRepo_GetByTelegramID - Retrieval by Telegram ID
  • TestUserRepo_GetByTelegramID_NotFound - Not found handling
  • TestUserRepo_Update - User update
  • TestUserRepo_Exists - Existence check

Coverage: 86.4%

Usecase Layer (internal/user-service/usecase/user_test.go)

  • TestUserUsecase_CreateUser - User creation
  • TestUserUsecase_CreateUser_Error - Error handling
  • TestUserUsecase_GetUserByID - Retrieval by ID
  • TestUserUsecase_GetUserByTelegramID - Retrieval by Telegram ID
  • TestUserUsecase_GetOrCreateUser_Existing - GetOrCreate with existing user
  • TestUserUsecase_GetOrCreateUser_New - GetOrCreate with new user
  • TestUserUsecase_UserExists - Existence check
  • TestUserUsecase_UserExists_NotFound - Non-existent user check

Coverage: 100.0%

HTTP Transport Layer (internal/user-service/transport/http/http_test.go)

  • TestGetOrCreateUser_Success - Successful user creation/retrieval
  • TestGetOrCreateUser_InvalidJSON - Invalid request handling
  • TestGetOrCreateUser_MissingTelegramID - Validation
  • TestGetUserByID_Success - Successful retrieval
  • TestGetUserByID_InvalidUUID - Invalid UUID handling
  • TestGetUserByID_NotFound - Not found handling
  • TestCheckUserExists_True - Existence check (exists)
  • TestCheckUserExists_False - Existence check (doesn't exist)

Coverage: 50.7%

2. API Service Tests

Usecase Layer (internal/api-service/usecase/link_test.go)

  • TestLinkService_Create - Link creation
  • TestLinkService_Create_WithResource - Link creation with resource
  • TestLinkService_GetByID - Retrieval by ID
  • TestLinkService_List - List links
  • TestLinkService_Random - Random link selection
  • TestLinkService_MarkViewed - Mark as viewed
  • TestLinkService_Delete - Link deletion
  • TestLinkService_GetViewStats - View statistics
  • TestLinkService_Error_Handling - Error scenarios
    • Create Error
    • GetByID Error
    • List Error
    • Random Error

Coverage: 52.2%

3. Integration Tests

End-to-End Tests (tests/integration_test.go)

  • TestIntegration_CreateAndGetUser - User creation and idempotency
  • TestIntegration_UserExists - User existence verification
  • TestIntegration_MultipleUsers - Multiple user management

Testing Tools & Libraries

Dependencies

  • testify/assert - Assertion library
  • testify/require - Required assertions
  • testify/mock - Mocking framework
  • gorm.io/driver/sqlite - In-memory database for tests

Test Database

  • Uses SQLite in-memory database for isolation
  • Each test gets a fresh database instance
  • No external dependencies required

Running Tests

Quick Commands

# Run all tests
task test

# Run with coverage
task test:coverage

# Run only unit tests
task test:unit

# Run only integration tests
task test:integration

# Run CI checks locally
task ci:local

Direct Go Commands

# All tests
go test ./...

# Verbose output
go test -v ./...

# With coverage
go test -coverprofile=coverage.out ./...

# Race detector
go test -race ./...

# Short mode (skip integration)
go test -short ./...

CI/CD Pipeline

GitHub Actions

The CI pipeline (.github/workflows/ci.yml) runs on every push and PR:

  1. Test Job

    • Go 1.23
    • PostgreSQL 16
    • Runs all tests with race detector
    • Generates coverage report
    • Uploads to Codecov
  2. Lint Job

    • golangci-lint
    • go fmt check
    • go vet
  3. Build Job

    • Builds all three services
    • Verifies compilation
  4. Docker Job

    • Builds Docker images (main branch only)
    • Uses build cache

Pre-commit Hooks

Install with:

task hooks:install

Hooks run:

  • Code formatting
  • go vet
  • go mod tidy
  • Unit tests

Test Patterns

1. Table-Driven Tests

Used for testing multiple scenarios with similar structure.

2. Mock-Based Testing

Using testify/mock for isolating units under test.

3. In-Memory Database

SQLite for fast, isolated integration tests.

4. HTTP Test Recorder

Using httptest.ResponseRecorder for HTTP handler tests.

Coverage Goals

Category Current Target
User Service Usecase 100.0% ✅ 100%
User Service Repository 86.4% ⚠️ 90%
API Service Usecase 52.2% ⚠️ 80%
User Service HTTP 50.7% ⚠️ 80%
Overall ~70% 🎯 85%

Future Improvements

Short Term

  • Add HTTP handler tests for API service
  • Add repository tests for API service (with real DB)
  • Increase coverage to 80%+
  • Add bot service tests

Medium Term

  • Add benchmark tests
  • Add E2E tests with Docker Compose
  • Add frontend tests
  • Performance regression tests

Long Term

  • Mutation testing
  • Fuzz testing
  • Load testing
  • Security testing

Test Maintenance

Best Practices

  1. Keep tests isolated and independent
  2. Use meaningful test names
  3. Test both success and error cases
  4. Mock external dependencies
  5. Clean up resources properly
  6. Use test fixtures for complex data
  7. Keep tests fast (<100ms per test)
  8. Write tests before fixing bugs

When to Update Tests

  • ✅ When adding new features
  • ✅ When fixing bugs (add test first)
  • ✅ When refactoring code
  • ✅ When changing interfaces
  • ✅ When dependencies change

Troubleshooting

Common Issues

Tests fail locally but pass in CI

go clean -cache -testcache
go test ./...

Race detector warnings

go test -race ./...

Coverage not accurate

go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out

Documentation

  • See TESTING.md for detailed testing guide
  • See CI_CD.md for CI/CD documentation
  • See .github/workflows/ci.yml for pipeline configuration
  • See .golangci.yml for linter configuration

Commit Guidelines

When committing test changes, use conventional commits:

test(user): add tests for user repository
test(api): improve link service coverage
test(integration): add e2e tests for user flow
ci: update GitHub Actions workflow

Contact & Support

For questions about testing:

  1. Check TESTING.md documentation
  2. Review existing tests for examples
  3. Ask in team chat
  4. Create an issue if you find problems