Skip to content

Commit

Permalink
Add test to show existing behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlin committed Jul 5, 2023
1 parent 333be85 commit 48b8646
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/io/test_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import augur.io.file


Expand Down Expand Up @@ -71,3 +72,47 @@ def test_open_file_write_zstd(self, tmpdir):
f_write.write('foo\nbar\n')
with zstd.open(path, 'rt') as f_read:
assert f_read.read() == 'foo\nbar\n'

def test_open_file_nested_read(self, tmpdir):
"""Open a text file as a path then buffer for reading."""
path = str(tmpdir / 'test.txt')
with open(path, 'wt') as f_write:
f_write.write('foo\nbar\n')

with augur.io.file.open_file(path) as f_read:
with augur.io.file.open_file(f_read) as f_read:
assert f_read.read() == 'foo\nbar\n'

def test_open_file_nested_read_zstd(self, tmpdir):
"""Open a zstd-compressed text file as a path then buffer for reading."""
import zstandard as zstd
path = str(tmpdir / 'test.txt.zst')
with zstd.open(path, 'wt') as f_write:
f_write.write('foo\nbar\n')

with augur.io.file.open_file(path) as f_read:
with augur.io.file.open_file(f_read) as f_read:
assert f_read.read() == 'foo\nbar\n'

def test_open_file_nested_write(self, tmpdir):
"""Open a text file as a path then buffer for writing."""
path = str(tmpdir / 'test.txt')
with augur.io.file.open_file(path, 'w') as f_write:
with augur.io.file.open_file(f_write, 'w') as f_write:
f_write.write('foo\nbar\n')

def test_open_file_nested_write_zstd(self, tmpdir):
"""Open a zstd-compressed text file as a path then buffer for writing."""
path = str(tmpdir / 'test.txt.zst')
with augur.io.file.open_file(path, 'w') as f_write:
with augur.io.file.open_file(f_write, 'w') as f_write:
f_write.write('foo\nbar\n')

def test_open_file_read_error(self):
"""Try reading from an unsupported type."""
path_or_buffer = len("bogus")

# TODO: Raise a more informative error.
with pytest.raises(AttributeError, match="'int' object has no attribute 'read'"):
with augur.io.file.open_file(path_or_buffer, 'r') as f:
f.read()

0 comments on commit 48b8646

Please sign in to comment.