1
0
mirror of synced 2024-12-12 15:01:09 +01:00

Make dump_and_load_then_compare default to using a temporary directory

Dump path format variables all need to be strings
This commit is contained in:
Stepland 2021-06-01 19:38:31 +02:00
parent a932893edc
commit e0809b7a8d
10 changed files with 21 additions and 31 deletions

View File

@ -75,9 +75,9 @@ class FormatParameters(TypedDict, total=False):
# uppercase BSC ADV EXT # uppercase BSC ADV EXT
difficulty: str difficulty: str
# 0-based # 0-based
difficulty_index: int difficulty_index: str
# 1-based # 1-based
difficulty_number: int difficulty_number: str
dedup: str dedup: str
@ -85,8 +85,8 @@ def extract_format_params(chartfile: ChartFile, dedup_index: int) -> FormatParam
return FormatParameters( return FormatParameters(
title=chartfile.song.metadata.title or "", title=chartfile.song.metadata.title or "",
difficulty=chartfile.difficulty, difficulty=chartfile.difficulty,
difficulty_index=DIFFICULTY_INDEX.get(chartfile.difficulty, 3), difficulty_index=str(DIFFICULTY_INDEX.get(chartfile.difficulty, 2)),
difficulty_number=DIFFICULTY_NUMBER.get(chartfile.difficulty, 4), difficulty_number=str(DIFFICULTY_NUMBER.get(chartfile.difficulty, 3)),
dedup="" if dedup_index == 0 else f"-{dedup_index}", dedup="" if dedup_index == 0 else f"-{dedup_index}",
) )

View File

@ -13,7 +13,7 @@ from jubeatools.formats.jubeat_analyser.memo.load import MemoParser
from jubeatools.testutils import strategies as jbst from jubeatools.testutils import strategies as jbst
from jubeatools.testutils.test_patterns import dump_and_load_then_compare from jubeatools.testutils.test_patterns import dump_and_load_then_compare
from ..test_utils import memo_compatible_song, temp_file_named_txt from ..test_utils import memo_compatible_song
from . import example1, example2, example3 from . import example1, example2, example3
@ -48,7 +48,6 @@ def test_that_full_chart_roundtrips(song: song.Song, circle_free: bool) -> None:
dump_and_load_then_compare( dump_and_load_then_compare(
Format.MEMO, Format.MEMO,
song, song,
temp_path=temp_file_named_txt(),
bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"), bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"),
dump_options={"circle_free": circle_free}, dump_options={"circle_free": circle_free},
) )

View File

@ -12,7 +12,7 @@ from jubeatools.formats.jubeat_analyser.memo1.load import Memo1Parser
from jubeatools.testutils.strategies import notes as notes_strat from jubeatools.testutils.strategies import notes as notes_strat
from jubeatools.testutils.test_patterns import dump_and_load_then_compare from jubeatools.testutils.test_patterns import dump_and_load_then_compare
from ..test_utils import memo_compatible_song, temp_file_named_txt from ..test_utils import memo_compatible_song
from . import example1 from . import example1
@ -44,7 +44,6 @@ def test_that_full_chart_roundtrips(song: song.Song, circle_free: bool) -> None:
dump_and_load_then_compare( dump_and_load_then_compare(
Format.MEMO_1, Format.MEMO_1,
song, song,
temp_path=temp_file_named_txt(),
bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"), bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"),
dump_options={"circle_free": circle_free}, dump_options={"circle_free": circle_free},
) )

View File

@ -22,7 +22,7 @@ from jubeatools.song import (
from jubeatools.testutils.strategies import notes as notes_strat from jubeatools.testutils.strategies import notes as notes_strat
from jubeatools.testutils.test_patterns import dump_and_load_then_compare from jubeatools.testutils.test_patterns import dump_and_load_then_compare
from ..test_utils import memo_compatible_song, temp_file_named_txt from ..test_utils import memo_compatible_song
from . import example1, example2, example3 from . import example1, example2, example3
@ -55,7 +55,6 @@ def test_that_full_chart_roundtrips(song: Song, circle_free: bool) -> None:
dump_and_load_then_compare( dump_and_load_then_compare(
Format.MEMO_2, Format.MEMO_2,
song, song,
temp_path=temp_file_named_txt(),
bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"), bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"),
dump_options={"circle_free": circle_free}, dump_options={"circle_free": circle_free},
) )

View File

@ -24,7 +24,7 @@ from jubeatools.testutils.strategies import notes as notes_strat
from jubeatools.testutils.strategies import tap_note from jubeatools.testutils.strategies import tap_note
from jubeatools.testutils.test_patterns import dump_and_load_then_compare from jubeatools.testutils.test_patterns import dump_and_load_then_compare
from ..test_utils import memo_compatible_song, temp_file_named_txt from ..test_utils import memo_compatible_song
@given(st.sets(tap_note(), min_size=1, max_size=100)) @given(st.sets(tap_note(), min_size=1, max_size=100))
@ -88,7 +88,6 @@ def test_that_full_chart_roundtrips(song: Song, circle_free: bool) -> None:
dump_and_load_then_compare( dump_and_load_then_compare(
Format.MONO_COLUMN, Format.MONO_COLUMN,
song, song,
temp_path=temp_file_named_txt(),
bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"), bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"),
dump_options={"circle_free": circle_free}, dump_options={"circle_free": circle_free},
) )

View File

@ -2,7 +2,7 @@ from hypothesis import given
from jubeatools import song from jubeatools import song
from jubeatools.formats import Format from jubeatools.formats import Format
from jubeatools.formats.konami.testutils import eve_compatible_song, open_temp_dir from jubeatools.formats.konami.testutils import eve_compatible_song
from jubeatools.testutils.test_patterns import dump_and_load_then_compare from jubeatools.testutils.test_patterns import dump_and_load_then_compare
@ -11,7 +11,6 @@ def test_that_full_chart_roundtrips(song: song.Song) -> None:
dump_and_load_then_compare( dump_and_load_then_compare(
Format.EVE, Format.EVE,
song, song,
temp_path=open_temp_dir(),
bytes_decoder=lambda b: b.decode("ascii"), bytes_decoder=lambda b: b.decode("ascii"),
load_options={"beat_snap": 12}, load_options={"beat_snap": 12},
) )

View File

@ -2,7 +2,7 @@ from hypothesis import given
from jubeatools import song from jubeatools import song
from jubeatools.formats import Format from jubeatools.formats import Format
from jubeatools.formats.konami.testutils import eve_compatible_song, open_temp_dir from jubeatools.formats.konami.testutils import eve_compatible_song
from jubeatools.testutils.test_patterns import dump_and_load_then_compare from jubeatools.testutils.test_patterns import dump_and_load_then_compare
from .construct import jbsq from .construct import jbsq
@ -13,7 +13,6 @@ def test_that_full_chart_roundtrips(song: song.Song) -> None:
dump_and_load_then_compare( dump_and_load_then_compare(
Format.JBSQ, Format.JBSQ,
song, song,
temp_path=open_temp_dir(),
bytes_decoder=lambda b: str(jbsq.parse(b)), bytes_decoder=lambda b: str(jbsq.parse(b)),
load_options={"beat_snap": 12}, load_options={"beat_snap": 12},
) )

View File

@ -1,8 +1,4 @@
import tempfile
from contextlib import contextmanager
from decimal import Decimal from decimal import Decimal
from pathlib import Path
from typing import Iterator
from hypothesis import strategies as st from hypothesis import strategies as st
@ -53,9 +49,3 @@ def eve_compatible_song(draw: DrawFunc) -> song.Song:
metadata=song.Metadata(), metadata=song.Metadata(),
charts={diff: chart}, charts={diff: chart},
) )
@contextmanager
def open_temp_dir() -> Iterator[Path]:
with tempfile.TemporaryDirectory() as temp_dir:
yield Path(temp_dir)

View File

@ -8,7 +8,6 @@ from hypothesis import strategies as st
from jubeatools import song from jubeatools import song
from jubeatools.formats import Format from jubeatools.formats import Format
from jubeatools.formats.konami.testutils import open_temp_dir
from jubeatools.formats.malody import schema as malody from jubeatools.formats.malody import schema as malody
from jubeatools.formats.malody.dump import dump_malody_chart from jubeatools.formats.malody.dump import dump_malody_chart
from jubeatools.testutils import strategies as jbst from jubeatools.testutils import strategies as jbst
@ -50,7 +49,6 @@ def test_that_full_chart_roundtrips(s: song.Song) -> None:
dump_and_load_then_compare( dump_and_load_then_compare(
Format.MALODY, Format.MALODY,
s, s,
temp_path=open_temp_dir(),
bytes_decoder=lambda b: b.decode("utf-8"), bytes_decoder=lambda b: b.decode("utf-8"),
) )

View File

@ -1,5 +1,7 @@
import tempfile
from contextlib import contextmanager
from pathlib import Path from pathlib import Path
from typing import Callable, ContextManager, Optional from typing import Callable, ContextManager, Iterator, Optional
from hypothesis import note from hypothesis import note
@ -9,11 +11,17 @@ from jubeatools.formats.enum import Format
from jubeatools.formats.guess import guess_format from jubeatools.formats.guess import guess_format
@contextmanager
def open_temp_dir() -> Iterator[Path]:
with tempfile.TemporaryDirectory() as temp_dir:
yield Path(temp_dir)
def dump_and_load_then_compare( def dump_and_load_then_compare(
format_: Format, format_: Format,
song: song.Song, song: song.Song,
temp_path: ContextManager[Path],
bytes_decoder: Callable[[bytes], str], bytes_decoder: Callable[[bytes], str],
temp_path: Callable[[], ContextManager[Path]] = open_temp_dir,
load_options: Optional[dict] = None, load_options: Optional[dict] = None,
dump_options: Optional[dict] = None, dump_options: Optional[dict] = None,
) -> None: ) -> None:
@ -21,7 +29,7 @@ def dump_and_load_then_compare(
dump_options = dump_options or {} dump_options = dump_options or {}
loader = LOADERS[format_] loader = LOADERS[format_]
dumper = DUMPERS[format_] dumper = DUMPERS[format_]
with temp_path as folder_path: with temp_path() as folder_path:
files = dumper(song, folder_path, **dump_options) files = dumper(song, folder_path, **dump_options)
for file_path, bytes_ in files.items(): for file_path, bytes_ in files.items():
file_path.write_bytes(bytes_) file_path.write_bytes(bytes_)