Skip to content

Commit

Permalink
Added smart_open flush test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucretiel committed Mar 11, 2015
1 parent a1f9b8d commit d99fb35
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions test/test_smart_open.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from autocommand import smart_open
from pytest import raises, fixture, yield_fixture
from pytest import raises, fixture, yield_fixture, mark
from unittest.mock import patch, MagicMock
from io import IOBase

Expand All @@ -19,30 +19,28 @@ def test_path():
'''
with patch_open() as mock_open:
with smart_open('/path/to/file') as file:
file.readline()
mock_open.assert_called_once_with('/path/to/file')
file.__enter__.assert_called_once_with()

mock_open.assert_called_once_with('/path/to/file')
file.__enter__.assert_called_once_with()
file.readline.assert_called_once_with()
file.__exit__.assert_called_once_with(None, None, None)
file.__exit__.assert_called_once_with(None, None, None)


def test_file():
@mark.parametrize('flush', [True, False])
def test_file(flush):
'''
Test smart_open with NOT a file path. The input object should be returned,
and no context methods should be called on it
and it should be flushed after
'''
fakefile = mockfile()
# For some reason open(MagicMock()) doesn't throw, so we have to mock open

with patch_open(side_effect=TypeError):
with smart_open(fakefile) as file:
file.readline()
with smart_open(fakefile, flush=flush) as file:
assert file is fakefile
assert not fakefile.__enter__.called

assert file is fakefile
assert not file.__enter__.called
file.readline.assert_called_once_with()
assert not file.__enter__.called
assert not fakefile.__exit__.called
if flush:
fakefile.flush.assert_called_once_with()


def test_open_args(tmpdir):
Expand Down

0 comments on commit d99fb35

Please sign in to comment.