def testNoSimilar(self): """Test no results when no similar items.""" haystack = ['1234', '5678'] needle = 'abcd' result = matching.GetMostLikelyMatchedObject(haystack, needle) self.assertFalse(result)
def _ParseArgs(argv, router): """Parse and validate arguments.""" parser = GetParser() opts, unknown = parser.parse_known_args( argv, namespace=commandline.ArgumentNamespace()) parser.DoPostParseSetup(opts, unknown) if unknown: logging.warning('Unknown args ignored: %s', ' '.join(unknown)) methods = router.ListMethods() # Positional service_method argument validation. parts = opts.service_method.split('/') if len(parts) != 2: parser.error( 'Invalid service/method specification format. It should be ' 'something like chromite.api.SdkService/Create.') if opts.service_method not in methods: # Unknown method, try to match against known methods and make a suggestion. # This is just for developer sanity, e.g. misspellings when testing. matched = matching.GetMostLikelyMatchedObject( methods, opts.service_method, matched_score_threshold=0.6) error = 'Unrecognized service name.' if matched: error += '\nDid you mean: \n%s' % '\n'.join(matched) parser.error(error) opts.service = parts[0] opts.method = parts[1] # Input and output validation. if not opts.output_binary and not opts.output_json: parser.error('At least one output file must be specified.') if not os.path.exists(opts.input_binary or opts.input_json): parser.error('Input file does not exist.') config_msg = build_api_config_pb2.BuildApiConfig() if opts.config_json: handler = message_util.get_message_handler(opts.config_json, message_util.FORMAT_JSON) else: handler = message_util.get_message_handler(opts.config_binary, message_util.FORMAT_BINARY) if opts.config_json or opts.config_binary: # We have been given a config, so read it. try: handler.read_into(config_msg) except message_util.Error as e: parser.error(str(e)) opts.config = api_config_lib.build_config_from_proto(config_msg) opts.config_handler = handler opts.Freeze() return opts
def testSimilarThreshold(self): """Test the threshold is correctly applied.""" haystack = ['abce', 'aecd', '1234'] needle = 'abcd' result = matching.GetMostLikelyMatchedObject( haystack, needle, matched_score_threshold=0.8) self.assertFalse(result)
def testSimilar(self): """Test similar items are found.""" haystack = ['abce', 'aecd', '1234'] needle = 'abcd' result = matching.GetMostLikelyMatchedObject( haystack, needle, matched_score_threshold=0.6) self.assertCountEqual(['abce', 'aecd'], result)
def _ParseArgs(argv, router): """Parse and validate arguments.""" parser = GetParser() opts = parser.parse_args(argv) methods = router.ListMethods() if opts.list_services: # We just need to print the methods and we're done. for method in methods: print(method) sys.exit(0) # Positional service_method argument validation. if not opts.service_method: parser.error('Must pass "Service/Method".') parts = opts.service_method.split('/') if len(parts) != 2: parser.error( 'Must pass the correct format: (e.g. chromite.api.SdkService/Create).' 'Use --list-methods to see a full list.') if opts.service_method not in methods: # Unknown method, try to match against known methods and make a suggestion. # This is just for developer sanity, e.g. misspellings when testing. matched = matching.GetMostLikelyMatchedObject( methods, opts.service_method, matched_score_threshold=0.6) error = 'Unrecognized service name.' if matched: error += '\nDid you mean: \n%s' % '\n'.join(matched) parser.error(error) opts.service = parts[0] opts.method = parts[1] # --input-json and --output-json validation. if not opts.input_json or not opts.output_json: parser.error('--input-json and --output-json are both required.') if not os.path.exists(opts.input_json): parser.error('Input file does not exist.') # Build the config object from the options. opts.config = api_config_lib.ApiConfig(validate_only=opts.validate_only, mock_call=opts.mock_call, mock_error=opts.mock_error) opts.Freeze() return opts