|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +from unittest import TestCase |
| 5 | + |
| 6 | +import requests |
| 7 | +from pygithub3.core.client import Client |
| 8 | +from pygithub3.exceptions import * |
| 9 | +from mock import patch |
| 10 | + |
| 11 | + |
| 12 | +class TestClient(TestCase): |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + self.c = Client() |
| 16 | + |
| 17 | + def test_set_credentials_with_valid(self): |
| 18 | + self.c.set_credentials('login', 'password') |
| 19 | + self.assertEqual(self.c.requester.auth, ('login', 'password')) |
| 20 | + |
| 21 | + def test_set_credentials_with_invalid(self): |
| 22 | + self.c.set_credentials('', '') |
| 23 | + self.assertIsNone(self.c.requester.auth) |
| 24 | + |
| 25 | + def test_set_token_with_valid(self): |
| 26 | + self.c.set_token('tokenize') |
| 27 | + self.assertEqual(self.c.requester.params['access_token'], 'tokenize') |
| 28 | + |
| 29 | + def test_set_token_with_invalid(self): |
| 30 | + self.c.set_token('') |
| 31 | + self.assertIsNone(self.c.requester.params.get('access_token')) |
| 32 | + |
| 33 | + def test_INIT_client_with_another_config_args(self): |
| 34 | + new_c = Client(base_url='url', per_page=10, user='me', repo='myrepo', |
| 35 | + verbose='stream') |
| 36 | + self.assertEqual(new_c.config['base_url'], 'url') |
| 37 | + self.assertEqual(new_c.requester.params['per_page'], 10) |
| 38 | + self.assertEqual(new_c.user, 'me') |
| 39 | + self.assertEqual(new_c.repo, 'myrepo') |
| 40 | + self.assertEqual(new_c.requester.config['verbose'], 'stream') |
| 41 | + |
| 42 | + @patch.object(requests.sessions.Session, 'request') |
| 43 | + def test_PARSE_args_in_request_without_params(self, request_method): |
| 44 | + extra = dict(arg1='arg1', arg2='arg2') |
| 45 | + self.c.request('', '', data='data', **extra) |
| 46 | + request_method.assert_called_with('', self.c.config['base_url'], |
| 47 | + data='data', params=extra) |
| 48 | + |
| 49 | + @patch.object(requests.sessions.Session, 'request') |
| 50 | + def test_PARSE_args_in_request_with_params(self, request_method): |
| 51 | + extra = dict(arg1='arg1', arg2='arg2') |
| 52 | + self.c.request('', '', params=dict(arg0='arg0'), **extra) |
| 53 | + request_method.assert_called_with('', self.c.config['base_url'], |
| 54 | + params=dict(arg0='arg0', **extra)) |
| 55 | + |
| 56 | + @patch.object(Client, 'request') |
| 57 | + def test_DELEGATES_methods(self, request_method): |
| 58 | + self.c.get('') |
| 59 | + request_method.assert_called_with('get', '') |
| 60 | + self.c.post('') |
| 61 | + request_method.assert_called_with('post', '') |
| 62 | + self.c.patch('') |
| 63 | + request_method.assert_called_with('patch', '') |
| 64 | + self.c.put('') |
| 65 | + request_method.assert_called_with('put', '') |
| 66 | + self.c.delete('') |
| 67 | + request_method.assert_called_with('delete', '') |
| 68 | + self.c.head('') |
| 69 | + request_method.assert_called_with('head', '') |
| 70 | + |
| 71 | + |
| 72 | +@patch.object(requests.sessions.Session, 'request') |
| 73 | +class TestClientRaises(TestCase): |
| 74 | + |
| 75 | + def setUp(self): |
| 76 | + self.c = Client() |
| 77 | + self.callback = (self.c.request, 'method', 'request') |
| 78 | + |
| 79 | + def test_raise_NotFound(self, request_method): |
| 80 | + request_method.return_value.status_code = 404 |
| 81 | + self.assertRaises(NotFound, *self.callback) |
| 82 | + |
| 83 | + def test_raise_BadRequest(self, request_method): |
| 84 | + request_method.return_value.status_code = 400 |
| 85 | + self.assertRaises(BadRequest, *self.callback) |
| 86 | + |
| 87 | + def test_raise_UnprocessableEntity(self, request_method): |
| 88 | + request_method.return_value.status_code = 422 |
| 89 | + self.assertRaises(UnprocessableEntity, *self.callback) |
0 commit comments