Skip to content

Commit 6ceb3f8

Browse files
author
Stephen Hoover
authored
BUG Fix tests in test_io (#248)
The `test_civis_to_file_local` function had been writing a file to disk which remained behind after the test. Additionally, `called_once_with` is not a method of a `Mock` object; it becomes its own `Mock`, which takes any parameter given and always evaluates to `True`. Change all instances of those calls to `assert_called_once_with`. Add `autospec` to `patch` calls where possible to test that mocked functions are being called with valid arguments.
1 parent 1218839 commit 6ceb3f8

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
- Include JSON files with pip distributions (#244)
1111
- Added flush to `civis_to_file` when passed a user-created buffer,
1212
ensuring the buffer contains the entire file when subsequently read.
13+
- Fix several tests in the `test_io` module (#248)
1314
- Travis tests for Python 3.4 are now restricted to pandas<=0.20, the
1415
last version which supported Python 3.4 (#249)
1516

civis/tests/test_io.py

+51-29
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
has_pandas = False
1616

1717
import civis
18-
from civis.compat import mock, FileNotFoundError
18+
from civis.io import _files
19+
from civis.compat import mock, FileNotFoundError, TemporaryDirectory
1920
from civis.response import Response
2021
from civis.base import CivisAPIError
2122
from civis.resources._resources import get_api_spec, generate_classes
@@ -343,32 +344,38 @@ def test_file_id_from_run_output_platform_error():
343344
@pytest.mark.skipif(not has_pandas, reason="pandas not installed")
344345
def test_file_to_dataframe_infer():
345346
m_client = mock.Mock()
347+
url = 'url'
346348
m_client.files.get.return_value = Response({'name': 'spam.csv',
347-
'file_url': 'url'})
348-
with mock.patch.object(civis.io._files.pd, 'read_csv') as mock_read_csv:
349+
'file_url': url})
350+
with mock.patch.object(civis.io._files.pd, 'read_csv',
351+
autospec=True) as mock_read_csv:
349352
civis.io.file_to_dataframe(121, compression='infer', client=m_client)
350-
assert mock_read_csv.called_once_with(121, compression='infer')
353+
mock_read_csv.assert_called_once_with(url, compression='infer')
351354

352355

353356
@pytest.mark.skipif(not has_pandas, reason="pandas not installed")
354357
def test_file_to_dataframe_infer_gzip():
355358
m_client = mock.Mock()
359+
url = 'url'
356360
m_client.files.get.return_value = Response({'name': 'spam.csv.gz',
357-
'file_url': 'url'})
358-
with mock.patch.object(civis.io._files.pd, 'read_csv') as mock_read_csv:
361+
'file_url': url})
362+
with mock.patch.object(civis.io._files.pd, 'read_csv',
363+
autospec=True) as mock_read_csv:
359364
civis.io.file_to_dataframe(121, compression='infer', client=m_client)
360-
assert mock_read_csv.called_once_with(121, compression='gzip')
365+
mock_read_csv.assert_called_once_with(url, compression='gzip')
361366

362367

363368
@pytest.mark.skipif(not has_pandas, reason="pandas not installed")
364369
def test_file_to_dataframe_kwargs():
365370
m_client = mock.Mock()
371+
url = 'url'
366372
m_client.files.get.return_value = Response({'name': 'spam.csv',
367-
'file_url': 'url'})
368-
with mock.patch.object(civis.io._files.pd, 'read_csv') as mock_read_csv:
373+
'file_url': url})
374+
with mock.patch.object(civis.io._files.pd, 'read_csv',
375+
autospec=True) as mock_read_csv:
369376
civis.io.file_to_dataframe(121, compression='special', client=m_client,
370377
delimiter='|', nrows=10)
371-
assert mock_read_csv.called_once_with(121, compression='special',
378+
mock_read_csv.assert_called_once_with(url, compression='special',
372379
delimiter='|', nrows=10)
373380

374381

@@ -383,25 +390,40 @@ def _dump_json(file_id, buf, *args, **kwargs):
383390
assert out == obj
384391

385392

386-
@mock.patch('civis.io._files._civis_to_file')
387-
@mock.patch('%s.open' % __name__, create=True)
388-
def test_civis_to_file_local(mock_open, mock_civis_to_file_helper):
389-
# Test that passing a path to civis_to_file opens a file.
390-
civis.io.civis_to_file(123, "foo")
391-
mock_open.return_value = mock_file = mock.Mock()
392-
assert mock_open.called_once_with("foo", "wb")
393-
assert mock_civis_to_file_helper.called_once_with(123, mock_file)
394-
395-
396-
@mock.patch('civis.io._files._file_to_civis')
397-
@mock.patch('%s.open' % __name__, create=True)
398-
def test_file_to_civis(mock_open, mock_file_to_civis_helper):
399-
# Test that passing a path to file_to_civis opens a file.
400-
civis.io.file_to_civis("foo", "foo_name")
401-
mock_open.return_value = mock_file = mock.Mock()
402-
assert mock_open.called_once_with("foo", "rb")
403-
assert mock_file_to_civis_helper.called_once_with(
404-
"foo", "foo_name", mock_file)
393+
@mock.patch.object(_files, 'requests', autospec=True)
394+
def test_civis_to_file_local(mock_requests):
395+
# Test that a call to civis_to_file uses `requests` to grab the contents
396+
# of a URL given by the API client and writes it to a file.
397+
mock_civis = create_client_mock()
398+
mock_requests.get.return_value.iter_content.return_value =\
399+
(l.encode() for l in 'abcdef')
400+
with TemporaryDirectory() as tdir:
401+
fname = os.path.join(tdir, 'testfile')
402+
_files.civis_to_file(137, fname, client=mock_civis)
403+
with open(fname, 'rt') as _fin:
404+
assert _fin.read() == 'abcdef'
405+
mock_civis.files.get.assert_called_once_with(137)
406+
mock_requests.get.assert_called_once_with(
407+
mock_civis.files.get.return_value.file_url, stream=True)
408+
409+
410+
@mock.patch.object(_files, 'requests', autospec=True)
411+
def test_file_to_civis(mock_requests):
412+
# Test that file_to_civis posts a Civis File with the API client
413+
# and calls `requests.post` on the returned URL.
414+
mock_civis = create_client_mock()
415+
civis_name, expected_id = 'newname', 137
416+
mock_civis.files.post.return_value.id = expected_id
417+
with TemporaryDirectory() as tdir:
418+
fname = os.path.join(tdir, 'testfile')
419+
with open(fname, 'wt') as _fout:
420+
_fout.write('abcdef')
421+
fid = _files.file_to_civis(fname, civis_name, expires_at=None,
422+
client=mock_civis)
423+
assert fid == expected_id
424+
mock_civis.files.post.assert_called_once_with(civis_name, expires_at=None)
425+
mock_requests.post.assert_called_once_with(
426+
mock_civis.files.post.return_value.upload_url, files=mock.ANY)
405427

406428

407429
@pytest.mark.parametrize("table,expected", [

0 commit comments

Comments
 (0)