Skip to content

Entiy Org Csv now looks for overlap and flags in reponse - #124

Merged
pooleycodes merged 3 commits into
mainfrom
2765-for-single-source-datasets-allow-entity-organisation-to-be-entire-entity-range
Jul 13, 2026
Merged

Entiy Org Csv now looks for overlap and flags in reponse#124
pooleycodes merged 3 commits into
mainfrom
2765-for-single-source-datasets-allow-entity-organisation-to-be-entire-entity-range

Conversation

@pooleycodes

@pooleycodes pooleycodes commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

When new entities are created during add-data processing, _create_entity_organisation now checks the downloaded entity-organisation.csv to see if the new entities already fall within an existing dataset range:

  • overlap: true is set if all new entities already fall within an existing entity-minimum/entity-maximum range for the dataset (single source dataset case).
  • error: true is set if entity-organisation.csv can't be loaded, without failing the request.
  • When either flag is true, entity-minimum/entity-maximum are omitted from the response entry so a downstream consumer (e.g. the config repo's add-data GitHub Action) can't commit an untrustworthy range to entity-organisation.csv.

Related Tickets & Documents

  • Ticket Link: 2765
  • Related Issue #
  • Closes #

QA Instructions, Screenshots, Recordings

Run the request-processor unit tests:

cd request-processor
.venv/bin/python -m pytest tests/unit/src/application/core/test_pipeline.py -q

Covers: overlap detected, no overlap, missing/unreadable CSV (error flagged), and empty new-entities cases.

Added/updated tests?

  • Yes

[optional] Are there any post deployment tasks we need to perform?

None

[optional] Are there any dependencies on other PRs or Work?

The config-manager preview UI and config repo's add_data.py were updated separately to consume the new overlap/error fields.

Summary by CodeRabbit

  • Bug Fixes

    • Improved entity mapping when processing new data resources.
    • Detects overlaps with existing entity ranges and reports mapping errors when source data cannot be loaded or parsed.
    • Prevents incomplete entity range details from being returned when overlaps or data errors are detected.
  • Tests

    • Added coverage for overlapping ranges, valid new ranges, missing mapping data and empty inputs.

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e809c6f7-31fa-4345-b206-237f7ce70885

📥 Commits

Reviewing files that changed from the base of the PR and between 9522b5e and cbc842d.

📒 Files selected for processing (2)
  • request-processor/src/application/core/pipeline.py
  • request-processor/tests/unit/src/application/core/test_pipeline.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • request-processor/tests/unit/src/application/core/test_pipeline.py
  • request-processor/src/application/core/pipeline.py

Walkthrough

_create_entity_organisation now accepts pipeline_dir, loads entity-organisation.csv, detects overlap with existing dataset ranges, and reports load errors. Its response conditionally includes entity bounds, with tests covering the new behaviours.

Changes

Entity organisation overlap detection

Layer / File(s) Summary
Overlap detection implementation and call site
request-processor/src/application/core/pipeline.py
The transformation passes pipeline_dir to _create_entity_organisation, which loads dataset ranges, computes overlap, handles invalid inputs and CSV errors, and conditionally includes entity-minimum and entity-maximum.
Unit tests for overlap and error scenarios
request-processor/tests/unit/src/application/core/test_pipeline.py
Tests cover overlapping ranges, non-overlapping ranges, missing CSV errors, and empty input behaviour.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: adding overlap and error flags to the entity-organisation CSV response.
Docstring Coverage ✅ Passed Docstring coverage is 90.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 2765-for-single-source-datasets-allow-entity-organisation-to-be-entire-entity-range

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.

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown

Caution

Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted.

Error details
putComment timed out

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
request-processor/tests/unit/src/application/core/test_pipeline.py (1)

464-546: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add test cases for edge scenarios not covered by the current suite.

The four tests cover the primary paths well. Consider adding tests for:

  • Partial overlap: some entities within an existing range, some outside — documents the all() semantics.
  • Non-numeric entity values: entity values like "" or "abc" — would catch the unhandled ValueError from int().
  • Multiple datasets in CSV: verifies the dataset filter at line 491.
  • Malformed CSV rows: missing or non-numeric entity-minimum/entity-maximum — verifies the continue at line 497.
🧪 Suggested additional test cases
def test_create_entity_organisation_partial_overlap_does_not_flag(tmp_path):
    """Entities split across different ranges should not flag overlap"""
    pipeline_dir = tmp_path / "pipeline"
    pipeline_dir.mkdir()
    (pipeline_dir / "entity-organisation.csv").write_text(
        "dataset,entity-minimum,entity-maximum,organisation\n"
        "nature-improvement-area,10100000,10100011,government-organisation:PB202\n"
        "nature-improvement-area,10200000,10200011,government-organisation:PB202\n"
    )
    new_entities = [{"entity": "10100005"}, {"entity": "10200005"}]
    result = _create_entity_organisation(
        new_entities, "nature-improvement-area",
        "government-organisation:PB202", str(pipeline_dir),
    )
    assert result[0]["overlap"] is False
    assert result[0]["entity-minimum"] == 10100005
    assert result[0]["entity-maximum"] == 10200005


def test_create_entity_organisation_filters_other_datasets(tmp_path):
    """Rows for other datasets should be ignored"""
    pipeline_dir = tmp_path / "pipeline"
    pipeline_dir.mkdir()
    (pipeline_dir / "entity-organisation.csv").write_text(
        "dataset,entity-minimum,entity-maximum,organisation\n"
        "other-dataset,10100000,10100011,government-organisation:PB202\n"
    )
    new_entities = [{"entity": "10100005"}]
    result = _create_entity_organisation(
        new_entities, "nature-improvement-area",
        "government-organisation:PB202", str(pipeline_dir),
    )
    assert result[0]["overlap"] is False


def test_create_entity_organisation_skips_malformed_csv_rows(tmp_path):
    """Rows with non-numeric min/max should be skipped, not crash"""
    pipeline_dir = tmp_path / "pipeline"
    pipeline_dir.mkdir()
    (pipeline_dir / "entity-organisation.csv").write_text(
        "dataset,entity-minimum,entity-maximum,organisation\n"
        "nature-improvement-area,N/A,N/A,government-organisation:PB202\n"
        "nature-improvement-area,10100000,10100011,government-organisation:PB202\n"
    )
    new_entities = [{"entity": "10100005"}]
    result = _create_entity_organisation(
        new_entities, "nature-improvement-area",
        "government-organisation:PB202", str(pipeline_dir),
    )
    assert result[0]["overlap"] is True
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@request-processor/tests/unit/src/application/core/test_pipeline.py` around
lines 464 - 546, Add edge-case tests for `_create_entity_organisation` to cover
behavior not exercised by the current suite. Include a partial-overlap case to
confirm the `all()` overlap logic, a non-numeric entity value case to document
handling around `int()` conversion, a CSV with multiple datasets to verify the
dataset filter, and malformed CSV rows to confirm bad
`entity-minimum`/`entity-maximum` values are skipped via the existing parsing
path. Use the existing `_create_entity_organisation` helper and
`entity-organisation.csv` test setup pattern so the new cases are easy to
locate.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@request-processor/src/application/core/pipeline.py`:
- Around line 469-473: The entity value extraction in the pipeline currently
only filters out None, so int() can still raise ValueError for empty or
non-numeric strings. Update the entity_values comprehension in the pipeline
logic to defensively handle invalid entity fields the same way the CSV parsing
path does, by guarding the int() conversion with try/except for TypeError and
ValueError or by filtering out non-numeric inputs before conversion. Use the
existing new_entities processing block and the surrounding CSV row parsing logic
as the reference points.
- Around line 488-500: The overlap check in the pipeline currently only sets
overlap when all new entity values fit inside a single existing row, so a batch
spanning multiple existing ranges can still pass. Update the overlap logic in
the range handling block to compare the proposed entity range against each
existing row in existing_rows for the same dataset, or test each entity value
against all rows, and only allow the range when none of the existing
[entity-minimum, entity-maximum] intervals intersect it.

---

Nitpick comments:
In `@request-processor/tests/unit/src/application/core/test_pipeline.py`:
- Around line 464-546: Add edge-case tests for `_create_entity_organisation` to
cover behavior not exercised by the current suite. Include a partial-overlap
case to confirm the `all()` overlap logic, a non-numeric entity value case to
document handling around `int()` conversion, a CSV with multiple datasets to
verify the dataset filter, and malformed CSV rows to confirm bad
`entity-minimum`/`entity-maximum` values are skipped via the existing parsing
path. Use the existing `_create_entity_organisation` helper and
`entity-organisation.csv` test setup pattern so the new cases are easy to
locate.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fef438c5-392c-437b-a8f0-413741f3ab7a

📥 Commits

Reviewing files that changed from the base of the PR and between fcd60c7 and 9522b5e.

📒 Files selected for processing (2)
  • request-processor/src/application/core/pipeline.py
  • request-processor/tests/unit/src/application/core/test_pipeline.py

Comment thread request-processor/src/application/core/pipeline.py
Comment thread request-processor/src/application/core/pipeline.py

@gibahjoe gibahjoe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Keep main's refactored _transform_add_data_resource loop and pass
pipeline_dir through to the branch's overlap-aware
_create_entity_organisation.
@pooleycodes
pooleycodes merged commit ef0414f into main Jul 13, 2026
3 checks passed
@pooleycodes
pooleycodes deleted the 2765-for-single-source-datasets-allow-entity-organisation-to-be-entire-entity-range branch July 13, 2026 14:41
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.

For single-source datasets, allow entity-organisation to be entire entity range

2 participants