-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,36 @@ | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import cast | ||
|
||
from dataclass_io.reader import DataclassReader | ||
|
||
import pytest | ||
|
||
@dataclass(kw_only=True, eq=True) | ||
class FakeDataclass: | ||
foo: str | ||
bar: int | ||
from dataclass_io.reader import DataclassReader | ||
|
||
|
||
def test_reader(tmp_path: Path) -> None: | ||
@pytest.mark.parametrize("kw_only", [True, False]) | ||
@pytest.mark.parametrize("eq", [True, False]) | ||
@pytest.mark.parametrize("frozen", [True, False]) | ||
def test_reader(kw_only: bool, eq: bool, frozen: bool, tmp_path: Path) -> None: | ||
fpath = tmp_path / "test.txt" | ||
|
||
@dataclass(frozen=frozen, eq=eq, kw_only=kw_only) # type: ignore[literal-required] | ||
class FakeDataclass: | ||
foo: str | ||
bar: int | ||
|
||
with fpath.open("w") as f: | ||
f.write("foo\tbar\n") | ||
f.write("abc\t1\n") | ||
|
||
rows: list[FakeDataclass] | ||
with DataclassReader.open(filename=fpath, dataclass_type=FakeDataclass) as reader: | ||
rows = [row for row in reader] | ||
|
||
assert rows[0] == FakeDataclass(foo="abc", bar=1) | ||
|
||
|
||
def test_reader_from_str(tmp_path: Path) -> None: | ||
"""Test that we can create a reader when `filename` is a `str`.""" | ||
fpath = tmp_path / "test.txt" | ||
|
||
with fpath.open("w") as f: | ||
f.write("foo\tbar\n") | ||
f.write("abc\t1\n") | ||
# TODO make `DataclassReader` generic | ||
rows = cast(list[FakeDataclass], [row for row in reader]) | ||
|
||
with DataclassReader.open(filename=str(fpath), dataclass_type=FakeDataclass) as reader: | ||
rows = [row for row in reader] | ||
assert len(rows) == 1 | ||
|
||
assert rows[0] == FakeDataclass(foo="abc", bar=1) | ||
if eq: | ||
assert rows[0] == FakeDataclass(foo="abc", bar=1) | ||
else: | ||
assert rows[0].foo == "abc" | ||
assert rows[0].bar == 1 |