Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Mock gluon.utils.download tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leezu committed Jul 9, 2018
1 parent 8257e0a commit 072dbf2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
3 changes: 3 additions & 0 deletions python/mxnet/gluon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ def download(url, path=None, overwrite=False, sha1_hash=None, retries=5, verify_
"""
if path is None:
fname = url.split('/')[-1]
# Empty filenames are invalid
assert fname, 'Can\'t construct file-name from this URL. ' \
'Please set the `path` option manually.'
else:
path = os.path.expanduser(path)
if os.path.isdir(path):
Expand Down
39 changes: 32 additions & 7 deletions tests/python/unittest/test_gluon_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,53 @@
# specific language governing permissions and limitations
# under the License.

import io
import os
import tempfile
import warnings

import mock
import mxnet as mx
from nose.tools import *
import requests
from nose.tools import raises


class MockResponse(requests.Response):
def __init__(self, status_code, content):
super(MockResponse, self).__init__()
assert isinstance(status_code, int)
self.status_code = status_code
self.raw = io.BytesIO(content.encode('utf-8'))


@raises(Exception)
@mock.patch(
'requests.get', mock.Mock(side_effect=requests.exceptions.ConnectionError))
def test_download_retries():
mx.gluon.utils.download("http://doesnotexist.notfound")


@mock.patch(
'requests.get',
mock.Mock(side_effect=
lambda *args, **kwargs: MockResponse(200, 'MOCK CONTENT' * 100)))
def test_download_successful():
tmp = tempfile.mkdtemp()
tmpfile = os.path.join(tmp, 'README.md')
mx.gluon.utils.download("https://raw.githubusercontent.com/apache/incubator-mxnet/master/README.md",
path=tmpfile)
mx.gluon.utils.download(
"https://raw.githubusercontent.com/apache/incubator-mxnet/master/README.md",
path=tmpfile)
assert os.path.getsize(tmpfile) > 100


@mock.patch(
'requests.get',
mock.Mock(
side_effect=lambda *args, **kwargs: MockResponse(200, 'MOCK CONTENT')))
def test_download_ssl_verify():
with warnings.catch_warnings(record=True) as warnings_:
mx.gluon.utils.download(
"https://mxnet.incubator.apache.org/", verify_ssl=False)
assert any(
str(w.message[0]).startswith('Unverified HTTPS request')
for w in warnings_)
"https://mxnet.incubator.apache.org/index.html", verify_ssl=False)
assert any(
str(w.message).startswith('Unverified HTTPS request')
for w in warnings_)

0 comments on commit 072dbf2

Please sign in to comment.