def assert_add(screen): """Asserts the add prompt.""" try: assert f"CoBib v{version} - 5 Entries" in screen.display[0] assert "Cao_2019" in screen.display[1] assert "dummy_entry_for_scroll_testing" in screen.display[2] assert "knuthwebsite" in screen.display[3] assert "latexcompanion" in screen.display[4] assert "einstein" in screen.display[5] finally: DeleteCommand().execute(['Cao_2019'])
def setup(): """Setup.""" # ensure configuration is empty CONFIG.config = {} root = os.path.abspath(os.path.dirname(__file__)) CONFIG.set_config(Path(root + '/../cobib/docs/debug.ini')) # NOTE: normally you would never trigger an Add command before reading the database but in this # controlled testing scenario we can be certain that this is fine AddCommand().execute(['-b', './test/dummy_scrolling_entry.bib']) read_database() yield setup DeleteCommand().execute(['dummy_entry_for_scroll_testing'])
def test_event_pre_delete_command(self, setup: Any) -> None: """Tests the PreDeleteCommand event.""" @Event.PreDeleteCommand.subscribe def hook(largs: Namespace) -> None: largs.labels = ["einstein"] assert Event.PreDeleteCommand.validate() DeleteCommand().execute(["knuthwebsite"]) assert "einstein" not in Database().keys() assert "knuthwebsite" in Database().keys()
def test_event_post_delete_command(self, setup: Any) -> None: """Tests the PostDeleteCommand event.""" @Event.PostDeleteCommand.subscribe def hook(deleted_entries: List[str]) -> None: for label in deleted_entries: print(f"WARNING: deleted entry '{label}'") with contextlib.redirect_stdout(StringIO()) as out: DeleteCommand().execute(["knuthwebsite"]) assert Event.PostDeleteCommand.validate() assert out.getvalue().strip() == "WARNING: deleted entry 'knuthwebsite'"
def test_tui_open_menu(): """Test the open prompt menu for multiple associated files.""" # ensure configuration is empty CONFIG.config = {} root = os.path.abspath(os.path.dirname(__file__)) CONFIG.set_config(Path(root + '/../cobib/docs/debug.ini')) # NOTE: normally you would never trigger an Add command before reading the database but in this # controlled testing scenario we can be certain that this is fine AddCommand().execute(['-b', './test/dummy_multi_file_entry.bib']) read_database() try: test_tui(None, 'o', assert_open, {}) finally: DeleteCommand().execute(['dummy_multi_file_entry'])
def test_tui_config_keys(command, key): """Test TUI key binding configuration.""" # ensure configuration is empty CONFIG.config = {} root = os.path.abspath(os.path.dirname(__file__)) CONFIG.set_config(Path(root + '/../cobib/docs/debug.ini')) # overwrite key binding configuration CONFIG.config['KEY_BINDINGS'][command] = key # NOTE: normally you would never trigger an Add command before reading the database but in this # controlled testing scenario we can be certain that this is fine AddCommand().execute(['-b', './test/dummy_scrolling_entry.bib']) read_database() try: test_tui(None, key, assert_show, {}) finally: DeleteCommand().execute(['dummy_entry_for_scroll_testing'])
def test_command(self, setup: Any, labels: List[str], skip_commit: bool) -> None: """Test the command itself. Args: setup: the `tests.commands.command_test.CommandTest.setup` fixture. labels: the list of labels to be deleted. skip_commit: whether to skip asserting the git commit message. """ git = setup.get("git", False) # delete some data (for testing simplicity we delete the entries from the end) DeleteCommand().execute(labels) self._assert(labels) if git and not skip_commit: # assert the git commit message self.assert_git_commit_message("delete", {"labels": labels, "preserve_files": False})
def test_remove_associated_file(self, setup: Any, preserve_files: bool) -> None: """Test removing associated files. Args: setup: the `tests.commands.command_test.CommandTest.setup` fixture. preserve_files: argument to `DeleteCommand`. """ with tempfile.TemporaryDirectory() as tmpdirname: path = RelPath(tmpdirname + "/dummy.pdf") open(path.path, "w", encoding="utf-8").close() # pylint: disable=consider-using-with Database()["knuthwebsite"].file = str(path) args = ["knuthwebsite"] if preserve_files: args += ["--preserve-files"] DeleteCommand().execute(args) assert path.path.exists() is preserve_files
def test_base_cmd_insufficient_git(self, setup: Any, caplog: pytest.LogCaptureFixture) -> None: """Test warning is raised by BaseCommand during insufficient git-configuration. While this is technically not related to the DeleteCommand, this is one of the faster commands to test this on. Args: setup: the `tests.commands.command_test.CommandTest.setup` fixture. caplog: the built-in pytest fixture. """ config.database.git = True DeleteCommand().execute(["knuthwebsite"]) self._assert(["knuthwebsite"]) assert ( "cobib.commands.base_command", 30, "You have configured coBib to track your database with git." "\nPlease run `cobib init --git`, to initialize this tracking.", ) in caplog.record_tuples