Skip to content

Commit 9d8bd2c

Browse files
committed
fix event type newest eventsourcingdb version
1 parent 64f1bd9 commit 9d8bd2c

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

eventsourcingdb/handlers/read_event_types/event_type.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import TypeVar
2+
from typing import TypeVar, Dict, Any, Union
33

44
from ...errors.validation_error import ValidationError
55

@@ -10,7 +10,7 @@
1010
class EventType:
1111
event_type: str
1212
is_phantom: bool
13-
schema: str | None = None
13+
schema: Union[str, Dict[str, Any], None] = None
1414

1515
@staticmethod
1616
def parse(unknown_object: dict) -> Self:
@@ -27,16 +27,21 @@ def parse(unknown_object: dict) -> Self:
2727
)
2828

2929
schema = unknown_object.get('schema')
30-
if schema is not None and not isinstance(schema, str):
30+
if schema is not None and not isinstance(schema, (str, dict)):
3131
raise ValidationError(
32-
f"Failed to parse schema '{schema}' to str."
32+
f"Failed to parse schema '{schema}'. Schema must be either str or dict."
3333
)
3434

3535
return EventType(
3636
event_type=event_type,
3737
is_phantom=is_phantom,
3838
schema=schema,
39-
)
39+
) # type: ignore
4040

4141
def __hash__(self):
42+
# Convert dictionary schema to a hashable form (tuple of items)
43+
if isinstance(self.schema, dict):
44+
# Sort items to ensure consistent hashing
45+
schema_items = tuple(sorted((k, str(v)) for k, v in self.schema.items()))
46+
return hash((self.event_type, self.is_phantom, schema_items))
4247
return hash((self.event_type, self.is_phantom, self.schema))

eventsourcingdb/handlers/read_event_types/read_event_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections.abc import AsyncGenerator
22
from http import HTTPStatus
3+
import json
34

45
from .event_type import EventType
56
from .is_event_type import is_event_type

tests/test_read_event_types.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ async def test_reads_all_types_of_existing_events_and_registered_schemas(
5151
])
5252

5353
# Use Python dictionaries instead of JSON strings
54-
await client.register_event_schema("org.ban.ban", {"type": "object"})
55-
await client.register_event_schema("org.bing.chilling", {"type": "object"})
54+
await client.register_event_schema(
55+
"org.ban.ban",
56+
{"type": "object"}
57+
)
58+
await client.register_event_schema(
59+
"org.bing.chilling",
60+
{"type": "object"}
61+
)
5662

5763
actual_event_types: set[EventType] = set()
5864
async for event_type in client.read_event_types():
@@ -82,12 +88,12 @@ async def test_reads_all_types_of_existing_events_and_registered_schemas(
8288
EventType(
8389
event_type="org.ban.ban",
8490
is_phantom=True,
85-
schema='{"type":"object"}',
91+
schema={"type": "object"},
8692
),
8793
EventType(
8894
event_type="org.bing.chilling",
8995
is_phantom=True,
90-
schema='{"type":"object"}',
96+
schema={"type": "object"},
9197
),
9298
}
9399

0 commit comments

Comments
 (0)