1import json 2from pathlib import Path 3 4import pytest 5from mock import MagicMock, patch 6from structured_logger import ( 7 AutoSaveDict, 8 CSVStrategy, 9 JSONStrategy, 10 StructuredLogger, 11 YAMLStrategy, 12) 13 14 15@pytest.fixture(params=[CSVStrategy, JSONStrategy, YAMLStrategy]) 16def strategy(request): 17 return request.param 18 19 20@pytest.fixture 21def file_extension(strategy): 22 if strategy == CSVStrategy: 23 return "csv" 24 elif strategy == JSONStrategy: 25 return "json" 26 elif strategy == YAMLStrategy: 27 return "yaml" 28 29 30@pytest.fixture 31def tmp_file(tmp_path): 32 return tmp_path / "test.json" 33 34 35def test_guess_strategy_from_file(tmp_path, strategy, file_extension): 36 file_name = tmp_path / f"test_guess.{file_extension}" 37 Path(file_name).touch() 38 guessed_strategy = StructuredLogger.guess_strategy_from_file(file_name) 39 assert isinstance(guessed_strategy, strategy) 40 41 42def test_get_strategy(strategy, file_extension): 43 result = StructuredLogger.get_strategy(file_extension) 44 assert isinstance(result, strategy) 45 46 47def test_invalid_file_extension(tmp_path): 48 file_name = tmp_path / "test_invalid.xyz" 49 Path(file_name).touch() 50 51 with pytest.raises(ValueError, match="Unknown strategy for: xyz"): 52 StructuredLogger.guess_strategy_from_file(file_name) 53 54 55def test_non_existent_file(tmp_path, strategy, file_extension): 56 file_name = tmp_path / f"non_existent.{file_extension}" 57 logger = StructuredLogger(file_name, strategy()) 58 59 assert logger.file_path.exists() 60 assert "_timestamp" in logger._data 61 62 63@pytest.fixture 64def structured_logger_module(): 65 with patch.dict("sys.modules", {"polars": None, "ruamel.yaml": None}): 66 import importlib 67 68 import structured_logger 69 70 importlib.reload(structured_logger) 71 yield structured_logger 72 73 74def test_missing_csv_library(tmp_path, structured_logger_module): 75 with pytest.raises(RuntimeError, match="Can't parse CSV files. Missing library"): 76 structured_logger_module.CSVStrategy() 77 78 79def test_missing_yaml_library(tmp_path, structured_logger_module): 80 with pytest.raises(RuntimeError, match="Can't parse YAML files. Missing library"): 81 structured_logger_module.YAMLStrategy() 82 83 84def test_autosavedict_setitem(): 85 save_callback = MagicMock() 86 d = AutoSaveDict(save_callback=save_callback) 87 d["key"] = "value" 88 assert d["key"] == "value" 89 save_callback.assert_called_once() 90 91 92def test_autosavedict_delitem(): 93 save_callback = MagicMock() 94 d = AutoSaveDict({"key": "value"}, save_callback=save_callback) 95 del d["key"] 96 assert "key" not in d 97 save_callback.assert_called_once() 98 99 100def test_autosavedict_pop(): 101 save_callback = MagicMock() 102 d = AutoSaveDict({"key": "value"}, save_callback=save_callback) 103 result = d.pop("key") 104 assert result == "value" 105 assert "key" not in d 106 save_callback.assert_called_once() 107 108 109def test_autosavedict_update(): 110 save_callback = MagicMock() 111 d = AutoSaveDict({"key": "old_value"}, save_callback=save_callback) 112 d.update({"key": "new_value"}) 113 assert d["key"] == "new_value" 114 save_callback.assert_called_once() 115 116 117def test_structured_logger_setitem(tmp_file): 118 logger = StructuredLogger(tmp_file, JSONStrategy()) 119 logger.data["field"] = "value" 120 121 with open(tmp_file, "r") as f: 122 data = json.load(f) 123 124 assert data["field"] == "value" 125 126 127def test_structured_logger_set_recursive(tmp_file): 128 logger = StructuredLogger(tmp_file, JSONStrategy()) 129 logger.data["field"] = {"test": True} 130 other = logger.data["field"] 131 other["late"] = True 132 133 with open(tmp_file, "r") as f: 134 data = json.load(f) 135 136 assert data["field"]["test"] 137 assert data["field"]["late"] 138 139 140def test_structured_logger_set_list(tmp_file): 141 logger = StructuredLogger(tmp_file, JSONStrategy()) 142 logger.data["field"] = [True] 143 other = logger.data["field"] 144 other.append(True) 145 146 with open(tmp_file, "r") as f: 147 data = json.load(f) 148 149 assert data["field"][0] 150 assert data["field"][1] 151 152 153def test_structured_logger_delitem(tmp_file): 154 logger = StructuredLogger(tmp_file, JSONStrategy()) 155 logger.data["field"] = "value" 156 del logger.data["field"] 157 158 with open(tmp_file, "r") as f: 159 data = json.load(f) 160 161 assert "field" not in data 162 163 164def test_structured_logger_pop(tmp_file): 165 logger = StructuredLogger(tmp_file, JSONStrategy()) 166 logger.data["field"] = "value" 167 logger.data.pop("field") 168 169 with open(tmp_file, "r") as f: 170 data = json.load(f) 171 172 assert "field" not in data 173 174 175def test_structured_logger_update(tmp_file): 176 logger = StructuredLogger(tmp_file, JSONStrategy()) 177 logger.data.update({"field": "value"}) 178 179 with open(tmp_file, "r") as f: 180 data = json.load(f) 181 182 assert data["field"] == "value" 183