@@ -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
210205def 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
228223def 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