• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dvolgyes / zenodo_get / 21323002247

24 Jan 2026 11:00PM UTC coverage: 90.588% (+0.02%) from 90.566%
21323002247

push

github

dvolgyes
Add Python API documentation to README

Document the download() function for programmatic use, including
installation instructions, usage examples, and key parameters.

1155 of 1275 relevant lines covered (90.59%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.36
/tests/test_api.py
1
"""Test the Python API for zenodo_get download functionality."""
2

3
import os
1✔
4
import shutil
1✔
5
import sys
1✔
6
from pathlib import Path
1✔
7

8
# Assuming zenodo_get is installed or PYTHONPATH is set correctly
9
# to find the zenodo_get package.
10
try:
1✔
11
    from zenodo_get import download
1✔
12
except ImportError:
×
13
    # Fallback for environments where zenodo_get might not be in the default path yet
14
    # This might happen if tests are run before installation in some CI setups.
15
    # Adjust path as necessary if your project structure is different.
16
    sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
×
17
    try:
×
18
        from zenodo_get import download
×
19
    except ImportError:
×
20
        # If it still fails, zget might be the direct module if __init__ is not fully set up
21
        from zenodo_get.zget import download
×
22

23

24
def test_api_download_specific_file() -> None:
1✔
25
    """Verify API can download files matching specific glob patterns.
26

27
    This test verifies that:
28
    1. Files matching the glob pattern are downloaded
29
    2. Files NOT matching the glob pattern are NOT downloaded (issue #39)
30

31
    The Zenodo record 1215979 contains 6 files:
32
    - fetch_data.py (should be downloaded with *.py glob)
33
    - 5 JSON files (should NOT be downloaded with *.py glob)
34
    """
35
    print("Running API Test: Download specific file (*.py)")
1✔
36
    output_dir = "test_api_r_output"
1✔
37

38
    # Cleanup before test
39
    shutil.rmtree(output_dir, ignore_errors=True)
1✔
40
    Path(output_dir).mkdir(parents=True, exist_ok=True)
1✔
41

42
    try:
1✔
43
        download(
1✔
44
            record_or_doi="10.5281/zenodo.1215979",
45
            output_dir=output_dir,
46
            file_glob="*.py",
47
            start_fresh=True,  # Corresponds to -n
48
            exceptions_on_failure=True,  # Ensure API raises exceptions
49
        )
50

51
        # Verify the matching file WAS downloaded
52
        assert Path(output_dir, "fetch_data.py").exists(), (
1✔
53
            "fetch_data.py was not downloaded"
54
        )
55

56
        # Verify that ONLY the matching file was downloaded (issue #39)
57
        # The record contains 5 JSON files that should NOT be downloaded
58
        downloaded_files = os.listdir(output_dir)
1✔
59
        assert len(downloaded_files) == 1, (
1✔
60
            f"Expected only 1 file (*.py), but got {len(downloaded_files)}: {downloaded_files}"
61
        )
62
        assert downloaded_files[0] == "fetch_data.py", (
1✔
63
            f"Expected only fetch_data.py, but got: {downloaded_files}"
64
        )
65

66
        print(
1✔
67
            f"API Test: Download specific file (*.py) PASSED. Files in {output_dir}: {downloaded_files}"
68
        )
69
    except Exception as e:
×
70
        print(f"API Test: Download specific file (*.py) FAILED: {e}")
×
71
        # Cleanup after test (even on failure)
72
        shutil.rmtree(output_dir, ignore_errors=True)
×
73
        raise  # Re-raise the exception to fail the test
×
74

75
    # Cleanup after successful test
76
    shutil.rmtree(output_dir, ignore_errors=True)
1✔
77

78

79
def test_api_error_handling() -> None:
1✔
80
    """Ensure API properly raises exceptions for invalid input."""
81
    print("Running API Test: Error handling for invalid DOI")
1✔
82
    raised_expected_exception = False
1✔
83
    try:
1✔
84
        download(record_or_doi="invalid_doi_for_api_test", exceptions_on_failure=True)
1✔
85
    except (
1✔
86
        ValueError,
87
        ConnectionError,
88
    ) as ve:  # DOI resolution failure can raise ValueError or ConnectionError
89
        print(f"API Test: Error handling - Caught expected exception: {ve}")
1✔
90
        raised_expected_exception = True
1✔
91
    except Exception as e:  # Catch any other exception to report it
×
92
        print(
×
93
            f"API Test: Error handling - Caught unexpected exception: {type(e).__name__} - {e}"
94
        )
95
        pass  # Not the expected exception
×
96

97
    assert raised_expected_exception, (
1✔
98
        "Expected ValueError or ConnectionError was not raised for invalid DOI."
99
    )
100
    print("API Test: Error handling for invalid DOI PASSED.")
1✔
101

102

103
def main() -> None:
1✔
104
    """Run all API tests and report results."""
105
    try:
×
106
        test_api_download_specific_file()
×
107
        test_api_error_handling()
×
108
        print("All Python API tests PASSED.")
×
109
        sys.exit(0)
×
110
    except AssertionError as e:
×
111
        print(f"Python API test FAILED: {e}")
×
112
        sys.exit(1)
×
113
    except Exception as e:
×
114
        print(f"Python API test FAILED with unexpected error: {e}")
×
115
        sys.exit(1)
×
116

117

118
if __name__ == "__main__":
1✔
119
    main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc