Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Bugfixes
* The time range sent when loading an asset's KPIs was off by the viewer's UTC offset, so KPIs could cover the wrong days [see `PR #2435 <https://www.github.com/FlexMeasures/flexmeasures/pull/2435>`_]
* An asset's status page showed only some of the sensors it reported on, in an order that changed between reloads, and it reported on fixed quantities from the flex-context, which have no data to be up to date with [see `PR #2489 <https://www.github.com/FlexMeasures/flexmeasures/pull/2489>`_]
* Saving an asset chart as PNG or SVG drew the legend over the graph whenever it listed more sensors than fit beside a subplot; the exported image now makes room for every entry beside its own plot, without shrinking the plot, and spells out the sensor names that the on-screen legend abbreviates [see `PR #2517 <https://www.github.com/FlexMeasures/flexmeasures/pull/2517>`_]
* Building a custom application in a test or with ``do_configure_logging=False`` no longer reconfigures root logger handlers and strips pytest's ``caplog`` handler [see `PR #2484 <https://www.github.com/FlexMeasures/flexmeasures/pull/2484>`_]



Expand Down
6 changes: 5 additions & 1 deletion flexmeasures/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def create( # noqa C901
env: str | None = None,
path_to_config: str | None = None,
plugins: list[str] | None = None,
do_configure_logging: bool = True,
) -> Flask:
"""
Create a Flask app and configure it.
Expand All @@ -37,6 +38,8 @@ def create( # noqa C901
A path to a config file can be passed in (otherwise a config file will be searched in the home or instance directories).

Also, a list of plugins can be set. Usually this works as a config setting, but this is useful for automated testing.

Pass ``do_configure_logging=False`` to prevent reconfiguring root logger handlers (e.g. during custom test setup).
"""

from flexmeasures.utils import config_defaults
Expand All @@ -53,7 +56,8 @@ def create( # noqa C901
from flexmeasures.utils.error_utils import add_basic_error_handlers
from flexmeasures.utils.secrets_utils import set_secret_key, set_totp_secrets

configure_logging() # do this first, see https://flask.palletsprojects.com/en/2.0.x/logging
if do_configure_logging and not os.getenv("PYTEST_CURRENT_TEST"):
configure_logging() # do this first, see https://flask.palletsprojects.com/en/2.0.x/logging
cfg_location = find_flexmeasures_cfg() # Find flexmeasures.cfg location
# Create app
app = Flask("flexmeasures")
Expand Down
33 changes: 33 additions & 0 deletions flexmeasures/utils/tests/test_config_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import pytest
from flask import Flask
from werkzeug.sansio.utils import host_is_trusted
Expand Down Expand Up @@ -171,3 +172,35 @@ def test_config_warnings_silent_when_trusted_hosts_is_set():
missing_settings, config_warnings = get_config_warnings(app)
assert "TRUSTED_HOSTS" not in missing_settings
assert not any("TRUSTED_HOSTS" in warning for warning in config_warnings)


def test_create_app_in_test_does_not_break_caplog(monkeypatch, caplog):
"""Building a custom app inside a test does not overwrite root logger handlers or break caplog."""
import flexmeasures.ui
from flexmeasures.app import create as create_app

monkeypatch.setattr(flexmeasures.ui, "register_at", lambda app: None)
custom_app = create_app(env="testing")
assert custom_app.testing is True

with caplog.at_level(logging.WARNING):
logging.getLogger().warning("test warning after create_app")

assert any(
record.message == "test warning after create_app" for record in caplog.records
)


def test_create_app_do_configure_logging_flag(monkeypatch):
"""create_app respects do_configure_logging."""
import flexmeasures.ui
from flexmeasures.app import create as create_app

monkeypatch.setattr(flexmeasures.ui, "register_at", lambda app: None)
called = []
monkeypatch.setattr(
"flexmeasures.utils.config_utils.configure_logging",
lambda: called.append(True),
)
create_app(env="testing", do_configure_logging=False)
assert not called
Loading