Skip to content

Commit 8c03876

Browse files
kylebarronomerbenamram
authored andcommitted
Add tests
1 parent 813c919 commit 8c03876

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

examples/path_or_file_like/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,21 @@ fn accepts_file_like_write(file_like: PyObject) -> PyResult<()> {
7171
}
7272
}
7373

74+
#[pyfunction]
75+
/// Access the name of a file-like object
76+
fn name_of_file_like(py: Python, file_like: PyObject) -> PyResult<Option<String>> {
77+
// is a file-like
78+
match PyFileLikeObject::with_requirements(file_like, false, true, false, false) {
79+
Ok(f) => Ok(f.py_name(py)),
80+
Err(e) => Err(e),
81+
}
82+
}
83+
7484
#[pymodule]
7585
fn path_or_file_like(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
7686
m.add_wrapped(wrap_pyfunction!(accepts_path_or_file_like_read))?;
7787
m.add_wrapped(wrap_pyfunction!(accepts_file_like_write))?;
88+
m.add_wrapped(wrap_pyfunction!(name_of_file_like))?;
7889

7990
Ok(())
8091
}

examples/path_or_file_like/tests/test_path_or_file_like.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import pytest
21
import io
32
from pathlib import Path
43

5-
from path_or_file_like import accepts_path_or_file_like_read, accepts_file_like_write
4+
import pytest
5+
from path_or_file_like import (
6+
accepts_file_like_write,
7+
accepts_path_or_file_like_read,
8+
name_of_file_like,
9+
)
610

711

812
@pytest.fixture
913
def small_sample() -> str:
1014
p = Path(__file__).parent.parent
11-
return str(p.joinpath('sample.txt'))
15+
return str(p.joinpath("sample.txt"))
1216

1317

1418
def test_it_reads_from_io_object(small_sample):
@@ -17,12 +21,14 @@ def test_it_reads_from_io_object(small_sample):
1721

1822
assert accepts_path_or_file_like_read(io.BytesIO(r)) == "Hello World!"
1923

24+
2025
def test_it_reads_from_textio_object(small_sample):
2126
with open(small_sample, "rt") as o:
2227
r = o.read()
2328

2429
assert accepts_path_or_file_like_read(io.StringIO(r)) == "Hello World!"
2530

31+
2632
def test_it_reads_from_file_backed_object(small_sample):
2733
with open(small_sample, "rb") as o:
2834
assert accepts_path_or_file_like_read(o) == "Hello World!"
@@ -35,11 +41,12 @@ def test_it_fails_on_non_file_object():
3541

3642
def test_it_fails_when_write_returns_none():
3743
class FileLike:
38-
3944
def write(self, _data):
4045
return None
4146

42-
with pytest.raises(OSError, match=r'write\(\) returned None, expected number of bytes written'):
47+
with pytest.raises(
48+
OSError, match=r"write\(\) returned None, expected number of bytes written"
49+
):
4350
accepts_file_like_write(FileLike())
4451

4552

@@ -53,3 +60,13 @@ def test_it_writes_to_textio_object():
5360
f = io.StringIO()
5461
accepts_file_like_write(f)
5562
assert f.getvalue() == "Hello, world!"
63+
64+
65+
def test_file_name_from_path(small_sample: str):
66+
with open(small_sample, "rb") as o:
67+
assert name_of_file_like(o) == small_sample
68+
69+
70+
def test_file_name_from_textio():
71+
f = io.StringIO()
72+
assert name_of_file_like(f) is None

0 commit comments

Comments
 (0)