tcga-downloader/tests/test_config.py
yunpeng.zhang a01a59b371
Some checks failed
CI / Lint (push) Failing after 9m32s
CI / Test (3.11) (push) Successful in 6m41s
CI / Test (3.12) (push) Successful in 4m21s
feat: add interactive cli
2026-02-09 13:13:39 +08:00

123 lines
3.7 KiB
Python

import pytest
from tcga_downloader.config import (
Config,
DownloadConfig,
QueryConfig,
_config_to_dict,
load_config,
save_default_config,
)
@pytest.mark.skipif("yaml" not in dir(__builtins__), reason="yaml module not available")
def test_save_default_yaml_config(tmp_path):
config = QueryConfig(
project="TCGA-BRCA", data_type="Gene Expression", sample_type="Primary Tumor"
)
assert config.project == "TCGA-BRCA"
assert config.data_type == "Gene Expression"
assert config.sample_type == "Primary Tumor"
assert config.max_files == 1000
def test_download_config():
config = DownloadConfig(out_dir="./data", processes=8, retries=5)
assert config.out_dir == "./data"
assert config.processes == 8
assert config.retries == 5
def test_config_dataclass():
query = QueryConfig(project="TCGA-BRCA", data_type="Gene Expression")
download = DownloadConfig(out_dir="./data")
config = Config(query=query, download=download, verbose=True)
assert config.query.project == "TCGA-BRCA"
assert config.download.out_dir == "./data"
assert config.verbose is True
def test_load_json_config(tmp_path):
config_data = {
"query": {
"project": "TCGA-BRCA",
"data_type": "Gene Expression",
"sample_type": "Primary Tumor",
"platform": "Illumina HiSeq",
},
"download": {"out_dir": "./data", "processes": 8, "retries": 5},
"verbose": True,
"log_file": "test.log",
}
config_file = tmp_path / "config.json"
import json
config_file.write_text(json.dumps(config_data))
config = load_config(config_file)
assert config.query.project == "TCGA-BRCA"
assert config.query.data_type == "Gene Expression"
assert config.query.sample_type == "Primary Tumor"
assert config.query.platform == "Illumina HiSeq"
assert config.download.out_dir == "./data"
assert config.download.processes == 8
assert config.download.retries == 5
assert config.verbose is True
assert config.log_file == "test.log"
def test_save_default_json_config(tmp_path):
config_file = tmp_path / "config.json"
save_default_config(config_file)
assert config_file.exists()
import json
data = json.loads(config_file.read_text())
assert "query" in data
assert "download" in data
assert data["query"]["project"] == "TCGA-BRCA"
assert data["query"]["data_type"] == "Gene Expression"
assert data["download"]["out_dir"] == "./data"
def test_save_default_yaml_config(tmp_path):
try:
import yaml
config_file = tmp_path / "config.yaml"
save_default_config(config_file)
assert config_file.exists()
except ImportError:
pytest.skip("PyYAML not installed")
def test_config_to_dict():
query = QueryConfig(project="TCGA-BRCA", data_type="Gene Expression", max_files=500)
download = DownloadConfig(out_dir="./data", processes=2)
config = Config(query=query, download=download, verbose=False, log_file="app.log")
result = _config_to_dict(config)
assert result["query"]["project"] == "TCGA-BRCA"
assert result["query"]["data_type"] == "Gene Expression"
assert result["query"]["max_files"] == 500
assert result["download"]["out_dir"] == "./data"
assert result["download"]["processes"] == 2
assert result["verbose"] is False
assert result["log_file"] == "app.log"
def test_load_nonexistent_config(tmp_path):
with pytest.raises(FileNotFoundError):
load_config(tmp_path / "nonexistent.json")
def test_load_unsupported_format(tmp_path):
config_file = tmp_path / "config.txt"
config_file.write_text("invalid")
with pytest.raises(ValueError, match="Unsupported config format"):
load_config(config_file)