Skip to content

Commit fbefc80

Browse files
committed
Add tests for ProgressFile
1 parent 95202a3 commit fbefc80

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

tests/test_progress_file.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import io
2+
from datetime import datetime, timezone
3+
from unittest.mock import Mock, call
4+
5+
import time_machine
6+
7+
from fastapi_cloud_cli.utils.progress_file import ProgressFile
8+
9+
10+
def _make_file(
11+
content: bytes = b"hello world", name: str = "test.tar.gz"
12+
) -> io.BytesIO:
13+
f = io.BytesIO(content)
14+
f.name = name
15+
return f
16+
17+
18+
def test_read_returns_data() -> None:
19+
file = _make_file(b"abc")
20+
pf = ProgressFile(file, progress_callback=lambda _: None)
21+
22+
assert pf.read() == b"abc"
23+
24+
25+
def test_read_with_size() -> None:
26+
file = _make_file(b"abcdef")
27+
pf = ProgressFile(file, progress_callback=lambda _: None)
28+
29+
assert pf.read(3) == b"abc"
30+
assert pf.read(3) == b"def"
31+
32+
33+
def test_callback_not_called_within_interval() -> None:
34+
file = _make_file(b"abcdef")
35+
mock_callback = Mock()
36+
pf = ProgressFile(file, progress_callback=mock_callback)
37+
38+
pf.read(3) # Should trigger callback
39+
pf.read(3) # Should NOT trigger
40+
41+
mock_callback.assert_called_once_with(3)
42+
43+
44+
def test_callback_called_after_interval_elapses() -> None:
45+
file = _make_file(b"abcdef")
46+
mock_callback = Mock()
47+
48+
with time_machine.travel(
49+
datetime(2026, 1, 1, tzinfo=timezone.utc), tick=False
50+
) as traveller:
51+
pf = ProgressFile(file, progress_callback=mock_callback)
52+
53+
pf.read(3)
54+
traveller.shift(0.6)
55+
pf.read(3)
56+
57+
mock_callback.assert_has_calls([call(3), call(6)])
58+
59+
60+
def test_callback_tracks_cumulative_bytes() -> None:
61+
file = _make_file(b"a" * 100)
62+
mock_callback = Mock()
63+
64+
with time_machine.travel(
65+
datetime(2026, 1, 1, tzinfo=timezone.utc), tick=False
66+
) as traveller:
67+
pf = ProgressFile(file, progress_callback=mock_callback)
68+
69+
pf.read(10) # Should trigger callback with 10 bytes read
70+
traveller.shift(0.1)
71+
pf.read(10)
72+
traveller.shift(0.5)
73+
pf.read(10) # Should trigger callback with 10 + 10 + 10 = 30 bytes read
74+
traveller.shift(0.6)
75+
pf.read(10) # Should trigger callback with 30 + 10 = 40 bytes read
76+
77+
mock_callback.assert_has_calls([call(10), call(30), call(40)])
78+
79+
80+
def test_name_property() -> None:
81+
file = _make_file(name="test.tar.gz")
82+
pf = ProgressFile(file, progress_callback=lambda _: None)
83+
84+
assert pf.name == "test.tar.gz"
85+
86+
87+
def test_seek_and_tell() -> None:
88+
file = _make_file(b"abcdef")
89+
pf = ProgressFile(file, progress_callback=lambda _: None)
90+
91+
pf.seek(3)
92+
assert pf.tell() == 3
93+
94+
pf.seek(0)
95+
assert pf.tell() == 0
96+
97+
98+
def test_iter_delegates() -> None:
99+
file = _make_file(b"line1\nline2\n")
100+
pf = ProgressFile(file, progress_callback=lambda _: None)
101+
102+
lines = list(pf)
103+
assert lines == [b"line1\n", b"line2\n"]

0 commit comments

Comments
 (0)