Files
you-music/UNIT_TESTS.md
T
2025-11-02 23:11:05 +11:00

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() function
  • Music.artist_list property
  • Music.display_artist property

Test Cases:

  1. Single artist names (should remain unchanged)
  2. Multiple artists with / separator (converted to , )
  3. Multiple artists with , separator (standardized spacing)
  4. Mixed separators (both / and ,)
  5. Extra whitespace cleanup
  6. Empty and Unknown values
  7. Real-world examples from database
  8. Preservation of ALL artists (not just first)
  9. Special characters in names (dots, apostrophes)
  10. 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:

  1. Basic functionality (null, undefined, unknown)
  2. Single artist display
  3. Multiple artist display
  4. Smart truncation: "FirstArtist, +N"
  5. Edge cases (very short maxLength)
  6. Real-world player scenarios
  7. Consistency with backend normalization
  8. 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 configuration
  • frontend/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

  1. Install dependencies (if not already done)
  2. Run tests to verify everything passes
  3. Add to CI/CD pipeline for automated testing
  4. Write new tests when adding features
  5. 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!