def test_when_generate_from_args_should_call_into_command_store_lib( self, mock_read_file: Mock) -> None: history_file_path = 'some/path' return_result_list = ['1', '2'] store = command_store_lib.SqlCommandStore() with patch('remember.command_store_lib.get_string_file_lines', return_value=return_result_list): with patch('remember.command_store_lib.load_command_store', return_value=store) : command_store_lib.generate_store_from_args(history_file_path, TEST_FILES_PATH) mock_read_file.assert_called_once_with()
def test_delete_sql_whenExists_shouldDeleteFromStore(self): file_name = os.path.join(TEST_FILES_PATH, "test_input.txt") self.assertTrue(os.path.isfile(file_name)) store = command_store_lib.SqlCommandStore(':memory:') command_store_lib.read_history_file(store, file_name, "doesntmatter", None, False) self.assertTrue(store.has_command_by_name("vim somefile.txt")) self.assertIsNotNone(store.delete_command('vim somefile.txt')) self.assertFalse(store.has_command_by_name("vim somefile.txt"))
def test_delete_sql_whenExists_shouldDeleteFromStore(self) -> None: file_name = os.path.join(TEST_FILES_PATH, "test_input.txt") self.assertTrue(os.path.isfile(file_name)) store = command_store_lib.SqlCommandStore(':memory:') history_processor = command_store_lib.HistoryProcessor(store, file_name, '', 1) history_processor.process_history_file() self.assertTrue(store.has_command_by_name("vim somefile.txt")) self.assertIsNotNone(store.delete_command('vim somefile.txt')) self.assertFalse(store.has_command_by_name("vim somefile.txt"))
def test_HistoryProcessor_when_process_history_fileOnProcessedFile_shouldNotRun(self) -> None: file_name = os.path.join(TEST_FILES_PATH, "test_processed.txt") with open(file_name, 'rb') as hist_file: hist_file_content = hist_file.read() store = command_store_lib.SqlCommandStore(':memory:') with patch('remember.command_store_lib.open', mock_open(read_data=hist_file_content)) as m: command_store_lib.start_history_processing(store, file_name, 'doesntmatter', 10) handle = m() handle.write.assert_not_called()
def test_delete_command_from_store_when_no_should_not_delete(self) -> None: store = command_store_lib.SqlCommandStore(':memory:') command = Command("testing delete this command") store.add_command(command) self.assertEqual(store.get_num_commands(), 1) user_input = ['1', 'n'] with patch('builtins.input', side_effect=user_input): self.assertFalse(InteractiveCommandExecutor.delete_interaction(store, [command])) self.assertEqual(store.get_num_commands(), 1)
def test_readFile_withIgnoreFile(self) -> None: file_name = os.path.join(TEST_FILES_PATH, "test_input.txt") store = command_store_lib.SqlCommandStore(':memory:') history_processor = command_store_lib.HistoryProcessor(store, file_name, TEST_FILES_PATH, 1) history_processor.process_history_file() self.assertFalse(store.has_command_by_name("vim somefile.txt")) self.assertTrue(store.has_command_by_name("rm somefile.txt")) self.assertTrue(store.has_command_by_name("whereis script")) self.assertFalse(store.has_command_by_name("vim /usr/bin/script")) self.assertFalse(store.has_command_by_name("vim somefil"))
def test_update_command_whenAddCommandToStore_shouldSetBackingStore(self) -> None: store = command_store_lib.SqlCommandStore() command = command_store_lib.Command('Some command') store.add_command(command) command_info_str = 'Some Info' command.set_command_info(command_info_str) store.update_command_info(command) result = store.search_commands(['Some'])[0] self.assertEqual(command.get_unique_command_id(), result.get_unique_command_id()) self.assertEqual(command_info_str, result.get_command_info())
def test_command_update_info_should_correctly_set_info(self) -> None: interactive_command = InteractiveCommandExecutor() store = command_store_lib.SqlCommandStore() command = Command("git rest --hard HEAD") command_info = 'command info' user_input = ['1', command_info] store.add_command(command) with patch('builtins.input', side_effect=user_input): interactive_command.command_info_interaction([command], store) self.assertEqual(command.get_command_info(), command_info) self.assertEqual(command_info, store.search_commands(['git'])[0].get_command_info())
def test_addCommandToSqlStore(self): command_store = command_store_lib.SqlCommandStore(':memory:') self.assertEqual(0, command_store.get_num_commands()) command_str = "some command string" command = command_store_lib.Command(command_str) command_store.add_command(command) self.assertTrue(command_store.has_command(command)) self.assertFalse( command_store.has_command( command_store_lib.Command("some other command"))) self.assertEqual(1, command_store.get_num_commands())
def test_delete_command_from_store_with_both_invalid_should_remove_0(self) -> None: store = command_store_lib.SqlCommandStore(':memory:') command = Command("testing delete this command") command2 = Command("remove this also") self.assertEqual(store.get_num_commands(), 0) store.add_command(command) store.add_command(command2) self.assertEqual(store.get_num_commands(), 2) user_input = ['8, 9'] with patch('builtins.input', side_effect=user_input): self.assertFalse(InteractiveCommandExecutor.delete_interaction(store, [command, command2])) self.assertEqual(store.get_num_commands(), 2)
def test_search_commands_with_sqlstore(self): file_name = os.path.join(TEST_FILES_PATH, "test_input.txt") store = command_store_lib.SqlCommandStore(':memory:') command_store_lib.read_history_file(store, file_name, "doesntmatter", None, False) 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) store.close()
def test_sqlCommandStore_whenAddingItem_shouldReturnTrueWhenSearching(self) -> None: file_path = '' try: file_path = os.path.join(TEST_PATH_DIR, "delete_test_sql_store.db") command_store = command_store_lib.SqlCommandStore(file_path) command_str = "git branch" command = command_store_lib.Command(command_str) command_store.add_command(command) command_store = command_store_lib.load_command_store(file_path) self.assertTrue(command_store.has_command(command)) finally: if file_path: os.remove(file_path)
def test_verifySqldb_shouldSaveAndStore_whenCommandIsAdded(self) -> None: file_name = '' try: file_name = os.path.join(TEST_PATH_DIR, "delete_test_pickle.db") command_store = command_store_lib.SqlCommandStore(file_name) command_str = "git branch" command = command_store_lib.Command(command_str) command_store.add_command(command) command_store = command_store_lib.load_command_store(file_name) self.assertTrue(command_store.has_command(command)) finally: if file_name: os.remove(file_name)
def test_run_when_command_is_executed(self, mock_subproc) -> None: store = command_store_lib.SqlCommandStore(':memory:') command_str = "testing delete this command" command = Command(command_str) command2 = Command("remove this also") self.assertEqual(store.get_num_commands(), 0) store.add_command(command) store.add_command(command2) self.assertEqual(store.get_num_commands(), 2) interactive_command = InteractiveCommandExecutor() user_input = ['1'] with patch('builtins.input', side_effect=user_input): self.assertTrue(interactive_command.run([command, command2])) shell_env = os.getenv('SHELL') call_args = [shell_env, '-i', '-c', command_str] mock_subproc.assert_called_once_with(call_args)
def test_start_history_processing_whenProcessCustomHistoryFile_shouldCorrectlyAddToStore(self) -> None: file_name = os.path.join(TEST_FILES_PATH, "custom_history_file.txt") with open(file_name, 'rb') as hist_file: hist_file_content = hist_file.read() store = command_store_lib.SqlCommandStore(':memory:') with patch('remember.command_store_lib.open', mock_open(read_data=hist_file_content)) as m: command_store_lib.start_history_processing(store, file_name, 'doesntmatter', 1) handle = m() handle.write.assert_called_with(f'{command_store_lib.CUSTOM_HIST_HEAD}') 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(["vim"], True) self.assertTrue(len(matches) == 1)
def test_SqlCommandStore_isEmpty(self): command_store = command_store_lib.SqlCommandStore(':memory:') self.assertEqual(0, command_store.get_num_commands())
def test_deleteSql_whenDoesntExists_shouldDeleteFromStore(self): store = command_store_lib.SqlCommandStore(':memory:') self.assertIsNone(store.delete_command('anything'))
def test_delete_whenDoesntExists_shouldReturnNone(self) -> None: store = command_store_lib.SqlCommandStore(':memory:') self.assertIsNone(store.delete_command('anything'))
def test_ignoreRule_whenFileDoestExist_shouldNotCrash(self) -> None: file_name = os.path.join(TEST_FILES_PATH, "test_input.txt") store = command_store_lib.SqlCommandStore() processor = command_store_lib.HistoryProcessor(store, file_name, 'not there') processor.process_history_file()