From 8592be283587f62e568108791fafb776995a9e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fukan=20=C3=87a=C4=9Fatay?= Date: Thu, 27 Mar 2025 21:42:15 +0100 Subject: [PATCH] test: replace toml usage with tomli and tomli-w Replaces toml library calls with `tomli` and `tomli-w` calls and removes the `toml` library dependency from dev dependencies. --- pdm.lock | 13 ++++++++++++- pyproject.toml | 2 +- requirements.txt | 3 +++ test/e2e/conftest.py | 21 ++++++++++++++------- test/e2e/utils/props_utils.py | 5 +++-- test/integration/test_config.py | 11 ++++++----- test/integration/test_reaping_job.py | 10 ++++++---- 7 files changed, 45 insertions(+), 20 deletions(-) diff --git a/pdm.lock b/pdm.lock index 3362584..7ae634d 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default", "dev"] strategy = ["inherit_metadata"] lock_version = "4.5.0" -content_hash = "sha256:abe845d9efa910f04f1df9efb28f05c84be8d041fe155c1ebc2837ffbcad05c2" +content_hash = "sha256:c28bd7f3a9481fc2c9ed7c41191ca82a15548bc4ebef51e6eac31d0242c86a2d" [[metadata.targets]] requires_python = ">=3.8" @@ -1594,6 +1594,17 @@ files = [ {file = "tomli-2.2.1.tar.gz", hash = "sha256:cd45e1dc79c835ce60f7404ec8119f2eb06d38b1deba146f07ced3bbc44505ff"}, ] +[[package]] +name = "tomli-w" +version = "1.0.0" +requires_python = ">=3.7" +summary = "A lil' TOML writer" +groups = ["dev"] +files = [ + {file = "tomli_w-1.0.0-py3-none-any.whl", hash = "sha256:9f2a07e8be30a0729e533ec968016807069991ae2fd921a78d42f429ae5f4463"}, + {file = "tomli_w-1.0.0.tar.gz", hash = "sha256:f463434305e0336248cac9c2dc8076b707d8a12d019dd349f5c1e382dd1ae1b9"}, +] + [[package]] name = "tornado" version = "6.4.2" diff --git a/pyproject.toml b/pyproject.toml index 05103cf..d42c300 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,7 +44,7 @@ dev = [ "urllib3>=2.0.7", "pytest-html>=4.1.1", "pytest-asyncio>=0.23.3", - "toml>=0.10.2", + "tomli-w>=1.0.0", ] [build-system] requires = ["pdm-pep517>=1.0"] diff --git a/requirements.txt b/requirements.txt index 125afe0..4c5c47d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -745,6 +745,9 @@ tomli==2.2.1 \ --hash=sha256:e85e99945e688e32d5a35c1ff38ed0b3f41f43fad8df0bdf79f72b2ba7bc5272 \ --hash=sha256:ece47d672db52ac607a3d9599a9d48dcb2f2f735c6c2d1f34130085bb12b112a \ --hash=sha256:f4039b9cbc3048b2416cc57ab3bda989a6fcf9b36cf8937f01a6e731b64f80d7 +tomli-w==1.0.0 \ + --hash=sha256:9f2a07e8be30a0729e533ec968016807069991ae2fd921a78d42f429ae5f4463 \ + --hash=sha256:f463434305e0336248cac9c2dc8076b707d8a12d019dd349f5c1e382dd1ae1b9 tornado==6.4.2 \ --hash=sha256:072ce12ada169c5b00b7d92a99ba089447ccc993ea2143c9ede887e0937aa803 \ --hash=sha256:1a017d239bd1bb0919f72af256a970624241f070496635784d9bf0db640d3fec \ diff --git a/test/e2e/conftest.py b/test/e2e/conftest.py index ca97226..dbd84ed 100644 --- a/test/e2e/conftest.py +++ b/test/e2e/conftest.py @@ -1,13 +1,15 @@ import logging import os +from pathlib import Path import subprocess import sys import tempfile import time import pytest -import toml +import tomli from threading import Thread -from toml import load + +import tomli_w from test.e2e.utils.database_utils import TestDb from test.e2e.env_config import PORT from test.e2e.pageobjects.create_paste_page import CreatePastePage @@ -69,11 +71,14 @@ def create_paste_page(page: Page) -> CreatePastePage: def database() -> Generator[None, None, None]: log.info("Setting up temp database") with tempfile.NamedTemporaryFile(suffix="", delete=False) as temp: - props = load(config_path) + props = tomli.loads(Path(config_path).read_text()) props["database_uri"] = f"sqlite:///{temp.name}" - props["expiries"] = {"1hour": 4, "1day": 4} # It expires in 4 seconds instead of an hour - with open(config_path, "w") as config_file: - toml.dump(props, config_file) + props["expiries"] = { + "1hour": 4, + "1day": 4, + } # It expires in 4 seconds instead of an hour + with open(config_path, "wb") as config_file: + tomli_w.dump(props, config_file) yield log.info("Tearing down temp database") close_all_sessions() @@ -85,4 +90,6 @@ def database() -> Generator[None, None, None]: @pytest.fixture def clear_db(): log.info("Clearing temp database") - TestDb(load(config_path)["database_uri"]).clear_tables("paste", "file") + TestDb( + tomli.loads(Path(config_path).read_text())["database_uri"] + ).clear_tables("paste", "file") diff --git a/test/e2e/utils/props_utils.py b/test/e2e/utils/props_utils.py index dde2c6a..bf5b8e9 100644 --- a/test/e2e/utils/props_utils.py +++ b/test/e2e/utils/props_utils.py @@ -1,8 +1,9 @@ import os +from pathlib import Path -from toml import load +from tomli import loads def load_props(): config_path = os.path.join("test", "e2e", "pinnwand.toml") - return load(config_path) + return loads(Path(config_path).read_text()) diff --git a/test/integration/test_config.py b/test/integration/test_config.py index ea9154f..793a90f 100644 --- a/test/integration/test_config.py +++ b/test/integration/test_config.py @@ -1,8 +1,10 @@ import os +import tomli_w + from pinnwand.configuration import Configuration import tempfile -import toml +import tomli import pytest @@ -39,14 +41,13 @@ def test_env_variables_precedence(monkeypatch, temp_dotenv_file): def test_load_file_config(): with tempfile.NamedTemporaryFile(suffix="", delete=False) as temp: - props = toml.load(temp.name) + props = tomli.load(temp) url = "sqlite:///database.db" props["database_uri"] = url - with open(temp.name, "w") as config_file: - toml.dump(props, config_file) + with open(temp.name, "wb") as config_file: + tomli_w.dump(props, config_file) config: Configuration = Configuration() config.load_config_file(temp.name) assert config.database_uri == url - diff --git a/test/integration/test_reaping_job.py b/test/integration/test_reaping_job.py index 4dad54b..7b05bca 100644 --- a/test/integration/test_reaping_job.py +++ b/test/integration/test_reaping_job.py @@ -1,13 +1,15 @@ import asyncio import os +from pathlib import Path import tempfile -import toml +import tomli from unittest.mock import patch, MagicMock from sqlalchemy import create_engine import pytest from click.testing import CliRunner +import tomli_w from pinnwand import command, utility from pinnwand.database import utils, manager, models @@ -19,11 +21,11 @@ def temp_database_url() -> str: """Override the config's database_url with a temporary one.""" with tempfile.NamedTemporaryFile(suffix="", delete=False) as temp: - props = toml.load(config_path) + props = tomli.loads(Path(config_path).read_text()) url = f"sqlite:///{temp.name}" props["database_uri"] = url - with open(config_path, "w") as config_file: - toml.dump(props, config_file) + with open(config_path, "wb") as config_file: + tomli_w.dump(props, config_file) yield url