def test_error_adapter_required_root_parameter(self): prop_name = 'herpaderp' message = "'%s' is a required property" % prop_name path = [] error = self._get_error_adapter('required', message, path) T.assert_is(error.message, message) T.assert_equal(error.dotted_path, prop_name)
def test_error_adapter_required_adapter(self): prop_name = 'herpaderp' message = "'%s' is a required property" % prop_name path = ['foo', 'bar'] error = self._get_error_adapter('required', message, path) T.assert_is(error.message, message) T.assert_equal(error.dotted_path, '.'.join(path + [prop_name]))
def test_multi_get_max_retry(self): """Tests the case when the number of the maximum retries is reached, due to the unsuccessful responses. `grequests.map` is called 3 times (based on `max_retry`), each time there is only one successful response. Eventually the call to `multi_get` returns the responses among which one is unsuccessful (`None`). """ number_of_requests = 4 query_params = [{'John Fitzgerald': 'Tom Hardy'}] * number_of_requests responses_to_calls = [ self.mock_ok_responses(number_of_requests), self.mock_ok_responses(number_of_requests - 1), self.mock_ok_responses(number_of_requests - 2) ] # mock unsuccessful responses to the first call to grequests.map self.mock_unsuccessful_responses(responses_to_calls[0][0:3]) # mock unsuccessful responses to the second call to grequests.map self.mock_unsuccessful_responses(responses_to_calls[1][1:3]) # mock unsuccessful response to the third call to grequests.map self.mock_unsuccessful_response(responses_to_calls[2][1]) grequests.map = MagicMock() grequests.map.side_effect = responses_to_calls actual_responses = MultiRequest(max_retry=3).multi_get('example.com', query_params) T.assert_equal(3, grequests.map.call_count) T.assert_is(None, actual_responses[2])
def test_multi_get_max_retry(self): """Tests the case when the number of the maximum retries is reached, due to the unsuccessful responses. `grequests.map` is called 3 times (based on `max_retry`), each time there is only one successful response. Eventually the call to `multi_get` returns the responses among which one is unsuccessful (`None`). """ number_of_requests = 4 query_params = [{'John Fitzgerald': 'Tom Hardy'}] * number_of_requests responses_to_calls = [ self.mock_ok_responses(number_of_requests), self.mock_ok_responses(number_of_requests - 1), self.mock_ok_responses(number_of_requests - 2) ] # mock unsuccessful responses to the first call to grequests.map self.mock_unsuccessful_responses(responses_to_calls[0][0:3]) # mock unsuccessful responses to the second call to grequests.map self.mock_unsuccessful_responses(responses_to_calls[1][1:3]) # mock unsuccessful response to the third call to grequests.map self.mock_unsuccessful_response(responses_to_calls[2][1]) grequests.map = MagicMock() grequests.map.side_effect = responses_to_calls actual_responses = MultiRequest(max_retry=3).multi_get( 'example.com', query_params) T.assert_equal(3, grequests.map.call_count) T.assert_is(None, actual_responses[2])
def test_multi_get_max_retry(self): """Tests the case when the number of the maximum retries is reached, due to the unsuccessful responses. Request is repeated 3 times (based on `max_retry`), each time there is only one successful response. Eventually the call to `multi_get` returns the responses among which one is unsuccessful (`None`). """ number_of_requests = 4 query_params = [{'John Fitzgerald': 'Tom Hardy'}] * number_of_requests responses_to_calls = [ self.mock_ok_responses(number_of_requests), self.mock_ok_responses(number_of_requests - 1), self.mock_ok_responses(number_of_requests - 2), ] # mock unsuccessful responses to the first call self.mock_unsuccessful_responses(responses_to_calls[0][0:3]) # mock unsuccessful responses to the second call self.mock_unsuccessful_responses(responses_to_calls[1][1:3]) # mock unsuccessful response to the third call self.mock_unsuccessful_response(responses_to_calls[2][1]) get_mock = self.mock_request_futures( chain.from_iterable(responses_to_calls)) actual_responses = MultiRequest(max_retry=3).multi_get( 'example.com', query_params) T.assert_equal(get_mock.call_count, 9) T.assert_is(actual_responses[2], None)
def test_servlet_with_multiple_people(self): """Test multiple names When we have lots of names, pushmanager should send multiple announcements instead of a single large annoucement. """ people = [ 'aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh', 'iii', 'jjj', 'kkk', 'lll', 'mmm', 'nnn', ] name_list = ''.join(['&people[]=' + person for person in people]) resp = self.fetch( '/msg', method='POST', body='message=foo' + name_list, ) T.assert_is(resp.error, None) T.assert_equal(self.call_mock.call_count, 3, message='multiple people should be divided into groups') self.call_mock.assert_any_call([ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, '[[pushmaster testuser]] aaa, bbb, ccc, ddd, eee', ], ) self.call_mock.assert_any_call([ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, ' fff, ggg, hhh, iii, jjj', ], ) self.call_mock.assert_any_call([ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, ' kkk, lll, mmm, nnn: foo', ], )
def test_set_deep_value_exists(self): foo = { 'a': { 'b': 'c', }, } value = 'd' set_deep(foo, 'a.b', value) T.assert_is(foo['a']['b'], value)
def test_msg_servlet_for_watchers(self, _): resp = self.fetch('/msg', method='POST', body='state=added&message=foo&id=1') T.assert_is(resp.error, None) call_arguments = self.call_mock.call_args[0][0] T.assert_true('/nail/sys/bin/nodebot' in call_arguments[0]) T.assert_true('-i' in call_arguments[1]) T.assert_true('testuser' in call_arguments[4]) T.assert_true('testwatcher_1' in call_arguments[4]) T.assert_true('foo' in call_arguments[4])
def test_multi_get_drop_404s(self): responses_to_calls = self.mock_ok_responses(3) self.mock_not_found_response(responses_to_calls[1]) query_params = [{'Hugh Glass': 'Leonardo DiCaprio'}] * 3 get_mock = self.mock_request_futures(responses_to_calls) result = MultiRequest(drop_404s=True).multi_get( 'example.org', query_params) T.assert_equal(get_mock.call_count, 3) T.assert_is(result[1], None)
def test_msg_servlet_for_watchers(self, _): resp = self.fetch("/msg", method="POST", body="state=added&message=foo&id=1") T.assert_is(resp.error, None) call_arguments = self.call_mock.call_args[0][0] T.assert_true("/nail/sys/bin/nodebot" in call_arguments[0]) T.assert_true("-i" in call_arguments[1]) T.assert_true("testuser" in call_arguments[4]) T.assert_true("testwatcher_1" in call_arguments[4]) T.assert_true("foo" in call_arguments[4])
def test_servlet_with_multiple_people(self): """Test multiple names When we have lots of names, pushmanager should send multiple announcements instead of a single large annoucement. """ people = [ 'aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh', 'iii', 'jjj', 'kkk', 'lll', 'mmm', 'nnn', ] name_list = ''.join(['&people[]=' + person for person in people]) resp = self.fetch( '/msg', method='POST', body='message=foo' + name_list, ) T.assert_is(resp.error, None) T.assert_equal( self.call_mock.call_count, 3, message='multiple people should be divided into groups' ) self.call_mock.assert_any_call( [ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, '[[pushmaster testuser]] aaa, bbb, ccc, ddd, eee', ], ) self.call_mock.assert_any_call( [ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, ' fff, ggg, hhh, iii, jjj', ], ) self.call_mock.assert_any_call( [ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, ' kkk, lll, mmm, nnn: foo', ], )
def test_msg_servlet_no_people(self): resp = self.fetch('/msg', method='POST', body='message=foo') T.assert_is(resp.error, None) self.call_mock.assert_called_once_with([ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, '[[pushmaster testuser]] foo', ], )
def test_http_reporter_reports(self): """A simple test to make sure the HTTPReporter actually reports things.""" runner = TestRunner(DummyTestCase, test_reporters=[HTTPReporter(None, self.connect_addr, 'runner1')]) ret = runner.run() assert_is(ret, True) (method_result, test_case_result) = self.results_reported assert_equal(method_result['runner_id'], 'runner1') assert_equal(method_result['method']['class'], 'DummyTestCase') assert_equal(method_result['method']['name'], 'test')
def test_pushcontent_insert_ignore(self, mock_transaction): request = {'request': 1, 'push': 1} response = self.fetch('/addrequest', method='POST', body=urllib.urlencode(request)) T.assert_equal(response.error, None) T.assert_equal(mock_transaction.call_count, 1) # Extract the string of the prefix of the insert query insert_ignore_clause = mock_transaction.call_args[0][0][0] T.assert_is(type(insert_ignore_clause), db.InsertIgnore)
def test_msg_servlet_no_people(self): resp = self.fetch('/msg', method='POST', body='message=foo') T.assert_is(resp.error, None) self.call_mock.assert_called_once_with( [ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, '[[pushmaster testuser]] foo', ], )
def test_multi_get_none_response(self): """Tests the behavior of the `multi_get()` method when one of the responses from `grequests.map` is `None`.""" number_of_requests = 10 query_params = [{'Jim Bridger': 'Will Poulter'}] * number_of_requests responses = self.mock_ok_responses(number_of_requests) responses[3] = None self.mock_grequests_map(responses) actual_responses = MultiRequest(max_retry=1).multi_get('example.com', query_params) T.assert_equals(10, len(actual_responses)) T.assert_is(None, actual_responses[3])
def test_msg_servlet_for_message_all(self, _): resp = self.fetch('/msg', method='POST', body='state=all&message=foo&id=1') T.assert_is(resp.error, None) self.call_mock.assert_called_once_with( [ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, '[[pushmaster testuser]] testuser_3: foo', ], )
def test_multi_get_none_response(self): """Tests the behavior of the `multi_get()` method when one of the responses from `grequests.map` is `None`.""" number_of_requests = 10 query_params = [{'Jim Bridger': 'Will Poulter'}] * number_of_requests responses = self.mock_ok_responses(number_of_requests) responses[3] = None self.mock_grequests_map(responses) actual_responses = MultiRequest(max_retry=1).multi_get( 'example.com', query_params) T.assert_equals(10, len(actual_responses)) T.assert_is(None, actual_responses[3])
def test_update_with_no_new_jar(self): version = str(object()) with mock.patch.object( VanillaJarDownloader, 'latest_downloaded_version', version, ): self.get_versions_json_mock.return_value = get_fake_versions_json( release_version=version, ) instance = VanillaJarDownloader(self.directory) return_value = instance.update() T.assert_is(return_value, None)
def test_pushcontent_insert_ignore(self, mock_transaction): request = { 'request': 1, 'push': 1 } response = self.fetch( '/addrequest', method='POST', body=urllib.urlencode(request) ) T.assert_equal(response.error, None) T.assert_equal(mock_transaction.call_count, 1) # Extract the string of the prefix of the insert query insert_ignore_clause = mock_transaction.call_args[0][0][0] T.assert_is(type(insert_ignore_clause), db.InsertIgnore)
def test_servlet_with_people(self): resp = self.fetch( '/msg', method='POST', body='message=foo&people[]=asottile&people[]=milki', ) T.assert_is(resp.error, None) self.call_mock.assert_called_once_with([ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, '[[pushmaster testuser]] asottile, milki: foo', ], )
def test_multi_get_response_to_json(self): """Tests the exception handling in the cases when the response was supposed to return JSON but did not.""" number_of_requests = 5 query_params = [{'Andrew Henry': 'Domhnall Gleeson'}] * number_of_requests responses = self.mock_ok_responses(number_of_requests) self.mock_json_convertion_error(responses[3]) self.mock_grequests_map(responses) logging.warning = MagicMock() actual_responses = MultiRequest().multi_get('example.com', query_params) T.assert_equals(5, len(actual_responses)) T.assert_is(None, actual_responses[3]) logging.warning.called_once_with( 'Expected response in JSON format from example.com/movie/TheRevenant but the actual response text is: This is not JSON')
def test_servlet_with_people(self): resp = self.fetch( '/msg', method='POST', body='message=foo&people[]=asottile&people[]=milki', ) T.assert_is(resp.error, None) self.call_mock.assert_called_once_with( [ '/nail/sys/bin/nodebot', '-i', mock.ANY, mock.ANY, '[[pushmaster testuser]] asottile, milki: foo', ], )
def test_multi_get_response_to_json(self): """Tests the exception handling in the cases when the response was supposed to return JSON but did not.""" number_of_requests = 5 query_params = [{ 'Andrew Henry': 'Domhnall Gleeson' }] * number_of_requests responses = self.mock_ok_responses(number_of_requests) self.mock_json_convertion_error(responses[3]) self.mock_grequests_map(responses) logging.warning = MagicMock() actual_responses = MultiRequest().multi_get('example.com', query_params) T.assert_equals(5, len(actual_responses)) T.assert_is(None, actual_responses[3]) logging.warning.called_once_with( 'Expected response in JSON format from example.com/movie/TheRevenant but the actual response text is: This is not JSON' )
def test_get_latest_version(self): release_version = '1.6.2' snapshot_version = '19w32a' self.get_versions_json_mock.return_value = get_fake_versions_json( release_version=release_version, snapshot_version=snapshot_version, ) instance = VanillaJarDownloader(self.directory) with mock.patch.dict( VanillaJarDownloader.config, {'jar_type': RELEASE} ): T.assert_is(instance._get_latest_version(), release_version) with mock.patch.dict( VanillaJarDownloader.config, {'jar_type': SNAPSHOT} ): T.assert_is(instance._get_latest_version(), snapshot_version)
def test_send_notifications_empty_user_list(self): """If there is no pending push request we'll only send IRC and email notifications, but not XMPP messages.""" self.people = [] self.pushurl = "fake_push_url" self.pushtype = "fake_puth_type" with self.mocked_notifications() as (mocked_call, mocked_mail, mocked_xmpp): send_notifications(self.people, self.pushtype, self.pushurl) mocked_call.assert_called_once_with( [ "/nail/sys/bin/nodebot", "-i", Settings["irc"]["nickname"], Settings["irc"]["channel"], mock.ANY, # msg ] ) mocked_mail.assert_called_once_with(Settings["mail"]["notifyall"], mock.ANY, mock.ANY) # msg # subject T.assert_is(mocked_xmpp.called, False)
def test_send_notifications_empty_user_list(self): """If there is no pending push request we'll only send IRC and email notifications, but not XMPP messages.""" self.people = [] self.pushmanager_url = "fake_push_url" self.pushtype = "fake_puth_type" with self.mocked_notifications() as (mocked_call, mocked_mail, mocked_xmpp): send_notifications(self.people, self.pushtype, self.pushmanager_url) mocked_call.assert_called_once_with([ '/nail/sys/bin/nodebot', '-i', Settings['irc']['nickname'], Settings['irc']['channel'], mock.ANY, # msg ]) mocked_mail.assert_called_once_with( Settings['mail']['notifyall'], mock.ANY, # msg mock.ANY, # subject ) T.assert_is(mocked_xmpp.called, False)
def test_returns_default_on_indexing_into_list(self): sentient = object() T.assert_is( get_deep(self.sample_dict, 'a.b.e.d', sentient), sentient, )
def test_get_deep_returns_none_by_default_for_missing_value(self): T.assert_is(get_deep({}, 'foo'), None)
def test_is_singleton(self): c1 = fakemtpd.config.Config.instance() c2 = fakemtpd.config.Config.instance() assert_is(c1, c2)
def test_current_method(self): """The actual method under test is in get_ec().""" testify.assert_is(self.get_ec().value, self.err)
def test_get_deep_returns_dict_value_correctly(self): T.assert_is( get_deep(self.sample_dict, 'a.b'), self.sample_dict['a']['b'], )
def test_noop_passthrough(self): value = 'foo' ret = transform_value(value, {'type': Types.STRING}) T.assert_is(ret, value)
def test_cached_property(self): instance = self.Foo() val = instance.foo val2 = instance.foo T.assert_is(val, val2)
def test_get_deep_returns_value(self): T.assert_is( get_deep(self.sample_dict, 'a.b.c'), self.sample_dict['a']['b']['c'], )
def test_returns_default_on_failed_key(self): sentient = object() T.assert_is( get_deep(self.sample_dict, 'notakey', sentient), sentient, )
def test_set_deep_simple(self): foo = {} value = 'bar' set_deep(foo, 'a', value) T.assert_is(foo['a'], value)
def test_set_deep_deeper(self): foo = {} value = 'bar' set_deep(foo, 'a.b.c', value) T.assert_is(foo['a']['b']['c'], value)
def test_transform_bad_value_returns_value(self): bad_value = 'abc' ret = transform_value(bad_value, {'type': Types.INTEGER}) T.assert_is(ret, bad_value)
def test_values(self): retval = auto_namedtuple(**self.kwargs) for key, value in self.kwargs.iteritems(): T.assert_is(getattr(retval, key), value)
def test_msg_servlet_for_message_all(self, _): resp = self.fetch("/msg", method="POST", body="state=all&message=foo&id=1") T.assert_is(resp.error, None) self.call_mock.assert_called_once_with( ["/nail/sys/bin/nodebot", "-i", mock.ANY, mock.ANY, "[[pushmaster testuser]] testuser_3: foo"] )