-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Appsilon/add-logging-testing
feat: this commit adds logging testing and adds settings
- Loading branch information
Showing
8 changed files
with
219 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
import sys | ||
|
||
from loguru import logger | ||
from shiny import App | ||
|
||
from pyshiny_template.config import AppConfig | ||
from pyshiny_template.shiny_modules.root import get_ui, server | ||
|
||
app_config = AppConfig() | ||
logger.remove() | ||
logger.add(sys.stderr, level=app_config.log_level) | ||
|
||
|
||
app_ui = get_ui() | ||
|
||
app = App(app_ui, server) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from pydantic_settings import BaseSettings | ||
|
||
|
||
class AppConfig(BaseSettings): | ||
log_level: str = "INFO" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
def square(x): | ||
return x**2 | ||
from loguru import logger | ||
|
||
|
||
@logger.catch(reraise=True) | ||
def divide(x, y): | ||
return x / y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import pytest | ||
from loguru import logger | ||
|
||
|
||
class LogSink: | ||
"""Custom log sink to capture log messages from loguru.""" | ||
|
||
def __init__(self): | ||
self.messages = [] | ||
|
||
def __call__(self, message): | ||
self.messages.append(message) | ||
|
||
|
||
@pytest.fixture() | ||
def loguru_sink(): | ||
sink = LogSink() | ||
logger.remove() | ||
logger.add(sink) | ||
yield sink | ||
logger.remove() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
from pyshiny_template.utils.utils import square | ||
import pytest | ||
|
||
from pyshiny_template.utils.utils import divide | ||
from tests.utils.test_helpers import log_contain_message | ||
|
||
def test_square(): | ||
|
||
def test_divide(): | ||
# Given | ||
x = 2 | ||
y = 2 | ||
expected = 1 | ||
# When | ||
result = divide(x, y) | ||
# Then | ||
assert result == expected | ||
|
||
|
||
def test_divide_by_zero(loguru_sink): | ||
# Given | ||
x = 2 | ||
y = 0 | ||
# When | ||
result = square(x) | ||
with pytest.raises(ZeroDivisionError): | ||
divide(x, y) | ||
# Then | ||
assert result == 4 | ||
assert log_contain_message(loguru_sink, "ZeroDivisionError: division by zero") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def log_contain_message(log_sink, message): | ||
return any(message in record for record in log_sink.messages) |