Skip to content

Commit

Permalink
RequestsData can use .input extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
grizz committed Jan 1, 2018
1 parent 64fa28c commit 0984098
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

## [Unreleased]
### Added
- RequestsData can use .input extensions

### Fixed
### Changed
- requests status code is optional, defaults to 200
Expand Down
17 changes: 14 additions & 3 deletions pytest_filedata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_test_files(dirname):
gets a list of files in directory specified by name
"""
if not os.path.isdir(dirname):
raise ValueError("data directory '{}' does not exist".format(dirname))
return []
path = dirname + "/{}"
return map(path.format, sorted(os.listdir(dirname)))

Expand All @@ -80,6 +80,9 @@ def get_filedata(name):
data = collections.OrderedDict()
dirname = os.path.join(test_dir, *name.split('_'))

if not os.path.isdir(dirname):
raise ValueError("data directory '{}' does not exist".format(dirname))

for each in get_test_files(dirname):
fname = os.path.basename(each)
if fname.startswith('.'):
Expand Down Expand Up @@ -113,12 +116,20 @@ def __init__(self, prefix):
def callback(self, request, context):
path = urlparse.urlparse(request.url).path
path = os.path.join(test_dir, 'data', self.prefix, path.lstrip('/'))

files = get_test_files(path)

if len(files) != 1:
if len(files) > 1:
raise NotImplementedError("Currently there must be only one response file")

fname = files[0]
if len(files) == 0:
# dir not found, check for file.input
fname = path + '.input'
if not os.path.exists(fname):
raise ValueError("failed to find data for {}".format(path))

else:
fname = files[0]

try:
# file extension is status code
Expand Down
5 changes: 5 additions & 0 deletions tests/test_filedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
def test_json_data(data_json):
parsed = json.loads(data_json.input)
assert data_json.expected == parsed


#with pytest.raises(ValueError):
# def test_nonexistant(data_nonexistant):
# pass
24 changes: 24 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import requests
import pytest


def assert_test0(data):
res = requests.get('https://example.com/test0')
assert res.status_code == 200
Expand All @@ -17,3 +18,26 @@ def test_decorator(data_json):
def test_context(data_json):
with pytest.RequestsData("req"):
assert_test0(data_json)


@pytest.RequestsData("json")
def test_input_extension(data_json):
assert_test0(data_json)


@pytest.RequestsData("req")
def test_multiple_files():
with pytest.raises(NotImplementedError):
requests.get('https://example.com/multiple_files')


@pytest.RequestsData("req")
def test_missing_data():
with pytest.raises(ValueError):
requests.get('https://example.com/nonexistant')


@pytest.RequestsData("req")
def test_status_code():
res = requests.get('https://example.com/test404')
res.status_code == 404

0 comments on commit 0984098

Please sign in to comment.