This document provides a summary of the testing implementation for the LinkKeeper project.
| 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 |
- 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
- ✅
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%
- ✅
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%
- ✅
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%
- ✅
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%
- ✅
TestIntegration_CreateAndGetUser- User creation and idempotency - ✅
TestIntegration_UserExists- User existence verification - ✅
TestIntegration_MultipleUsers- Multiple user management
- testify/assert - Assertion library
- testify/require - Required assertions
- testify/mock - Mocking framework
- gorm.io/driver/sqlite - In-memory database for tests
- Uses SQLite in-memory database for isolation
- Each test gets a fresh database instance
- No external dependencies required
# 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# 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 ./...The CI pipeline (.github/workflows/ci.yml) runs on every push and PR:
-
Test Job
- Go 1.23
- PostgreSQL 16
- Runs all tests with race detector
- Generates coverage report
- Uploads to Codecov
-
Lint Job
- golangci-lint
- go fmt check
- go vet
-
Build Job
- Builds all three services
- Verifies compilation
-
Docker Job
- Builds Docker images (main branch only)
- Uses build cache
Install with:
task hooks:installHooks run:
- Code formatting
- go vet
- go mod tidy
- Unit tests
Used for testing multiple scenarios with similar structure.
Using testify/mock for isolating units under test.
SQLite for fast, isolated integration tests.
Using httptest.ResponseRecorder for HTTP handler tests.
| Category | Current | Target |
|---|---|---|
| User Service Usecase | 100.0% | ✅ 100% |
| User Service Repository | 86.4% | |
| API Service Usecase | 52.2% | |
| User Service HTTP | 50.7% | |
| Overall | ~70% | 🎯 85% |
- Add HTTP handler tests for API service
- Add repository tests for API service (with real DB)
- Increase coverage to 80%+
- Add bot service tests
- Add benchmark tests
- Add E2E tests with Docker Compose
- Add frontend tests
- Performance regression tests
- Mutation testing
- Fuzz testing
- Load testing
- Security testing
- Keep tests isolated and independent
- Use meaningful test names
- Test both success and error cases
- Mock external dependencies
- Clean up resources properly
- Use test fixtures for complex data
- Keep tests fast (<100ms per test)
- Write tests before fixing bugs
- ✅ When adding new features
- ✅ When fixing bugs (add test first)
- ✅ When refactoring code
- ✅ When changing interfaces
- ✅ When dependencies change
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- See TESTING.md for detailed testing guide
- See CI_CD.md for CI/CD documentation
- See
.github/workflows/ci.ymlfor pipeline configuration - See
.golangci.ymlfor linter configuration
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
For questions about testing:
- Check TESTING.md documentation
- Review existing tests for examples
- Ask in team chat
- Create an issue if you find problems