Skip to content

Commit

Permalink
[fix] Handle empty PMID array (#220)
Browse files Browse the repository at this point in the history
Bug fix to address handling an empty PMID array.
  • Loading branch information
AlexJSully authored Sep 10, 2024
1 parent b09d21e commit 25640e4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function main() {
/** Array of PubMed Central IDs (PMCIDs) for the articles found. */
const pmids = await searchArticlesBySpecies(throttle, species);

if (pmids.length > 0) {
if (pmids?.length > 0) {
await fetchArticleDetails(throttle, pmids, species);
} else {
console.log(`No articles found for the species: ${species}.`);
Expand Down
12 changes: 12 additions & 0 deletions src/processor/fetchArticleDetails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ describe("fetchArticleDetails", () => {

expect(axios.get).toHaveBeenCalledTimes(1); // Should not call axios.get again
});

it("should handle empty PMID array gracefully", async () => {
const emptyPmids: string[] = [];
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation(() => {});

await fetchArticleDetails(throttle, emptyPmids, species);

expect(axios.get).not.toHaveBeenCalled();
expect(consoleLogSpy).toHaveBeenCalledWith("No PMC IDs provided for Homo sapiens.");

consoleLogSpy.mockRestore();
});
});
6 changes: 6 additions & 0 deletions src/processor/fetchArticleDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export async function fetchArticleDetails(
fs.mkdirSync(path.dirname(cachedIDsFilePath), { recursive: true });
}

// Check if PMIDs array is undefined/null or empty
if (!pmids || pmids.length === 0) {
console.log(`No PMC IDs provided for ${species.replace("_", " ")}.`);
return;
}

// Get article details based on PMC IDs
for (let i = 0; i < pmids.length; i += batchSize) {
// Extract a batch of 50 PMC IDs
Expand Down

0 comments on commit 25640e4

Please sign in to comment.