Skip to content

Commit 7d5836c

Browse files
committed
🛠 [fix][backend] Sort event transactions and entries by index
1 parent 884aad7 commit 7d5836c

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

backend/kayman/schemas/event.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ class Event(EventBase, table=True):
4040
)
4141
# Auto calculated for Expense or Income
4242
# Manually logged for Transfer or Exchange
43-
transactions: list["Transaction"] = Relationship(back_populates="event")
44-
entries: list["EventEntry"] = Relationship(back_populates="event")
43+
transactions: list["Transaction"] = Relationship(
44+
back_populates="event",
45+
sa_relationship_kwargs={"order_by": "Transaction.index"},
46+
)
47+
entries: list["EventEntry"] = Relationship(
48+
back_populates="event",
49+
sa_relationship_kwargs={"order_by": "EventEntry.index"},
50+
)
4551

4652

4753
class EventCreate(EventBase):

backend/kayman/tests/crud/test_event.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
from unittest.mock import patch
33

44
import pytest
5+
from sqlalchemy import inspect
56
from sqlmodel import Session
67

78
from kayman.crud.event import create_events, read_events, update_events
8-
from kayman.schemas.event import EventType, EventUpdate
9+
from kayman.schemas.event import Event, EventType, EventUpdate
910
from kayman.tests.factories import (
1011
CategoryFactory,
1112
EventEntryFactory,
1213
EventFactory,
14+
TransactionFactory,
1315
)
1416

1517

@@ -87,6 +89,36 @@ def test_read_events_by_ids(session: Session):
8789
assert {event.id for event in multiple} == {event_1.id, event_2.id}
8890

8991

92+
def test_read_events_details_ordered_by_index(
93+
session: Session, # noqa: ARG001 (binds factory session)
94+
session_2: Session,
95+
):
96+
event = EventFactory()
97+
for index in (2, 0, 1):
98+
EventEntryFactory(event=event, index=index)
99+
TransactionFactory(event=event, index=index)
100+
101+
events = read_events(session_2, event_ids=[event.id])
102+
103+
assert len(events) == 1
104+
assert len(events[0].entries) == 3
105+
assert [entry.index for entry in events[0].entries] == [0, 1, 2]
106+
assert len(events[0].transactions) == 3
107+
assert [transaction.index for transaction in events[0].transactions] == [0, 1, 2]
108+
109+
# The assertions above cannot fail on SQLite: it answers both lazy loads
110+
# from the (event_id, index) unique index, so rows come back in index order
111+
# even without an ORDER BY. Assert the relationships are configured to sort,
112+
# which is what actually holds on Postgres.
113+
relationships = inspect(Event).relationships
114+
assert [str(column) for column in relationships["entries"].order_by] == [
115+
"event_entry.index"
116+
]
117+
assert [str(column) for column in relationships["transactions"].order_by] == [
118+
"transaction.index"
119+
]
120+
121+
90122
def test_read_events_all(session: Session):
91123
for _ in range(10):
92124
EventFactory()

0 commit comments

Comments
 (0)