Skip to content

Commit

Permalink
Merge pull request #47 from cosmocracy/master
Browse files Browse the repository at this point in the history
Additional error checking on API calls; See #12
  • Loading branch information
teleyinex authored Feb 21, 2018
2 parents 208eaaf + 719fe57 commit c9e182b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
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 @@ -365,7 +365,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 @@ -381,7 +386,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

0 comments on commit c9e182b

Please sign in to comment.