Skip to content

test: replace toml usage with tomli and tomli-w #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
13 changes: 12 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
21 changes: 14 additions & 7 deletions test/e2e/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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")
5 changes: 3 additions & 2 deletions test/e2e/utils/props_utils.py
Original file line number Diff line number Diff line change
@@ -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())
11 changes: 6 additions & 5 deletions test/integration/test_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os

import tomli_w

from pinnwand.configuration import Configuration
import tempfile
import toml
import tomli
import pytest


Expand Down Expand Up @@ -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

10 changes: 6 additions & 4 deletions test/integration/test_reaping_job.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
Loading