def test_addCommand_whenMultipleCommandsAndQueryParams_shouldReturnAppropriateMatchingQuery( self) -> None: store = SqlCommandStore(':memory:') self.assertEqual(0, store.get_num_commands()) command_str1 = "some command string" directory_path1 = 'directory/path' command1 = Command(command_str1, last_used=1, directory_context=directory_path1) command_str2 = "some different command" command_str3 = "different command" command2 = Command(command_str2, last_used=2, directory_context=directory_path1) command3 = Command(command_str3, last_used=3, directory_context=directory_path1) store.add_command(command1) store.add_command(command2) store.add_command(command3) commands = store.get_command_with_context(directory_path1, ['different']) self.assertEqual(2, len(commands)) result_command = commands[0] # Newer on first self.assertEqual(command3.get_unique_command_id(), result_command.get_unique_command_id())
def test_store_add_whenCommandHasContext_shouldInsertWithContext( self) -> None: command_store = SqlCommandStore(':memory:') self.assertEqual(0, command_store.get_num_commands()) command_str = "some command string" context_path = 'some/directory/context' command1 = Command(command_str, 10.0, 1, 'command info', context_path) command_str2 = "some second command" command2 = Command(command_str2, 10.0, 1, 'command info', context_path) command_store.add_command(command1) command_store.add_command(command2) self.assertEqual(2, command_store.get_num_commands())
def test_addCommandToSqlStore_whenAddingCommand_shouldBeInStore( self) -> None: command_store = SqlCommandStore(':memory:') self.assertEqual(0, command_store.get_num_commands()) command_str = "some command string" directory_path = 'directory/path' command = Command(command_str, directory_context=directory_path) command_store.add_command(command) self.assertTrue(command_store.has_command(command)) self.assertFalse( command_store.has_command(Command("some other command"))) self.assertEqual(1, command_store.get_num_commands())
def test_rerank_whenMoreTermsInLater_shouldReorderCommands(self) -> None: command_str = 'one two three' c1 = Command(command_str) command_str = 'one match only' c2 = Command(command_str) command_str = 'one two matches in this' c3 = Command(command_str) command_str = 'two matches in this one also' c4 = Command(command_str) matches = [c3, c2, c4, c1] reranked_result = _rerank_matches(matches, ['one', 'two', 'three']) expected = [c1, c3, c4, c2] self.assertListEqual(expected, reranked_result)
def test_search_commands_whenHasContext_shouldCorrectlyAddContext( self) -> None: store = SqlCommandStore(':memory:') store.add_command( Command(command_str='Add', directory_context='directory/path')) matches = store.search_commands(["Add"]) self.assertEqual(1, len(matches))
def print_command(index: int, command: Command, highlighted_terms: Optional[List[str]] = None) -> None: """Pretty print a single command.""" if highlighted_terms is None: highlighted_terms = [] command_str = command.get_unique_command_id() info_str = command.get_command_info() for term in highlighted_terms: command_str = _highlight_term_in_string(command_str, term) info_str = _highlight_term_in_string(info_str, term) print(_create_indexed_highlighted_print_string(index, command_str, command)) if info_str: print(BColors.FAIL + "Command context/info: " + info_str + BColors.ENDC)
def test_search_commands_whenTermIsDifferentCase_shouldNotReturn( self) -> None: store = SqlCommandStore(':memory:') store.add_command(Command('Add')) matches = store.search_commands(["add"]) self.assertEqual(0, len(matches)) matches = store.search_commands(["Add"]) self.assertEqual(1, len(matches))
def test_add_commands_whenCommandAdded2Time_shouldReflectInCount( self) -> None: command_store = SqlCommandStore(':memory:') self.assertEqual(0, command_store.get_num_commands()) command_str = "some command string" context_path = 'some/directory/context' context_path2 = 'notqueried/dir' command1 = Command(command_str, 11.0, 1, 'command info 1', context_path) command_str2 = "some second command" command2 = Command(command_str2, 12.0, 2, 'command info 2', context_path2) command_store.add_command(command1) command_store.add_command(command2) results = command_store.get_command_with_context(context_path, []) self.assertEqual(2, command_store.get_num_commands()) self.assertEqual(1, len(results)) self.assertEqual(context_path, results[0].get_directory_context())
def test_addCommand_whenSameCommandButContextChanges_shouldReturnAppropriateCountof3( self) -> None: store = SqlCommandStore(':memory:') self.assertEqual(0, store.get_num_commands()) command_str = "some command string" directory_path = 'directory/path' command = Command(command_str, directory_context=directory_path) command_diff_context = Command(command_str, directory_context='differnt/context') store.add_command(command) store.add_command(command) store.add_command(command) store.add_command(command_diff_context) self.assertTrue(store.has_command(command)) self.assertEqual(1, store.get_num_commands()) commands = store.get_command_with_context(directory_path, []) self.assertEqual(1, len(commands)) result_command = commands[0] self.assertEqual(3, result_command.get_count_seen())
def test_get_commands_from_context_whenContextQueried_shouldReturn2Commands( self) -> None: command_store = SqlCommandStore(':memory:') self.assertEqual(0, command_store.get_num_commands()) command_str = "some command string" context_path = 'some/directory/context' command1 = Command(command_str, 11.0, 1, 'command info 1', context_path) command_str2 = "some second command" command2 = Command(command_str2, 12.0, 2, 'command info 2', context_path) command_store.add_command(command1) command_store.add_command(command2) results = command_store.get_command_with_context(context_path, []) self.assertEqual(2, command_store.get_num_commands()) self.assertEqual(2, len(results)) self.assertEqual(command_str2, results[0].get_unique_command_id()) self.assertEqual(command_str, results[1].get_unique_command_id()) self.assertEqual(context_path, results[0].get_directory_context()) self.assertEqual(context_path, results[1].get_directory_context())
def test_addCommand_when2Commands_shouldReturnAppropriateTimeOrder( self) -> None: store = SqlCommandStore(':memory:') self.assertEqual(0, store.get_num_commands()) command_str1 = "some command string" directory_path1 = 'directory/path' command1 = Command(command_str1, last_used=1, directory_context=directory_path1) command_str2 = "some different command" command2 = Command(command_str2, last_used=2, directory_context=directory_path1) store.add_command(command1) store.add_command(command2) commands = store.get_command_with_context(directory_path1, []) self.assertEqual(2, len(commands)) result_command = commands[0] # Newer on first self.assertEqual(command2.get_unique_command_id(), result_command.get_unique_command_id())
def test_addCommand_whenCommandsDeleted_shouldNotShowupInResultsforDirSearch( self) -> None: store = SqlCommandStore(':memory:') self.assertEqual(0, store.get_num_commands()) command_str1 = "some command string" directory_path1 = 'directory/path' command1 = Command(command_str1, last_used=1, directory_context=directory_path1) command_str2 = "some different command" command2 = Command(command_str2, last_used=2, directory_context=directory_path1) store.add_command(command1) store.add_command(command2) commands = store.get_command_with_context(directory_path1, []) self.assertEqual(2, len(commands)) store.delete_command(command_str1) commands = store.get_command_with_context(directory_path1, []) self.assertEqual(1, len(commands)) self.assertEqual(commands[0].get_unique_command_id(), command2.get_unique_command_id()) store.add_command(command1) store.delete_command(command_str2) commands = store.get_command_with_context(directory_path1, []) self.assertEqual(1, len(commands)) self.assertEqual(commands[0].get_unique_command_id(), command1.get_unique_command_id()) self.assertEqual(commands[0].get_count_seen(), 1)
def process_history_commands(store: SqlCommandStore, commands: List[CommandAndContext], ignore_file: Optional[str] = None) -> None: """Process the commands from the history file.""" output = [] if ignore_file: ignore_rules = create_ignore_rule(ignore_file) else: ignore_rules = IgnoreRules() # get the max count current_time = time.time() for command_and_context in commands: current_time += 1 command = Command( command_str=command_and_context.command_line(), last_used=current_time, directory_context=command_and_context.directory_context()) if ignore_rules.is_match(command.get_unique_command_id()): continue store.add_command(command) output.append(command.get_unique_command_id())
def test_add_command_whenSameContextAddedTwice_shouldUpdateTheEntryCount( self) -> None: command_store = SqlCommandStore(':memory:') self.assertEqual(0, command_store.get_num_commands()) command_str = "some command string" context_path = 'some/directory/context' command1 = Command(command_str, 11.0, 1, 'command info 1', context_path) command_store.add_command(command1) command_store.add_command(command1) results = command_store.get_command_with_context(context_path, []) self.assertEqual(1, command_store.get_num_commands()) self.assertEqual(1, len(results)) self.assertEqual(context_path, results[0].get_directory_context())
def test_search_commands_sorted(self) -> None: command_store = SqlCommandStore(':memory:') self.assertEqual(0, command_store.get_num_commands()) command_str = "some command string" command = Command(command_str, 10.0, 1) command_store.add_command(command) command_str2 = "somelater command string" command2 = Command(command_str2, 20.0, 1) command_store.add_command(command2) result = command_store.search_commands(["some"], starts_with=False, sort=True) self.assertEqual(result[0].get_unique_command_id(), command2.get_unique_command_id()) self.assertEqual(result[1].get_unique_command_id(), command.get_unique_command_id())
def test_Command_whenCommandStringIsDot_shouldParseCorrectlyAndNotCrash( self) -> None: command_str = '.' c1 = Command(command_str) c1.get_command_args()