Skip to content

Commit 2691c3e

Browse files
committed
fix(SinkStatus): make finalized filters perfect negations of each other
1 parent 67c9756 commit 2691c3e

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

sc_audit/views/sink_status.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Any, Literal
1010

1111
import pandas as pd
12-
from sqlalchemy import Select, or_, select
12+
from sqlalchemy import Select, exists, not_, select
1313
from sqlalchemy.orm import contains_eager, selectinload
1414

1515
from sc_audit.db_schema.association import SinkStatus
@@ -90,13 +90,17 @@ def construct_stx_query(
9090
before_dt = dt.datetime(before_date.year, before_date.month, before_date.day)
9191
q_txs = q_txs.where(SinkingTx.created_at < before_dt)
9292

93-
if finalized is not None:
94-
q_txs = q_txs.outerjoin(SinkStatus)
9593
if finalized is False:
96-
# not_ and in_ do not work here because NULL and false are treated differently
97-
q_txs = q_txs.where(or_(SinkStatus.finalized == None, SinkStatus.finalized == False))
94+
# filter on sink txs without any SinkStatus with finalized=True
95+
# this clause needs to be the perfect negation of `finalized is True`
96+
q_txs = q_txs.where(
97+
not_(exists().where(
98+
SinkStatus.sinking_tx_hash == SinkingTx.hash, SinkStatus.finalized == True
99+
).correlate(SinkingTx))
100+
)
98101
elif finalized is True:
99-
q_txs = q_txs.where(SinkStatus.finalized == True)
102+
# select sink txs that have at least one SinkStatus with finalized=True
103+
q_txs = q_txs.outerjoin(SinkStatus).where(SinkStatus.finalized == True)
100104

101105
return q_txs
102106

tests/test_views.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def test_construct_query_finalized(self):
125125
before_date=None,
126126
finalized=False
127127
)
128-
assert "WHERE sink_status.finalized IS NULL OR sink_status.finalized = false" in str(stxq)
128+
assert str(stxq).replace(' \n', '').endswith(
129+
"WHERE NOT (EXISTS (SELECT *FROM sink_statusWHERE sink_status.sinking_tx_hash = sinking_txs.hash AND sink_status.finalized = true))"
130+
)
129131

130132

131133
class TestSinkStatusView:
@@ -174,10 +176,13 @@ def test_sink_status_finalized_true(self, mock_session_with_associations):
174176

175177
def test_sink_status_finalized_false(self, mock_session_with_associations):
176178
txdf = sink_status_view.view_sinking_txs(finalized=False)
177-
assert len(txdf) == 2
179+
assert len(txdf) == 3
178180
assert txdf.statuses.map(add_amount_filled).sum() == Decimal('1.985')
179-
assert txdf.carbon_amount.sum() == Decimal('3.298')
180-
assert not any(txdf.statuses.astype(bool))
181+
assert txdf.carbon_amount.sum() == Decimal('5.283')
182+
# expect the last tx to have a sink status with finalized=False
183+
assert len(txdf[txdf.statuses.astype(bool) == True]) == 1
184+
assert len(txdf.statuses.iloc[-1]) == 1
185+
assert txdf.statuses.iloc[-1][0]['finalized'] == False
181186

182187
def test_sink_status_pagination_asc(self, mock_session_with_associations):
183188
txdf = sink_status_view.view_sinking_txs(limit=2, order='asc')

0 commit comments

Comments
 (0)