HexCasting/doc/test/test_snapshots.py

96 lines
3.1 KiB
Python
Raw Normal View History

import subprocess
import sys
2023-06-21 07:03:25 +02:00
from dataclasses import Field, fields
2023-06-06 15:24:18 +02:00
from pathlib import Path
2023-06-21 07:03:25 +02:00
from typing import Any, Iterator
2023-06-06 15:24:18 +02:00
import pytest
2023-06-20 06:42:49 +02:00
from bs4 import BeautifulSoup as bs
2023-06-06 15:24:18 +02:00
from syrupy.assertion import SnapshotAssertion
from syrupy.extensions.amber import AmberSnapshotExtension
from syrupy.types import SerializedData
2023-06-21 07:03:25 +02:00
from common.properties import Properties
from common.types import LocalizedStr
from hexcasting.hex_state import HexBookState
2023-06-27 16:01:18 +02:00
from hexcasting.scripts.main import Args, main
2023-06-26 04:58:36 +02:00
from patchouli import Book, FormatTree
2023-06-20 06:42:49 +02:00
def prettify(data: SerializedData) -> str:
return bs(data, features="html.parser").prettify()
class NoDiffSnapshotEx(AmberSnapshotExtension):
def diff_snapshots(
self, serialized_data: SerializedData, snapshot_data: SerializedData
) -> SerializedData:
return "no diff"
def diff_lines(
self, serialized_data: SerializedData, snapshot_data: SerializedData
) -> Iterator[str]:
yield from ["no diff"]
2023-06-06 15:24:18 +02:00
2023-06-27 16:01:18 +02:00
_RUN = [sys.executable, "-m" "hexcasting.scripts.main"]
_ARGV = ["properties.toml", "-o"]
longrun = pytest.mark.skipif("not config.getoption('longrun')")
def test_file(tmp_path: Path, snapshot: SnapshotAssertion):
# generate output docs html file and assert it hasn't changed vs. the snapshot
2023-06-06 15:24:18 +02:00
out_path = tmp_path / "out.html"
main(Args().parse_args(_ARGV + [out_path.as_posix()]))
assert out_path.read_text("utf-8") == snapshot.use_extension(NoDiffSnapshotEx)
@longrun
def test_file_pretty(tmp_path: Path, snapshot: SnapshotAssertion):
# generate output docs html file and assert it hasn't changed vs. the snapshot
out_path = tmp_path / "out.html"
main(Args().parse_args(_ARGV + [out_path.as_posix()]))
assert prettify(out_path.read_text("utf-8")) == snapshot
def test_cmd(tmp_path: Path, snapshot: SnapshotAssertion):
# as above, but running the command we actually want to be using
out_path = tmp_path / "out.html"
subprocess.run(
_RUN + _ARGV + [out_path.as_posix()],
stdout=sys.stdout,
stderr=sys.stderr,
)
assert out_path.read_text("utf-8") == snapshot.use_extension(NoDiffSnapshotEx)
2023-06-07 05:01:25 +02:00
def test_stdout(capsys: pytest.CaptureFixture[str], snapshot: SnapshotAssertion):
main(Args().parse_args(["properties.toml"]))
assert capsys.readouterr() == snapshot.use_extension(NoDiffSnapshotEx)
2023-06-21 07:03:25 +02:00
def test_book_text(snapshot: SnapshotAssertion):
def test_field(data_class: Any, field: Field[Any]):
value = getattr(data_class, field.name, None)
if isinstance(value, (LocalizedStr, FormatTree)):
assert value == snapshot
props = Properties.load(Path("properties.toml"))
book = Book.load(HexBookState(props))
2023-06-21 07:03:25 +02:00
for field in fields(book):
test_field(book, field)
for category in book.categories.values():
for field in fields(category):
test_field(category, field)
for entry in category.entries:
for field in fields(entry):
test_field(entry, field)
for page in entry.pages:
for field in fields(page):
test_field(page, field)