Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
laurejt committed Nov 15, 2024
1 parent 96b56bb commit be47d87
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions ppa/archive/tests/test_hathi_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ def test_log_action(self):
def test_log_download(self, mock_log_action):
stats = hathi_images.DownloadStats()
stats.log_download("image_type")
mock_log_action.called_once_with("image_type", "fetch")
mock_log_action.assert_called_once_with("image_type", "fetch")

@patch.object(hathi_images.DownloadStats, "_log_action")
def test_log_skip(self, mock_log_action):
stats = hathi_images.DownloadStats()
stats.log_download("image_type")
mock_log_action.called_once_with("image_type", "skip")
stats.log_skip("image_type")
mock_log_action.assert_called_once_with("image_type", "skip")

def test_update(self):
stats_a = hathi_images.DownloadStats()
Expand Down Expand Up @@ -120,21 +120,21 @@ def test_interrupt_handler(self, mock_signal):
"Ctrl-C / Interrupt to quit immediately\n"
)

@patch("requests.get")
def test_download_image(self, mock_get, tmp_path):
def test_download_image(self, tmp_path):
cmd = hathi_images.Command()
cmd.session = Mock()

# Not ok status
mock_get.return_value = Mock(status_code=503)
cmd.session.get.return_value = Mock(status_code=503)
result = cmd.download_image("page_url", "out_file")
assert mock_get.called_once_with("page_url")
cmd.session.get.assert_called_once_with("page_url")
assert result is False

# Ok status
out_file = tmp_path / "test.jpg"
mock_get.reset_mock()
mock_get.return_value = Mock(status_code=200, content=b"image content")
cmd.session.reset_mock()
cmd.session.get.return_value = Mock(status_code=200, content=b"image content")
result = cmd.download_image("page_url", out_file)
assert mock_get.called_once_with("page_url")
cmd.session.get.assert_called_once_with("page_url")
assert result is True
assert out_file.read_text() == "image content"

0 comments on commit be47d87

Please sign in to comment.