def test_check_required_fields(): # When a field is marked as required in a .thrift file, the # Python code generated by the Thrift compiler only seems to # capture this requirement in the validate() function for the # generated class. While the ThriftGeneratedClass.thrift_spec # structure captures the names and types of the fields, # thrift_spec does not seem to store any flags indicating # whether or not a field is required. # # Here is the validate() function for the Communication class: # # def validate(): # if self.id is None: # raise TProtocol.TProtocolException( # message='Required field id is unset!') # if self.uuid is None: # raise TProtocol.TProtocolException( # message='Required field uuid is unset!') # if self.type is None: # raise TProtocol.TProtocolException( # message='Required field type is unset!') # return # # The validate() function raises an exception when it can't # find a required field. There doesn't seem to be any way to # determine whether multiple required fields are missing, # aside from assigning a value to the required field and # running validate() again. comm = concrete.Communication() with LogCapture() as log_capture: assert not validate_thrift_deep(comm) log_capture.check( ('root', 'ERROR', "Communication: Required Field 'id' is unset!")) comm.id = "ID" with LogCapture() as log_capture: assert not validate_thrift_deep(comm) log_capture.check( ('root', 'ERROR', "Communication: Required Field 'uuid' is unset!")) comm.uuid = concrete.UUID(uuidString="TEST_UUID") with LogCapture() as log_capture: assert not validate_thrift_deep(comm) log_capture.check( ('root', 'ERROR', StringComparison(r".*TEST_UUID.*Required Field 'type' is unset!"))) comm.metadata = concrete.AnnotationMetadata(tool="TEST", timestamp=int(time.time())) comm.type = "OTHER" assert validate_thrift_deep(comm)
def test_entity_mention_tokenization(): comm = read_test_comm() assert validate_communication(comm) assert validate_entity_mention_ids(comm) comm.entityMentionSetList[0].mentionList[0].tokens.tokenizationId = ( concrete.UUID(uuidString='BAD_TOKENIZATION_UUID')) with LogCapture() as log_capture: assert not validate_entity_mention_tokenization_ids(comm) log_capture.check( ('root', 'ERROR', StringComparison(r'.*invalid tokenizationId.*BAD_TOKENIZATION_UUID')))
def test_entity_mention_ids(): comm = read_test_comm() assert validate_communication(comm) assert validate_entity_mention_ids(comm) comm.entitySetList[0].entityList[0].mentionIdList[0] = concrete.UUID( uuidString='BAD_ENTITY_MENTION_UUID') with LogCapture() as log_capture: assert not validate_entity_mention_ids(comm) log_capture.check( ('root', 'ERROR', StringComparison( r'.*invalid entityMentionId.*BAD_ENTITY_MENTION_UUID')))