-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cli.py
More file actions
180 lines (125 loc) · 4.91 KB
/
test_cli.py
File metadata and controls
180 lines (125 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
Unit tests for CLI interface
"""
import tempfile
from pathlib import Path
from unittest.mock import Mock, patch
import pytest
import yaml
from samrfi.config.config_loader import ConfigLoader
@pytest.fixture
def temp_config():
"""Create temporary config file"""
config_dict = {
"model": {"checkpoint": "tiny"},
"training": {"num_epochs": 2, "batch_size": 1, "device": "cpu"},
"dataset": {"stretch": "SQRT", "flag_sigma": 5, "patch_size": 64},
}
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
yaml.dump(config_dict, f)
temp_path = f.name
yield temp_path
Path(temp_path).unlink()
@pytest.fixture
def temp_ms_path(tmp_path):
"""Create temporary MS path"""
ms_path = tmp_path / "test.ms"
ms_path.mkdir()
return str(ms_path)
class TestCLICreateConfig:
"""Test create-config command"""
def test_create_config_default_name(self):
"""Test creating config with default name"""
from samrfi.cli import create_config_command
args = Mock()
args.output = None
with tempfile.TemporaryDirectory() as tmpdir:
import os
original_cwd = os.getcwd()
os.chdir(tmpdir)
try:
create_config_command(args)
# Check default file created
assert Path("sam2_config.yaml").exists()
finally:
os.chdir(original_cwd)
def test_create_config_custom_name(self):
"""Test creating config with custom name"""
from samrfi.cli import create_config_command
with tempfile.TemporaryDirectory() as tmpdir:
output_path = Path(tmpdir) / "custom_config.yaml"
args = Mock()
args.output = str(output_path)
create_config_command(args)
assert output_path.exists()
# Verify it's a valid config
config = ConfigLoader.load(str(output_path))
assert config.model_checkpoint == "large"
class TestCLIValidateConfig:
"""Test validate-config command"""
def test_validate_valid_config(self, temp_config):
"""Test validating a valid config file"""
from samrfi.cli import validate_config_command
args = Mock()
args.config = temp_config
result = validate_config_command(args)
assert result == 0 # Success
def test_validate_nonexistent_config(self):
"""Test validating nonexistent config file"""
from samrfi.cli import validate_config_command
args = Mock()
args.config = "/nonexistent/config.yaml"
result = validate_config_command(args)
assert result == 1 # Failure
def test_validate_invalid_config(self):
"""Test validating invalid config file"""
from samrfi.cli import validate_config_command
# Create invalid config
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
yaml.dump({"model": {"checkpoint": "invalid_checkpoint"}}, f)
temp_path = f.name
try:
args = Mock()
args.config = temp_path
result = validate_config_command(args)
assert result == 1 # Failure
finally:
Path(temp_path).unlink()
class TestCLITrainCommand:
"""Test train command validation"""
def test_train_command_requires_dataset(self, temp_config):
"""Test that train command requires dataset path"""
from samrfi.cli import train_command
args = Mock()
args.config = temp_config
args.dataset = None # Missing
args.device = None
args.output_dir = None
with pytest.raises(ValueError, match="--dataset is required"):
train_command(args)
class TestCLIMain:
"""Test main CLI entry point"""
def test_main_no_command_shows_help(self):
"""Test that calling CLI with no command shows help"""
from samrfi.cli import main
with patch("sys.argv", ["samrfi"]):
with patch("sys.exit") as _mock_exit:
try:
main()
except SystemExit:
pass
def test_main_create_config_command(self):
"""Test main with create-config command"""
from samrfi.cli import main
with tempfile.TemporaryDirectory() as tmpdir:
output_path = Path(tmpdir) / "test_config.yaml"
with patch("sys.argv", ["samrfi", "create-config", "--output", str(output_path)]):
result = main()
assert result == 0
assert output_path.exists()
def test_main_validate_config_command(self, temp_config):
"""Test main with validate-config command"""
from samrfi.cli import main
with patch("sys.argv", ["samrfi", "validate-config", "--config", temp_config]):
result = main()
assert result == 0 # Valid config