|
2 | 2 |
|
3 | 3 | from unittest import mock
|
4 | 4 |
|
| 5 | +import requests |
5 | 6 | import pytest
|
6 | 7 |
|
7 | 8 | from git_pw import api
|
@@ -74,6 +75,52 @@ def test_version(mock_server):
|
74 | 75 | assert api.version() == (1, 1)
|
75 | 76 |
|
76 | 77 |
|
| 78 | +def test_handle_error__server_error(caplog): |
| 79 | + fake_response = mock.MagicMock(autospec=requests.Response) |
| 80 | + fake_response.content = b'InternalServerError' |
| 81 | + fake_response.status_code = 500 |
| 82 | + exc = requests.exceptions.RequestException(response=fake_response) |
| 83 | + |
| 84 | + with pytest.raises(SystemExit): |
| 85 | + api._handle_error('fetch', exc) |
| 86 | + |
| 87 | + assert 'Server error.' in caplog.text |
| 88 | + |
| 89 | + |
| 90 | +def test_handle_error__not_found(caplog): |
| 91 | + fake_response = mock.MagicMock(autospec=requests.Response) |
| 92 | + fake_response.content = b'NotFound' |
| 93 | + fake_response.status_code = 404 |
| 94 | + exc = requests.exceptions.RequestException(response=fake_response) |
| 95 | + |
| 96 | + with pytest.raises(SystemExit): |
| 97 | + api._handle_error('fetch', exc) |
| 98 | + |
| 99 | + assert 'Resource not found' in caplog.text |
| 100 | + |
| 101 | + |
| 102 | +def test_handle_error__other(caplog): |
| 103 | + fake_response = mock.MagicMock(autospec=requests.Response) |
| 104 | + fake_response.content = b'{"key": "value"}' |
| 105 | + fake_response.status_code = 403 |
| 106 | + fake_response.text = '{"key": "value"}' |
| 107 | + exc = requests.exceptions.RequestException(response=fake_response) |
| 108 | + |
| 109 | + with pytest.raises(SystemExit): |
| 110 | + api._handle_error('fetch', exc) |
| 111 | + |
| 112 | + assert '{"key": "value"}' in caplog.text |
| 113 | + |
| 114 | + |
| 115 | +def test_handle_error__no_response(caplog): |
| 116 | + exc = requests.exceptions.RequestException() |
| 117 | + |
| 118 | + with pytest.raises(SystemExit): |
| 119 | + api._handle_error('fetch', exc) |
| 120 | + |
| 121 | + assert 'Failed to fetch resource.' in caplog.text |
| 122 | + |
| 123 | + |
77 | 124 | @mock.patch.object(api, 'index')
|
78 | 125 | def test_retrieve_filter_ids_too_short(mock_index):
|
79 | 126 | with pytest.raises(SystemExit):
|
|
0 commit comments