async def test_play_repeating_sound_plays_sound_for_repeat_count_times( self, example_sound_manager, monkeypatch): """ Test that `_play_repeating_sound()` will call `_play_sound()` for `repeat_count` times (attribute on the `Sound` instance). """ group = example_sound_manager.groups[0] sound = group.sounds[0] sound.repeat_count = 5 play_sound_mock = CoroutineMock() example_sound_manager._play_sound = play_sound_mock await example_sound_manager._play_repeating_sound(MagicMock(), 0, 0) assert play_sound_mock.await_count == 5 play_sound_mock.assert_has_awaits( [call(0, 0), call(0, 0), call(0, 0), call(0, 0)])
async def test_play_repeating_sound_waits_for_repeat_delay_before_repeating( self, example_sound_manager, monkeypatch): """ Test that the `_play_repeating_sound()` will wait for `repeat_delay` ms. """ sleep_mock = CoroutineMock() monkeypatch.setattr("src.sound.sound_manager.asyncio.sleep", sleep_mock) group = example_sound_manager.groups[0] sound = group.sounds[0] sound.repeat_count = 4 sound.repeat_delay = 42 example_sound_manager._play_sound = CoroutineMock() await example_sound_manager._play_repeating_sound(MagicMock(), 0, 0) assert sleep_mock.await_count == 3 sleep_mock.assert_has_awaits( [call(42 / 1000.0), call(42 / 1000.0), call(42 / 1000.0)])
async def test_report_job_statuses_failure(self, sleep_mock: CoroutineMock): self.session.post.side_effect = [ ConnectionError(), Mock(status=502), ValueError(), Mock(status=400), ] job_updates = { EmailResult.SUCCESS: [{ "id": 142, "message_id": "df431-ac221" }], EmailResult.FAILURE: [42, 49], } client = WebClient("http://service", self.session) await client.report_job_status(job_updates) self.session.post.assert_has_awaits([ call( "http://service", json={ "success": [{ "id": 142, "message_id": "df431-ac221" }], "failure": [42, 49], }, ) ] * 4) sleep_mock.assert_has_awaits([call(3), call(9), call(27)]) self.logger_mock.warning.assert_called() self.logger_mock.error.assert_called() self.logger_mock.critical.assert_called()
async def test_play_track_list_plays_all_tracks_once_if_no_loop( self, example_music_manager, monkeypatch): """ The _play_track_list() method should call _play_track() for every track in it. """ play_track_mock = CoroutineMock() monkeypatch.setattr("src.music.music_manager.MusicManager._play_track", play_track_mock) group = example_music_manager.groups[0] track_list = group.track_lists[0] track_list._tracks = [Track("track-1.mp3"), Track("track-2.mp3")] track_list.loop = False await example_music_manager._play_track_list(request=None, group_index=0, track_list_index=0) assert play_track_mock.await_count == 2 play_track_mock.assert_has_awaits( [ call(group, track_list, track_list._tracks[0]), call(group, track_list, track_list._tracks[1]) ], any_order=True, )
async def test_play_repeating_sound_repeats_if_repeat_count_is_zero( self, example_sound_manager, monkeypatch): """ Test that the `_play_repeating_sound()` will repeatedly call `_play_sound()` if the `repeat_count` attribute on the `Sound` instance is zero (meaning infinite replays). """ group = example_sound_manager.groups[0] sound = group.sounds[0] sound.repeat_count = 0 count = 0 def unset_infinite_repeat(*args, **kwargs): nonlocal count nonlocal sound count += 1 if count == 3: sound.repeat_count = 1 # to stop the replay as it has exceeded a single replay play_sound_mock = CoroutineMock(side_effect=unset_infinite_repeat) example_sound_manager._play_sound = play_sound_mock request_mock = MagicMock() await example_sound_manager._play_repeating_sound(request_mock, 0, 0) assert play_sound_mock.await_count == 3 play_sound_mock.assert_has_awaits([call(0, 0), call(0, 0), call(0, 0)])
async def test_send_emails(self): send_email_mock = CoroutineMock(side_effect=[ ( EmailResult.SUCCESS, "<*****@*****.**>", ), (EmailResult.FAILURE, None), (EmailResult.AUTH_FAILURE, None), ( EmailResult.SUCCESS, "<*****@*****.**>", ), (EmailResult.RECOVERABLE_FAILURE, None), ]) client = FlypsGatewayClient("http://test.co", self.session) send_email_patch = patch.object(client, "_send_email", send_email_mock) jobs = [ { "id": 132, "email": "*****@*****.**", "first_name": "Janusz" }, { "id": 133, "email": "*****@*****.**", "first_name": "Jan" }, { "id": 134, "email": "*****@*****.**", "first_name": "Janina" }, { "id": 135, "email": "*****@*****.**", "first_name": "Janusz" }, { "id": 136, "email": "*****@*****.**", "first_name": "Frida" }, ] auth = ("admin", "admin1") headers = {"reply-to": "admin <*****@*****.**>"} email_from = {"name": "Admin", "email": "*****@*****.**"} template = "Hi! This is an email." subject = "Subject" with send_email_patch: results, retry = await client.send_emails(jobs, template, subject, auth, email_from, headers) self.assertEqual( results, { EmailResult.SUCCESS: [ { "id": 132, "message_id": "<*****@*****.**>", }, { "id": 135, "message_id": "<*****@*****.**>", }, ], EmailResult.FAILURE: [{ "id": 133, "message_id": "" }], EmailResult.RECOVERABLE_FAILURE: [{ "id": 136, "message_id": "" }], EmailResult.AUTH_FAILURE: [{ "id": 134, "message_id": "" }], }, ) self.assertEqual(retry, [{ "id": 136, "email": "*****@*****.**", "first_name": "Frida" }]) send_email_mock.assert_has_awaits([ call(job, template, subject, auth, email_from, headers) for job in jobs ])