Repository navigation
fix(container): enforce acyclic, single-parent relationships #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
4444J99
wants to merge
3
commits into
main
Choose a base branch
from
ranked-queue/issue-17-container-relationships
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| """Focused regressions for acyclic, single-parent container relationships. | ||
|
|
||
| Covers rules-system-bound#17: self-parenting and ancestor cycles must be | ||
| rejected before mutation, re-adding must not duplicate, reparenting must | ||
| be atomic, and removal/membership must use object identity. | ||
| """ | ||
|
|
||
| import pytest | ||
| from rules_system_bound.container import Container | ||
|
|
||
|
|
||
| def make(identifier): | ||
| return Container(id=identifier, function="f", purpose="p") | ||
|
|
||
|
|
||
| def test_self_parenting_rejected_and_state_unchanged(): | ||
| container = make("a") | ||
| with pytest.raises(ValueError): | ||
| container.add_child(container) | ||
| assert container.parent is None | ||
| assert container.children == [] | ||
|
|
||
|
|
||
| def test_ancestor_cycle_rejected_and_state_unchanged(): | ||
| root, mid, leaf = make("root"), make("mid"), make("leaf") | ||
| root.add_child(mid) | ||
| mid.add_child(leaf) | ||
| with pytest.raises(ValueError): | ||
| leaf.add_child(root) | ||
| assert root.parent is None | ||
| assert root.children == [mid] | ||
| assert mid.children == [leaf] | ||
| assert leaf.children == [] | ||
| assert leaf.get_lineage() == [leaf, mid, root] | ||
|
|
||
|
|
||
| def test_readding_same_child_does_not_duplicate(): | ||
| parent, child = make("p"), make("ch") | ||
| parent.add_child(child) | ||
| parent.add_child(child) | ||
| assert parent.children == [child] | ||
| assert child.parent is parent | ||
|
|
||
|
|
||
| def test_reparenting_moves_atomically(): | ||
| old_parent, new_parent, child = make("p1"), make("p2"), make("ch") | ||
| old_parent.add_child(child) | ||
| new_parent.add_child(child) | ||
| assert old_parent.children == [] | ||
| assert new_parent.children == [child] | ||
| assert child.parent is new_parent | ||
| assert child.get_lineage() == [child, new_parent] | ||
|
|
||
|
|
||
| def test_descendant_promoted_to_direct_child_without_cycle(): | ||
| root, mid, leaf = make("root"), make("mid"), make("leaf") | ||
| root.add_child(mid) | ||
| mid.add_child(leaf) | ||
| root.add_child(leaf) | ||
| assert mid.children == [] | ||
| assert root.children == [mid, leaf] | ||
| assert leaf.parent is root | ||
| assert leaf.get_lineage() == [leaf, root] | ||
|
|
||
|
|
||
| def test_remove_uses_identity_not_dataclass_equality(): | ||
| parent, child = make("p"), make("ch") | ||
| parent.add_child(child) | ||
| twin = Container(id="ch", function="f", purpose="p") | ||
| twin.parent = parent | ||
| assert twin == child | ||
| parent.remove_child(twin) | ||
| assert parent.children == [child] | ||
| assert child.parent is parent | ||
| assert twin.parent is parent | ||
| parent.remove_child(child) | ||
| assert parent.children == [] | ||
| assert child.parent is None | ||
|
|
||
|
|
||
| def test_envelops_uses_identity_not_dataclass_equality(): | ||
| parent, child = make("p"), make("ch") | ||
| parent.add_child(child) | ||
| twin = Container(id="ch", function="f", purpose="p") | ||
| twin.parent = parent | ||
| assert twin == child | ||
| assert parent.envelops(child) | ||
| assert not parent.envelops(twin) | ||
|
|
||
|
|
||
| def test_readd_reconciles_parent_when_children_list_supplied_directly(): | ||
| # CodeRabbit review on PR #19: a directly-constructed children list | ||
| # can leave child.parent unset while membership claims otherwise. | ||
| child = make("ch") | ||
| parent = Container(id="p", function="f", purpose="p", children=[child]) | ||
| assert child.parent is None | ||
| parent.add_child(child) | ||
| assert parent.children == [child] | ||
| assert child.parent is parent | ||
| assert child.get_lineage() == [child, parent] | ||
|
|
||
|
|
||
| def test_readd_via_direct_list_detaches_old_parent(): | ||
| # Same construction path, but the pointer names another parent that | ||
| # still lists the child: re-adding must move it, not duplicate it. | ||
| old_parent, child = make("p1"), make("ch") | ||
| old_parent.add_child(child) | ||
| new_parent = Container(id="p2", function="f", purpose="p", children=[child]) | ||
| new_parent.add_child(child) | ||
| assert old_parent.children == [] | ||
| assert new_parent.children == [child] | ||
| assert child.parent is new_parent | ||
| assert child.get_lineage() == [child, new_parent] | ||
|
|
||
|
|
||
| def test_children_graph_cycle_rejected_and_state_unchanged(): | ||
| # Second CodeRabbit review on PR #19: a directly-constructed list | ||
| # (parent lists child, child.parent unset) must not let | ||
| # child.add_child(parent) close a cycle in the children lists. | ||
| child = make("ch") | ||
| parent = Container(id="p", function="f", purpose="p", children=[child]) | ||
| with pytest.raises(ValueError): | ||
| child.add_child(parent) | ||
| assert parent.parent is None | ||
| assert parent.children == [child] | ||
| assert child.parent is None | ||
| assert child.children == [] | ||
| assert parent.get_descendants() == [child] | ||
|
|
||
|
|
||
| def test_normal_lineage_and_descendants_with_unrelated_unchanged(): | ||
| root = make("root") | ||
| child1, child2 = make("c1"), make("c2") | ||
| grandchild = make("gc") | ||
| root.add_child(child1) | ||
| root.add_child(child2) | ||
| child1.add_child(grandchild) | ||
| assert grandchild.get_lineage() == [grandchild, child1, root] | ||
| assert root.get_descendants() == [child1, grandchild, child2] | ||
| assert child2.get_lineage() == [child2, root] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.