def _get_validation(stub, shell, version, input_arg): """Validates the input JSON resource(s) against the FHIR version. Args: stub: gRPC client stub library. shell: an instance of the iPython shell that invoked the magic command. version: the FHIR version to be used for validation. input_arg: the FHIR resource to be validated against the specified version. Returns: The ValidationResponse containing the validation results of the resources or an error. """ req = wstlservice_pb2.ValidationRequest( input=_location.parse_location(shell, input_arg)) if version.lower() == "r4": req.fhir_version = wstlservice_pb2.ValidationRequest.FhirVersion.R4 elif version.lower() == "stu3": req.fhir_version = wstlservice_pb2.ValidationRequest.FhirVersion.STU3 else: return None, ValueError( """FHIR version {} is incorrect or not supported, {} are supported versions""".format( version, wstlservice_pb2.ValidationRequest.FhirVersion.keys())) try: resp = stub.FhirValidate(req) except grpc.RpcError as rpc_error: return None, rpc_error return resp, None
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