def test_remove_listener(self): mock = unittest.mock.Mock() nh = TagDispatcher() nh.add_callback("tag", mock) nh.remove_listener("tag") with self.assertRaises(KeyError): nh.unicast("tag", object()) mock.assert_not_called()
def test_abort_display_aborts_message_when_quiet(): "abort message is *not* displayed when `settings.display_aborts` is `True` and `settings.quiet` is also `True`" result = {"foo": "bar"} with state.settings(display_aborts=True, quiet=True): with patch("threadbare.operations.LOG.error") as mock: with pytest.raises(RuntimeError): operations.abort(result, "failed to succeed") mock.assert_not_called()
def test_nonexistent_email(self, mock: mock.Mock) -> None: self.submit_form('login', 'input_id_login', send_form_keys={ 'id_email': '*****@*****.**' }) mock.assert_not_called() self.assert_url_equal('token_sent')
def test_update_training_data_set_no_update(mock, data_set_manager, file_repository_stub, workspace_stub): initial_file_count_in_repository = len(file_repository_stub.files) data_set_manager.update_training_data_set() mock.assert_not_called() assert workspace_stub.training_data_set.last_modified == DataSourceStub.last_modified( workspace_stub.user_id, workspace_stub._id) assert len(file_repository_stub.files) == initial_file_count_in_repository
def test_gsheets_periodically_process_pending_does_not_always_rotate(gsheets_handler_no_thread): def side_effect(): gsheets_handler_no_thread.close() with unittest.mock.patch.object(gsheets_handler_no_thread, 'process_pending_rows', side_effect=side_effect) as k: with unittest.mock.patch.object(gsheets_handler_no_thread, '_rotate_to_new_sheet_in_workbook') as mock: gsheets_handler_no_thread._periodically_process_pending_rows() k.assert_called_once() mock.assert_not_called()
def test_no_inputs(self, mocker): """Ensure the proper default parameters.""" # Make sure that eval wasn't called mock = mocker.patch('matl_online.octave.OctaveSession.eval') session = OctaveSession() assert session.octaverc is None assert session.paths == [] mock.assert_not_called()
def test_connect_async(self): signal = AdHocSignal() mock = unittest.mock.MagicMock() fun = functools.partial(mock) signal.connect(fun, AdHocSignal.ASYNC_WITH_LOOP(None)) signal.fire() mock.assert_not_called() run_coroutine(asyncio.sleep(0)) mock.assert_called_once_with()
def test_gsheets_process_pending_rows(gsheets_handler_no_thread): # no rows means don't call _add_rows_to_active_sheet gsheets_handler_no_thread._pending_rows = [] with unittest.mock.patch.object(gsheets_handler_no_thread, '_add_rows_to_active_sheet') as mock: gsheets_handler_no_thread.process_pending_rows() mock.assert_not_called() # more rows than max per interval, means call with max allowed and remove those from pending gsheets_handler_no_thread._pending_rows = [a for a in range(google_sheets_handler.MAX_EVENTS_TO_PROCESS_PER_INTERVAL * 2)] with unittest.mock.patch.object(gsheets_handler_no_thread, '_add_rows_to_active_sheet') as mock: gsheets_handler_no_thread.process_pending_rows() mock.assert_called_once_with([a for a in range(google_sheets_handler.MAX_EVENTS_TO_PROCESS_PER_INTERVAL)]) assert len(gsheets_handler_no_thread._pending_rows) == google_sheets_handler.MAX_EVENTS_TO_PROCESS_PER_INTERVAL assert gsheets_handler_no_thread._pending_rows == [a for a in range(google_sheets_handler.MAX_EVENTS_TO_PROCESS_PER_INTERVAL, google_sheets_handler.MAX_EVENTS_TO_PROCESS_PER_INTERVAL*2)]
def test_download_preexisting(self, mock): with self.mocked_dataset(pre_extract=True) as (dataset, data): mock.assert_not_called()
def testFalseSms(self, mock: unittest.mock.Mock): sms = RawSMS(None, None, '#34#25#3#') handleWifiSMS(sms) mock.assert_not_called()
def test_gsheets_init_calls_make_owner_if_not_already_if_email_given(gsheets_handler_no_thread): with unittest.mock.patch.object(gsheets_handler_no_thread, '_make_owner_if_not_already') as mock: gsheets_handler_no_thread.__init__('test') mock.assert_not_called() gsheets_handler_no_thread.__init__('test', share_email='*****@*****.**') mock.assert_called_once()
def test_get_elided_page_range(self): # Paginator.validate_number() must be called: paginator = Paginator([1, 2, 3], 2) with unittest.mock.patch.object(paginator, "validate_number") as mock: mock.assert_not_called() list(paginator.get_elided_page_range(2)) mock.assert_called_with(2) ELLIPSIS = Paginator.ELLIPSIS # Range is not elided if not enough pages when using default arguments: paginator = Paginator(range(10 * 100), 100) page_range = paginator.get_elided_page_range(1) self.assertIsInstance(page_range, collections.abc.Generator) self.assertNotIn(ELLIPSIS, page_range) paginator = Paginator(range(10 * 100 + 1), 100) self.assertIsInstance(page_range, collections.abc.Generator) page_range = paginator.get_elided_page_range(1) self.assertIn(ELLIPSIS, page_range) # Range should be elided if enough pages when using default arguments: tests = [ # on_each_side=3, on_ends=2 (1, [1, 2, 3, 4, ELLIPSIS, 49, 50]), (6, [1, 2, 3, 4, 5, 6, 7, 8, 9, ELLIPSIS, 49, 50]), (7, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ELLIPSIS, 49, 50]), (8, [1, 2, ELLIPSIS, 5, 6, 7, 8, 9, 10, 11, ELLIPSIS, 49, 50]), (43, [1, 2, ELLIPSIS, 40, 41, 42, 43, 44, 45, 46, ELLIPSIS, 49, 50]), (44, [1, 2, ELLIPSIS, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]), (45, [1, 2, ELLIPSIS, 42, 43, 44, 45, 46, 47, 48, 49, 50]), (50, [1, 2, ELLIPSIS, 47, 48, 49, 50]), ] paginator = Paginator(range(5000), 100) for number, expected in tests: with self.subTest(number=number): page_range = paginator.get_elided_page_range(number) self.assertIsInstance(page_range, collections.abc.Generator) self.assertEqual(list(page_range), expected) # Range is not elided if not enough pages when using custom arguments: tests = [ (6, 2, 1, 1), (8, 1, 3, 1), (8, 4, 0, 1), (4, 1, 1, 1), # When on_each_side and on_ends are both <= 1 but not both == 1 it # is a special case where the range is not elided until an extra # page is added. (2, 0, 1, 2), (2, 1, 0, 2), (1, 0, 0, 2), ] for pages, on_each_side, on_ends, elided_after in tests: for offset in range(elided_after + 1): with self.subTest( pages=pages, offset=elided_after, on_each_side=on_each_side, on_ends=on_ends, ): paginator = Paginator(range((pages + offset) * 100), 100) page_range = paginator.get_elided_page_range( 1, on_each_side=on_each_side, on_ends=on_ends, ) self.assertIsInstance(page_range, collections.abc.Generator) if offset < elided_after: self.assertNotIn(ELLIPSIS, page_range) else: self.assertIn(ELLIPSIS, page_range) # Range should be elided if enough pages when using custom arguments: tests = [ # on_each_side=2, on_ends=1 (1, 2, 1, [1, 2, 3, ELLIPSIS, 50]), (4, 2, 1, [1, 2, 3, 4, 5, 6, ELLIPSIS, 50]), (5, 2, 1, [1, 2, 3, 4, 5, 6, 7, ELLIPSIS, 50]), (6, 2, 1, [1, ELLIPSIS, 4, 5, 6, 7, 8, ELLIPSIS, 50]), (45, 2, 1, [1, ELLIPSIS, 43, 44, 45, 46, 47, ELLIPSIS, 50]), (46, 2, 1, [1, ELLIPSIS, 44, 45, 46, 47, 48, 49, 50]), (47, 2, 1, [1, ELLIPSIS, 45, 46, 47, 48, 49, 50]), (50, 2, 1, [1, ELLIPSIS, 48, 49, 50]), # on_each_side=1, on_ends=3 (1, 1, 3, [1, 2, ELLIPSIS, 48, 49, 50]), (5, 1, 3, [1, 2, 3, 4, 5, 6, ELLIPSIS, 48, 49, 50]), (6, 1, 3, [1, 2, 3, 4, 5, 6, 7, ELLIPSIS, 48, 49, 50]), (7, 1, 3, [1, 2, 3, ELLIPSIS, 6, 7, 8, ELLIPSIS, 48, 49, 50]), (44, 1, 3, [1, 2, 3, ELLIPSIS, 43, 44, 45, ELLIPSIS, 48, 49, 50]), (45, 1, 3, [1, 2, 3, ELLIPSIS, 44, 45, 46, 47, 48, 49, 50]), (46, 1, 3, [1, 2, 3, ELLIPSIS, 45, 46, 47, 48, 49, 50]), (50, 1, 3, [1, 2, 3, ELLIPSIS, 49, 50]), # on_each_side=4, on_ends=0 (1, 4, 0, [1, 2, 3, 4, 5, ELLIPSIS]), (5, 4, 0, [1, 2, 3, 4, 5, 6, 7, 8, 9, ELLIPSIS]), (6, 4, 0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ELLIPSIS]), (7, 4, 0, [ELLIPSIS, 3, 4, 5, 6, 7, 8, 9, 10, 11, ELLIPSIS]), (44, 4, 0, [ELLIPSIS, 40, 41, 42, 43, 44, 45, 46, 47, 48, ELLIPSIS]), (45, 4, 0, [ELLIPSIS, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]), (46, 4, 0, [ELLIPSIS, 42, 43, 44, 45, 46, 47, 48, 49, 50]), (50, 4, 0, [ELLIPSIS, 46, 47, 48, 49, 50]), # on_each_side=0, on_ends=1 (1, 0, 1, [1, ELLIPSIS, 50]), (2, 0, 1, [1, 2, ELLIPSIS, 50]), (3, 0, 1, [1, 2, 3, ELLIPSIS, 50]), (4, 0, 1, [1, ELLIPSIS, 4, ELLIPSIS, 50]), (47, 0, 1, [1, ELLIPSIS, 47, ELLIPSIS, 50]), (48, 0, 1, [1, ELLIPSIS, 48, 49, 50]), (49, 0, 1, [1, ELLIPSIS, 49, 50]), (50, 0, 1, [1, ELLIPSIS, 50]), # on_each_side=0, on_ends=0 (1, 0, 0, [1, ELLIPSIS]), (2, 0, 0, [1, 2, ELLIPSIS]), (3, 0, 0, [ELLIPSIS, 3, ELLIPSIS]), (48, 0, 0, [ELLIPSIS, 48, ELLIPSIS]), (49, 0, 0, [ELLIPSIS, 49, 50]), (50, 0, 0, [ELLIPSIS, 50]), ] paginator = Paginator(range(5000), 100) for number, on_each_side, on_ends, expected in tests: with self.subTest(number=number, on_each_side=on_each_side, on_ends=on_ends): page_range = paginator.get_elided_page_range( number, on_each_side=on_each_side, on_ends=on_ends, ) self.assertIsInstance(page_range, collections.abc.Generator) self.assertEqual(list(page_range), expected)
def test_skipping_s3_storage_decorator_with_non_s3_storage(self): mock = MagicMock() skip_if_s3_storage_not_used(mock)() mock.assert_not_called()