diff --git a/CHANGELOG.md b/CHANGELOG.md index a8c5482..5090816 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## 1.1.1 + +### Added + +- Test with empty papers list in `tests/test_telegrm.py` + +### Changed + +- Improve handling of papers list when it is empty + ## 1.1.0 ### Added diff --git a/pyproject.toml b/pyproject.toml index b393d96..e8d1615 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "PaperBee" -version = "1.1.0" +version = "1.1.1" description = "" authors = ["Daniele Lucarelli ", "Vladimir Shitov "] repository = "https://github.com/theislab/paperbee" diff --git a/src/PaperBee/papers/utils.py b/src/PaperBee/papers/utils.py index 1bcc2b1..299b1f1 100644 --- a/src/PaperBee/papers/utils.py +++ b/src/PaperBee/papers/utils.py @@ -43,44 +43,47 @@ def process_articles(self) -> None: def filter_columns(self) -> None: """Filters the DataFrame to include specific columns.""" columns = ["databases", "publication_date", "title", "keywords", "url"] - self.articles = self.articles.loc[:, columns] + + if self.articles.empty: + self.articles = pd.DataFrame(columns=columns) + else: + self.articles = self.articles.loc[:, columns] def extract_doi(self) -> None: """Extracts DOIs from URLs and adds them as a new column.""" - self.articles["DOI"] = self.articles["url"].apply(lambda x: x[x.find("10.") :]) + if not self.articles.empty: + self.articles["DOI"] = self.articles["url"].apply(lambda x: x[x.find("10.") :]) def set_dates(self) -> None: """Sets the publication date and the date of processing.""" - self.articles["Date"] = self.today_str - self.articles["PostedDate"] = self.articles["publication_date"] + if not self.articles.empty: + self.articles["Date"] = self.today_str + self.articles["PostedDate"] = self.articles["publication_date"] def determine_preprint_status(self) -> None: """Determines whether each article is a preprint based on its database.""" - self.articles["IsPreprint"] = self.articles["databases"].apply( - lambda dbs: "FALSE" if "PubMed" in dbs else "TRUE" - ) + if not self.articles.empty: + self.articles["IsPreprint"] = self.articles["databases"].apply( + lambda dbs: "FALSE" if "PubMed" in dbs else "TRUE" + ) def rename_and_process_columns(self) -> None: """Renames columns and processes keywords.""" - self.articles["Title"] = self.articles["title"] - self.articles["Keywords"] = self.articles["keywords"].apply(lambda kws: ", ".join(kw[2:] for kw in kws)) - self.articles["URL"] = self.articles["url"] + if not self.articles.empty: + self.articles["Title"] = self.articles["title"] + self.articles["Keywords"] = self.articles["keywords"].apply(lambda kws: ", ".join(kw[2:] for kw in kws)) + self.articles["URL"] = self.articles["url"] def select_last_columns(self) -> None: """Selects and rearranges the final set of columns for the DataFrame.""" - self.articles["Preprint"] = None # TODO add search for preprint of published articles - self.articles = self.articles[ - [ - "DOI", - "Date", - "PostedDate", - "IsPreprint", - "Title", - "Keywords", - "Preprint", - "URL", - ] - ] + expected_columns = ["DOI", "Date", "PostedDate", "IsPreprint", "Title", "Keywords", "Preprint", "URL"] + if self.articles.empty: + self.articles["Preprint"] = [] + # Create empty DataFrame with expected columns + self.articles = pd.DataFrame(columns=expected_columns) + else: + self.articles["Preprint"] = None # TODO add search for preprint of published articles + self.articles = self.articles[expected_columns] class PubMedClient: diff --git a/tests/test_telegram.py b/tests/test_telegram.py index 77c2e34..4555c2a 100644 --- a/tests/test_telegram.py +++ b/tests/test_telegram.py @@ -67,3 +67,11 @@ async def test_publish_many_papers(publisher, papers): message = await publisher.publish_papers(papers, preprints, today=None, spreadsheet_id=None) assert message.message_id is not None + + +@pytest.mark.asyncio +async def test_publish_empty_list(publisher, papers): + papers, preprints = publisher.format_papers([]) + message = await publisher.publish_papers(papers, preprints, today=None, spreadsheet_id=None) + + assert message.message_id is not None