Skip to content

Commit 08d46ee

Browse files
committed
Updated mock to make tests green again
1 parent b4066ed commit 08d46ee

File tree

4 files changed

+102
-94
lines changed

4 files changed

+102
-94
lines changed

tests/integrations/asgi/test_asgi.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ async def app(scope, receive, send):
128128

129129
@pytest.fixture
130130
def asgi3_custom_transaction_app():
131-
132131
async def app(scope, receive, send):
133132
sentry_sdk.get_current_scope().set_transaction_name("foobar", source="custom")
134133
await send(

tests/integrations/celery/test_celery.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ def test_messaging_destination_name_default_exchange(
597597
mock_request.delivery_info = {"routing_key": routing_key, "exchange": ""}
598598

599599
@celery_app.task()
600-
def task(): ...
600+
def task():
601+
...
601602

602603
task.apply_async()
603604

@@ -621,7 +622,8 @@ def test_messaging_destination_name_nondefault_exchange(
621622
mock_request.delivery_info = {"routing_key": "celery", "exchange": "custom"}
622623

623624
@celery_app.task()
624-
def task(): ...
625+
def task():
626+
...
625627

626628
task.apply_async()
627629

@@ -635,7 +637,8 @@ def test_messaging_id(init_celery, capture_events):
635637
events = capture_events()
636638

637639
@celery.task
638-
def example_task(): ...
640+
def example_task():
641+
...
639642

640643
example_task.apply_async()
641644

@@ -649,7 +652,8 @@ def test_retry_count_zero(init_celery, capture_events):
649652
events = capture_events()
650653

651654
@celery.task()
652-
def task(): ...
655+
def task():
656+
...
653657

654658
task.apply_async()
655659

@@ -666,7 +670,8 @@ def test_retry_count_nonzero(mock_request, init_celery, capture_events):
666670
events = capture_events()
667671

668672
@celery.task()
669-
def task(): ...
673+
def task():
674+
...
670675

671676
task.apply_async()
672677

@@ -684,7 +689,8 @@ def test_messaging_system(system, init_celery, capture_events):
684689
celery.conf.broker_url = f"{system}://example.com" # noqa: E231
685690

686691
@celery.task()
687-
def task(): ...
692+
def task():
693+
...
688694

689695
task.apply_async()
690696

@@ -707,7 +713,8 @@ def publish(*args, **kwargs):
707713
events = capture_events()
708714

709715
@celery.task()
710-
def task(): ...
716+
def task():
717+
...
711718

712719
with start_transaction():
713720
task.apply_async()
@@ -729,7 +736,8 @@ def test_receive_latency(init_celery, capture_events):
729736
events = capture_events()
730737

731738
@celery.task()
732-
def task(): ...
739+
def task():
740+
...
733741

734742
task.apply_async()
735743

@@ -746,7 +754,8 @@ def tests_span_origin_consumer(init_celery, capture_events):
746754
events = capture_events()
747755

748756
@celery.task()
749-
def task(): ...
757+
def task():
758+
...
750759

751760
task.apply_async()
752761

@@ -770,7 +779,8 @@ def publish(*args, **kwargs):
770779
events = capture_events()
771780

772781
@celery.task()
773-
def task(): ...
782+
def task():
783+
...
774784

775785
with start_transaction(name="custom_transaction"):
776786
task.apply_async()

tests/integrations/django/asgi/test_asgi.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,8 @@ async def test_asgi_request_body(
566566
async def test_asgi_mixin_iscoroutinefunction_before_3_12():
567567
sentry_asgi_mixin = _asgi_middleware_mixin_factory(lambda: None)
568568

569-
async def get_response(): ...
569+
async def get_response():
570+
...
570571

571572
instance = sentry_asgi_mixin(get_response)
572573
assert asyncio.iscoroutinefunction(instance)
@@ -581,7 +582,8 @@ async def get_response(): ...
581582
def test_asgi_mixin_iscoroutinefunction_when_not_async_before_3_12():
582583
sentry_asgi_mixin = _asgi_middleware_mixin_factory(lambda: None)
583584

584-
def get_response(): ...
585+
def get_response():
586+
...
585587

586588
instance = sentry_asgi_mixin(get_response)
587589
assert not asyncio.iscoroutinefunction(instance)
@@ -597,7 +599,8 @@ def get_response(): ...
597599
async def test_asgi_mixin_iscoroutinefunction_after_3_12():
598600
sentry_asgi_mixin = _asgi_middleware_mixin_factory(lambda: None)
599601

600-
async def get_response(): ...
602+
async def get_response():
603+
...
601604

602605
instance = sentry_asgi_mixin(get_response)
603606
assert inspect.iscoroutinefunction(instance)
@@ -612,7 +615,8 @@ async def get_response(): ...
612615
def test_asgi_mixin_iscoroutinefunction_when_not_async_after_3_12():
613616
sentry_asgi_mixin = _asgi_middleware_mixin_factory(lambda: None)
614617

615-
def get_response(): ...
618+
def get_response():
619+
...
616620

617621
instance = sentry_asgi_mixin(get_response)
618622
assert not inspect.iscoroutinefunction(instance)

tests/integrations/spark/test_spark.py

Lines changed: 74 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -87,88 +87,83 @@ def sentry_listener():
8787
return listener
8888

8989

90-
@pytest.fixture
91-
def mock_add_breadcrumb():
92-
with patch("sentry_sdk.add_breadcrumb") as mock:
93-
yield mock
94-
95-
96-
def test_sentry_listener_on_job_start(sentry_listener, mock_add_breadcrumb):
90+
def test_sentry_listener_on_job_start(sentry_listener):
9791
listener = sentry_listener
92+
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
9893

99-
class MockJobStart:
100-
def jobId(self): # noqa: N802
101-
return "sample-job-id-start"
94+
class MockJobStart:
95+
def jobId(self): # noqa: N802
96+
return "sample-job-id-start"
10297

103-
mock_job_start = MockJobStart()
104-
listener.onJobStart(mock_job_start)
98+
mock_job_start = MockJobStart()
99+
listener.onJobStart(mock_job_start)
105100

106-
mock_add_breadcrumb.assert_called_once()
107-
mock_hub = mock_add_breadcrumb.call_args
101+
mock_add_breadcrumb.assert_called_once()
102+
mock_hub = mock_add_breadcrumb.call_args
108103

109-
assert mock_hub.kwargs["level"] == "info"
110-
assert "sample-job-id-start" in mock_hub.kwargs["message"]
104+
assert mock_hub.kwargs["level"] == "info"
105+
assert "sample-job-id-start" in mock_hub.kwargs["message"]
111106

112107

113108
@pytest.mark.parametrize(
114109
"job_result, level", [("JobSucceeded", "info"), ("JobFailed", "warning")]
115110
)
116-
def test_sentry_listener_on_job_end(
117-
sentry_listener, mock_add_breadcrumb, job_result, level
118-
):
111+
def test_sentry_listener_on_job_end(sentry_listener, job_result, level):
119112
listener = sentry_listener
113+
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
120114

121-
class MockJobResult:
122-
def toString(self): # noqa: N802
123-
return job_result
115+
class MockJobResult:
116+
def toString(self): # noqa: N802
117+
return job_result
124118

125-
class MockJobEnd:
126-
def jobId(self): # noqa: N802
127-
return "sample-job-id-end"
119+
class MockJobEnd:
120+
def jobId(self): # noqa: N802
121+
return "sample-job-id-end"
128122

129-
def jobResult(self): # noqa: N802
130-
result = MockJobResult()
131-
return result
123+
def jobResult(self): # noqa: N802
124+
result = MockJobResult()
125+
return result
132126

133-
mock_job_end = MockJobEnd()
134-
listener.onJobEnd(mock_job_end)
127+
mock_job_end = MockJobEnd()
128+
listener.onJobEnd(mock_job_end)
135129

136-
mock_add_breadcrumb.assert_called_once()
137-
mock_hub = mock_add_breadcrumb.call_args
130+
mock_add_breadcrumb.assert_called_once()
131+
mock_hub = mock_add_breadcrumb.call_args
138132

139-
assert mock_hub.kwargs["level"] == level
140-
assert mock_hub.kwargs["data"]["result"] == job_result
141-
assert "sample-job-id-end" in mock_hub.kwargs["message"]
133+
assert mock_hub.kwargs["level"] == level
134+
assert mock_hub.kwargs["data"]["result"] == job_result
135+
assert "sample-job-id-end" in mock_hub.kwargs["message"]
142136

143137

144-
def test_sentry_listener_on_stage_submitted(sentry_listener, mock_add_breadcrumb):
138+
def test_sentry_listener_on_stage_submitted(sentry_listener):
145139
listener = sentry_listener
140+
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
146141

147-
class StageInfo:
148-
def stageId(self): # noqa: N802
149-
return "sample-stage-id-submit"
142+
class StageInfo:
143+
def stageId(self): # noqa: N802
144+
return "sample-stage-id-submit"
150145

151-
def name(self):
152-
return "run-job"
146+
def name(self):
147+
return "run-job"
153148

154-
def attemptId(self): # noqa: N802
155-
return 14
149+
def attemptId(self): # noqa: N802
150+
return 14
156151

157-
class MockStageSubmitted:
158-
def stageInfo(self): # noqa: N802
159-
stageinf = StageInfo()
160-
return stageinf
152+
class MockStageSubmitted:
153+
def stageInfo(self): # noqa: N802
154+
stageinf = StageInfo()
155+
return stageinf
161156

162-
mock_stage_submitted = MockStageSubmitted()
163-
listener.onStageSubmitted(mock_stage_submitted)
157+
mock_stage_submitted = MockStageSubmitted()
158+
listener.onStageSubmitted(mock_stage_submitted)
164159

165-
mock_add_breadcrumb.assert_called_once()
166-
mock_hub = mock_add_breadcrumb.call_args
160+
mock_add_breadcrumb.assert_called_once()
161+
mock_hub = mock_add_breadcrumb.call_args
167162

168-
assert mock_hub.kwargs["level"] == "info"
169-
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
170-
assert mock_hub.kwargs["data"]["attemptId"] == 14
171-
assert mock_hub.kwargs["data"]["name"] == "run-job"
163+
assert mock_hub.kwargs["level"] == "info"
164+
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
165+
assert mock_hub.kwargs["data"]["attemptId"] == 14
166+
assert mock_hub.kwargs["data"]["name"] == "run-job"
172167

173168

174169
@pytest.fixture
@@ -208,39 +203,39 @@ def stageInfo(self): # noqa: N802
208203

209204

210205
def test_sentry_listener_on_stage_completed_success(
211-
sentry_listener, mock_add_breadcrumb, get_mock_stage_completed
206+
sentry_listener, get_mock_stage_completed
212207
):
213208
listener = sentry_listener
209+
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
210+
mock_stage_completed = get_mock_stage_completed(failure_reason=False)
211+
listener.onStageCompleted(mock_stage_completed)
214212

215-
mock_stage_completed = get_mock_stage_completed(failure_reason=False)
216-
listener.onStageCompleted(mock_stage_completed)
213+
mock_add_breadcrumb.assert_called_once()
214+
mock_hub = mock_add_breadcrumb.call_args
217215

218-
mock_add_breadcrumb.assert_called_once()
219-
mock_hub = mock_add_breadcrumb.call_args
220-
221-
assert mock_hub.kwargs["level"] == "info"
222-
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
223-
assert mock_hub.kwargs["data"]["attemptId"] == 14
224-
assert mock_hub.kwargs["data"]["name"] == "run-job"
225-
assert "reason" not in mock_hub.kwargs["data"]
216+
assert mock_hub.kwargs["level"] == "info"
217+
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
218+
assert mock_hub.kwargs["data"]["attemptId"] == 14
219+
assert mock_hub.kwargs["data"]["name"] == "run-job"
220+
assert "reason" not in mock_hub.kwargs["data"]
226221

227222

228223
def test_sentry_listener_on_stage_completed_failure(
229-
sentry_listener, mock_add_breadcrumb, get_mock_stage_completed
224+
sentry_listener, get_mock_stage_completed
230225
):
231226
listener = sentry_listener
232-
233-
mock_stage_completed = get_mock_stage_completed(failure_reason=True)
234-
listener.onStageCompleted(mock_stage_completed)
235-
236-
mock_add_breadcrumb.assert_called_once()
237-
mock_hub = mock_add_breadcrumb.call_args
238-
239-
assert mock_hub.kwargs["level"] == "warning"
240-
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
241-
assert mock_hub.kwargs["data"]["attemptId"] == 14
242-
assert mock_hub.kwargs["data"]["name"] == "run-job"
243-
assert mock_hub.kwargs["data"]["reason"] == "failure-reason"
227+
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
228+
mock_stage_completed = get_mock_stage_completed(failure_reason=True)
229+
listener.onStageCompleted(mock_stage_completed)
230+
231+
mock_add_breadcrumb.assert_called_once()
232+
mock_hub = mock_add_breadcrumb.call_args
233+
234+
assert mock_hub.kwargs["level"] == "warning"
235+
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
236+
assert mock_hub.kwargs["data"]["attemptId"] == 14
237+
assert mock_hub.kwargs["data"]["name"] == "run-job"
238+
assert mock_hub.kwargs["data"]["reason"] == "failure-reason"
244239

245240

246241
################

0 commit comments

Comments
 (0)