Internal API for @atauln's MTG Preconstructed Deck League
The frontend is automatically deployed to GitHub Pages: https://atauln.github.io/PreconLeague/
This is a FastAPI-based REST API for managing MTG (Magic: The Gathering) preconstructed deck leagues. It supports deck tracking from Moxfield and Archidekt, snapshot management, and integration with Commander Salt for deck analysis.
- User Management: Create and manage users
- Deck Management: Register decks from Moxfield or Archidekt
- Snapshot System: Track deck evolution over time
- Card Database: Store and retrieve card information
- Commander Salt Integration: Fetch deck ratings and analysis
- Framework: FastAPI
- Database: PostgreSQL with asyncpg
- External APIs: Moxfield, Archidekt, Commander Salt
- Testing: pytest with coverage reporting
- Clone the repository:
git clone https://github.com/atauln/PreconLeague.git
cd PreconLeague- Create a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r api/requirements.txt- Set up environment variables:
cp api/.env.template api/.env
# Edit api/.env with your database credentials- Initialize the database:
cd api
python db.pyStart the development server:
cd api
python api.pyThe API will be available at http://localhost:8000
Access the interactive API documentation at http://localhost:8000/docs
The project uses pytest for testing. All tests are located in the tests/ directory.
Run all tests:
pytestRun tests with coverage:
pytest --cov=api --cov-report=term-missingRun tests with detailed output:
pytest -vRun tests with coverage HTML report:
pytest --cov=api --cov-report=html
# Open htmlcov/index.html in your browserRun specific test file:
pytest tests/test_models.pyRun tests matching a pattern:
pytest -k "test_user"tests/test_models.py- Tests for data models (Card, Deck, CommanderSaltData)tests/test_db.py- Tests for database functionstests/test_routers.py- Tests for API endpointstests/test_archidekt.py- Tests for Archidekt integrationtests/conftest.py- Shared fixtures and configuration
The project includes a GitHub Actions workflow that automatically runs tests on:
- Every push to the
mainbranch - Every pull request targeting the
mainbranch
Workflow features:
- Tests on Python 3.11 and 3.12
- Automatic dependency caching
- Code coverage reporting
- Test result summaries in PR checks
- Basic linting with flake8
Viewing test results:
- Navigate to the "Actions" tab in the GitHub repository
- Click on a workflow run to see detailed results
- Check the "Run Tests" job for test output and coverage
- Pull requests will show test status in the checks section
Coverage reports:
- Coverage is automatically uploaded to Codecov (if configured)
- Coverage summary appears in the GitHub Actions workflow summary
- HTML coverage reports are generated but not uploaded (run locally to view)
GET /users/- Get all usersGET /users/{user_id}- Get user by IDPOST /users/- Create a new user
GET /decks/- Get all decksGET /decks/{deck_id}- Get deck by IDGET /decks/user/{user_id}- Get all decks for a userPOST /decks/- Register a new deck
GET /snapshots/- Get all snapshotsGET /snapshots/{snapshot_id}- Get snapshot by IDGET /snapshots/deck/{deck_id}- Get all snapshots for a deckGET /snapshots/with_library/{snapshot_id}- Get snapshot with library cardsPOST /snapshots/create_snapshot/{deck_id}- Create a new snapshot
GET /cards/- Get all cardsGET /cards/{card_id}- Get card by IDPOST /cards/- Create a new cardPOST /cards/associate- Associate a card with a snapshot
The application uses PostgreSQL with the following main tables:
users- User informationcards- MTG card datadecks- Deck information with source URLssnapshots- Deck snapshots with ratings and metricslibrary_cards- Association between snapshots and cards
- Follow PEP 8 guidelines
- Use type hints where appropriate
- Keep functions focused and testable
- Write tests for new functionality
- Use
pytestfor all tests - Mock external API calls and database connections
- Use
@pytest.mark.asynciofor async tests - Organize tests in classes by functionality
- Use descriptive test names that explain what is being tested
- Create a feature branch
- Write tests for new functionality
- Ensure all tests pass locally
- Submit a pull request
- Wait for CI checks to pass
- Address any review feedback
This project is for personal use by @atauln.
@atauln - GitHub Profile