def testSetValueAtFieldIndex_repeatCompositeField_setsValue(self): """Test set_value_at_field_index with a repeated composite type.""" patient = self._create_patient_with_names(["A", "B", "C"]) new_name = datatypes_pb2.HumanName( text=datatypes_pb2.String(value="Foo"), family=datatypes_pb2.String(value="Bar")) proto_utils.set_value_at_field_index(patient, "name", 1, new_name) self.assertEqual(patient.name[1], new_name)
def testSetValueAtFieldIndex_invalidIndex_raisesException(self): """Test set_value_at_field_index with an invalid index.""" patient = self._create_patient_with_names(["A", "B", "C"]) new_name = datatypes_pb2.HumanName( text=datatypes_pb2.String(value="Foo"), family=datatypes_pb2.String(value="Bar")) with self.assertRaises(ValueError) as ve: proto_utils.set_value_at_field_index(patient, "name", 3, new_name) self.assertIsInstance(ve.exception, ValueError)
def testSetValueAtFieldIndex_singleCompositeField_setsValue(self): """Test set_value_at_field_index with a singular composite type.""" known_gender = patient_pb2.Patient.GenderCode( value=codes_pb2.AdministrativeGenderCode.MALE) unknown_gender = patient_pb2.Patient.GenderCode( value=codes_pb2.AdministrativeGenderCode.UNKNOWN) patient = patient_pb2.Patient(gender=unknown_gender) self.assertEqual(patient.gender, unknown_gender) proto_utils.set_value_at_field_index(patient, "gender", 0, known_gender) self.assertEqual(patient.gender, known_gender)
def testSetValueAtFieldIndex_singlePrimitiveField_setsValue(self): """Test set_value_at_field_index with a singular primitive type.""" arbitrary_string = datatypes_pb2.String(value="foo") proto_utils.set_value_at_field_index(arbitrary_string, "value", 0, "bar") self.assertEqual(arbitrary_string.value, "bar")