""" 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