-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_write_events.py
More file actions
314 lines (265 loc) · 10 KB
/
test_write_events.py
File metadata and controls
314 lines (265 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
from collections.abc import Callable, Awaitable
from http import HTTPStatus
import pytest
from eventsourcingdb.client import Client
from eventsourcingdb.errors.client_error import ClientError
from eventsourcingdb.errors.invalid_parameter_error import InvalidParameterError
from eventsourcingdb.errors.server_error import ServerError
from eventsourcingdb.event.event_candidate import EventCandidate
from eventsourcingdb.handlers.write_events import \
IsSubjectPristinePrecondition, \
IsSubjectOnEventIdPrecondition
from .conftest import TestData
from .shared.database import Database
from .shared.start_local_http_server import \
AttachHandler, \
Response, \
AttachHandlers
class TestWriteSubjects:
@staticmethod
@pytest.mark.asyncio
async def test_throws_an_error_if_server_is_not_reachable(
database: Database,
test_data: TestData,
):
client = database.with_invalid_url.client
with pytest.raises(ServerError):
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='com.foo.bar',
data={}
)
])
@staticmethod
@pytest.mark.asyncio
async def test_throws_an_error_for_empty_event_candidates(
database: Database,
):
client = database.with_authorization.client
with pytest.raises(InvalidParameterError):
await client.write_events([])
@staticmethod
@pytest.mark.asyncio
async def test_throws_an_error_for_malformed_subject(
database: Database,
test_data: TestData,
):
client = database.with_authorization.client
with pytest.raises(InvalidParameterError):
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='',
event_type='com.foo.bar',
data={}
)
])
@staticmethod
@pytest.mark.asyncio
async def test_throws_an_error_for_malformed_type(
database: Database,
test_data: TestData,
):
client = database.with_authorization.client
with pytest.raises(InvalidParameterError):
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='',
data={}
)
])
@staticmethod
@pytest.mark.asyncio
async def test_supports_authorization(
database: Database,
test_data: TestData,
):
client = database.with_authorization.client
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='com.foo.bar',
data={}
)
])
@staticmethod
@pytest.mark.asyncio
async def test_is_pristine_precondition_works_for_new_subject(
database: Database,
test_data: TestData,
):
client = database.with_authorization.client
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='com.foo.bar',
data={}
)],
[IsSubjectPristinePrecondition('/')]
)
@staticmethod
@pytest.mark.asyncio
async def test_is_pristine_precondition_fails_for_existing_subject(
database: Database,
test_data: TestData,
):
client = database.with_authorization.client
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='com.foo.bar',
data={}
)]
)
with pytest.raises(ClientError):
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='com.foo.bar',
data={}
)],
[IsSubjectPristinePrecondition('/')]
)
@staticmethod
@pytest.mark.asyncio
async def test_is_on_event_id_precondition_works_for_correct_id(
database: Database,
test_data: TestData,
):
client = database.with_authorization.client
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='com.foo.bar',
data={}
)]
)
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='com.foo.bar',
data={}
)],
[IsSubjectOnEventIdPrecondition('/', '0')]
)
@staticmethod
@pytest.mark.asyncio
async def test_is_subject_on_event_id_fails_for_wrong_id(
database: Database,
test_data: TestData,
):
client = database.with_authorization.client
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='com.foo.bar',
data={}
)]
)
with pytest.raises(ClientError):
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject='/',
event_type='com.foo.bar',
data={}
)],
[IsSubjectOnEventIdPrecondition('/', '2')]
)
@staticmethod
@pytest.mark.asyncio
async def test_throws_error_if_event_does_not_match_schema(
database: Database,
test_data: TestData,
):
client = database.with_authorization.client
await client.register_event_schema(
"com.super.duper",
{"type": "object", "additionalProperties": False}
)
with pytest.raises(ClientError, match="event candidate does not match schema"):
await client.write_events([
test_data.TEST_SOURCE.new_event(
subject="/",
event_type="com.super.duper",
data={
"haft": "befehl",
},
),
])
class TestWriteEventsWithMockServer:
@staticmethod
@pytest.mark.asyncio
async def test_throws_error_if_server_responds_with_5xx_status_code(
get_client: Callable[[AttachHandlers], Awaitable[Client]],
events_for_mocked_server: list[EventCandidate],
):
def attach_handlers(attach_handler: AttachHandler):
def handle_write_events(response: Response) -> Response:
response.status_code = HTTPStatus.BAD_GATEWAY
response.set_data(HTTPStatus.BAD_GATEWAY.phrase)
return response
attach_handler('/api/v1/write-events', 'POST', handle_write_events)
client = await get_client(attach_handlers)
with pytest.raises(ServerError):
await client.write_events(events_for_mocked_server)
@staticmethod
@pytest.mark.asyncio
async def test_throws_error_if_protocol_version_does_not_match(
get_client: Callable[[AttachHandlers], Awaitable[Client]],
events_for_mocked_server: list[EventCandidate],
):
def attach_handlers(attach_handler: AttachHandler):
def handle_write_events(response: Response) -> Response:
response.headers['X-EventSourcingDB-Protocol-Version'] = '0.0.0'
response.status_code = HTTPStatus.UNPROCESSABLE_ENTITY
response.set_data(HTTPStatus.UNPROCESSABLE_ENTITY.phrase)
return response
attach_handler('/api/v1/write-events', 'POST', handle_write_events)
client = await get_client(attach_handlers)
with pytest.raises(ClientError):
await client.write_events(events_for_mocked_server)
@staticmethod
@pytest.mark.asyncio
async def test_throws_error_if_server_responds_with_4xx_status_code(
get_client: Callable[[AttachHandlers], Awaitable[Client]],
events_for_mocked_server: list[EventCandidate],
):
def attach_handlers(attach_handler: AttachHandler):
def handle_write_events(response: Response) -> Response:
response.status_code = HTTPStatus.NOT_FOUND
response.set_data(HTTPStatus.NOT_FOUND.phrase)
return response
attach_handler('/api/v1/write-events', 'POST', handle_write_events)
client = await get_client(attach_handlers)
with pytest.raises(ClientError):
await client.write_events(events_for_mocked_server)
@staticmethod
@pytest.mark.asyncio
async def test_throws_error_if_server_responds_with_unexpected_status_code(
get_client: Callable[[AttachHandlers], Awaitable[Client]],
events_for_mocked_server: list[EventCandidate],
):
def attach_handlers(attach_handler: AttachHandler):
def handle_write_events(response: Response) -> Response:
response.status_code = HTTPStatus.ACCEPTED
response.set_data(HTTPStatus.ACCEPTED.phrase)
return response
attach_handler('/api/v1/write-events', 'POST', handle_write_events)
client = await get_client(attach_handlers)
with pytest.raises(ServerError):
await client.write_events(events_for_mocked_server)
@staticmethod
@pytest.mark.asyncio
async def test_throws_error_if_response_cannot_be_parsed(
get_client: Callable[[AttachHandlers], Awaitable[Client]],
events_for_mocked_server: list[EventCandidate],
):
def attach_handlers(attach_handler: AttachHandler):
def handle_write_events(response: Response) -> Response:
response.set_data('this is not data')
return response
attach_handler('/api/v1/write-events', 'POST', handle_write_events)
client = await get_client(attach_handlers)
with pytest.raises(ServerError):
await client.write_events(events_for_mocked_server)