5.1 KiB
Unit Tests Added
Overview
Comprehensive unit tests have been added for the multi-artist name handling logic to ensure correct behavior and prevent regressions when making changes.
Test Files Created
Backend Tests (Python/pytest)
File: backend/tests/test_artist_normalization.py
Coverage:
normalize_artist_name()functionMusic.artist_listpropertyMusic.display_artistproperty
Test Cases:
- Single artist names (should remain unchanged)
- Multiple artists with
/separator (converted to,) - Multiple artists with
,separator (standardized spacing) - Mixed separators (both
/and,) - Extra whitespace cleanup
- Empty and Unknown values
- Real-world examples from database
- Preservation of ALL artists (not just first)
- Special characters in names (dots, apostrophes)
- Unicode characters (Chinese, accented chars)
Total Test Cases: 20+ individual tests
Frontend Tests (TypeScript/Vitest)
File: frontend/src/lib/__tests__/utils.test.ts
Coverage:
formatArtist()function
Test Cases:
- Basic functionality (null, undefined, unknown)
- Single artist display
- Multiple artist display
- Smart truncation: "FirstArtist, +N"
- Edge cases (very short maxLength)
- Real-world player scenarios
- Consistency with backend normalization
- Special characters and unicode
Total Test Cases: 25+ individual tests
Configuration Files
Backend
backend/pytest.ini- pytest configuration- Updated
backend/pyproject.toml- added pytest dev dependencies
Frontend
frontend/vitest.config.ts- Vitest configurationfrontend/src/lib/__tests__/- test directory
Documentation
TESTING.md- Complete testing guide
Running Tests
Backend (Quick Start)
cd backend
uv pip install pytest pytest-asyncio
pytest tests/test_artist_normalization.py -v
Frontend (Setup Required)
cd frontend
npm install --save-dev vitest jsdom @testing-library/react @testing-library/jest-dom
npm test
Example Test Output
Backend
tests/test_artist_normalization.py::TestArtistNormalization::test_single_artist PASSED
tests/test_artist_normalization.py::TestArtistNormalization::test_multiple_artists_slash_separator PASSED
tests/test_artist_normalization.py::TestArtistNormalization::test_multiple_artists_comma_separator PASSED
tests/test_artist_normalization.py::TestArtistNormalization::test_preserves_all_artists PASSED
...
======================== 20 passed in 0.15s ========================
Frontend
✓ formatArtist > Basic functionality > returns "Unknown Artist" for null
✓ formatArtist > Multiple artists display > shows "FirstArtist, +N" format
✓ formatArtist > Real-world examples > handles 6 artists with limited space
...
Test Files 1 passed (1)
Tests 25 passed (25)
Test Examples
Backend Example
def test_multiple_artists_slash_separator(self):
"""Artists separated by / should be converted to comma-space"""
assert normalize_artist_name("蒋明/冬子/刘东明") == "蒋明, 冬子, 刘东明"
assert normalize_artist_name("Calvin Harris/John Newman") == "Calvin Harris, John Newman"
Frontend Example
test('shows "FirstArtist, +N" format when space is limited', () => {
expect(formatArtist('蒋明, 冬子, 刘东明', 10)).toBe('蒋明, +2')
expect(formatArtist('A, B, C, D, E', 8)).toBe('A, +4')
})
Benefits
✅ Confidence in changes - Modify code knowing tests will catch breaks ✅ Documentation - Tests show expected behavior clearly ✅ Regression prevention - Catch bugs before deployment ✅ Refactoring safety - Change implementation without fear ✅ Real-world coverage - Tests use actual data from the music library ✅ Edge case handling - Tests cover unusual but possible scenarios
Key Test Scenarios Covered
Multi-Artist Handling
- ✅ "蒋明/冬子/刘东明" → "蒋明, 冬子, 刘东明"
- ✅ "Justin Timberlake/Carey Mulligan/Stark Sands" → "Justin Timberlake, Carey Mulligan, Stark Sands"
- ✅ All artists preserved (not just first)
- ✅ Consistent separator (always ", ")
Display Formatting
- ✅ Full display when space permits
- ✅ "蒋明, +5" when limited to 10 characters
- ✅ Smart truncation algorithm
- ✅ Edge cases (very short limits)
Next Steps
- Install dependencies (if not already done)
- Run tests to verify everything passes
- Add to CI/CD pipeline for automated testing
- Write new tests when adding features
- Update tests when changing behavior
CI/CD Integration
Add to your GitHub Actions or CI pipeline:
# .github/workflows/test.yml
- name: Run Backend Tests
run: |
cd backend
uv pip install pytest pytest-asyncio
pytest
- name: Run Frontend Tests
run: |
cd frontend
npm ci
npm test -- --ci
Maintenance
- Add tests for new features before implementing
- Update tests when requirements change
- Run tests before committing changes
- Review coverage periodically to find gaps
The tests ensure the multi-artist feature works correctly and will continue to work as the codebase evolves!