diff options
-rw-r--r-- | pygithub3/tests/core/test_client.py | 6 | ||||
-rw-r--r-- | pygithub3/tests/requests/test_core.py | 18 | ||||
-rw-r--r-- | pygithub3/tests/services/test_core.py | 4 | ||||
-rw-r--r-- | pygithub3/tests/services/test_users.py | 150 | ||||
-rw-r--r-- | pygithub3/tests/utils/base.py | 12 |
5 files changed, 174 insertions, 16 deletions
diff --git a/pygithub3/tests/core/test_client.py b/pygithub3/tests/core/test_client.py index e5b1ea5..4a68687 100644 --- a/pygithub3/tests/core/test_client.py +++ b/pygithub3/tests/core/test_client.py @@ -61,18 +61,18 @@ class TestClient(TestCase): self.c.get('') request_method.assert_called_with('get', '') - request_method.return_value = mock_response(201) + request_method.return_value = mock_response('post') self.c.post('') request_method.assert_called_with('post', '') - request_method.return_value = mock_response(200) + request_method.return_value = mock_response('patch') self.c.patch('') request_method.assert_called_with('patch', '') self.c.put('') request_method.assert_called_with('put', '') - request_method.return_value = mock_response(204) + request_method.return_value = mock_response('delete') self.c.delete('') request_method.assert_called_with('delete', '') diff --git a/pygithub3/tests/requests/test_core.py b/pygithub3/tests/requests/test_core.py index f29ab1d..98c5f3f 100644 --- a/pygithub3/tests/requests/test_core.py +++ b/pygithub3/tests/requests/test_core.py @@ -34,6 +34,7 @@ class TestFactory(TestCase): request = self.f('users.get') self.assertIsInstance(request, Request) + class TestRequestUri(TestCase): def test_SIMPLE_with_correct_args(self): @@ -80,12 +81,17 @@ class TestRequestBody(TestCase): class TestBodyParsers(TestCase): - def setUp(self): - self.b = Body( + def test_only_valid_keys(self): + body = Body( dict(arg1='arg1', arg2='arg2', arg3='arg3', arg4='arg4'), ('arg1', 'arg3', 'arg4')) - - def test_RETURN_only_valid_keys(self): - get_body_returns = self.b.parse() - self.assertEqual(get_body_returns, dict(arg1='arg1', arg3='arg3', + self.assertEqual(body.parse(), dict(arg1='arg1', arg3='arg3', arg4='arg4')) + + def test_none(self): + body = Body({}, ('arg1', 'arg2')) + self.assertEqual(body.parse(), {}) + + def test_invalid_content(self): + body = Body('invalid', ('arg1',)) + self.assertRaises(ValidationError, body.parse) diff --git a/pygithub3/tests/services/test_core.py b/pygithub3/tests/services/test_core.py index cdfe176..6e5b2b4 100644 --- a/pygithub3/tests/services/test_core.py +++ b/pygithub3/tests/services/test_core.py @@ -32,13 +32,13 @@ class TestServiceCalls(TestCase): data=data, params=self.args) def test_DELETE(self, request_method): - request_method.return_value = mock_response(204) + request_method.return_value = mock_response('delete') self.s._delete(self.r, **self.args) request_method.assert_called_with('delete', _('dummyrequest'), data=None, params=self.args) def test_POST(self, request_method): - request_method.return_value = mock_response(201) + request_method.return_value = mock_response('post') self.s._post(self.r, **self.args) request_method.assert_called_with('post', _('dummyrequest'), data=None, params=self.args) diff --git a/pygithub3/tests/services/test_users.py b/pygithub3/tests/services/test_users.py index 9c333ec..f61d60c 100644 --- a/pygithub3/tests/services/test_users.py +++ b/pygithub3/tests/services/test_users.py @@ -6,14 +6,17 @@ from unittest import TestCase import requests from mock import patch, Mock -from pygithub3.services.users import User +from pygithub3.core.client import Client +from pygithub3.services.users import User, Emails, Followers, Keys +from pygithub3.exceptions import ValidationError from pygithub3.resources.base import json -from pygithub3.tests.utils.base import mock_response +from pygithub3.tests.utils.base import mock_response, mock_response_result from pygithub3.tests.utils.services import _, mock_json json.dumps = Mock(side_effect=mock_json) json.loads = Mock(side_effect=mock_json) + @patch.object(requests.sessions.Session, 'request') class TestUserService(TestCase): @@ -23,4 +26,145 @@ class TestUserService(TestCase): def test_GET_without_user(self, request_method): request_method.return_value = mock_response() self.us.get() - request_method.assert_called_with('get', _('user'), params={}) + self.assertEqual(request_method.call_args[0], ('get', _('user'))) + + def test_GET_with_user_in_arg(self, request_method): + request_method.return_value = mock_response() + self.us.get('octocat') + self.assertEqual(request_method.call_args[0], + ('get', _('users/octocat'))) + + def test_GET_with_user_in_service(self, request_method): + request_method.return_value = mock_response() + self.us.set_user('octocat_service') + self.us.get() + self.assertEqual(request_method.call_args[0], + ('get', _('users/octocat_service'))) + + def test_UPDATE_with_valid_data(self, request_method): + request_method.return_value = mock_response('patch') + self.us.update({'name': 'dummy'}) + self.assertEqual(request_method.call_args[0], ('patch', _('user'))) + + def test_UPDATE_without_data(self, request_method): + self.assertRaises(ValidationError, self.us.update, {}) + + +@patch.object(requests.sessions.Session, 'request') +class TestEmailsService(TestCase): + + def setUp(self): + self.es = Emails() + + def test_LIST(self, request_method): + request_method.return_value = mock_response_result() + self.es.list().all() + self.assertEqual(request_method.call_args[0], + ('get', _('user/emails'))) + + def test_ADD_without_emails(self, request_method): + self.assertRaises(ValidationError, self.es.add) + self.assertFalse(request_method.called) + + def test_ADD_with_emails(self, request_method): + request_method.return_value = mock_response('post') + self.es.add('test@example.com', 'test2@example.com') + self.assertEqual(request_method.call_args[0], + ('post', _('user/emails'))) + + @patch.object(Client, 'request') + def test_ADD_filter_emails(self, client_request, dummy): + client_request.return_value = mock_response('post') + self.es.add('invalidemail.com', 'inva@lid@xam.com', 'test@xample.com') + self.assertEqual(client_request.call_args[1], + dict(data=('test@xample.com', ))) + + def test_DELETE(self, request_method): + request_method.return_value = mock_response('delete') + self.es.delete('email_must_be_founded') # or 404 raises + self.assertEqual(request_method.call_args[0], + ('delete', _('user/emails'))) + + +@patch.object(requests.sessions.Session, 'request') +class TestFollowersService(TestCase): + + def setUp(self): + self.fs = Followers() + + def test_LIST_without_user(self, request_method): + request_method.return_value = mock_response_result() + self.fs.list().all() + self.assertEqual(request_method.call_args[0], + ('get', _('user/followers'))) + + def test_LIST_with_user_in_arg(self, request_method): + request_method.return_value = mock_response_result() + self.fs.list('octocat').all() + self.assertEqual(request_method.call_args[0], + ('get', _('users/octocat/followers'))) + + def test_LIST_with_user_in_service(self, request_method): + request_method.return_value = mock_response_result() + self.fs.set_user('octocat_service') + self.fs.list().all() + self.assertEqual(request_method.call_args[0], + ('get', _('users/octocat_service/followers'))) + + def test_LIST_FOLLOWING_without_user(self, request_method): + request_method.return_value = mock_response_result() + self.fs.list_following().all() + self.assertEqual(request_method.call_args[0], + ('get', _('user/following'))) + + def test_LIST_FOLLOWING_with_user_in_arg(self, request_method): + request_method.return_value = mock_response_result() + self.fs.list_following('octocat').all() + self.assertEqual(request_method.call_args[0], + ('get', _('users/octocat/following'))) + + def test_LIST_FOLLOWING_with_user_in_service(self, request_method): + request_method.return_value = mock_response_result() + self.fs.set_user('octocat_service') + self.fs.list_following().all() + self.assertEqual(request_method.call_args[0], + ('get', _('users/octocat_service/following'))) + + def test_IS_FOLLOWING(self, request_method): + self.fs.is_following('octocat') + self.assertEqual(request_method.call_args[0], + ('head', _('user/following/octocat'))) + + def test_FOLLOW(self, request_method): + #request_method.return_value = mock_response('put') + self.fs.follow('octocat') + self.assertEqual(request_method.call_args[0], + ('put', _('user/following/octocat'))) + + def test_UNFOLLOW(self, request_method): + request_method.return_value = mock_response('delete') + self.fs.unfollow('octocat') + self.assertEqual(request_method.call_args[0], + ('delete', _('user/following/octocat'))) + + +@patch.object(requests.sessions.Session, 'request') +class TestKeysService(TestCase): + + def setUp(self): + self.ks = Keys() + + def test_LIST(self, request_method): + request_method.return_value = mock_response_result() + self.ks.list().all() + self.assertEqual(request_method.call_args[0], + ('get', _('user/key'))) + + def test_GET(self, request_method): + request_method.return_value = mock_response() + self.ks.get(1) + self.assertEqual(request_method.call_args[0], + ('get', _('user/keys/1'))) + + def test_ADD(self, request_method): + request_method.return_value = mock_response('post') diff --git a/pygithub3/tests/utils/base.py b/pygithub3/tests/utils/base.py index 571d8ac..d9049c9 100644 --- a/pygithub3/tests/utils/base.py +++ b/pygithub3/tests/utils/base.py @@ -11,12 +11,20 @@ def mock_json(content): return content -def mock_response(status_code=200, content={}): +def mock_response(status_code='get', content={}): + CODES = dict(get=200, patch=200, post=201, delete=204) response = Mock(name='response') - response.status_code = status_code + response.status_code = CODES[str(status_code).lower()] or status_code response.content = content return response + +def mock_response_result(status_code='get'): + response = mock_response(status_code, content=[{}, {}]) + response.headers = {'link': ''} + return response + + class DummyResource(Resource): pass |