-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_image_loopback.py
More file actions
143 lines (103 loc) · 5.32 KB
/
Copy pathtest_image_loopback.py
File metadata and controls
143 lines (103 loc) · 5.32 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
#!/usr/bin/env python
"""Tests for _parse_history_indices function in image_loopback.nodes module."""
import pytest
from image_loopback.nodes import _parse_history_indices
class TestParseHistoryIndicesSingleIndex:
"""Tests for single index parsing."""
def test_single_index(self):
"""Single index '1' should return [1]."""
assert _parse_history_indices("1") == [1]
def test_single_index_with_whitespace(self):
"""Single index with whitespace should be trimmed."""
assert _parse_history_indices(" 5 ") == [5]
def test_larger_single_index(self):
"""Larger single indices should work."""
assert _parse_history_indices("100") == [100]
class TestParseHistoryIndicesRanges:
"""Tests for range parsing (ascending, descending)."""
def test_ascending_range(self):
"""Range '1-3' should expand to [1, 2, 3]."""
assert _parse_history_indices("1-3") == [1, 2, 3]
def test_reverse_range(self):
"""Reverse range '3-1' should expand to [3, 2, 1]."""
assert _parse_history_indices("3-1") == [3, 2, 1]
def test_single_element_range(self):
"""Range where start equals end '5-5' should return [5]."""
assert _parse_history_indices("5-5") == [5]
def test_larger_range(self):
"""Larger ranges should work correctly."""
assert _parse_history_indices("10-15") == [10, 11, 12, 13, 14, 15]
class TestParseHistoryIndicesMixed:
"""Tests for mixed comma-separated indices and ranges."""
def test_mixed_indices_and_ranges(self):
"""Mixed '1,3-5,7' should expand to [1, 3, 4, 5, 7]."""
assert _parse_history_indices("1,3-5,7") == [1, 3, 4, 5, 7]
def test_multiple_single_indices(self):
"""Multiple comma-separated indices '1,2,5' should return [1, 2, 5]."""
assert _parse_history_indices("1,2,5") == [1, 2, 5]
def test_multiple_ranges(self):
"""Multiple ranges '1-2,5-6' should expand correctly."""
assert _parse_history_indices("1-2,5-6") == [1, 2, 5, 6]
def test_mixed_with_whitespace(self):
"""Mixed with whitespace should be handled gracefully."""
assert _parse_history_indices(" 1 , 3 - 5 , 7 ") == [1, 3, 4, 5, 7]
class TestParseHistoryIndicesEmpty:
"""Tests for empty and None inputs."""
def test_empty_string(self):
"""Empty string should return empty list."""
assert _parse_history_indices("") == []
def test_whitespace_only(self):
"""Whitespace-only string should return empty list."""
assert _parse_history_indices(" ") == []
def test_none_input(self):
"""None input should return empty list."""
assert _parse_history_indices(None) == []
class TestParseHistoryIndicesInvalidZero:
"""Tests for invalid index 0 (current run cannot be sampled)."""
def test_zero_index_raises(self):
"""Index 0 should raise ValueError."""
with pytest.raises(ValueError, match="index 0 is invalid"):
_parse_history_indices("0")
def test_zero_in_range_raises(self):
"""Range containing 0 should raise ValueError."""
with pytest.raises(ValueError, match="index 0 is invalid"):
_parse_history_indices("0-3")
def test_zero_in_mixed_raises(self):
"""Mixed spec containing 0 should raise ValueError."""
with pytest.raises(ValueError, match="index 0 is invalid"):
_parse_history_indices("1,0,3")
class TestParseHistoryIndicesNegative:
"""Tests for negative indices (not allowed).
Note: Negative indices are caught by different validation paths:
- '-1' is parsed as a range with empty start, triggering 'Invalid history range'
- A truly negative number parsed successfully would hit 'must be positive'
Both cases correctly reject negative indices.
"""
def test_negative_index_raises(self):
"""Negative index '-1' should raise ValueError (detected as invalid range)."""
# '-1' contains '-' so it's parsed as a range with empty start
with pytest.raises(ValueError, match="Invalid history range"):
_parse_history_indices("-1")
def test_negative_in_range_raises(self):
"""Range with negative bound '-3--1' should raise ValueError."""
# '-3--1' is parsed as range '' to '3--1', triggering invalid range
with pytest.raises(ValueError, match="Invalid history range"):
_parse_history_indices("-3--1")
class TestParseHistoryIndicesInvalidFormat:
"""Tests for invalid format strings."""
def test_invalid_non_numeric(self):
"""Non-numeric index should raise ValueError."""
with pytest.raises(ValueError, match="Invalid history index"):
_parse_history_indices("abc")
def test_invalid_range_format(self):
"""Invalid range format should raise ValueError."""
with pytest.raises(ValueError, match="Invalid history range"):
_parse_history_indices("1-")
def test_invalid_range_missing_start(self):
"""Range missing start should raise ValueError."""
with pytest.raises(ValueError, match="Invalid history range"):
_parse_history_indices("-3")
def test_invalid_range_non_numeric_bound(self):
"""Range with non-numeric bounds should raise ValueError."""
with pytest.raises(ValueError, match="Invalid history range"):
_parse_history_indices("a-b")