aboutsummaryrefslogtreecommitdiff
path: root/tests/test_init.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_init.py')
-rw-r--r--tests/test_init.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/test_init.py b/tests/test_init.py
index 0966b19..373af01 100644
--- a/tests/test_init.py
+++ b/tests/test_init.py
@@ -8,6 +8,7 @@ import subprocess
import sys
from pathlib import Path
+import pytest
from flask import Flask
from sqlalchemy import create_engine
@@ -207,3 +208,60 @@ def test_sqlite_connect_listener_does_not_touch_other_dbapis() -> None:
# Non-SQLite DB-API connections bypass the listener completely.
set_sqlite_pragmas(object(), object())
+
+
+@pytest.mark.parametrize("development", [False, True])
+def test_create_app_selects_default_configuration_from_environment(
+ monkeypatch: pytest.MonkeyPatch, development: bool
+) -> None:
+ import webmentions_ssg as package
+
+ class DefaultTestConfig(TestingConfig):
+ LOG_FILE = str(Path.cwd() / "testing" / "default-config-test.log")
+
+ class DevelopmentTestConfig(DefaultTestConfig):
+ CONFIG_SELECTION = "development"
+
+ class ProductionTestConfig(DefaultTestConfig):
+ CONFIG_SELECTION = "production"
+
+ monkeypatch.setattr(package, "DevelopmentConfig", DevelopmentTestConfig)
+ monkeypatch.setattr(package, "ProductionConfig", ProductionTestConfig)
+ if development:
+ monkeypatch.setenv("WEBMENTIONS_SSG_DEV", "true")
+ else:
+ monkeypatch.delenv("WEBMENTIONS_SSG_DEV", raising=False)
+
+ application = create_app()
+ assert application.config["CONFIG_SELECTION"] == (
+ "development" if development else "production"
+ )
+
+
+def test_sqlite_connect_rejects_journal_mode_that_cannot_be_applied(
+ app: Flask, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ from unittest.mock import Mock
+
+ from webmentions_ssg import extensions
+
+ cursor = Mock()
+ cursor.fetchall.return_value = [(0, "main", "/tmp/test-file.sqlite")]
+ cursor.fetchone.side_effect = [("DELETE",), ("DELETE",)]
+
+ class FakeConnection:
+ def __init__(self) -> None:
+ self.autocommit = False
+
+ def cursor(self) -> Mock:
+ return cursor
+
+ connection = FakeConnection()
+ with app.app_context(), monkeypatch.context() as patcher:
+ patcher.setattr(extensions.sqlite3, "Connection", FakeConnection)
+ with pytest.raises(RuntimeError, match="journal_mode WAL was not applied"):
+ extensions.set_sqlite_pragmas(connection, None)
+
+ assert connection.autocommit is False
+ cursor.close.assert_called_once_with()
+ cursor.execute.assert_any_call("PRAGMA journal_mode = WAL")