wrds-download

TUI/CLI tool for browsing and downloading WRDS data
Log | Files | Refs | README

test_config.py (2565B)


      1 """Tests for config module — credentials file parsing."""
      2 
      3 import os
      4 from pathlib import Path
      5 from unittest import mock
      6 
      7 from wrds_dl.config import apply_credentials, credentials_path, load_credentials
      8 
      9 
     10 class TestCredentialsPath:
     11     def test_default(self):
     12         with mock.patch.dict(os.environ, {}, clear=True):
     13             path = credentials_path()
     14             assert path == Path.home() / ".config" / "wrds-dl" / "credentials"
     15 
     16     def test_xdg(self):
     17         with mock.patch.dict(os.environ, {"XDG_CONFIG_HOME": "/tmp/xdg"}):
     18             path = credentials_path()
     19             assert path == Path("/tmp/xdg/wrds-dl/credentials")
     20 
     21 
     22 class TestLoadCredentials:
     23     def test_missing_file(self, tmp_path):
     24         with mock.patch("wrds_dl.config.credentials_path", return_value=tmp_path / "missing"):
     25             user, pw, db = load_credentials()
     26             assert user == ""
     27             assert pw == ""
     28             assert db == ""
     29 
     30     def test_valid_file(self, tmp_path):
     31         creds = tmp_path / "credentials"
     32         creds.write_text("PGUSER=alice\nPGPASSWORD=secret\nPGDATABASE=wrds\n")
     33         with mock.patch("wrds_dl.config.credentials_path", return_value=creds):
     34             user, pw, db = load_credentials()
     35             assert user == "alice"
     36             assert pw == "secret"
     37             assert db == "wrds"
     38 
     39     def test_comments_and_blanks(self, tmp_path):
     40         creds = tmp_path / "credentials"
     41         creds.write_text("# comment\n\nPGUSER=bob\n")
     42         with mock.patch("wrds_dl.config.credentials_path", return_value=creds):
     43             user, pw, db = load_credentials()
     44             assert user == "bob"
     45             assert pw == ""
     46 
     47 
     48 class TestApplyCredentials:
     49     def test_sets_missing_env(self, tmp_path):
     50         creds = tmp_path / "credentials"
     51         creds.write_text("PGUSER=alice\nPGPASSWORD=secret\n")
     52         with mock.patch("wrds_dl.config.credentials_path", return_value=creds):
     53             env = {"PATH": os.environ.get("PATH", "")}
     54             with mock.patch.dict(os.environ, env, clear=True):
     55                 apply_credentials()
     56                 assert os.environ["PGUSER"] == "alice"
     57                 assert os.environ["PGPASSWORD"] == "secret"
     58 
     59     def test_does_not_override(self, tmp_path):
     60         creds = tmp_path / "credentials"
     61         creds.write_text("PGUSER=alice\n")
     62         with mock.patch("wrds_dl.config.credentials_path", return_value=creds):
     63             with mock.patch.dict(os.environ, {"PGUSER": "existing"}):
     64                 apply_credentials()
     65                 assert os.environ["PGUSER"] == "existing"