Skip to content

Commit

Permalink
Update codes.code_string_to_enum_value_descriptor error handling.
Browse files Browse the repository at this point in the history
In the event that we're unable to properly parse a provided code string into a
FHIR Code enum, we raise a `fhir_errors.InvalidFhirError` exception, instead
of a `ValueError`.

Closes #29.

PiperOrigin-RevId: 347741644
  • Loading branch information
Cameron Tew authored and nickgeorge committed Jan 19, 2021
1 parent d0b9ab4 commit 1b494bb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
9 changes: 5 additions & 4 deletions py/google/fhir/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def code_string_to_enum_value_descriptor(
An instance of EnumValueDescriptor that the code_string represents.
Raises:
ValueError: In the event that a conversion from code_string was
unsuccessful.
fhir_errors.InvalidFhirError: In the event that a conversion from
code_string was unsuccessful.
"""
# Check the shared memos mapping
value_descriptor = _get_enum_value_descriptor_memo(enum_descriptor,
Expand Down Expand Up @@ -116,8 +116,9 @@ def code_string_to_enum_value_descriptor(
value_descriptor)
return value_descriptor

raise ValueError(f'Failed to convert {code_string} to '
f'{enum_descriptor.full_name}. No matching enum found')
raise fhir_errors.InvalidFhirError(
f'Failed to convert {code_string!r} to {enum_descriptor.full_name}. No '
f'matching enum found.')


def copy_coding(source: message.Message, target: message.Message):
Expand Down
1 change: 1 addition & 0 deletions py/google/fhir/r4/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ py_test(
"//proto/google/fhir/proto/r4/core/resources:patient_py_pb2",
"//py/google/fhir:codes",
"//py/google/fhir/testing:testdata_utils",
"//py/google/fhir:fhir_errors",
requirement("absl-py"),
requirement("six"),
],
Expand Down
11 changes: 10 additions & 1 deletion py/google/fhir/r4/codes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from proto.google.fhir.proto.r4.core import datatypes_pb2
from proto.google.fhir.proto.r4.core.resources import patient_pb2
from google.fhir import codes
from google.fhir import fhir_errors
from google.fhir.testing import testdata_utils

_CODES_DIR = os.path.join('testdata', 'r4', 'codes')
Expand Down Expand Up @@ -66,13 +67,21 @@ def testEnumValueDescriptorToCodeString(self):
self.assertEqual(
'>', codes.enum_value_descriptor_to_code_string(gt_value_descriptor))

def testCodeStringToEnumValueDescriptor(self):
def testCodeStringToEnumValueDescriptor_withValidCodeString(self):
"""Tests code_string_to_enum_value_descriptor functionality."""
enum_descriptor = codes_pb2.QuestionnaireItemOperatorCode.Value.DESCRIPTOR
enum_value_descriptor = enum_descriptor.values_by_name['GREATER_THAN']
result = codes.code_string_to_enum_value_descriptor('>', enum_descriptor)
self.assertEqual(result.name, enum_value_descriptor.name)

def testCodeStringToEnumValueDescriptor_withInvalidCodeString(self):
"""Tests code_string_to_enum_value_descriptor error handling."""
enum_descriptor = codes_pb2.AssertionOperatorTypeCode.Value.DESCRIPTOR
with self.assertRaises(fhir_errors.InvalidFhirError) as fe:
_ = codes.code_string_to_enum_value_descriptor('InvalidCode!',
enum_descriptor)
self.assertIsInstance(fe.exception, fhir_errors.InvalidFhirError)

def testCopyCode_fromTypedToGeneric(self):
"""Tests copy_code from a generic to typed Code."""
typed_code = patient_pb2.Patient.GenderCode(
Expand Down
3 changes: 1 addition & 2 deletions py/google/fhir/stu3/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ py_test(
"//py",
"//proto/google/fhir/proto/stu3:codes_py_pb2",
"//proto/google/fhir/proto/stu3:datatypes_py_pb2",
"//proto/google/fhir/proto/stu3:resources_py_pb2",
"//proto/google/fhir/proto/stu3:uscore_py_pb2",
"//py/google/fhir:codes",
"//py/google/fhir/testing:testdata_utils",
"//py/google/fhir:fhir_errors",
requirement("absl-py"),
requirement("six"),
],
Expand Down
11 changes: 10 additions & 1 deletion py/google/fhir/stu3/codes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from proto.google.fhir.proto.stu3 import codes_pb2
from proto.google.fhir.proto.stu3 import datatypes_pb2
from google.fhir import codes
from google.fhir import fhir_errors
from google.fhir.testing import testdata_utils

_CODES_DIR = os.path.join('testdata', 'stu3', 'codes')
Expand Down Expand Up @@ -58,14 +59,22 @@ def testEnumValueDescriptorToCodeString(self):
'female',
codes.enum_value_descriptor_to_code_string(female_value_descriptor))

def testCodeStringToEnumValueDescriptor(self):
def testCodeStringToEnumValueDescriptor_withValidCodeString(self):
"""Tests code_string_to_enum_value_descriptor functionality."""
enum_descriptor = codes_pb2.AssertionOperatorTypeCode.Value.DESCRIPTOR
enum_value_descriptor = enum_descriptor.values_by_name['GREATERTHAN']
result = codes.code_string_to_enum_value_descriptor('greaterthan',
enum_descriptor)
self.assertEqual(result.name, enum_value_descriptor.name)

def testCodeStringToEnumValueDescriptor_withInvalidCodeString(self):
"""Tests code_string_to_enum_value_descriptor error handling."""
enum_descriptor = codes_pb2.AssertionOperatorTypeCode.Value.DESCRIPTOR
with self.assertRaises(fhir_errors.InvalidFhirError) as fe:
_ = codes.code_string_to_enum_value_descriptor('InvalidCode!',
enum_descriptor)
self.assertIsInstance(fe.exception, fhir_errors.InvalidFhirError)

def testCopyCode_fromTypedToGeneric(self):
"""Tests copy_code from a generic to typed Code."""
typed_code = codes_pb2.AdministrativeGenderCode(
Expand Down

0 comments on commit 1b494bb

Please sign in to comment.