Skip to content

Commit da57ea7

Browse files
committed
tests for log_image and log_data_object
1 parent 9eff334 commit da57ea7

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

fancylog/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
# package is not installed
77
pass
88

9-
from fancylog.fancylog import start_logging
9+
from fancylog.fancylog import (
10+
start_logging,
11+
log_image,
12+
log_data_object,
13+
)

tests/tests/test_general.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from importlib.metadata import distributions
88
from unittest.mock import MagicMock, patch
99

10+
import numpy as np
1011
import pytest
12+
import tifffile
1113
from rich.logging import RichHandler
1214

1315
import fancylog
@@ -425,3 +427,101 @@ def test_mock_no_environment(tmp_path):
425427

426428
assert f"{'fancylog':20} {'1.1.1'}"
427429
assert f"{'pytest':20} {'1.1.1'}"
430+
431+
432+
def test_log_image_creates_tiff_and_metadata(tmp_path, caplog):
433+
"""Test that log_image writes a TIFF
434+
and optional metadata file.
435+
"""
436+
437+
img = np.random.randint(0, 255, (5, 5))
438+
439+
with caplog.at_level(logging.INFO):
440+
filepath = fancylog.log_image(
441+
img,
442+
name="test_img",
443+
logging_dir=tmp_path,
444+
metadata={"desc": "test image"},
445+
)
446+
447+
assert filepath.exists()
448+
assert filepath.suffix == ".tiff"
449+
450+
loaded_img = tifffile.imread(filepath)
451+
np.testing.assert_array_equal(img, loaded_img)
452+
453+
# Check metadata file
454+
meta_file = filepath.with_name("test_img_meta.json")
455+
assert meta_file.exists()
456+
with open(meta_file) as f:
457+
meta = json.load(f)
458+
assert meta["desc"] == "test image"
459+
460+
# Check log output
461+
assert any(
462+
"[fancylog] Saved image:" in message for message in caplog.messages
463+
)
464+
465+
466+
def test_log_image_without_metadata(tmp_path, caplog):
467+
"""Test that log_image works without metadata."""
468+
469+
img = np.zeros((3, 3))
470+
with caplog.at_level(logging.INFO):
471+
filepath = fancylog.log_image(img, "no_meta", tmp_path)
472+
473+
assert filepath.exists()
474+
475+
# Ensure no metadata file
476+
meta_file = filepath.with_name("no_meta_meta.json")
477+
assert not meta_file.exists()
478+
479+
assert any(
480+
"[fancylog] Saved image:" in message for message in caplog.messages
481+
)
482+
483+
484+
def test_log_data_object_dict_and_list(tmp_path, caplog):
485+
"""Test logging of dict and list objects."""
486+
487+
data_dict = {"a": 1}
488+
with caplog.at_level(logging.INFO):
489+
filepath_dict = fancylog.log_data_object(data_dict, "mydict", tmp_path)
490+
491+
assert filepath_dict.exists()
492+
493+
with open(filepath_dict) as f:
494+
loaded = json.load(f)
495+
assert loaded == data_dict
496+
497+
data_list = [1, 2, 3]
498+
499+
with caplog.at_level(logging.INFO):
500+
filepath_list = fancylog.log_data_object(data_list, "mylist", tmp_path)
501+
502+
with open(filepath_list) as f:
503+
loaded = json.load(f)
504+
assert loaded == data_list
505+
506+
assert any(
507+
"[fancylog] Saved data object:" in message
508+
for message in caplog.messages
509+
)
510+
511+
512+
def test_log_data_object_numpy_array(tmp_path):
513+
"""Test logging of numpy array."""
514+
515+
arr = np.array([[1, 2], [3, 4]])
516+
filepath = fancylog.log_data_object(arr, "array", tmp_path, ext="npy")
517+
518+
assert filepath.exists()
519+
loaded = np.load(filepath)
520+
np.testing.assert_array_equal(arr, loaded)
521+
522+
523+
def test_log_data_object_invalid_type(tmp_path):
524+
"""Test that unsupported types raise ValueError."""
525+
526+
with pytest.raises(ValueError):
527+
fancylog.log_data_object("not supported", "bad", tmp_path)

0 commit comments

Comments
 (0)