forked from ga4gh/ga4gh-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial outline of protocol error testing.
- Adds a simple script to decode error codes. - Adds tests for bad page sizes, and adds a configuration key for default page size. - Improves request validation error messages
- Loading branch information
1 parent
182543f
commit 872159f
Showing
6 changed files
with
185 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Simple script to decode exception error codes. This translates | ||
the error code received by clients into an exception class. | ||
""" | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
import argparse | ||
|
||
import ga4gh.exceptions as exceptions | ||
|
||
|
||
def parseArgs(): | ||
parser = argparse.ArgumentParser( | ||
description=( | ||
"Converts an error code received by a clients to the " | ||
"corresponding exception class.")) | ||
parser.add_argument( | ||
"errorCode", type=int, | ||
help="The errorCode value in a GAException object.") | ||
args = parser.parse_args() | ||
return args | ||
|
||
|
||
def main(): | ||
args = parseArgs() | ||
exceptionClass = exceptions.getExceptionClass(args.errorCode) | ||
print(args.errorCode, exceptionClass, sep="\t") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
""" | ||
Unit tests for frontend error conditions. | ||
""" | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
import unittest | ||
|
||
import ga4gh.frontend as frontend | ||
import ga4gh.exceptions as exceptions | ||
import ga4gh.protocol as protocol | ||
import tests.utils as utils | ||
|
||
_app = None | ||
|
||
|
||
def setUp(): | ||
""" | ||
Set up the test Flask app. | ||
""" | ||
global _app | ||
frontend.configure(baseConfig="TestConfig") | ||
_app = frontend.app.test_client() | ||
|
||
|
||
def tearDown(): | ||
global _app | ||
_app = None | ||
|
||
|
||
class TestFrontendErrors(unittest.TestCase): | ||
""" | ||
Tests the frontend for various errors that can occur and verify | ||
that the correct exception was raised by the error code sent | ||
back. | ||
""" | ||
def setUp(self): | ||
self.app = _app | ||
# TODO replace this with ALL post methods once the rest of the | ||
# end points have been implemented. This should also add an API | ||
# to protocol.py to simplify and document the process of getting | ||
# the correct API endpoints and classes. That is, we shouldn't | ||
# use protocol.postMethods directly, but instead call a function. | ||
supportedMethods = set([ | ||
protocol.GASearchCallSetsRequest, | ||
protocol.GASearchVariantSetsRequest, | ||
protocol.GASearchVariantsRequest, | ||
]) | ||
self.endPointMap = {} | ||
for endPoint, requestClass, responseClass in protocol.postMethods: | ||
if requestClass in supportedMethods: | ||
path = utils.applyVersion(endPoint) | ||
self.endPointMap[path] = requestClass | ||
|
||
def _createInstance(self, requestClass): | ||
""" | ||
Returns a valid instance of the specified class. | ||
""" | ||
return utils.InstanceGenerator().generateInstance(requestClass) | ||
|
||
def assertRawRequestRaises(self, exceptionClass, url, requestString): | ||
""" | ||
Verifies that the specified request string returns a protocol | ||
exception corresponding to the specified class when applied to | ||
all POST endpoints. | ||
""" | ||
response = self.app.post( | ||
url, headers={'Content-type': 'application/json'}, | ||
data=requestString) | ||
self.assertEqual(response.status_code, exceptionClass.httpStatus) | ||
error = protocol.GAException.fromJsonString(response.data) | ||
self.assertEqual( | ||
error.errorCode, exceptionClass.getErrorCode()) | ||
self.assertGreater(len(error.message), 0) | ||
|
||
def assertRequestRaises(self, exceptionClass, url, request): | ||
""" | ||
Verifies that the specified request returns a protocol exception | ||
corresponding to the specified exception class. | ||
""" | ||
self.assertRawRequestRaises( | ||
exceptionClass, url, request.toJsonString()) | ||
|
||
def testPageSize(self): | ||
for url, requestClass in self.endPointMap.items(): | ||
for badType in ["", "1", "None", 0.0, 1e3]: | ||
request = self._createInstance(requestClass) | ||
request.pageSize = badType | ||
self.assertRequestRaises( | ||
exceptions.RequestValidationFailureException, url, request) | ||
for badSize in [-100, -1, 0]: | ||
request = self._createInstance(requestClass) | ||
request.pageSize = badSize | ||
self.assertRequestRaises( | ||
exceptions.BadPageSizeException, url, request) | ||
|
||
def testPageToken(self): | ||
for url, requestClass in self.endPointMap.items(): | ||
for badType in [0, 0.0, 1e-3, {}, [], [None]]: | ||
request = self._createInstance(requestClass) | ||
request.pageToken = badType | ||
self.assertRequestRaises( | ||
exceptions.RequestValidationFailureException, url, request) |