Skip to content

Commit 3ec692a

Browse files
committed
fix unit tests
1 parent e35b083 commit 3ec692a

1 file changed

Lines changed: 108 additions & 48 deletions

File tree

test/unit/test_index_unit.py

Lines changed: 108 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,58 @@
11
"""Unit tests for the index command."""
22

3+
from pathlib import Path
34
from unittest import mock
45

56
from wily.commands.index import index
67

78

8-
def get_mock_State_and_config(revs):
9-
"""Build a mock State and a mock config for command tests."""
10-
revisions = []
11-
for rev in range(revs):
12-
rev_dict = {
13-
"revision.key": f"abcdef{rev}",
14-
"revision.author_name": f"Author {rev}",
15-
"revision.message": f"Message {rev}",
16-
"revision.date": rev,
17-
}
18-
mock_revision = mock.Mock(**rev_dict)
19-
revisions.append(mock_revision)
20-
mock_revisions = mock.Mock(revisions=revisions)
21-
mock_state = mock.Mock(index={"git": mock_revisions}, archivers=("git",))
22-
mock.seal(mock_state)
23-
mock_State = mock.Mock(return_value=mock_state)
24-
mock_config = mock.Mock(path="", archiver="", operator="")
25-
return mock_State, mock_config
9+
def create_mock_rows(num_revisions):
10+
"""Create mock rows for WilyIndex."""
11+
rows = []
12+
for i in range(num_revisions):
13+
rows.append({
14+
"revision": f"abcdef{i}",
15+
"revision_author": f"Author {i}",
16+
"revision_message": f"Message {i}",
17+
"revision_date": i * 1000, # Unix timestamps
18+
"path": "test.py",
19+
"path_type": "file",
20+
})
21+
return rows
22+
23+
24+
class MockWilyIndex:
25+
"""Mock WilyIndex context manager."""
26+
27+
def __init__(self, rows):
28+
self.rows = rows
29+
30+
def __enter__(self):
31+
return self
32+
33+
def __exit__(self, *args):
34+
pass
35+
36+
def __iter__(self):
37+
return iter(self.rows)
2638

2739

2840
def test_index_without_message(capsys):
2941
"""Test index command outputs expected data without message."""
30-
mock_State, mock_config = get_mock_State_and_config(3)
42+
mock_rows = create_mock_rows(3)
43+
mock_idx = MockWilyIndex(mock_rows)
44+
45+
mock_config = mock.Mock()
46+
mock_config.path = "/test/path"
47+
mock_config.archiver = "git"
48+
mock_config.operators = "raw"
49+
mock_config.cache_path = "/test/.wily"
3150

32-
with mock.patch("wily.commands.index.State", mock_State):
33-
index(mock_config, include_message=False)
51+
with mock.patch("wily.commands.index.list_archivers", return_value=["git"]):
52+
with mock.patch("wily.commands.index.get_default_metrics_path", return_value="/test/.wily/git/metrics.parquet"):
53+
with mock.patch.object(Path, "exists", return_value=True):
54+
with mock.patch("wily.commands.index.WilyIndex", return_value=mock_idx):
55+
index(mock_config, include_message=False)
3456

3557
captured = capsys.readouterr()
3658

@@ -47,15 +69,23 @@ def test_index_without_message(capsys):
4769
assert "abcdef2" in captured.out
4870
assert "Author 2" in captured.out
4971

50-
mock_State.assert_called_once_with(config=mock_config)
51-
5272

5373
def test_index_without_message_wrapped(capsys):
5474
"""Test index command with wrapping enabled."""
55-
mock_State, mock_config = get_mock_State_and_config(3)
75+
mock_rows = create_mock_rows(3)
76+
mock_idx = MockWilyIndex(mock_rows)
77+
78+
mock_config = mock.Mock()
79+
mock_config.path = "/test/path"
80+
mock_config.archiver = "git"
81+
mock_config.operators = "raw"
82+
mock_config.cache_path = "/test/.wily"
5683

57-
with mock.patch("wily.commands.index.State", mock_State):
58-
index(mock_config, include_message=False, wrap=True)
84+
with mock.patch("wily.commands.index.list_archivers", return_value=["git"]):
85+
with mock.patch("wily.commands.index.get_default_metrics_path", return_value="/test/.wily/git/metrics.parquet"):
86+
with mock.patch.object(Path, "exists", return_value=True):
87+
with mock.patch("wily.commands.index.WilyIndex", return_value=mock_idx):
88+
index(mock_config, include_message=False, wrap=True)
5989

6090
captured = capsys.readouterr()
6191

@@ -69,15 +99,23 @@ def test_index_without_message_wrapped(capsys):
6999
assert "abcdef1" in captured.out
70100
assert "abcdef2" in captured.out
71101

72-
mock_State.assert_called_once_with(config=mock_config)
73-
74102

75103
def test_index_with_message(capsys):
76104
"""Test index command with message column included."""
77-
mock_State, mock_config = get_mock_State_and_config(3)
105+
mock_rows = create_mock_rows(3)
106+
mock_idx = MockWilyIndex(mock_rows)
107+
108+
mock_config = mock.Mock()
109+
mock_config.path = "/test/path"
110+
mock_config.archiver = "git"
111+
mock_config.operators = "raw"
112+
mock_config.cache_path = "/test/.wily"
78113

79-
with mock.patch("wily.commands.index.State", mock_State):
80-
index(mock_config, include_message=True)
114+
with mock.patch("wily.commands.index.list_archivers", return_value=["git"]):
115+
with mock.patch("wily.commands.index.get_default_metrics_path", return_value="/test/.wily/git/metrics.parquet"):
116+
with mock.patch.object(Path, "exists", return_value=True):
117+
with mock.patch("wily.commands.index.WilyIndex", return_value=mock_idx):
118+
index(mock_config, include_message=True)
81119

82120
captured = capsys.readouterr()
83121

@@ -92,15 +130,23 @@ def test_index_with_message(capsys):
92130
assert "Message 1" in captured.out
93131
assert "Message 2" in captured.out
94132

95-
mock_State.assert_called_once_with(config=mock_config)
96-
97133

98134
def test_index_with_message_wrapped(capsys):
99135
"""Test index command with message column and wrapping."""
100-
mock_State, mock_config = get_mock_State_and_config(3)
136+
mock_rows = create_mock_rows(3)
137+
mock_idx = MockWilyIndex(mock_rows)
101138

102-
with mock.patch("wily.commands.index.State", mock_State):
103-
index(mock_config, include_message=True, wrap=True)
139+
mock_config = mock.Mock()
140+
mock_config.path = "/test/path"
141+
mock_config.archiver = "git"
142+
mock_config.operators = "raw"
143+
mock_config.cache_path = "/test/.wily"
144+
145+
with mock.patch("wily.commands.index.list_archivers", return_value=["git"]):
146+
with mock.patch("wily.commands.index.get_default_metrics_path", return_value="/test/.wily/git/metrics.parquet"):
147+
with mock.patch.object(Path, "exists", return_value=True):
148+
with mock.patch("wily.commands.index.WilyIndex", return_value=mock_idx):
149+
index(mock_config, include_message=True, wrap=True)
104150

105151
captured = capsys.readouterr()
106152

@@ -112,15 +158,23 @@ def test_index_with_message_wrapped(capsys):
112158
# Verify data is present
113159
assert "abcdef0" in captured.out
114160

115-
mock_State.assert_called_once_with(config=mock_config)
116-
117161

118162
def test_empty_index_without_message(capsys):
119163
"""Test index command with empty data."""
120-
mock_State, mock_config = get_mock_State_and_config(0)
164+
mock_rows = []
165+
mock_idx = MockWilyIndex(mock_rows)
121166

122-
with mock.patch("wily.commands.index.State", mock_State):
123-
index(mock_config, include_message=False)
167+
mock_config = mock.Mock()
168+
mock_config.path = "/test/path"
169+
mock_config.archiver = "git"
170+
mock_config.operators = "raw"
171+
mock_config.cache_path = "/test/.wily"
172+
173+
with mock.patch("wily.commands.index.list_archivers", return_value=["git"]):
174+
with mock.patch("wily.commands.index.get_default_metrics_path", return_value="/test/.wily/git/metrics.parquet"):
175+
with mock.patch.object(Path, "exists", return_value=True):
176+
with mock.patch("wily.commands.index.WilyIndex", return_value=mock_idx):
177+
index(mock_config, include_message=False)
124178

125179
captured = capsys.readouterr()
126180

@@ -129,15 +183,23 @@ def test_empty_index_without_message(capsys):
129183
assert "Author" in captured.out
130184
assert "Date" in captured.out
131185

132-
mock_State.assert_called_once_with(config=mock_config)
133-
134186

135187
def test_empty_index_with_message(capsys):
136188
"""Test index command with empty data and message column."""
137-
mock_State, mock_config = get_mock_State_and_config(0)
189+
mock_rows = []
190+
mock_idx = MockWilyIndex(mock_rows)
138191

139-
with mock.patch("wily.commands.index.State", mock_State):
140-
index(mock_config, include_message=True)
192+
mock_config = mock.Mock()
193+
mock_config.path = "/test/path"
194+
mock_config.archiver = "git"
195+
mock_config.operators = "raw"
196+
mock_config.cache_path = "/test/.wily"
197+
198+
with mock.patch("wily.commands.index.list_archivers", return_value=["git"]):
199+
with mock.patch("wily.commands.index.get_default_metrics_path", return_value="/test/.wily/git/metrics.parquet"):
200+
with mock.patch.object(Path, "exists", return_value=True):
201+
with mock.patch("wily.commands.index.WilyIndex", return_value=mock_idx):
202+
index(mock_config, include_message=True)
141203

142204
captured = capsys.readouterr()
143205

@@ -146,5 +208,3 @@ def test_empty_index_with_message(capsys):
146208
assert "Author" in captured.out
147209
assert "Message" in captured.out
148210
assert "Date" in captured.out
149-
150-
mock_State.assert_called_once_with(config=mock_config)

0 commit comments

Comments
 (0)