Skip to content

test(cleanup): Add repository unit tests - #10

Merged
k8ika0s merged 1 commit into
mainfrom
feature/cleanup-backend-unit-tests
Jan 19, 2026
Merged

test(cleanup): Add repository unit tests#10
k8ika0s merged 1 commit into
mainfrom
feature/cleanup-backend-unit-tests

Conversation

@k8ika0s

@k8ika0s k8ika0s commented Jan 19, 2026

Copy link
Copy Markdown
Owner

Summary

Adds comprehensive unit tests for the cleanup repository layer, covering all CRUD operations and statistics management.

Changes Made

1. Repository Tests (backend/internal/cleanup/repository_test.go - 428 lines)

  • CreateJob Tests: Success and database error cases
  • GetJob Tests: Successful retrieval and not found cases
  • UpdateJob Tests: Success and not found cases
  • ListJobs Tests: With filters and empty results
  • DeleteJob Tests: Success and not found cases
  • GetJobStats Tests: Success and not found (returns empty stats)
  • UpdateJobStats Tests: Success and database error cases

2. Repository Interface Update (backend/internal/cleanup/repository.go)

  • Added DBPool interface for database operations
  • Modified repository to accept DBPool instead of *pgxpool.Pool
  • Enables mocking for unit tests while maintaining production compatibility

3. Dependencies (go.mod, go.sum)

  • Added github.com/pashagolub/pgxmock/v4 v4.9.0
  • Upgraded github.com/jackc/pgx/v5 to v5.7.4

Testing Performed

✅ All 14 repository unit tests passing

=== RUN   TestRepository_CreateJob
=== RUN   TestRepository_CreateJob/successful_job_creation
=== RUN   TestRepository_CreateJob/database_error
--- PASS: TestRepository_CreateJob (0.00s)
=== RUN   TestRepository_GetJob
=== RUN   TestRepository_GetJob/successful_job_retrieval
=== RUN   TestRepository_GetJob/job_not_found
--- PASS: TestRepository_GetJob (0.00s)
=== RUN   TestRepository_UpdateJob
=== RUN   TestRepository_UpdateJob/successful_job_update
=== RUN   TestRepository_UpdateJob/job_not_found
--- PASS: TestRepository_UpdateJob (0.00s)
=== RUN   TestRepository_ListJobs
=== RUN   TestRepository_ListJobs/list_jobs_with_filters
=== RUN   TestRepository_ListJobs/empty_result
--- PASS: TestRepository_ListJobs (0.00s)
=== RUN   TestRepository_DeleteJob
=== RUN   TestRepository_DeleteJob/successful_job_deletion
=== RUN   TestRepository_DeleteJob/job_not_found
--- PASS: TestRepository_DeleteJob (0.00s)
=== RUN   TestRepository_GetJobStats
=== RUN   TestRepository_GetJobStats/successful_stats_retrieval
=== RUN   TestRepository_GetJobStats/stats_not_found
--- PASS: TestRepository_GetJobStats (0.00s)
=== RUN   TestRepository_UpdateJobStats
=== RUN   TestRepository_UpdateJobStats/successful_stats_update
=== RUN   TestRepository_UpdateJobStats/database_error
--- PASS: TestRepository_UpdateJobStats (0.00s)
PASS
ok  	github.com/k8ika0s/s3-web/backend/internal/cleanup	0.007s

Test Coverage Details

CreateJob

  • ✅ Successful job creation with auto-generated ID and timestamp
  • ✅ Database error handling

GetJob

  • ✅ Successful job retrieval with all fields
  • ✅ Not found error (pgx.ErrNoRows)

UpdateJob

  • ✅ Successful job status update
  • ✅ Not found error (0 rows affected)

ListJobs

  • ✅ List with multiple filters (LocationID, Status, Limit, Offset)
  • ✅ Empty result handling
  • ✅ Proper pagination support

DeleteJob

  • ✅ Successful job deletion
  • ✅ Not found error (0 rows affected)

GetJobStats

  • ✅ Successful stats retrieval
  • ✅ Not found returns empty stats (not error)

UpdateJobStats

  • ✅ Successful stats upsert (INSERT ... ON CONFLICT)
  • ✅ Database error handling

Breaking Changes

None - this is purely additive test coverage.

Dependencies Added

  • pgxmock/v4 - PostgreSQL mock for unit testing

Checklist

  • Code follows project style guidelines
  • Tests added and passing (14 new tests)
  • Documentation updated (test comments)
  • No breaking changes
  • Commit messages follow convention
  • Branch is up to date with main

Related Issues

Part of cleanup feature test coverage initiative.
Follows PR #8 (cleanupStore tests) and PR #9 (UI component tests).

Next Steps

  • Service layer unit tests
  • Activity and workflow tests
  • Integration tests with testcontainers
  • End-to-end tests

- Add comprehensive unit tests for cleanup repository
- Test all CRUD operations: CreateJob, GetJob, UpdateJob, ListJobs, DeleteJob
- Test job statistics operations: GetJobStats, UpdateJobStats
- Use pgxmock for database mocking
- Modify repository to accept DBPool interface for testability
- Add pgxmock/v4 dependency
- All 14 repository tests passing

Coverage:
- CreateJob: success and error cases
- GetJob: success and not found cases
- UpdateJob: success and not found cases
- ListJobs: with filters and empty results
- DeleteJob: success and not found cases
- GetJobStats: success and not found (returns empty stats)
- UpdateJobStats: success and error cases
@k8ika0s
k8ika0s merged commit 3a3c400 into main Jan 19, 2026
6 of 7 checks passed
@k8ika0s
k8ika0s deleted the feature/cleanup-backend-unit-tests branch January 19, 2026 21:23
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