def test_HpdsBypassAPI_endpoint_trailing_slash(self): test_bad_url = "http://endpoint.url/pic-sure" test_expected_url = "http://endpoint.url/pic-sure/" test_token = "my-JWT-token" api_obj = PicSureHpds.BypassConnectionAPI(test_expected_url, test_token) self.assertEqual( test_expected_url, api_obj.url, "correct url should be passed unchanged into BypassConnectionAPI") api_obj = PicSureHpds.BypassConnectionAPI(test_bad_url, test_token) self.assertEqual( test_expected_url, api_obj.url, "incorrect url should add trailing backslash to endpoint url")
def test_HpdsBypassAPI_func_queryResult(self): test_url = "http://endpoint.url/pic-sure/" test_token = "my-JWT-token" test_uuid = "my-test-uuid" api_obj = PicSureHpds.BypassConnectionAPI(test_url, test_token) api_obj.queryResult(test_uuid, test_uuid) self.fail("This is not yet implemented by HPDS")
def test_HpdsQuery_func_getResultsDataFrame(self, mock_picsure_connection, mock_http_request): test_uuid = "my-test-uuid" test_url = "http://my-test.url/" test_token = "this.is.my.test.token" test_return_CSV = "ROW\tVALUE\n" + "1\tTrue\n" + "2\tFalse\n" + "3\tUnknown\n" conn = mock_picsure_connection() API = PicSureHpds.BypassConnectionAPI(test_url, test_token) def ret_API(): return API conn._api_obj = ret_API resource = PicSureHpds.HpdsResourceConnection(conn, test_uuid) query = resource.query() resp_headers = {"status": "200"} mock_http_request.return_value = (resp_headers, test_return_CSV.encode("utf-8")) req_body = json.dumps(query.buildQuery("DATAFRAME")) query.getResultsDataFrame() mock_http_request.assert_called_with( uri=test_url + "query/sync", method="POST", body=req_body, headers={'Content-Type': 'application/json'})
def test_HpdsQuery_func_getCount_handles_connection_error( self, mock_picsure_connection, mock_http_request): test_uuid = "my-test-uuid" test_url = "http://my-test.url/" test_token = "this.is.my.test.token" test_connection_error = '{"results":{}, "error":"true"}' conn = mock_picsure_connection() API = PicSureHpds.BypassConnectionAPI(test_url, test_token) def ret_API(): return API conn._api_obj = ret_API resource = PicSureHpds.HpdsResourceConnection(conn, test_uuid) query = resource.query() resp_headers = {"status": "200"} mock_http_request.return_value = ( resp_headers, test_connection_error.encode("utf-8")) req_body = json.dumps(query.buildQuery("COUNT")) # micro test to confirm warning is printed on error with patch('sys.stdout', new=io.StringIO()) as fake_stdout: test = query.getCount() sys.stdout = sys.__stdout__ # Reset redirect. Needed for it to work! captured = fake_stdout.getvalue() print("Message on parameter useage:\n" + captured) self.assertTrue(len(captured) > 0) self.assertEqual(test, test_connection_error)
def test_HpdsQuery_func_getCount(self, mock_picsure_connection, mock_http_request): test_uuid = "my-test-uuid" test_url = "http://my-test.url/" test_token = "this.is.my.test.token" conn = mock_picsure_connection() API = PicSureHpds.BypassConnectionAPI(test_url, test_token) def ret_API(): return API conn._api_obj = ret_API resource = PicSureHpds.HpdsResourceConnection(conn, test_uuid) query = resource.query() resp_headers = {"status": "200"} mock_http_request.return_value = (resp_headers, "1000".encode("utf-8")) req_body = json.dumps(query.buildQuery("COUNT")) query.getCount() mock_http_request.assert_called_with( uri=test_url + "query/sync", method="POST", body=req_body, headers={'Content-Type': 'application/json'})
def test_HpdsBypassAPI_create(self): test_url = "http://endpoint.url/pic-sure/" test_token = "my-JWT-token" api_obj = PicSureHpds.BypassConnectionAPI(test_url, test_token) self.assertIsInstance(api_obj, PicSureHpds.BypassConnectionAPI, "Should be of BypassConnectionAPI type") self.assertEqual(test_url, api_obj.url, "correct url should be passed into BypassConnection") self.assertEqual( test_token, api_obj._token, "correct JWT token should be passed into BypassConnection")
def test_HpdsQuery_list_filter_input_a_list(self, mock_picsure_connection): test_uuid = "my-test-uuid" test_key1 = "my-test-key1" test_key2 = "my-test-key2" test_value = 1000 test_class = "my-test-class" conn = mock_picsure_connection() API = PicSureHpds.BypassConnectionAPI("", "") API.search = MagicMock(return_value='{"results": {"' + test_class + '": {"' + test_key1 + '": "foo", "' + test_key2 + '": "bar"}}}') def ret_API(): return API conn._api_obj = ret_API resource = PicSureHpds.HpdsResourceConnection(conn, test_uuid) query = resource.query() # micro test to confirm warning is printed if parameters are passed with patch('sys.stdout', new=io.StringIO()) as fake_stdout: query.filter().add([test_key1, test_key2], test_value) self.assertEqual(len(query.filter().data), 2) self.assertDictEqual( query.filter().data[test_key1], { 'type': 'minmax', 'min': test_value, 'max': test_value, 'HpdsDataType': test_class }) self.assertDictEqual( query.filter().data[test_key2], { 'type': 'minmax', 'min': test_value, 'max': test_value, 'HpdsDataType': test_class }) sys.stdout = sys.__stdout__ # Reset redirect. Needed for it to work! captured = fake_stdout.getvalue() print("Message on parameter useage:\n" + captured) self.assertTrue(len(captured) == 0)
def test_HpdsBypassAPI_func_search_no_term(self, MockHttp): resp_headers = {"status": "200"} content = ["test-uuid-1", "test-uuid-2", "test-uuid-3"] json_content = json.dumps(content) MockHttp.return_value = (resp_headers, json_content.encode("utf-8")) test_url = "http://endpoint.url/pic-sure/" test_token = "my-JWT-token" test_uuid = "my-test-uuid" search_term = "asthma" api_obj = PicSureHpds.BypassConnectionAPI(test_url, test_token) api_obj.search(test_uuid, search_term) MockHttp.assert_called_with( uri=test_url + "search", method="POST", body=search_term, headers={'Content-Type': 'application/json'})
def test_HpdsBypassAPI_func_info(self, MockHttp): self.fail("This is not yet implemented into the API codebase") resp_headers = {"status": "200"} content = {"test": "success"} json_content = json.dumps(content) MockHttp.return_value = (resp_headers, json_content.encode("utf-8")) test_url = "http://endpoint.url/pic-sure/" test_token = "my-JWT-token" test_uuid = "my-test-uuid" api_obj = PicSureHpds.BypassConnectionAPI(test_url, test_token) api_obj.info(test_uuid) MockHttp.assert_called_with( uri=test_url + "info", method="POST", body="{}", headers={'Content-Type': 'application/json'})
def test_HpdsQuery_func_load(self, mock_picsure_connection): test_uuid = "my-test-uuid" query_obj = { "query": { "fields": ["\\select\\key"], "crossCountFields": ["\\crosscounts\\key1", "\\crosscounts\\key2"], "requiredFields": ["\\require\\key"], "anyRecordOf": ["\\anyof\\key1", "\\anyof\\key2"], "numericFilters": { "\\filter_numeric\\range1": { "min": 40, "max": 60 }, "\\filter_numeric\\value1": { "min": 50, "max": 50 } }, "categoryFilters": { "\\filter_categorical\\set1": ["cat1"], "\\filter_categorical\\set2": ["catA", "catC"] }, "variantInfoFilters": [{ "categoryVariantInfoFilters": { "Variant_severity": ["HIGH"] }, "numericVariantInfoFilters": { "AF": { "min": 0.1, "max": 0.9 } } }] }, "resourceUUID": "my-test-uuid" } query_str = json.dumps(query_obj) return_vals = {} conn = mock_picsure_connection() API = PicSureHpds.BypassConnectionAPI("", "") API.search = MagicMock(return_value=json.dumps(return_vals)) def ret_API(): return API conn._api_obj = ret_API resource = PicSureHpds.HpdsResourceConnection(conn, test_uuid) query = resource.query() # micro test to confirm warning is printed if parameters are passed with patch('sys.stdout', new=io.StringIO()) as fake_stdout: query.load(query_str) sys.stdout = sys.__stdout__ # Reset redirect. Needed for it to work! captured = fake_stdout.getvalue() print("Message on parameter useage:\n" + captured) self.assertDictEqual(query_obj, json.loads(query.save()))
def test_HpdsQuery_func_save(self, mock_picsure_connection): test_uuid = "my-test-uuid" test_value = 50 return_vals = { "results": { "phenotypes": { "\\select\\key": {}, "\\require\\key": {}, "\\crosscounts\\key1": {}, "\\crosscounts\\key2": {}, "\\anyof\\key1": {}, "\\anyof\\key2": {}, "\\filter_numeric\\key1": { "name": "key1", "categorical": False, "min": 0, "max": 100 }, "\\filter_numeric\\range1": { "name": "term1", "categorical": False, "min": 0, "max": 100 }, "\\filter_numeric\\value1": { "name": "value1", "categorical": False, "min": 0, "max": 100 }, "\\filter_categorical\\set1": { "name": "set1", "categorical": True, "categoryValues": ["cat1"] }, "\\filter_categorical\\set2": { "name": "set2", "categorical": True, "categoryValues": ["catA", "catB", "catC"] } }, "info": { "Variant_severity": { "values": ["HIGH", "LOW"], "continuous": False }, "AF": { "values": [], "continuous": True } } } } conn = mock_picsure_connection() API = PicSureHpds.BypassConnectionAPI("", "") API.search = MagicMock(return_value=json.dumps(return_vals)) def ret_API(): return API conn._api_obj = ret_API resource = PicSureHpds.HpdsResourceConnection(conn, test_uuid) query = resource.query() # micro test to confirm warning is printed if parameters are passed with patch('sys.stdout', new=io.StringIO()) as fake_stdout: query.select().add("\\select\\key") query.crosscounts().add( ["\\crosscounts\\key1", "\\crosscounts\\key2"]) query.require().add("\\require\\key") query.anyof().add(["\\anyof\\key1", "\\anyof\\key2"]) query.filter().add("\\filter_numeric\\range1", test_value - 10, test_value + 10) query.filter().add("\\filter_numeric\\value1", test_value) query.filter().add("\\filter_categorical\\set1", "cat1") query.filter().add("\\filter_categorical\\set2", ["catA", "catC"]) query.filter().add("Variant_severity", ["HIGH"]) query.filter().add("AF", min=0.1, max=0.9) sys.stdout = sys.__stdout__ # Reset redirect. Needed for it to work! captured = fake_stdout.getvalue() print("Message on parameter useage:\n" + captured) query_obj = { "query": { "fields": ["\\select\\key"], "crossCountFields": ["\\crosscounts\\key1", "\\crosscounts\\key2"], "requiredFields": ["\\require\\key"], "anyRecordOf": ["\\anyof\\key1", "\\anyof\\key2"], "numericFilters": { "\\filter_numeric\\range1": { "min": 40, "max": 60 }, "\\filter_numeric\\value1": { "min": 50, "max": 50 } }, "categoryFilters": { "\\filter_categorical\\set1": ["cat1"], "\\filter_categorical\\set2": ["catA", "catC"] }, "variantInfoFilters": [{ "categoryVariantInfoFilters": { "Variant_severity": ["HIGH"] }, "numericVariantInfoFilters": { "AF": { "min": 0.1, "max": 0.9 } } }] }, "resourceUUID": "my-test-uuid" } self.assertDictEqual(query_obj, json.loads(query.save()))