Skip to content

Commit

Permalink
Merge pull request #74 from bagerard/Equality_check_RWSRequest
Browse files Browse the repository at this point in the history
added comparion operator for RWSRequest + minor styling fix
  • Loading branch information
Ian Sparks authored Oct 3, 2016
2 parents ed57374 + f4c5f7d commit 96715e4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 27 deletions.
10 changes: 9 additions & 1 deletion rwslib/rws_requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,22 @@ class RWSRequest(object):
requires_authorization = False
method = "GET" # Default

def __eq__(self, other):
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False

def __ne__(self, other):
return not self.__eq__(other)

def result(self, request):
"""Process a result to create a custom output"""
# By default return text
return request.text

def url_path(self):
"""Return url path list"""
raise NotImplementedError("Override url_path in descendants of RWSRequest") # pragma: no cover
raise NotImplementedError("Override url_path in descendants of RWSRequest") # pragma: no cover

def args(self):
"""Return additional args here as dict"""
Expand Down
3 changes: 2 additions & 1 deletion rwslib/rws_requests/odm_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"""
from . import RWSAuthorizedGetRequest, QueryOptionGetRequest


class AuditRecordsRequest(QueryOptionGetRequest):
"""Clinical Audit Records Dataset"""
KNOWN_QUERY_OPTIONS = ['studyoid','startid','per_page']
KNOWN_QUERY_OPTIONS = ['studyoid', 'startid', 'per_page']

def __init__(self, project_name, environment_name, startid=1, per_page=100):
self.project_name = project_name
Expand Down
2 changes: 1 addition & 1 deletion rwslib/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ def all_tests():
loader = unittest.TestLoader()
suite.addTests(loader.discover('rwslib.tests'))
suite.addTests(loader.discover('rwslib.extras.audit_event'))
suite.addTests(loader.discover('rwslib.extras')) #Local cv tests
suite.addTests(loader.discover('rwslib.extras')) # Local cv tests
return suite
18 changes: 11 additions & 7 deletions rwslib/tests/test_biostats_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
__author__ = 'glow'

import unittest

from six.moves.urllib_parse import quote

from rwslib.rws_requests.biostats_gateway import check_dataset_format, DATASET_FORMATS, \
dataset_format_to_extension, CVMetaDataRequest, FormDataRequest, MetaDataRequest, \
ProjectMetaDataRequest, ViewMetaDataRequest, CommentDataRequest, ProtocolDeviationsRequest, \
Expand Down Expand Up @@ -35,15 +37,15 @@ def test_poor_cases(self):
class TestDatasetFormatToExtension(unittest.TestCase):
def test_good_cases(self):
"""Good happy case for check_dataset_format"""
for dsf, format in DATASET_FORMATS.items():
for dsf, format_ in DATASET_FORMATS.items():
match = dataset_format_to_extension(dsf)
self.assertEqual(format, match)
self.assertEqual(format_, match)

def test_poor_cases(self):
"""Bad formats for check_dataset_format"""
for nonsense in ['xls', 'dat', 'xslx']:
with self.assertRaises(ValueError) as exc:
match = dataset_format_to_extension(nonsense)
dataset_format_to_extension(nonsense)
self.assertEqual("dataset_format is expected to be one of "
"%s. '%s' is not valid" % (', '.join(DATASET_FORMATS.keys()),
nonsense),
Expand Down Expand Up @@ -162,14 +164,15 @@ def test_successful_configuration(self):
self.assertEqual('datasets/SDTMComments?studyid=%s' % quote(t.studyname_environment()),
t.url_path())


class TestProtocolDeviationsRequest(unittest.TestCase):
def create_request_object(self,
project_name,
environment_name,
dataset_format="csv"):
t = ProtocolDeviationsRequest(project_name=project_name,
environment_name=environment_name,
dataset_format=dataset_format)
environment_name=environment_name,
dataset_format=dataset_format)
return t

def test_successful_configuration(self):
Expand All @@ -181,14 +184,15 @@ def test_successful_configuration(self):
self.assertEqual('datasets/SDTMProtocolDeviations?studyid=%s' % quote(t.studyname_environment()),
t.url_path())


class TestDataDictionariesRequest(unittest.TestCase):
def create_request_object(self,
project_name,
environment_name,
dataset_format="csv"):
t = DataDictionariesRequest(project_name=project_name,
environment_name=environment_name,
dataset_format=dataset_format)
environment_name=environment_name,
dataset_format=dataset_format)
return t

def test_successful_configuration(self):
Expand Down
35 changes: 32 additions & 3 deletions rwslib/tests/test_rws_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,43 @@
import requests
from rwslib.rwsobjects import RWSPostResponse, RWSSubjects, RWSSubjectListItem, \
RWSStudyMetadataVersions, RWSStudies, RWSResponse
from rwslib.rws_requests import StudySubjectsRequest, check_dataset_type, SubjectDatasetRequest, \
from rwslib.rws_requests import RWSRequest, StudySubjectsRequest, check_dataset_type, SubjectDatasetRequest, \
VersionDatasetRequest, StudyDatasetRequest, PostDataRequest, PostMetadataRequest, \
GlobalLibraryVersionRequest, GlobalLibraryVersionsRequest, GlobalLibraryDraftsRequest, \
GlobalLibrariesRequest, StudyVersionRequest, StudyVersionsRequest, StudyDraftsRequest, \
MetadataStudiesRequest, ClinicalStudiesRequest, CacheFlushRequest, DiagnosticsRequest, \
BuildVersionRequest, CodeNameRequest, TwoHundredRequest


class TestRWSRequest(unittest.TestCase):
class FooRWSRequest(RWSRequest):
method = "POST"
requires_authorization = True

def __init__(self, project_name):
self.project_name = project_name

def test___eq___on_RWSRequest(self):
self.assertEqual(RWSRequest(), RWSRequest())

def test___eq___same_subclass(self):
request1 = TestRWSRequest.FooRWSRequest('project')
request2 = TestRWSRequest.FooRWSRequest('project')
self.assertEqual(request1, request2)

def test___eq___different_subclass(self):
class BarRWSRequest(TestRWSRequest.FooRWSRequest):
pass
request1 = TestRWSRequest.FooRWSRequest('project')
request2 = BarRWSRequest('project')
self.assertNotEqual(request1, request2)

def test___ne__(self):
request1 = TestRWSRequest.FooRWSRequest('project1')
request2 = TestRWSRequest.FooRWSRequest('project2')
self.assertNotEqual(request1, request2)


class TestStudySubjectsRequest(unittest.TestCase):
def setUp(self):
self.project_name = "A Project"
Expand Down Expand Up @@ -779,11 +808,11 @@ def test_timeout(self):
# Test that unauthorised request times out
rave = rwslib.RWSConnection('http://innovate.mdsol.com')
with self.assertRaises(requests.exceptions.Timeout):
rave.send_request(rwslib.rws_requests.ClinicalStudiesRequest(),timeout=0.0001, verify=False)
rave.send_request(rwslib.rws_requests.ClinicalStudiesRequest(), timeout=0.0001, verify=False)

# Raise timeout and check no timeout occurs. An exception will be raised because the request is unauthorised
with self.assertRaises(rwslib.AuthorizationException):
rave.send_request(rwslib.rws_requests.ClinicalStudiesRequest(),timeout=3600, verify=False)
rave.send_request(rwslib.rws_requests.ClinicalStudiesRequest(), timeout=3600, verify=False)


if __name__ == '__main__':
Expand Down
29 changes: 15 additions & 14 deletions rwslib/tests/test_rwsobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
__author__ = 'isparks'

import unittest

from rwslib import rwsobjects


class TestParse(unittest.TestCase):

def test_parse_with_bom(self):
"""Test parser can throw away BOM"""
text = u"""\xef\xbb\xbf<?xml version="1.0" encoding="utf-8"?><ODM/>"""
self.assertEqual("ODM",rwsobjects.parseXMLString(text).tag)
self.assertEqual("ODM", rwsobjects.parseXMLString(text).tag)

def test_parse_without_bom(self):
"""Test parser can throw away BOM"""
text = u"""<?xml version="1.0" encoding="utf-8"?><ODM/>"""
self.assertEqual("ODM",rwsobjects.parseXMLString(text).tag)
self.assertEqual("ODM", rwsobjects.parseXMLString(text).tag)

def test_parse_empty_string(self):
text = u""""""
self.assertEqual("",rwsobjects.parseXMLString(text))
self.assertEqual("", rwsobjects.parseXMLString(text))


class TestParseEnvironment(unittest.TestCase):
Expand All @@ -39,7 +41,6 @@ def test_single_left_brace_in_name(self):
env = rwsobjects.getEnvironmentFromNameAndProtocol('TEST :( (PROD)', 'TEST :(')
self.assertEqual(env, 'PROD')


def test_braces_tight_spaces(self):
env = rwsobjects.getEnvironmentFromNameAndProtocol('TEST(99)(AUX)', 'TEST(99)')
self.assertEqual(env, 'AUX')
Expand All @@ -52,6 +53,7 @@ def test_no_env(self):
env = rwsobjects.getEnvironmentFromNameAndProtocol('TEST(99)', 'TEST(99)')
self.assertEqual(env, '')


class TestRWSErrorResponse(unittest.TestCase):
"""Test that RWSErrorResponse correctly reads an error response"""

Expand All @@ -71,6 +73,7 @@ def test_parse(self):
self.assertEqual("RWS00092", resp.reasoncode)
self.assertEqual("0b47fe86-542f-4070-9e7d-16396a5ef08a", resp.referencenumber)


class TestRWSError(unittest.TestCase):
"""Test that RWSError correctly reads an error response"""

Expand Down Expand Up @@ -113,6 +116,7 @@ def test_parse(self):
self.assertEqual(4, response.fields_touched)
self.assertEqual(5, response.loglines_touched)


class TestRWSSubjects(unittest.TestCase):
"""Test RWSSubjects"""

Expand Down Expand Up @@ -146,7 +150,7 @@ def test_parse(self):
self.assertEqual(False, subjects[0].overdue)
self.assertEqual(None, subjects[1].overdue) # Example where status was not asked for.
self.assertEqual(True, subjects[2].incomplete)
self.assertEqual(text,str(subjects))
self.assertEqual(text, str(subjects))

def test_parse_no_uuid(self):
"""
Expand Down Expand Up @@ -176,10 +180,10 @@ def test_parse_no_uuid(self):
self.assertEqual(3, len(subjects))
self.assertEqual(True, subjects[0].touched)
self.assertEqual(False, subjects[0].overdue)
self.assertEqual(None, subjects[1].overdue) # Example where status was not asked for.
self.assertEqual(0, len(subjects[1].links)) # Example where link was not asked for
self.assertEqual(None, subjects[1].overdue) # Example where status was not asked for.
self.assertEqual(0, len(subjects[1].links)) # Example where link was not asked for
self.assertEqual(True, subjects[2].incomplete)
self.assertEqual(text,str(subjects))
self.assertEqual(text, str(subjects))
self.assertEqual("1", subjects[0].subject_name)
self.assertEqual("2", subjects[1].subject_name)
self.assertEqual("3", subjects[2].subject_name)
Expand Down Expand Up @@ -212,7 +216,7 @@ def test_parse_out_subject_key_where_uuid(self):
self.assertEqual(3, len(subjects))
self.assertEqual(True, subjects[0].touched)
self.assertEqual(False, subjects[0].overdue)
self.assertEqual(None, subjects[1].overdue) #Example where status was not asked for.
self.assertEqual(None, subjects[1].overdue) #Example where status was not asked for.
self.assertEqual(True, subjects[2].incomplete)
self.assertEqual(text, str(subjects))
self.assertEqual("1", subjects[0].subject_name)
Expand Down Expand Up @@ -303,11 +307,8 @@ def test_bad_success_stats(self):
NewRecords=""
SubjectNumberInStudy="1103" SubjectNumberInStudySite="55">
</Response>"""

def doparse():
parsed = rwsobjects.RWSPostResponse(text)

self.assertRaises(KeyError,doparse)
with self.assertRaises(KeyError):
rwsobjects.RWSPostResponse(text)


class TestRWSPostErrorResponse(unittest.TestCase):
Expand Down

0 comments on commit 96715e4

Please sign in to comment.