def parse_location(shell, input_wstl_arg, file_ext=None, load_contents=True): r"""Parses the argument and returns a Location protobuf message. Input arguments with the following prefixes are supported: * json://{\"hello\": \"world\"} - using python dict and list notation to define a json object or list. * file://<file_path> - path to file on local file system. * gs://<gcs_path> - path to file on Google Cloud Storage. * py://<name_of_python_variable> - name of python variable instantiated within session. Note that a python list will be parsed as a single JSON Array and a single Location protobuf message is created. If list entries should be parsed separately, use pylist instead. * pylist://<name_of_python_variable> - name of python list variable instantiated within session. Each entry in the list will be parsed as a separate Location protobuf message. Args: shell: ipython interactive shell. input_wstl_arg: wstl magic command input argument. file_ext: list of valid file extensions. Either `_constants.JSON_FILE_EXT` or `WSTL_FILE_EXT`. load_contents: flag indicating whether to load contents from disk instead of storing a path. Returns: A Location protobuf message. Raises: ValueError: An unknown location prefix or the python variable can not be encoded into json. """ (inputs, gcs_paths) = _parser.parse_object( shell, input_wstl_arg, file_ext=file_ext, load_contents=load_contents) if input_wstl_arg.startswith(_constants.JSON_ARG_PREFIX) or \ input_wstl_arg.startswith(_constants.PYTHON_ARG_PREFIX) or \ input_wstl_arg.startswith(_constants.PYTHON_LIST_ARG_PREFIX) or \ input_wstl_arg.startswith(_constants.FILE_ARG_PREFIX): if not load_contents: return [ wstlservice_pb2.Location(local_path=inline_json) for inline_json in inputs ] else: return [ wstlservice_pb2.Location(inline_json=inline_json) for inline_json in inputs ] elif input_wstl_arg.startswith(_constants.GS_ARG_PREFIX): return [ wstlservice_pb2.Location(gcs_location=gcs_path) for gcs_path in gcs_paths ] else: raise ValueError("Missing {} supported prefix".format(",".join([ _constants.JSON_ARG_PREFIX, _constants.GS_ARG_PREFIX, _constants.PYTHON_ARG_PREFIX, _constants.PYTHON_LIST_ARG_PREFIX ])))
def test_fhir_validate_magic_gcs(self, mock_bucket, mock_client, mock_stub, mock_channel): class FakeChannel: def __init__(self, channel): self.channel = channel def __enter__(self): return self.channel def __exit__(self, exc_type, exc_val, exc_tb): self.channel._close() return False class FakeService: def __init__(self, res): self.resp = res def FhirValidate(self, req): del req raise grpc.RpcError(code_pb2.UNIMPLEMENTED, "GCS source not implemented yet") class Item(object): def __init__(self, bucket, name): self.bucket = bucket self.name = name class FakeBucket(object): def __init__(self, bucket_name): self.name = bucket_name mock_channel.return_value = FakeChannel(self._channel) bucket = FakeBucket("fake_bucket") items = [Item(bucket, "file.wstl")] mock_bucket.list_blobs.return_value = items mock_client.return_value.get_bucket.return_value = mock_bucket ip = self.shell.get_ipython() failure = ip.magics_manager.register(wstl.WSTLMagics) self.assertIsNone(failure) with mock.patch.object(FakeService, "FhirValidate") as mock_method: mock_stub.return_value = FakeService(None) mock_method.side_effect = grpc.RpcError result = ip.run_line_magic( "fhir_validate", "--version=stu3 --input=gs://fake_bucket/file.wstl") self.assertIsInstance(result, grpc.RpcError) req_gs = wstlservice_pb2.ValidationRequest( fhir_version=wstlservice_pb2.ValidationRequest.FhirVersion.STU3, input=[ wstlservice_pb2.Location( gcs_location="gs://fake_bucket/file.wstl") ]) mock_method.assert_called_once_with(req_gs)
def test_fhir_validate_magic_ipython(self, mock_stub, mock_channel): class FakeChannel: def __init__(self, channel): self.channel = channel def __enter__(self): return self.channel def __exit__(self, exc_type, exc_val, exc_tb): self.channel._close() return False class FakeService: def __init__(self, res): self.resp = res def FhirValidate(self, req): del req return self.resp mock_channel.return_value = FakeChannel(self._channel) ip = self.shell.get_ipython() failure = ip.magics_manager.register(wstl.WSTLMagics) self.assertIsNone(failure) st1 = "{'id':'example','resourceType':'Device','udi':{'carrierHRF':'test'}}" st2 = "{'id':'example','resourceType':'3','udi':{'carrierHRF':'test'}}" stList = [st1, st1] ip.push("st1") ip.push("st2") ip.push("stList") lines = [ "--version=stu3 --input=py://st1", "--version=stu3 --input=py://st2", "--version=stu3 --input=py://stList", "--version=stu3 --input=pylist://stList" ] results = [] resps = [ wstlservice_pb2.ValidationResponse(status=[ status_pb2.Status(code=code_pb2.OK, message="Validation Success") ]), wstlservice_pb2.ValidationResponse(status=[ status_pb2.Status( code=code_pb2.INVALID_ARGUMENT, message="invalid FHIR resource") ]), wstlservice_pb2.ValidationResponse(status=[ status_pb2.Status( code=code_pb2.INVALID_ARGUMENT, message="invalid FHIR resource") ]), wstlservice_pb2.ValidationResponse(status=[ status_pb2.Status(code=code_pb2.OK, message="Validation Success") ]), ] reqs = [ wstlservice_pb2.ValidationRequest( fhir_version=wstlservice_pb2.ValidationRequest.FhirVersion.STU3, input=[ wstlservice_pb2.Location( inline_json="{'id':'example','resourceType':" + "'Device','udi':{'carrierHRF':'test'}}") ]), wstlservice_pb2.ValidationRequest( fhir_version=wstlservice_pb2.ValidationRequest.FhirVersion.STU3, input=[ wstlservice_pb2.Location( inline_json="{'id':'example','resourceType':" + "'3','udi':{'carrierHRF':'test'}}") ]), wstlservice_pb2.ValidationRequest( fhir_version=wstlservice_pb2.ValidationRequest.FhirVersion.STU3, input=[ wstlservice_pb2.Location( inline_json="[\"{'id':'example','resourceType':" + "'Device','udi':{'carrierHRF':'test'}}\", " + "\"{'id':'example','resourceType':" + "'Device','udi':{'carrierHRF':'test'}}\"]") ]), wstlservice_pb2.ValidationRequest( fhir_version=wstlservice_pb2.ValidationRequest.FhirVersion.STU3, input=[ wstlservice_pb2.Location( inline_json="{'id':'example','resourceType':" + "'Device','udi':{'carrierHRF':'test'}}"), wstlservice_pb2.Location( inline_json="{'id':'example','resourceType':" + "'Device','udi':{'carrierHRF':'test'}}"), ]), ] for i in range(len(lines)): mock_service = mock.create_autospec(FakeService) mock_service.FhirValidate.return_value = resps[i] mock_stub.return_value = mock_service result = ip.run_line_magic("fhir_validate", lines[i]) results.append(result) mock_service.FhirValidate.assert_called_once_with(reqs[i]) wants = [ "{'status': [{'message': 'Validation Success'}]}", "{'status': [{'code': 3, 'message': 'invalid FHIR resource'}]}", "{'status': [{'code': 3, 'message': 'invalid FHIR resource'}]}", "{'status': [{'message': 'Validation Success'}]}", ] for j in range(len(wants)): result = results[j] want = JSON(wants[j]) self.assertEqual( result.data, want.data, msg="JSON.data mismatch on input {}".format(lines[j])) self.assertEqual( result.url, want.url, msg="JSON.url mismatch on input {}".format(lines[j])) self.assertEqual( result.filename, want.filename, msg="JSON.filename mismatch on input {}".format(lines[j])) # Delete created variables to suppress the unused-variable linter warning. del st1 del st2 del stList