lablup / lablup/backend.ai

Convert service tests to mock-based

Open
#7,669 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
670
Forks
183
Avg merge
17h 7m
Merged PRs (30d)
358

Description

## Overview

Convert service layer tests from using real database to mock-based testing. Service tests should only verify business logic, not database interactions.

## Current Problem

- Service tests depend on `database_fixture` (creates all tables)
- `extra_fixtures` parametrize creates multiple DB scenarios per test
- Each test takes ~2.5s due to DB overhead
- This is essentially integration testing, not unit testing

## Target Files

|Test File|Current Time|Tests|Expected Time|
|---|---|---|---|
|`test_domain.py` (services)|92.26s|37|~7.4s|
|`test_users.py`|42.45s|12|~2.4s|
|`test_user_integration.py`|38.67s|11|~2.2s|
|Image action tests (4 files)|120.82s|8|~1.6s|
|Model serving tests (7 files)|175.64s|9|~1.8s|
|`test_group.py` (services)|30.40s|7|~1.4s|

## Work Items

### 1. Setup Mock Base (`tests/unit/manager/services/conftest.py`)

```python
from unittest.mock import AsyncMock

@pytest.fixture
def mock_domain_repository():
return AsyncMock(spec=DomainRepository)

@pytest.fixture
def domain_service(mock_domain_repository):
return DomainService(
repository=mock_domain_repository,
admin_repository=AsyncMock(),
)
```

### 2. Convert Each Service Test

- Remove `database_fixture`, `database_engine` dependencies
- Remove `extra_fixtures` parametrize
- Replace repository with `AsyncMock(spec=Repository)`
- Set up mock return values for each test scenario

### Example Conversion

```python
# Before
@pytest.fixture
def processors(database_fixture, database_engine):
repository = DomainRepository(db=database_engine)
...

# After
@pytest.fixture
def processors(mock_domain_repository):
service = DomainService(repository=mock_domain_repository)
...

async def test_create_domain(processors, mock_domain_repository):
mock_domain_repository.create_domain.return_value = expected_result
result = await processors.service.create_domain(input_data)
mock_domain_repository.create_domain.assert_called_once()
```

## Acceptance Criteria

- [ ] All service tests run without database dependency
- [ ] `extra_fixtures` parametrize removed from service tests
- [ ] Mock fixtures created for each repository type
- [ ] All service tests pass
- [ ] Test execution time reduced by ~96%

## Parent Epic

BA-3611

JIRA Issue: BA-3614

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.