Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests for local.py #70

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions tests/test_local.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import hashlib
from unittest.mock import patch
import pytest
from ingest_wikimedia.local import (
setup_temp_dir,
Expand All @@ -16,7 +17,6 @@
def setup_and_teardown_temp_dir():
setup_temp_dir()
yield
print("cleanup")
cleanup_temp_dir()


Expand All @@ -31,13 +31,17 @@
def test_get_file_hash(tmp_path):
test_file = tmp_path / "test_file.txt"
test_file.write_text("test content")
expected_hash = hashlib.sha1(test_file.read_bytes()).hexdigest()
expected_hash = hashlib.sha1(

Check warning on line 34 in tests/test_local.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_local.py#L34

Detected SHA1 hash algorithm which is considered insecure.
test_file.read_bytes(), usedforsecurity=False
).hexdigest()
assert get_file_hash(str(test_file)) == expected_hash


def test_get_bytes_hash():
data = "test content"
expected_hash = hashlib.sha1(data.encode("utf-8")).hexdigest()
expected_hash = hashlib.sha1(
data.encode("utf-8"), usedforsecurity=False
).hexdigest()
assert get_bytes_hash(data) == expected_hash


Expand All @@ -52,3 +56,37 @@
test_file = tmp_path / "test_file.txt"
test_file.write_bytes(SPACER_GIF)
assert get_content_type(str(test_file)) == "image/gif"


def test_clean_up_tmp_file_none_does_not_raise():
clean_up_tmp_file(None) # Should not raise any errors


def test_get_file_hash_file_not_found():
with pytest.raises(FileNotFoundError):
get_file_hash("non_existent_file.txt")


def test_get_file_hash_empty_file(tmp_path):
test_file = tmp_path / "empty_file.txt"
test_file.write_text("")
expected_hash = hashlib.sha1(b"", usedforsecurity=False).hexdigest()

Check warning on line 73 in tests/test_local.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_local.py#L73

Detected SHA1 hash algorithm which is considered insecure.
assert get_file_hash(str(test_file)) == expected_hash


@patch("ingest_wikimedia.local.magic.from_file")
def test_get_content_type_magic_called(mock_magic_from_file, tmp_path):
mock_magic_from_file.return_value = "application/octet-stream"
test_file = tmp_path / "test.bin"
test_file.write_bytes(b"some data")
content_type = get_content_type(str(test_file))
assert content_type == "application/octet-stream"


def test_get_content_type_file_not_found():
with pytest.raises(FileNotFoundError):
get_content_type("non_existent_file.txt")


def test_get_bytes_hash_empty_string():
assert get_bytes_hash("") == "da39a3ee5e6b4b0d3255bfef95601890afd80709"
Loading