tests for logs utils

This commit is contained in:
hjoaquim
2023-07-26 13:05:53 +01:00
parent 5f905b2105
commit 87e99b5505
4 changed files with 122 additions and 4 deletions

View File

@@ -24,8 +24,7 @@ def get_app_id(contextual_user_data_directory: str) -> str:
print(
"Note for macOS users: copy `OpenBB Terminal` folder outside the DMG."
)
else:
raise e
raise e
except Exception as e:
raise e
@@ -35,19 +34,37 @@ def get_app_id(contextual_user_data_directory: str) -> str:
def get_log_dir(contextual_user_data_directory: str) -> Path:
"""Retrieve application's log directory."""
log_dir = Path(contextual_user_data_directory).joinpath("logs").absolute()
log_dir = create_log_dir_if_not_exists(contextual_user_data_directory)
logging_uuid = create_log_uuid_if_not_exists(log_dir)
uuid_log_dir = create_uuid_dir_if_not_exists(log_dir, logging_uuid)
return uuid_log_dir
def create_log_dir_if_not_exists(contextual_user_data_directory: str) -> None:
log_dir = Path(contextual_user_data_directory).joinpath("logs").absolute()
if not log_dir.is_dir():
log_dir.mkdir(parents=True, exist_ok=True)
log_id = (log_dir / ".logid").absolute()
return log_dir
def create_log_uuid_if_not_exists(log_dir: Path) -> str:
log_id = get_log_id(log_dir)
if not log_id.is_file():
logging_id = f"{uuid.uuid4()}"
log_id.write_text(logging_id, encoding="utf-8")
else:
logging_id = log_id.read_text(encoding="utf-8").rstrip()
return logging_id
def get_log_id(log_dir):
return (log_dir / ".logid").absolute()
def create_uuid_dir_if_not_exists(log_dir, logging_id) -> str:
uuid_log_dir = (log_dir / logging_id).absolute()
if not uuid_log_dir.is_dir():

View File

@@ -0,0 +1,6 @@
from openbb_core.app.logs.utils.expired_files import get_timestamp_from_x_days
def test_get_timestamp_from_x_days():
result = get_timestamp_from_x_days(0)
assert isinstance(result, float)

View File

@@ -0,0 +1 @@
# TODO : add tests once the code is fixed

View File

@@ -0,0 +1,94 @@
import pytest
import uuid
from pathlib import Path
from unittest.mock import patch
from openbb_core.app.logs.utils.utils import get_session_id, get_app_id, get_log_dir
## get_session_id
def test_get_session_id_return_type():
# Test if the returned value is a string
session_id = get_session_id()
assert isinstance(session_id, str)
def test_get_session_id_format():
# Test if the returned string has the format "UUID-current_time"
session_id = get_session_id()
parts = session_id.split("-")
assert len(parts) == 6
uuid_part = "-".join(parts[0:5])
time_part = str(parts[5])
# Check if the first part (UUID) is a valid UUID
assert uuid.UUID(uuid_part)
# Check if the second part (current_time) is numeric
assert int(time_part)
def test_get_session_id_uniqueness():
# Test if subsequent calls return different session IDs
session_id1 = get_session_id()
session_id2 = get_session_id()
assert session_id1 != session_id2
## get_app_id
def test_get_app_id_success():
# Mock the return value of get_log_dir to simulate a successful scenario
with patch("openbb_core.app.logs.utils.utils.get_log_dir") as mock_get_log_dir:
mock_get_log_dir
mock_get_log_dir.return_value = Path(
"/path/to/contextual_user_data_directory/app_id.log"
)
app_id = get_app_id("/path/to/contextual_user_data_directory")
assert app_id == "app_id"
def test_get_app_id_os_error():
# Test handling of OSError with errno 30 (Read-only file system)
with patch("openbb_core.app.logs.utils.utils.get_log_dir") as mock_get_log_dir:
mock_get_log_dir.side_effect = OSError(30, "Read-only file system")
with pytest.raises(OSError):
get_app_id("/path/to/contextual_user_data_directory")
def test_get_app_id_other_exception():
# Test handling of other exceptions
with patch("openbb_core.app.logs.utils.utils.get_log_dir") as mock_get_log_dir:
mock_get_log_dir.side_effect = Exception("Some other error")
with pytest.raises(Exception, match="Some other error"):
get_app_id("/path/to/contextual_user_data_directory")
## get_log_dir
def test_get_log_dir():
with patch(
"openbb_core.app.logs.utils.utils.create_log_dir_if_not_exists",
return_value="/test_dir",
) as mock_create_log_dir, patch(
"openbb_core.app.logs.utils.utils.create_log_uuid_if_not_exists",
return_value="12345",
) as mock_create_log_uuid, patch(
"openbb_core.app.logs.utils.utils.create_uuid_dir_if_not_exists",
return_value="/test_dir/12345",
) as mock_create_uuid_dir:
# Call the get_log_dir function
result = get_log_dir("contextual_user_data_directory")
# Assertions
assert result == "/test_dir/12345"
mock_create_log_dir.assert_called_once_with("contextual_user_data_directory")
mock_create_log_uuid.assert_called_once_with("/test_dir")
mock_create_uuid_dir.assert_called_once_with("/test_dir", "12345")