Skip to content

Commit

Permalink
refactor: use pathlib in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
msto committed Oct 18, 2023
1 parent 27402c2 commit ed6df15
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
5 changes: 2 additions & 3 deletions fgpyo/fasta/tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from tempfile import NamedTemporaryFile as NamedTemp

import pytest
from py._path.local import LocalPath as TmpDir
from pytest import raises

from fgpyo.fasta.builder import FastaBuilder
Expand Down Expand Up @@ -64,14 +63,14 @@ def test_bases_string_from_ContigBuilder_add(
bases: str,
times: int,
expected: str,
tmpdir: TmpDir,
tmp_path: Path,
) -> None:
"""
Reads bases back from fasta and checks that extra spaces are removed and bases are uppercase
"""
builder = FastaBuilder()
builder.add(name).add(bases, times)
with NamedTemp(suffix=".fa", dir=tmpdir, mode="w", delete=True) as fp:
with NamedTemp(suffix=".fa", dir=tmp_path, mode="w", delete=True) as fp:
builder.to_file(Path(fp.name))
with open(fp.name, "r") as read_fp:
for line in read_fp.readlines():
Expand Down
11 changes: 6 additions & 5 deletions fgpyo/sam/tests/test_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import pysam
import pytest
from py._path.local import LocalPath as TmpDir
from pysam import AlignmentHeader

import fgpyo.sam as sam
Expand Down Expand Up @@ -141,10 +140,10 @@ def test_sam_file_open_writing(
file_type: SamFileType,
expected_records: List[pysam.AlignedSegment],
header_dict: AlignmentHeader,
tmpdir: TmpDir,
tmp_path: Path,
) -> None:
# use header as a keyword argument
with NamedTemp(suffix=file_type.extension, dir=tmpdir, mode="w", delete=False) as fp:
with NamedTemp(suffix=file_type.extension, dir=tmp_path, mode="w", delete=False) as fp:
kwargs = {"header": header_dict}
with sam._pysam_open(
path=fp.file, open_for_reading=False, file_type=file_type, **kwargs # type: ignore
Expand All @@ -155,11 +154,13 @@ def test_sam_file_open_writing(


def test_sam_file_open_writing_header_keyword(
expected_records: List[pysam.AlignedSegment], header_dict: AlignmentHeader, tmpdir: TmpDir
expected_records: List[pysam.AlignedSegment],
header_dict: AlignmentHeader,
tmp_path: Path,
) -> None:
# Use SamWriter
# use header as a keyword argument
with NamedTemp(suffix=".sam", dir=tmpdir, mode="w", delete=False) as fp:
with NamedTemp(suffix=".sam", dir=tmp_path, mode="w", delete=False) as fp:
with sam.writer(path=fp.name, header=header_dict, file_type=SamFileType.SAM) as sam_writer:
for r in expected_records:
sam_writer.write(r)
Expand Down
24 changes: 13 additions & 11 deletions fgpyo/util/tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import attr
import pytest
from py._path.local import LocalPath as TmpDir

from fgpyo.util.metric import Metric

Expand Down Expand Up @@ -139,8 +138,11 @@ class PersonDefault(Metric["PersonDefault"]):


@pytest.mark.parametrize("metric", DUMMY_METRICS)
def test_metric_roundtrip(tmpdir: TmpDir, metric: DummyMetric) -> None:
path: Path = Path(tmpdir) / "metrics.txt"
def test_metric_roundtrip(
tmp_path: Path,
metric: DummyMetric,
) -> None:
path: Path = tmp_path / "metrics.txt"

DummyMetric.write(path, metric)
metrics: List[DummyMetric] = list(DummyMetric.read(path=path))
Expand All @@ -149,8 +151,8 @@ def test_metric_roundtrip(tmpdir: TmpDir, metric: DummyMetric) -> None:
assert metrics[0] == metric


def test_metrics_roundtrip(tmpdir: TmpDir) -> None:
path: Path = Path(tmpdir) / "metrics.txt"
def test_metrics_roundtrip(tmp_path: Path) -> None:
path: Path = tmp_path / "metrics.txt"

DummyMetric.write(path, *DUMMY_METRICS)
metrics: List[DummyMetric] = list(DummyMetric.read(path=path))
Expand All @@ -173,9 +175,9 @@ def test_metrics_roundtrip_gzip(tmp_path: Path) -> None:
assert metrics == DUMMY_METRICS


def test_metrics_read_extra_columns(tmpdir: TmpDir) -> None:
def test_metrics_read_extra_columns(tmp_path: Path) -> None:
person = Person(name="Max", age=42)
path = Path(tmpdir) / "metrics.txt"
path = tmp_path / "metrics.txt"
with path.open("w") as writer:
header = Person.header()
header.append("foo")
Expand All @@ -188,9 +190,9 @@ def test_metrics_read_extra_columns(tmpdir: TmpDir) -> None:
list(Person.read(path=path, ignore_extra_fields=False))


def test_metrics_read_missing_optional_columns(tmpdir: TmpDir) -> None:
def test_metrics_read_missing_optional_columns(tmp_path: Path) -> None:
person = PersonMaybeAge(name="Max", age=None)
path = Path(tmpdir) / "metrics.txt"
path = tmp_path / "metrics.txt"

# The "age" column is optional, and not in the file, but that's ok
with path.open("w") as writer:
Expand All @@ -204,9 +206,9 @@ def test_metrics_read_missing_optional_columns(tmpdir: TmpDir) -> None:
list(PersonMaybeAge.read(path=path))


def test_metric_read_missing_column_with_default(tmpdir: TmpDir) -> None:
def test_metric_read_missing_column_with_default(tmp_path: Path) -> None:
person = PersonDefault(name="Max")
path = Path(tmpdir) / "metrics.txt"
path = tmp_path / "metrics.txt"

# The "age" column hs a default, and not in the file, but that's ok
with path.open("w") as writer:
Expand Down

0 comments on commit ed6df15

Please sign in to comment.