Skip to content

Commit

Permalink
Add a few unit tests for HttpFile
Browse files Browse the repository at this point in the history
This just tests the success case. Failure cases will need additional
work.
  • Loading branch information
bmerry committed Jun 10, 2020
1 parent b552933 commit 022dbc8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/data/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ rfi_mask_ranges_is_group.hdf5
There is a group called ``ranges`` rather than a dataset.
wrong_extension.blah
Copy of rfi_mask_ranges.hdf5 with a different extension.

Other
-----
all_bytes.bin
Byte values running from 0 to 255.
Binary file added test/data/all_bytes.bin
Binary file not shown.
41 changes: 41 additions & 0 deletions test/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import contextlib
import hashlib
import io

import h5py
import pytest
Expand All @@ -28,6 +29,46 @@
from test_utils import get_data, get_data_url, get_file_url, DummyModel


@pytest.fixture
def http_file(web_server):
with requests.Session() as session:
with fetch.HttpFile(session, web_server('all_bytes.bin')) as file:
yield file


def test_http_file_seek_tell(http_file):
assert http_file.tell() == 0
http_file.seek(10, io.SEEK_SET)
assert http_file.tell() == 10
assert http_file.read(2) == b'\x0A\x0B'
assert http_file.tell() == 12
http_file.seek(5)
assert http_file.tell() == 5
http_file.seek(5, io.SEEK_CUR)
assert http_file.tell() == 10
http_file.seek(-5, io.SEEK_CUR)
assert http_file.tell() == 5
http_file.seek(-10, io.SEEK_END)
assert http_file.tell() == 246


def test_http_file_close(http_file):
assert not http_file.closed
http_file.close()
assert http_file.closed
http_file.close()
assert http_file.closed


def test_http_file_read(http_file):
assert http_file.read(2) == b'\x00\x01'
assert http_file.read(3) == b'\x02\x03\x04'
http_file.seek(-2, io.SEEK_END)
# Short read at end of file
assert http_file.read(4) == b'\xFE\xFF'
assert http_file.tell() == 256


@pytest.mark.parametrize('use_file', [True, False])
@pytest.mark.parametrize('filename', ['rfi_mask_ranges.hdf5', 'direct.alias', 'indirect.alias'])
def test_fetch_model_simple(use_file, filename, web_server) -> None:
Expand Down

0 comments on commit 022dbc8

Please sign in to comment.