Skip to content

Commit 964e65f

Browse files
committed
docs(tests): explain why relationship tests mock the query builder
The masoniteorm relationship classes are not wired to this fork's async QueryBuilder, so the relationship unit tests mock the builder instead of running against real sqlite. Document the specifics inline (missing QueryBuilder.table()/without_global_scopes()/add_select(), the BaseRelationship.get_builder() no-arg self.fn() TypeError, the attach/detach Pivot chain, and the MorphOne/MorphToMany undefined load_config) so the intent is clear and the framework gaps are traceable. No behavioral or framework changes.
1 parent 0c4b5de commit 964e65f

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

fastapi_startkit/tests/masoniteorm/relationships/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
"""Shared test doubles for the relationship unit tests.
2+
3+
``make_builder`` returns a chainable mock standing in for masoniteorm's async
4+
``QueryBuilder``. The relationship classes are unit-tested against this mock
5+
rather than real sqlite because they are not currently wired to the fork's async
6+
``QueryBuilder``:
7+
8+
* the builder (``masoniteorm/models/builder.py``) lacks ``table()``,
9+
``without_global_scopes()`` and ``add_select()``, all called by
10+
``BelongsToMany``;
11+
* ``BaseRelationship.get_builder()`` calls ``self.fn()`` with no args against
12+
``BelongsToMany``'s ``lambda x:`` factory, raising ``TypeError``;
13+
* ``attach``/``detach`` chain those same missing methods on a plain ``Pivot``
14+
model.
15+
16+
The ``CHAINABLE`` list below mirrors exactly the builder methods the relationship
17+
code expects, so the relationship logic can be tested in isolation. Real-DB
18+
wiring is tracked in the project backlog.
19+
"""
20+
121
from unittest.mock import AsyncMock, MagicMock
222

323

fastapi_startkit/tests/masoniteorm/relationships/test_base_relationship.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
"""Unit tests for the abstract ``BaseRelationship`` contract.
2+
3+
These tests assert ``BaseRelationship``'s key handling and that its abstract
4+
hooks raise ``NotImplementedError``. The join-clause test mocks the query builder
5+
(see ``conftest.make_builder``) because this fork's async ``QueryBuilder`` is not
6+
wired for the relationship builder chain (it lacks ``table()`` /
7+
``without_global_scopes()`` / ``add_select()``); this is documented for the whole
8+
relationships suite in ``conftest.py``.
9+
"""
10+
111
from unittest.mock import MagicMock
212

313
import pytest

fastapi_startkit/tests/masoniteorm/relationships/test_belongs_to_many.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
"""Unit tests for :class:`BelongsToMany`, run against a mocked query builder.
2+
3+
These tests mock the query builder (see ``conftest.make_builder``) instead of
4+
running against real sqlite because ``BelongsToMany`` is not currently wired to
5+
this fork's async ``QueryBuilder``. A real many-to-many fixture already exists
6+
(``Store``/``Product`` + the ``product_store`` pivot in ``fixtures/migration.py``),
7+
so the blocker is framework wiring, not test data:
8+
9+
* The async ``QueryBuilder`` (``masoniteorm/models/builder.py``) does not implement
10+
``table()``, ``without_global_scopes()`` or ``add_select()``, all of which
11+
``BelongsToMany`` calls (e.g. ``BelongsToMany.py`` lines 78, 227, 463). Real
12+
access/eager-load/``where_has`` raise ``AttributeError``.
13+
* ``BaseRelationship.get_builder()`` calls ``self.fn()`` with no arguments while
14+
``BelongsToMany.__init__`` assigns ``self.fn = lambda x: ...``, so resolving the
15+
related builder raises ``TypeError: <lambda>() missing 1 required positional
16+
argument: 'x'``.
17+
* ``attach``/``detach`` chain ``Pivot.on(...).table(...).without_global_scopes()
18+
.create(...)`` on a plain ``Pivot`` model that has none of those chainable
19+
builder methods, so the chain resolves to ``None`` and raises ``TypeError``.
20+
21+
Mocking the builder lets these tests exercise the relationship's own logic (key
22+
inference, pivot hydration, join/select construction) in isolation. Wiring the
23+
async ``QueryBuilder`` for real-DB coverage is tracked in the project backlog.
24+
"""
25+
126
from unittest.mock import MagicMock, patch
227

328

fastapi_startkit/tests/masoniteorm/relationships/test_morph_relations.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
"""Unit tests for the polymorphic relationships (MorphOne/MorphMany/MorphToMany/MorphTo).
2+
3+
``MorphOne`` and ``MorphToMany`` are tested with mocks because their
4+
``morph_map()`` implementations reference an undefined ``load_config`` name
5+
(``MorphOne.py`` line 134, ``MorphToMany.py`` line 103) and raise ``NameError``
6+
when called for real. ``MorphMany`` is likewise mocked: its resolved builder is
7+
not wired to this fork's async ``QueryBuilder``. ``MorphTo`` does work against
8+
real sqlite and is additionally covered end-to-end in
9+
``sqlite/relationships/test_sqlite_polymorphic.py``. The framework fixes needed
10+
for real-DB coverage of the other morphs are tracked in the project backlog.
11+
"""
12+
113
from unittest.mock import MagicMock
214

315
import pytest

0 commit comments

Comments
 (0)