|
| 1 | +import pytest |
| 2 | +import yaml |
| 3 | +from v2.config import load_user_config |
| 4 | + |
| 5 | +EXAMPLE_CONFIG = { |
| 6 | + "run_id": "test-run", |
| 7 | + "quoridor": {"board_size": 5, "max_walls": 3, "max_steps": 50}, |
| 8 | + "alphazero": {"network": {"type": "mlp"}, "mcts_n": 300, "mcts_c_puct": 1.2}, |
| 9 | + "wandb": {"project": "example", "upload_model": {"every": "20 models", "when_max": ["raw_win_perc", "elo_score"]}}, |
| 10 | + "self_play": {"num_workers": 2, "parallel_games": 8, "alphazero": {"mcts_noise_epsilon": 0.25}}, |
| 11 | + "training": { |
| 12 | + "games_per_training_step": 25.0, |
| 13 | + "learning_rate": 0.001, |
| 14 | + "batch_size": 256, |
| 15 | + "weight_decay": 0.0001, |
| 16 | + "replay_buffer_size": 1000000, |
| 17 | + }, |
| 18 | + "benchmarks": [ |
| 19 | + { |
| 20 | + "every": "10 models", |
| 21 | + "jobs": [ |
| 22 | + {"type": "tournament", "prefix": "raw", "times": 10, "opponents": ["random", "greedy"]}, |
| 23 | + {"type": "dumb_score", "prefix": "raw"}, |
| 24 | + ], |
| 25 | + }, |
| 26 | + ], |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def config_file(tmp_path): |
| 32 | + path = tmp_path / "config.yaml" |
| 33 | + path.write_text(yaml.safe_dump(EXAMPLE_CONFIG, sort_keys=False)) |
| 34 | + return str(path) |
| 35 | + |
| 36 | + |
| 37 | +def test_no_overrides(config_file): |
| 38 | + config = load_user_config(config_file) |
| 39 | + assert config.wandb is not None |
| 40 | + assert config.wandb.project == "example" |
| 41 | + assert config.training.learning_rate == 0.001 |
| 42 | + |
| 43 | + |
| 44 | +def test_override_none(config_file): |
| 45 | + config = load_user_config(config_file, overrides=["wandb=None"]) |
| 46 | + assert config.wandb is None |
| 47 | + |
| 48 | + |
| 49 | +def test_override_boolean_true(config_file): |
| 50 | + config = load_user_config(config_file, overrides=["training.model_save_timing=True"]) |
| 51 | + assert config.training.model_save_timing is True |
| 52 | + |
| 53 | + |
| 54 | +def test_override_boolean_false(config_file): |
| 55 | + config = load_user_config(config_file, overrides=["training.save_pytorch=false"]) |
| 56 | + assert config.training.save_pytorch is False |
| 57 | + |
| 58 | + |
| 59 | +def test_override_int(config_file): |
| 60 | + config = load_user_config(config_file, overrides=["alphazero.mcts_n=500"]) |
| 61 | + assert config.alphazero.mcts_n == 500 |
| 62 | + |
| 63 | + |
| 64 | +def test_override_float(config_file): |
| 65 | + config = load_user_config(config_file, overrides=["training.learning_rate=0.01"]) |
| 66 | + assert config.training.learning_rate == 0.01 |
| 67 | + |
| 68 | + |
| 69 | +def test_override_string(config_file): |
| 70 | + config = load_user_config(config_file, overrides=["run_id=my-custom-run"]) |
| 71 | + assert config.run_id == "my-custom-run" |
| 72 | + |
| 73 | + |
| 74 | +def test_override_list(config_file): |
| 75 | + config = load_user_config(config_file, overrides=["wandb.upload_model.when_max=[dumb_score,tournament]"]) |
| 76 | + assert config.wandb.upload_model.when_max == ["dumb_score", "tournament"] |
| 77 | + |
| 78 | + |
| 79 | +def test_override_empty_list(config_file): |
| 80 | + config = load_user_config(config_file, overrides=["wandb.upload_model.when_max=[]"]) |
| 81 | + assert config.wandb.upload_model.when_max == [] |
| 82 | + |
| 83 | + |
| 84 | +def test_override_list_index(config_file): |
| 85 | + config = load_user_config(config_file, overrides=["benchmarks.0.every=5 models"]) |
| 86 | + assert config.benchmarks[0].every == "5 models" |
| 87 | + |
| 88 | + |
| 89 | +def test_override_nested_list_index(config_file): |
| 90 | + config = load_user_config(config_file, overrides=["benchmarks.0.jobs.0.times=20"]) |
| 91 | + assert config.benchmarks[0].jobs[0].times == 20 |
| 92 | + |
| 93 | + |
| 94 | +def test_multiple_overrides(config_file): |
| 95 | + config = load_user_config( |
| 96 | + config_file, overrides=["alphazero.mcts_n=100", "training.learning_rate=0.05", "wandb=None"] |
| 97 | + ) |
| 98 | + assert config.alphazero.mcts_n == 100 |
| 99 | + assert config.training.learning_rate == 0.05 |
| 100 | + assert config.wandb is None |
| 101 | + |
| 102 | + |
| 103 | +def test_invalid_override_format(config_file): |
| 104 | + with pytest.raises(ValueError, match="Invalid override format"): |
| 105 | + load_user_config(config_file, overrides=["no_equals_sign"]) |
| 106 | + |
| 107 | + |
| 108 | +def test_invalid_key_rejected_by_pydantic(config_file): |
| 109 | + with pytest.raises(Exception): |
| 110 | + load_user_config(config_file, overrides=["nonexistent_key=value"]) |
0 commit comments