Skip to content

Commit 8718532

Browse files
feat: add IsEventQLTrue precondition and corresponding tests
1 parent 262c971 commit 8718532

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

eventsourcingdb/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
)
1111
from .read_events import ReadEventsOptions, ReadFromLatestEvent, IfEventIsMissingDuringRead, Order
1212
from .read_event_types import EventType
13-
from .write_events import Precondition, IsSubjectOnEventId, IsSubjectPristine
13+
from .write_events import Precondition, IsSubjectOnEventId, IsSubjectPristine, IsEventQLTrue
1414

1515

1616
__all__ = [
@@ -20,7 +20,7 @@
2020
'Event', 'EventCandidate',
2121
'EventType',
2222
'ObserveEventsOptions', 'ObserveFromLatestEvent', 'IfEventIsMissingDuringObserve',
23-
'Precondition', 'IsSubjectOnEventId', 'IsSubjectPristine',
23+
'Precondition', 'IsSubjectOnEventId', 'IsSubjectPristine', 'IsEventQLTrue',
2424
'ReadEventsOptions', 'ReadFromLatestEvent', 'IfEventIsMissingDuringRead', 'Order',
2525
'ClientError', 'CustomError', 'InternalError', 'ServerError', 'ValidationError',
2626
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .preconditions import Precondition
22
from .preconditions import IsSubjectPristine
33
from .preconditions import IsSubjectOnEventId
4+
from .preconditions import IsEventQLTrue

eventsourcingdb/write_events/preconditions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ def to_json(self) -> dict[str, Any]:
3535
'eventId': self.event_id
3636
}
3737
}
38+
39+
@dataclass
40+
class IsEventQLTrue(Precondition):
41+
query: str
42+
43+
def to_json(self) -> dict[str, Any]:
44+
return {
45+
'type': 'isEventQLTrue',
46+
'payload': {
47+
'query': self.query
48+
}
49+
}

tests/test_write_events.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from eventsourcingdb import ServerError
55
from eventsourcingdb import EventCandidate
66
from eventsourcingdb import IsSubjectPristine, IsSubjectOnEventId
7+
from eventsourcingdb.write_events.preconditions import IsEventQLTrue
78

89
from .conftest import TestData
910

@@ -215,6 +216,69 @@ async def test_is_subject_on_event_id_fails_for_wrong_id(
215216
[IsSubjectOnEventId('/', '2')]
216217
)
217218

219+
@staticmethod
220+
@pytest.mark.asyncio
221+
async def test_is_event_ql_true_precondition_works(
222+
database: Database,
223+
test_data: TestData,
224+
) -> None:
225+
client = database.get_client()
226+
227+
await client.write_events(
228+
[
229+
EventCandidate(
230+
source=test_data.TEST_SOURCE_STRING,
231+
subject='/',
232+
type='com.foo.bar',
233+
data={}
234+
)
235+
]
236+
)
237+
238+
await client.write_events(
239+
[
240+
EventCandidate(
241+
source=test_data.TEST_SOURCE_STRING,
242+
subject='/',
243+
type='com.foo.bar',
244+
data={}
245+
)
246+
],
247+
[IsEventQLTrue('FROM e IN events PROJECT INTO COUNT() > 0')]
248+
)
249+
250+
@staticmethod
251+
@pytest.mark.asyncio
252+
async def test_is_event_ql_true_precondition_fails(
253+
database: Database,
254+
test_data: TestData,
255+
) -> None:
256+
client = database.get_client()
257+
258+
await client.write_events(
259+
[
260+
EventCandidate(
261+
source=test_data.TEST_SOURCE_STRING,
262+
subject='/',
263+
type='com.foo.bar',
264+
data={}
265+
)
266+
]
267+
)
268+
269+
with pytest.raises(ServerError):
270+
await client.write_events(
271+
[
272+
EventCandidate(
273+
source=test_data.TEST_SOURCE_STRING,
274+
subject='/',
275+
type='com.foo.bar',
276+
data={}
277+
)
278+
],
279+
[IsEventQLTrue('FROM e IN events PROJECT INTO COUNT() == 0')]
280+
)
281+
218282
@staticmethod
219283
@pytest.mark.asyncio
220284
async def test_throws_error_if_event_does_not_match_schema(

0 commit comments

Comments
 (0)