Skip to content

Commit

Permalink
Merge pull request #4 from Appsilon/add-logging-testing
Browse files Browse the repository at this point in the history
feat: this commit adds logging testing and adds settings
  • Loading branch information
pstorozenko authored Mar 14, 2024
2 parents 4a2ebd1 + 4928154 commit f09e58a
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 7 deletions.
9 changes: 9 additions & 0 deletions app.py
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)
156 changes: 155 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rich = "^13.7.1"
loguru = "^0.7.2"
pytest-watch = "^4.2.0"
pytest-cov = "^4.1.0"
pydantic-settings = "^2.2.1"

[tool.poetry.group.dev.dependencies]
icecream = "^2.1.3"
Expand Down
5 changes: 5 additions & 0 deletions pyshiny_template/config.py
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"
8 changes: 6 additions & 2 deletions pyshiny_template/utils/utils.py
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
21 changes: 21 additions & 0 deletions tests/conftest.py
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()
24 changes: 20 additions & 4 deletions tests/pytest/test_utils.py
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")
2 changes: 2 additions & 0 deletions tests/utils/test_helpers.py
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)

0 comments on commit f09e58a

Please sign in to comment.