Raise masoniteorm Collection test coverage to 100%#154
Conversation
Cover the Collection subclass's post-query eager loading (load) and with_relationship_autoloading no-op, which previously had ~28% coverage: - has-one and has-many relations registered after load() - loading multiple relations in one call - empty-collection short-circuit and no-matching-records path - non-Collection get_related result routed through add_relation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
tmgbedu
left a comment
There was a problem hiding this comment.
✅ Code Reviewer verdict: APPROVE (posted as comment — GitHub blocks formal self-approval; author == reviewer account).
Test-only change, verified locally and against CI.
Coverage: masoniteorm/collection/Collection.py reaches 100% (18/18 lines). CI: Pytest ✅, Ruff ✅ (×2), codecov/patch ✅.
Behavioral audit (traced against load() source — no tautologies):
- has-one / has-many tests assert identity return +
_relationshipsregistration + concrete instance types → exercise theisinstance(result_set, Collection)→register_relatedbranch. test_load_without_matching_related_recordsgenuinely exercises the falsyif result_set:skip branch (Jane id 2 has no articles).- empty-collection tests cover the
if not self._items: return selfshort-circuit. _StubRelationshiptest routes a non-Collection (dict) result through theelse: model.add_relation(...)branch and asserts the exact payload — real behavior.- autoloading no-op asserts
Nonereturn matching thepassbody.
Patterns consistent with the existing test file. No framework source modified. Good to go (not merging per instructions).
Replace the hand-rolled _StubModel/_StubRelationship with a real Model subclass and a real HasOne whose get_related is overridden to return a single row. This keeps map_related, add_relation, and _relationships real, and documents why the non-Collection branch is unreachable by any real relationship driven through load() (get_related always returns a Collection when handed the whole Collection).
…hips The load() tests now exercise real HasOne/HasMany relationships against the sqlite test harness (User has-one Profile, User has-many Articles) end-to-end, asserting related records attach via load(). Removes the _Widget / _SingleResultHasOne stubs and the duplicated empty-collection test. The non-Collection else branch of load() is unreachable by real relationships (get_related always returns a Collection), so it is now uncovered by design rather than driven by a synthetic stub.
|
Code review verdict: APPROVE (re-review of current head Verified against the criteria:
No issues. Approve. Leaving the merge decision to the maintainer. |
Summary
Adds unit tests for the
masoniteormCollectionsubclass, whoseload()(post-query eager loading) andwith_relationship_autoloading()were previously untested (~28% coverage). The file now reaches 100% line coverage.Tests added
DB-backed (
TestCollection):load()registers a has-one relation (profile) on each modelload()registers a has-many relation (articles)selfNo-DB (
TestCollectionLoad):get_relatedresult that is not aCollectionis routed throughadd_relationwith_relationship_autoloading()is a no-opVerification
pytest tests/masoniteorm/collection/test_collection.py --cov→ target file 100%, 62 passedpytest tests/masoniteorm --ignore=tests/masoniteorm/postgres→ 423 passed, 7 skippedTest-only change; no framework source modified.