Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional error checking on API calls; See #12 #47

Merged
merged 4 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,19 @@ helping materials file.
pbs add_helpingmaterials --help
```

## Running the Tests

To run the test suite for pbs, first install [note](https://nose.readthedocs.io/en/latest/):

```bash
apt-get install python-nose
```

To run all tests, execute the following from the pbs project directory:

```bash
nosetests test
```

# Documentation

Expand Down
8 changes: 7 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,12 @@ def find_project_by_short_name(short_name, pbclient, all=None):


def check_api_error(api_response):
print(api_response)
"""Check if returned API response contains an error."""
if type(api_response) == dict and 'code' in api_response and api_response['code'] <> 200:
print("Server response code: %s" % api_response['code'])
print("Server response: %s" % api_response)
raise exceptions.HTTPError('Unexpected response', response=api_response)
if type(api_response) == dict and (api_response.get('status') == 'failed'):
if 'ProgrammingError' in api_response.get('exception_cls'):
raise DatabaseError(message='PyBossa database error.',
Expand All @@ -377,7 +382,8 @@ def check_api_error(api_response):
raise TaskNotFound(message='PyBossa Task not found',
error=api_response)
else:
raise exceptions.HTTPError
print("Server response: %s" % api_response)
raise exceptions.HTTPError('Unexpected response', response=api_response)


def format_error(module, error):
Expand Down
5 changes: 5 additions & 0 deletions test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def test_check_api_error_raises_exception(self):
error = dict(status='failed', target='diff', exception_cls='err')
assert_raises(exceptions.HTTPError, check_api_error, error)

def test_check_general_http_api_error_raises_exception(self):
"""Test check_api_error raises HTTPError exception."""
error = dict(code=500)
assert_raises(exceptions.HTTPError, check_api_error, error)

def test_check_api_error_raises_database_error(self):
"""Test check_api_error raises DatabaseError exception."""
error = dict(status='failed', target='diff',
Expand Down
2 changes: 1 addition & 1 deletion test/test_pbs_create_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestPbsCreateProject(TestDefault):
def test_create_project_create(self):
"""Test create_project works."""
pbclient = MagicMock()
pbclient.create_project.return_value = {'short_name': 'short_name'}
pbclient.create_project.return_value = {'short_name': 'short_name', 'status_code': 200}
self.config.pbclient = pbclient
res = _create_project(self.config)
assert res == 'Project: short_name created!', res
Expand Down