pubmed-client 0.1.0

An async Rust client for PubMed and PMC APIs for retrieving biomedical research articles
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Comprehensive PMC (PubMed Central) API integration tests
//!
//! These tests make actual network calls to PMC APIs to verify
//! real-world functionality and catch API changes.
//!
//! **IMPORTANT**: These tests are only run when:
//! 1. The `integration-tests` feature is enabled
//! 2. The `PUBMED_REAL_API_TESTS` environment variable is set
//!
//! To run these tests:
//! ```bash
//! PUBMED_REAL_API_TESTS=1 cargo test --features integration-tests --test pmc_api_tests
//! ```
//!
//! With API key for higher rate limits:
//! ```bash
//! PUBMED_REAL_API_TESTS=1 NCBI_API_KEY=your_key cargo test --features integration-tests --test pmc_api_tests
//! ```

mod common;

#[cfg(feature = "integration-tests")]
mod integration_tests {
    use std::time::{Duration, Instant};
    use tokio::time::sleep;
    use tracing::{debug, info, warn};
    use tracing_test::traced_test;

    use pubmed_client::pmc::markdown::PmcMarkdownConverter;

    // Import test utilities
    use crate::common::integration_test_utils::{
        create_test_pmc_client, create_test_pubmed_client, should_run_real_api_tests, TEST_PMCIDS,
    };

    /// Test fetching PMC full-text articles
    #[tokio::test]
    #[traced_test]
    async fn test_fetch_pmc_fulltext_integration() {
        if !should_run_real_api_tests() {
            info!(
                "Skipping real API test - enable with PUBMED_REAL_API_TESTS=1 and --features integration-tests"
            );
            return;
        }

        info!("Testing PMC full-text fetching integration");

        let client = create_test_pmc_client();

        for pmcid in TEST_PMCIDS.iter().take(3) {
            info!(pmcid = pmcid, "Testing PMC full-text fetch");

            let start_time = Instant::now();
            match client.fetch_full_text(pmcid).await {
                Ok(fulltext) => {
                    let duration = start_time.elapsed();
                    // Count figures and tables across all sections
                    let mut total_figures = 0;
                    let mut total_tables = 0;
                    let mut has_abstract = false;

                    for section in &fulltext.sections {
                        total_figures += section.figures.len();
                        total_tables += section.tables.len();
                        if section.section_type == "abstract" {
                            has_abstract = true;
                        }
                    }

                    info!(
                        pmcid = pmcid,
                        title_length = fulltext.title.len(),
                        authors_count = fulltext.authors.len(),
                        sections_count = fulltext.sections.len(),
                        figures_count = total_figures,
                        tables_count = total_tables,
                        has_abstract = has_abstract,
                        duration_ms = duration.as_millis(),
                        "PMC full-text fetch successful"
                    );

                    // Verify required fields
                    assert!(!fulltext.title.is_empty(), "PMC article should have title");
                    assert!(
                        !fulltext.authors.is_empty(),
                        "PMC article should have authors"
                    );
                    assert!(
                        !fulltext.sections.is_empty(),
                        "PMC article should have sections"
                    );

                    // Verify PMCID format
                    assert!(pmcid.starts_with("PMC"), "PMCID should start with PMC");

                    // Verify section structure
                    for (i, section) in fulltext.sections.iter().take(3).enumerate() {
                        if let Some(ref title) = section.title {
                            assert!(!title.is_empty(), "Section title should not be empty");
                        }
                        assert!(!section.content.is_empty(), "Section should have content");
                        debug!(
                            section_index = i,
                            section_title = ?section.title,
                            section_type = %section.section_type,
                            content_length = section.content.len(),
                            "Section details"
                        );
                    }

                    // Log title preview for verification
                    let title_preview = if fulltext.title.len() > 100 {
                        let preview = &fulltext.title[..100];
                        format!("{preview}...")
                    } else {
                        fulltext.title.clone()
                    };
                    debug!(pmcid = pmcid, title_preview = %title_preview, "PMC article title");
                }
                Err(e) => {
                    warn!(pmcid = pmcid, error = %e, "PMC full-text fetch failed");
                    // Don't panic here as some PMCIDs might not be available
                    info!("PMC article not available (expected for some PMCIDs)");
                }
            }

            sleep(Duration::from_millis(200)).await;
        }
    }

    /// Test PMC article conversion to Markdown
    #[tokio::test]
    #[traced_test]
    async fn test_pmc_to_markdown_integration() {
        if !should_run_real_api_tests() {
            info!(
                "Skipping real API test - enable with PUBMED_REAL_API_TESTS=1 and --features integration-tests"
            );
            return;
        }

        info!("Testing PMC to Markdown conversion integration");

        let client = create_test_pmc_client();

        // Try to find an available PMC article
        for pmcid in TEST_PMCIDS.iter().take(3) {
            info!(pmcid = pmcid, "Testing PMC to Markdown conversion");

            match client.fetch_full_text(pmcid).await {
                Ok(fulltext) => {
                    info!(
                        pmcid = pmcid,
                        "Successfully fetched PMC article for Markdown test"
                    );

                    // Convert to Markdown
                    let converter = PmcMarkdownConverter::new();
                    let markdown = converter.convert(&fulltext);

                    info!(
                        pmcid = pmcid,
                        markdown_length = markdown.len(),
                        "Markdown conversion successful"
                    );

                    // Verify Markdown structure
                    assert!(!markdown.is_empty(), "Markdown should not be empty");
                    assert!(markdown.contains("#"), "Markdown should contain headers");

                    // Should contain title
                    assert!(
                        markdown.contains(&fulltext.title)
                            || markdown.contains(&fulltext.title.replace(" ", "")),
                        "Markdown should contain article title"
                    );

                    // Should contain author information
                    if !fulltext.authors.is_empty() {
                        let first_author = &fulltext.authors[0];
                        let author_name = format!(
                            "{} {}",
                            first_author.given_names.as_deref().unwrap_or(""),
                            first_author.surname.as_deref().unwrap_or("")
                        );
                        assert!(
                            markdown.contains(author_name.trim())
                                || markdown.contains(first_author.surname.as_deref().unwrap_or(""))
                                || markdown.contains(&first_author.full_name),
                            "Markdown should contain author information"
                        );
                    }

                    // Should contain section content
                    if !fulltext.sections.is_empty() {
                        let first_section = &fulltext.sections[0];
                        let title_check = if let Some(ref title) = first_section.title {
                            markdown.contains(title)
                        } else {
                            true // Skip title check if no title
                        };

                        let content_preview =
                            &first_section.content[..50.min(first_section.content.len())];
                        assert!(
                            title_check || markdown.contains(content_preview),
                            "Markdown should contain section content"
                        );
                    }

                    debug!(
                        pmcid = pmcid,
                        markdown_preview = &markdown[..200.min(markdown.len())],
                        "Markdown preview"
                    );

                    // Only test one successful conversion
                    return;
                }
                Err(e) => {
                    debug!(pmcid = pmcid, error = %e, "PMC article not available, trying next");
                }
            }

            sleep(Duration::from_millis(200)).await;
        }

        warn!("No PMC articles were available for Markdown conversion test");
    }

    /// Test PMC article structure and content validation
    #[tokio::test]
    #[traced_test]
    async fn test_pmc_content_structure_integration() {
        if !should_run_real_api_tests() {
            info!(
                "Skipping real API test - enable with PUBMED_REAL_API_TESTS=1 and --features integration-tests"
            );
            return;
        }

        info!("Testing PMC content structure integration");

        let client = create_test_pmc_client();

        // Find an available PMC article with rich content
        for pmcid in TEST_PMCIDS.iter().take(5) {
            match client.fetch_full_text(pmcid).await {
                Ok(fulltext) => {
                    info!(
                        pmcid = pmcid,
                        "Found available PMC article for structure validation"
                    );

                    // Test author structure
                    if !fulltext.authors.is_empty() {
                        let author = &fulltext.authors[0];
                        assert!(!author.full_name.is_empty(), "Author should have name");

                        debug!(
                            first_author_given_names = ?author.given_names,
                            first_author_surname = ?author.surname,
                            first_author_full_name = %author.full_name,
                            affiliations = ?author.affiliations,
                            "First author details"
                        );
                    }

                    // Test section structure
                    let mut introduction_found = false;
                    let mut methods_found = false;
                    let mut results_found = false;
                    let mut discussion_found = false;

                    for section in &fulltext.sections {
                        let section_type_lower = section.section_type.to_lowercase();
                        let title_lower = section
                            .title
                            .as_ref()
                            .map(|t| t.to_lowercase())
                            .unwrap_or_default();

                        if section_type_lower.contains("introduction")
                            || title_lower.contains("introduction")
                            || section_type_lower.contains("background")
                            || title_lower.contains("background")
                        {
                            introduction_found = true;
                        } else if section_type_lower.contains("method")
                            || title_lower.contains("method")
                            || section_type_lower.contains("material")
                            || title_lower.contains("material")
                        {
                            methods_found = true;
                        } else if section_type_lower.contains("result")
                            || title_lower.contains("result")
                            || section_type_lower.contains("finding")
                            || title_lower.contains("finding")
                        {
                            results_found = true;
                        } else if section_type_lower.contains("discussion")
                            || title_lower.contains("discussion")
                            || section_type_lower.contains("conclusion")
                            || title_lower.contains("conclusion")
                        {
                            discussion_found = true;
                        }

                        // Verify section content is not empty
                        assert!(
                            !section.content.is_empty(),
                            "Section content should not be empty"
                        );
                        assert!(
                            section.content.len() > 10,
                            "Section content should be substantial"
                        );
                    }

                    info!(
                        pmcid = pmcid,
                        sections_count = fulltext.sections.len(),
                        introduction_found = introduction_found,
                        methods_found = methods_found,
                        results_found = results_found,
                        discussion_found = discussion_found,
                        "PMC article structure analysis"
                    );

                    // Test figures and tables from sections
                    let mut total_figures = 0;
                    let mut total_tables = 0;

                    for section in &fulltext.sections {
                        total_figures += section.figures.len();
                        total_tables += section.tables.len();

                        if !section.figures.is_empty() {
                            let figure = &section.figures[0];
                            assert!(!figure.caption.is_empty(), "Figure should have caption");

                            debug!(
                                figure_id = %figure.id,
                                caption_length = figure.caption.len(),
                                "Figure details"
                            );
                        }

                        if !section.tables.is_empty() {
                            let table = &section.tables[0];
                            assert!(!table.caption.is_empty(), "Table should have caption");

                            debug!(
                                table_id = %table.id,
                                caption_length = table.caption.len(),
                                "Table details"
                            );
                        }
                    }

                    debug!(
                        total_figures = total_figures,
                        total_tables = total_tables,
                        "Total figures and tables across all sections"
                    );

                    // Only analyze one successful article
                    return;
                }
                Err(e) => {
                    debug!(pmcid = pmcid, error = %e, "PMC article not available, trying next");
                }
            }

            sleep(Duration::from_millis(200)).await;
        }

        warn!("No PMC articles were available for content structure test");
    }

    /// Test PMC API error handling
    #[tokio::test]
    #[traced_test]
    async fn test_pmc_error_handling_integration() {
        if !should_run_real_api_tests() {
            info!(
                "Skipping real API test - enable with PUBMED_REAL_API_TESTS=1 and --features integration-tests"
            );
            return;
        }

        info!("Testing PMC error handling integration");

        let client = create_test_pmc_client();

        // Test with invalid PMCID
        let invalid_pmcids = [
            "PMC999999999", // Very high number, likely doesn't exist
            "PMC0",         // Invalid format
            "PMC00000001",  // Invalid format with leading zeros
        ];

        for invalid_pmcid in &invalid_pmcids {
            info!(invalid_pmcid = invalid_pmcid, "Testing invalid PMCID");

            match client.fetch_full_text(invalid_pmcid).await {
                Ok(_) => {
                    warn!(
                        invalid_pmcid = invalid_pmcid,
                        "Unexpected success with invalid PMCID"
                    );
                    // This might happen if the "invalid" PMCID actually exists
                }
                Err(e) => {
                    info!(
                        invalid_pmcid = invalid_pmcid,
                        error = %e,
                        "Expected error for invalid PMCID"
                    );

                    // Verify error message is descriptive
                    let error_str = e.to_string();
                    assert!(!error_str.is_empty(), "Error message should not be empty");
                }
            }

            sleep(Duration::from_millis(100)).await;
        }
    }

    /// Test PMC rate limiting compliance
    #[tokio::test]
    #[traced_test]
    async fn test_pmc_rate_limiting_integration() {
        if !should_run_real_api_tests() {
            info!(
                "Skipping real API test - enable with PUBMED_REAL_API_TESTS=1 and --features integration-tests"
            );
            return;
        }

        info!("Testing PMC rate limiting integration");

        let client = create_test_pmc_client();
        let start_time = Instant::now();

        // Make several requests to test rate limiting
        let mut successful_requests = 0;
        let mut failed_requests = 0;

        for (i, pmcid) in TEST_PMCIDS.iter().enumerate().take(5) {
            info!(
                request_number = i + 1,
                pmcid = pmcid,
                "Making PMC request for rate limiting test"
            );

            let request_start = Instant::now();
            match client.fetch_full_text(pmcid).await {
                Ok(_) => {
                    successful_requests += 1;
                    let request_duration = request_start.elapsed();
                    debug!(
                        pmcid = pmcid,
                        request_duration_ms = request_duration.as_millis(),
                        "PMC request successful"
                    );
                }
                Err(e) => {
                    failed_requests += 1;
                    debug!(
                        pmcid = pmcid,
                        error = %e,
                        "PMC request failed (may be unavailable)"
                    );
                }
            }
        }

        let total_duration = start_time.elapsed();

        info!(
            total_requests = TEST_PMCIDS.len().min(5),
            successful_requests = successful_requests,
            failed_requests = failed_requests,
            total_duration_ms = total_duration.as_millis(),
            "PMC rate limiting test completed"
        );

        // Verify rate limiting is working (should take reasonable time)
        let min_expected_duration = Duration::from_millis(1500); // At least 1.5 seconds for 5 requests
        assert!(
            total_duration >= min_expected_duration,
            "PMC requests completed too quickly - rate limiting may not be working"
        );

        // At least some requests should work (even if articles are unavailable)
        // The important thing is that the rate limiting and API calls function
        info!("PMC rate limiting test completed - verified proper timing behavior");
    }

    /// Test PMC combined workflow with search and fetch
    #[tokio::test]
    #[traced_test]
    async fn test_pmc_combined_workflow_integration() {
        if !should_run_real_api_tests() {
            info!(
                "Skipping real API test - enable with PUBMED_REAL_API_TESTS=1 and --features integration-tests"
            );
            return;
        }

        info!("Testing PMC combined workflow integration");

        // Create both PubMed and PMC clients for the workflow
        let pubmed_client = create_test_pubmed_client();
        let pmc_client = create_test_pmc_client();

        // Step 1: Search for articles that might have PMC full text
        let search_query = "open access[Filter] AND COVID-19[Title] AND 2023[PDAT]";

        info!(
            query = search_query,
            "Step 1: Searching for open access articles"
        );

        let pmids = match pubmed_client.search_articles(search_query, 10, None).await {
            Ok(pmids) => {
                info!(results_count = pmids.len(), "Search completed");
                pmids
            }
            Err(e) => {
                warn!(error = %e, "Search failed");
                return; // Skip rest of test
            }
        };

        if pmids.is_empty() {
            info!("No articles found, ending workflow test");
            return;
        }

        // Convert PMIDs to u32 for ELink API
        let pmids_u32: Vec<u32> = pmids
            .iter()
            .filter_map(|pmid| pmid.parse::<u32>().ok())
            .collect();

        if pmids_u32.is_empty() {
            info!("No valid PMIDs found for ELink, ending workflow test");
            return;
        }

        // Step 2: Get PMC links for the found articles
        info!("Step 2: Getting PMC links for found articles");

        let pmc_links = match pubmed_client
            .get_pmc_links(&pmids_u32[..5.min(pmids_u32.len())])
            .await
        {
            Ok(links) => {
                info!(
                    pmc_articles_count = links.pmc_ids.len(),
                    "PMC links retrieved"
                );
                links
            }
            Err(e) => {
                warn!(error = %e, "Failed to get PMC links");
                return;
            }
        };

        if pmc_links.pmc_ids.is_empty() {
            info!("No PMC full-text available for searched articles");
            return;
        }

        // Step 3: Attempt to fetch PMC full text
        info!("Step 3: Fetching PMC full text");

        let mut pmc_articles_fetched = 0;

        for (i, _pmc_id) in pmc_links.pmc_ids.iter().take(2).enumerate() {
            // Convert PMID to PMCID format - this is a simplification
            // In reality, you'd need proper PMID->PMCID mapping
            info!(attempt = i + 1, "Attempting to find PMC article");

            // For this test, we'll try our known working PMCIDs
            if let Some(pmcid) = TEST_PMCIDS.get(i) {
                match pmc_client.fetch_full_text(pmcid).await {
                    Ok(fulltext) => {
                        pmc_articles_fetched += 1;
                        info!(
                            pmcid = pmcid,
                            title_length = fulltext.title.len(),
                            sections_count = fulltext.sections.len(),
                            "PMC full text retrieved successfully"
                        );

                        // Verify we can convert to markdown
                        let converter = PmcMarkdownConverter::new();
                        let markdown = converter.convert(&fulltext);
                        assert!(
                            !markdown.is_empty(),
                            "Should be able to convert to markdown"
                        );

                        debug!(
                            pmcid = pmcid,
                            markdown_length = markdown.len(),
                            "Markdown conversion successful"
                        );
                    }
                    Err(e) => {
                        debug!(pmcid = pmcid, error = %e, "PMC article not available");
                    }
                }
            }

            sleep(Duration::from_millis(200)).await;
        }

        info!(
            workflow_summary = "completed",
            pmids_found = pmids.len(),
            pmc_links_found = pmc_links.pmc_ids.len(),
            pmc_articles_fetched = pmc_articles_fetched,
            "Combined workflow test completed"
        );

        // The workflow is successful if we can execute all steps without errors
        // Even if no full text is available, the API integration is working
        assert!(!pmids.is_empty(), "Should find some articles in search");
    }
}

// Placeholder module for non-integration builds
#[cfg(not(feature = "integration-tests"))]
mod placeholder {
    //! Integration tests are only available with the `integration-tests` feature.
    //!
    //! To run these tests:
    //! ```bash
    //! cargo test --features integration-tests --test pmc_api_tests
    //! ```
}