Skip to content

Adding unit_testing examples from presentation#19

Open
Arun-bioinformatics wants to merge 3 commits intomainfrom
Unit_testing_codeschool_2026
Open

Adding unit_testing examples from presentation#19
Arun-bioinformatics wants to merge 3 commits intomainfrom
Unit_testing_codeschool_2026

Conversation

@Arun-bioinformatics
Copy link
Contributor

@Arun-bioinformatics Arun-bioinformatics commented Feb 23, 2026

  • Updated the main README.md to specify the inclusion of the new content
  • Added a second README.md inside the Unit_testing_python_2026 with some explanation on how to run pytest.

This change is Reviewable

Summary by CodeRabbit

  • Documentation

    • Added a "2026 additions" section and expanded learning-session README with a unit testing presentation, usage instructions, prerequisites and test commands.
  • New Features

    • Added utilities for ClinVar significance lookup, DNA GC-content calculation and BED file length computation.
  • Tests

    • Added comprehensive unit tests covering normal cases, edge cases and error handling for all new utilities.

- Updated the main README.md to specify the inclusion of the new content
- Added a second README.md inside the Unit_testing_python_2026 with some explanation on how to run pytest.
@coderabbitai
Copy link

coderabbitai bot commented Feb 23, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Jira integration is disabled

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 41a6b0b and 1af58e4.

📒 Files selected for processing (1)
  • learning-sessions/Unit_testing_python_2026/example_code/tests/test_script.py

Walkthrough

Adds a Unit Testing learning session: three new utility modules (GC content, BED length, ClinVar lookup), READMEs updated with instructions, and pytest suites covering normal, edge and error cases including mocks and file fixtures.

Changes

Cohort / File(s) Summary
Documentation
README.md, learning-sessions/Unit_testing_python_2026/example_code/README.md
Inserted "2026 additions" and a Codeschool presentation README describing purpose, prerequisites, and commands to run pytest and pytest-cov.
Utility modules
learning-sessions/Unit_testing_python_2026/example_code/bin/script.py, learning-sessions/Unit_testing_python_2026/example_code/bin/script_bed.py, learning-sessions/Unit_testing_python_2026/example_code/bin/clinvar_utils.py
Added calculate_gc_content(sequence) with validation and rounding, calculate_total_bed_length(filepath) reading BED with strict validation and explicit errors, and get_clinvar_significance(variation_id) calling ClinVar API, handling HTTP and JSON errors.
Tests
learning-sessions/Unit_testing_python_2026/example_code/tests/test_script.py, learning-sessions/Unit_testing_python_2026/example_code/tests/test_script_bed.py, learning-sessions/Unit_testing_python_2026/example_code/tests/test_clinvar_utils.py
Added pytest modules covering expected results, edge cases, and exception paths; includes fixtures, file-based BED fixtures, and mocking for external API responses.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the primary objective: adding unit testing examples from a presentation, which is clearly demonstrated by the new learning-sessions directory and comprehensive test files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch Unit_testing_codeschool_2026

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 8

🧹 Nitpick comments (1)
learning-sessions/Unit_testing_python_2026/example_code/bin/clinvar_utils.py (1)

39-41: Consider moving return to the else block for clarity.

Having return significance inside the try body subtly conflates the success path with the error path. Moving it to an else block makes the intent explicit — which is a useful teaching point for the audience.

♻️ Proposed refactor
     try:
         significance = data['clinical_significance']['description']
-        return significance
     except KeyError as exc:
         raise ValueError(
             f"Could not find significance data for ID {variation_id}") from exc
+    else:
+        return significance
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@learning-sessions/Unit_testing_python_2026/example_code/bin/clinvar_utils.py`
around lines 39 - 41, The try block in the function that reads clinical
significance (the code setting significance =
data['clinical_significance']['description']) currently returns inside the try;
change this to return in an else block after the try/except so the success path
is explicit: keep the same try to extract significance, keep the except handling
as-is, and add an else: return significance after the try/except (refer to the
significance variable and the extraction expression
data['clinical_significance']['description'] to locate the code).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@learning-sessions/Unit_testing_python_2026/example_code/README.md`:
- Line 5: The README instructs users to run pytest in `unit_test_example_code`
which doesn't exist; either update the text to reference the actual folder
`example_code` or rename the directory to `unit_test_example_code`. Edit the
README.md text (the sentence that mentions `unit_test_example_code`) to use
`example_code` (or change the directory name to match the README) so the command
`pytest` runs from the correct folder; ensure any other README references are
consistent with the `example_code` directory name.
- Around line 1-9: The README is missing the pytest-mock dependency needed by
tests/test_clinvar_utils.py which uses the mocker fixture; update the docs to
declare pytest-mock as a test/dev dependency and show how to install it (e.g.,
pip install pytest-mock) or add it to your project's
requirements/dev-requirements, and mention that pytest should be run after
installing this dependency (the pytest and pytest-cov instructions should remain
but note pytest-mock is required for the mocker fixture to work).

In
`@learning-sessions/Unit_testing_python_2026/example_code/tests/test_clinvar_utils.py`:
- Around line 24-31: The test parameter name is wrong: test_significance_logic
currently requests a nonexistent fixture `_clinvar_mock` so pytest will error;
rename the test parameter to the registered fixture name `clinvar_mock` so the
fixture (declared with `@pytest.fixture`(name="clinvar_mock")) is applied and the
call to get_clinvar_significance('67890') runs as intended.
- Line 1: The module docstring in tests/test_clinvar_utils.py incorrectly
references "calculate_gc_content"; update the test file docstring to reference
the actual function under test by replacing "calculate_gc_content function" with
"get_clinvar_significance function" so the docstring correctly describes the
tests for get_clinvar_significance in bin/clinvar_utils.py.

In
`@learning-sessions/Unit_testing_python_2026/example_code/tests/test_script_bed.py`:
- Line 1: The module docstring in the test file incorrectly references
calculate_gc_content; update the top-line docstring to mention
calculate_total_bed_length instead. Edit the string literal at the top of
tests/test_script_bed.py to read something like "Unit tests for the
calculate_total_bed_length function in bin/script_bed.py" so the docstring
accurately reflects the tested function (calculate_total_bed_length).
- Around line 6-11: The fixture docstring for valid_bed_factory claims "3-line"
but the written file has only 2 lines; update the docstring in the pytest
fixture valid_bed (function valid_bed_factory) to accurately state "2-line" (or
"two-line") to match the content written to f ("valid.bed"), or alternatively
add a third BED line to f.write_text if you intended three lines—ensure the
docstring and the actual data produced by valid_bed_factory stay consistent.

In
`@learning-sessions/Unit_testing_python_2026/example_code/tests/test_script.py`:
- Around line 42-44: The regex in the pytest.raises match contains unescaped
dots which act as wildcards; update the test in the with pytest.raises(...)
block that calls calculate_gc_content("ATGCX") so the expected message uses an
escaped-dot pattern (e.g., use a raw string with \. for each period) or simplify
to a partial, literal-safe substring (e.g., "Sequence contains invalid
characters" or "Only A, T, G, C are allowed") to avoid unintended matches;
ensure the change targets the pytest.raises(..., match=...) argument in
tests/test_script.py.

---

Nitpick comments:
In
`@learning-sessions/Unit_testing_python_2026/example_code/bin/clinvar_utils.py`:
- Around line 39-41: The try block in the function that reads clinical
significance (the code setting significance =
data['clinical_significance']['description']) currently returns inside the try;
change this to return in an else block after the try/except so the success path
is explicit: keep the same try to extract significance, keep the except handling
as-is, and add an else: return significance after the try/except (refer to the
significance variable and the extraction expression
data['clinical_significance']['description'] to locate the code).

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Jira integration is disabled

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 5fb742a and 564fcaa.

📒 Files selected for processing (10)
  • README.md
  • learning-sessions/Unit_testing_python_2026/example_code/README.md
  • learning-sessions/Unit_testing_python_2026/example_code/bin/__init__.py
  • learning-sessions/Unit_testing_python_2026/example_code/bin/clinvar_utils.py
  • learning-sessions/Unit_testing_python_2026/example_code/bin/script.py
  • learning-sessions/Unit_testing_python_2026/example_code/bin/script_bed.py
  • learning-sessions/Unit_testing_python_2026/example_code/tests/__init__.py
  • learning-sessions/Unit_testing_python_2026/example_code/tests/test_clinvar_utils.py
  • learning-sessions/Unit_testing_python_2026/example_code/tests/test_script.py
  • learning-sessions/Unit_testing_python_2026/example_code/tests/test_script_bed.py

Comment on lines +27 to +28
url = ("https://api.ncbi.nlm.nih.gov/variation/v0/beta/"
f"clinical-significance/variation/{variation_id}")
Copy link

@coderabbitai coderabbitai bot Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

NCBI Variation Services API v0 beta clinical-significance endpoint status 2025

💡 Result:

In 2025, NCBI’s Variation Services API v0 was still publicly available (e.g., called out alongside dbSNP Build 157 release products) and still described publicly as “beta testing mode.” [1], [2]

For the “v0 beta” RefSNP route, NCBI appears to have been moving away from the /beta/ path: requests to …/variation/v0/beta/refsnp/{rsid} redirect to the non-beta route …/variation/v0/refsnp/{rsid}/. [3]

Also, there isn’t a distinct, standalone “clinical-significance endpoint” in v0 that you call directly; rather, clinical significance is provided as fields inside RefSNP/allele-style responses (i.e., you fetch a RefSNP record and read its clinical-significance-related fields). [4]

Sources:
[1] dbSNP Build 157 Release (NCBI Insights, 2025-03-18) (ncbiinsights.ncbi.nlm.nih.gov)
[2] SPDI Variation Service listing (catalog.data.gov; metadata updated 2025-06-19; notes “still in beta testing mode”) (catalog.data.gov)
[3] Live API behavior: /variation/v0/beta/refsnp/8127500 redirects to /variation/v0/refsnp/8127500/ (api.ncbi.nlm.nih.gov)
[4] Example of calling the RefSNP API (/variation/v0/refsnp/{rsid}) rather than a clinical-significance-specific endpoint (github-wiki-see.page)

Citations:


Update the endpoint structure; clinical-significance data is embedded in RefSNP responses, not accessed via a standalone endpoint.

The /beta/ path is deprecated—NCBI redirects /variation/v0/beta/refsnp/ requests to /variation/v0/refsnp/. More fundamentally, there's no standalone clinical-significance/variation/ endpoint in v0. Clinical significance is returned as a field within RefSNP or allele responses. The code should fetch the relevant RefSNP or allele record and extract clinical-significance data from that response instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really matter whether the function works or not. This is just supposed to be an example for unit testing code.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arun-bioinformatics, understood—since this is example code for demonstrating unit testing principles, the API endpoint accuracy isn't critical. Thanks for clarifying!


✏️ Learnings added
Learnt from: Arun-bioinformatics
Repo: eastgenomics/code_school PR: 19
File: learning-sessions/Unit_testing_python_2026/example_code/bin/clinvar_utils.py:27-28
Timestamp: 2026-02-26T09:59:34.602Z
Learning: In the eastgenomics/code_school repository, code within the `learning-sessions/` directory is educational material intended to demonstrate concepts (e.g., unit testing), not production-ready code. Functional correctness of external API calls or similar implementation details may not be the primary focus.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Contributor Author

@Arun-bioinformatics Arun-bioinformatics left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arun-bioinformatics made 2 comments.
Reviewable status: 0 of 10 files reviewed, 2 unresolved discussions (waiting on RSWilson1).

Comment on lines +27 to +28
url = ("https://api.ncbi.nlm.nih.gov/variation/v0/beta/"
f"clinical-significance/variation/{variation_id}")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really matter whether the function works or not. This is just supposed to be an example for unit testing code.

Copy link
Contributor Author

@Arun-bioinformatics Arun-bioinformatics left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick comment addressed

@Arun-bioinformatics made 1 comment.
Reviewable status: 0 of 10 files reviewed, 1 unresolved discussion (waiting on RSWilson1).

Copy link
Contributor Author

@Arun-bioinformatics Arun-bioinformatics left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arun-bioinformatics made 1 comment.
Reviewable status: 0 of 10 files reviewed, 1 unresolved discussion (waiting on RSWilson1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant