Skip to content

Commit 48f8d71

Browse files
committed
Add LoggerFactory test
1 parent ecaa90c commit 48f8d71

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2024 Microsoft Corporation.
2+
# Licensed under the MIT License
3+
"""LoggerFactory Tests.
4+
5+
These tests will test the LoggerFactory class and the creation of each reporting type that is natively supported.
6+
"""
7+
8+
import logging
9+
10+
import pytest
11+
12+
from graphrag.config.enums import ReportingType
13+
from graphrag.logger.blob_workflow_logger import BlobWorkflowLogger
14+
from graphrag.logger.factory import LoggerFactory
15+
16+
# cspell:disable-next-line well-known-key
17+
WELL_KNOWN_BLOB_STORAGE_KEY = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
18+
# cspell:disable-next-line well-known-key
19+
WELL_KNOWN_COSMOS_CONNECTION_STRING = "AccountEndpoint=https://127.0.0.1:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
20+
21+
22+
@pytest.mark.skip(reason="Blob storage emulator is not available in this environment")
23+
def test_create_blob_logger():
24+
kwargs = {
25+
"type": "blob",
26+
"connection_string": WELL_KNOWN_BLOB_STORAGE_KEY,
27+
"base_dir": "testbasedir",
28+
"container_name": "testcontainer",
29+
}
30+
logger = LoggerFactory.create_logger(ReportingType.blob, kwargs)
31+
assert isinstance(logger, BlobWorkflowLogger)
32+
33+
34+
def test_register_and_create_custom_logger():
35+
"""Test registering and creating a custom logger type."""
36+
from unittest.mock import MagicMock
37+
38+
custom_logger_class = MagicMock(spec=logging.Handler)
39+
instance = MagicMock()
40+
instance.initialized = True
41+
custom_logger_class.return_value = instance
42+
43+
LoggerFactory.register("custom", lambda **kwargs: custom_logger_class(**kwargs))
44+
logger = LoggerFactory.create_logger("custom", {})
45+
46+
assert custom_logger_class.called
47+
assert logger is instance
48+
# Access the attribute we set on our mock
49+
assert logger.initialized is True # type: ignore # Attribute only exists on our mock
50+
51+
# Check if it's in the list of registered logger types
52+
assert "custom" in LoggerFactory.get_logger_types()
53+
assert LoggerFactory.is_supported_type("custom")
54+
55+
56+
def test_get_logger_types():
57+
logger_types = LoggerFactory.get_logger_types()
58+
# Check that built-in types are registered
59+
assert ReportingType.file.value in logger_types
60+
assert ReportingType.blob.value in logger_types
61+
62+
63+
def test_create_unknown_logger():
64+
with pytest.raises(ValueError, match="Unknown logger type: unknown"):
65+
LoggerFactory.create_logger("unknown", {})
File renamed without changes.

0 commit comments

Comments
 (0)