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 test_search_commands_with_sqlstore(self) -> None: file_name = os.path.join(TEST_FILES_PATH, "test_input.txt") store = SqlCommandStore(':memory:') history_processor = command_store_lib.HistoryProcessor(store, file_name, '', 1) history_processor.process_history_file() matches = store.search_commands(["add"], search_info=True) self.assertIsNotNone(matches) matches = store.search_commands(["add"], True) self.assertTrue(len(matches) == 0) matches = store.search_commands(["subl"], True) self.assertTrue(len(matches) == 1)
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_addCommand_whenSameCommandAndContext_shouldReturnAppropriateCount( 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) store.add_command(command) store.add_command(command) store.add_command(command) store.add_command(command) 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(4, result_command.get_count_seen())
def test_readHistoryFile_whenFileRead_shouldFinishWithWriteEnd(self) -> None: file_name = os.path.join(TEST_FILES_PATH, "test_input.txt") with open(file_name, 'rb') as hist_file: hist_file_content = hist_file.read() store = SqlCommandStore(':memory:') history_processor = command_store_lib.HistoryProcessor(store, file_name, '', 1) with patch('remember.command_store_lib.open', mock_open(read_data=hist_file_content)) as m: history_processor.process_history_file() history_processor.update_history_file() handle = m() handle.write.assert_called_with(f'{command_store_lib.PROCESSED_TO_TAG}\n') matches = store.search_commands(["add"], search_info=True) self.assertIsNotNone(matches) matches = store.search_commands(["add"], True) self.assertTrue(len(matches) == 0) matches = store.search_commands(["subl"], True) self.assertTrue(len(matches) == 1)
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 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_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_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_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_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_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_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_SqlCommandStore_isEmpty(self) -> None: command_store = SqlCommandStore(':memory:') self.assertEqual(0, command_store.get_num_commands())
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 load_command_store(db_file_name: str) -> SqlCommandStore: """Get the sql command store from the input file.""" if not os.path.exists(db_file_name): msg = f'db file: {db_file_name} does not exist. Please run remember_setup.py to create it.' raise Exception(msg) return SqlCommandStore(db_file_name)