paper-dynasty-database/tests/test_dependencies.py
Cal Corum 64be5eabdc feat: add initial test suite with pytest (#28)
- Add SQLITE_DB_PATH env var to db_engine.py for test isolation
- Create tests/conftest.py with in-memory SQLite fixture and sample data helpers
- Add tests/test_dependencies.py: unit tests for valid_token, mround, param_char, get_req_url
- Add tests/test_card_pricing.py: tests for Player.change_on_sell/buy and get_all_pos
- Add tests/test_api_packs.py: integration tests for GET/POST/DELETE /api/v2/packs
- Add requirements-test.txt with pytest and httpx
- Add test job to CI workflow (build now requires tests to pass first)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-04 16:35:53 -06:00

97 lines
3.3 KiB
Python

"""
Unit tests for app/dependencies.py utility functions.
Tests pure functions that have no database or HTTP dependencies.
These run fast and verify foundational logic used across all API routes.
"""
import os
import pytest
from app.dependencies import valid_token, mround, param_char, get_req_url
class TestValidToken:
"""Tests for valid_token() — verifies API bearer token auth."""
def test_matching_token_returns_true(self):
"""valid_token should return True when the token matches API_TOKEN env var."""
expected = os.environ.get("API_TOKEN", "")
assert valid_token(expected) is True
def test_wrong_token_returns_false(self):
"""valid_token should return False for any non-matching string."""
assert valid_token("wrong_token") is False
def test_empty_token_returns_false(self):
"""valid_token should return False for an empty string."""
assert valid_token("") is False
class TestMround:
"""Tests for mround() — rounds a float to the nearest multiple of `base`."""
def test_rounds_to_nearest_0_05(self):
"""Default base=0.05 should round 0.06 to 0.05."""
assert mround(0.06) == 0.05
def test_rounds_up_to_nearest_0_05(self):
"""0.08 should round up to 0.10 with base=0.05."""
assert mround(0.08) == 0.10
def test_exact_multiple_unchanged(self):
"""A value that is already a multiple of base should be unchanged."""
assert mround(0.25) == 0.25
def test_custom_base(self):
"""Custom base=0.25 should round to nearest quarter."""
assert mround(0.3, base=0.25) == 0.25
def test_custom_precision(self):
"""Custom prec=4 should return more decimal places."""
result = mround(0.123456, prec=4, base=0.01)
assert result == 0.12
class TestParamChar:
"""Tests for param_char() — returns ? or & for URL query string building."""
def test_returns_question_mark_when_no_other_params(self):
"""First parameter in a URL should use ?."""
assert param_char(False) == "?"
def test_returns_ampersand_when_other_params_exist(self):
"""Subsequent parameters should use &."""
assert param_char(True) == "&"
class TestGetReqUrl:
"""Tests for get_req_url() — builds API URLs with optional params."""
def test_basic_endpoint(self):
"""Endpoint with no object_id or params should produce a clean URL."""
url = get_req_url("players")
assert url.endswith("/v2/players")
def test_endpoint_with_object_id(self):
"""object_id should be appended to the URL path."""
url = get_req_url("players", object_id=42)
assert url.endswith("/v2/players/42")
def test_endpoint_with_params(self):
"""Params list should be appended as query string."""
url = get_req_url("players", params=[("season", "1")])
assert "?season=1" in url
def test_endpoint_with_multiple_params(self):
"""Multiple params should be joined with &."""
url = get_req_url("players", params=[("season", "1"), ("limit", "10")])
assert "?season=1" in url
assert "&limit=10" in url
def test_api_version_override(self):
"""api_ver parameter controls the version segment."""
url = get_req_url("players", api_ver=1)
assert "/v1/players" in url