1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
"""Unit tests for ``git_pw/api.py``."""
from unittest import mock
import requests
import pytest
from git_pw import api
@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_server_undefined(mock_conf, mock_log):
mock_conf.server = None
with pytest.raises(SystemExit):
api._get_server()
assert mock_log.error.called
@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_server_missing_version(mock_conf, mock_log):
mock_conf.server = 'https://example.com/api'
server = api._get_server()
assert mock_log.warning.called
assert server == 'https://example.com/api'
@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_server_missing_version_and_path(mock_conf, mock_log):
mock_conf.server = 'https://example.com/'
server = api._get_server()
assert mock_log.warning.called
assert server == 'https://example.com/api'
@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_project_undefined(mock_conf, mock_log):
mock_conf.project = None
with pytest.raises(SystemExit):
api._get_project()
assert mock_log.error.called
@mock.patch.object(api, 'CONF')
def test_get_project_wildcard(mock_conf):
mock_conf.project = '*'
project = api._get_project()
assert project == ''
@mock.patch.object(api, '_get_server')
def test_version_missing(mock_server):
mock_server.return_value = 'https://example.com/api'
assert api.version() == (1, 0)
@mock.patch.object(api, '_get_server')
def test_version(mock_server):
mock_server.return_value = 'https://example.com/api/1.1'
assert api.version() == (1, 1)
def test_handle_error__server_error(caplog):
fake_response = mock.MagicMock(autospec=requests.Response)
fake_response.content = b'InternalServerError'
fake_response.status_code = 500
exc = requests.exceptions.RequestException(response=fake_response)
with pytest.raises(SystemExit):
api._handle_error('fetch', exc)
assert 'Server error.' in caplog.text
def test_handle_error__not_found(caplog):
fake_response = mock.MagicMock(autospec=requests.Response)
fake_response.content = b'NotFound'
fake_response.status_code = 404
exc = requests.exceptions.RequestException(response=fake_response)
with pytest.raises(SystemExit):
api._handle_error('fetch', exc)
assert 'Resource not found' in caplog.text
def test_handle_error__other(caplog):
fake_response = mock.MagicMock(autospec=requests.Response)
fake_response.content = b'{"key": "value"}'
fake_response.status_code = 403
fake_response.text = '{"key": "value"}'
exc = requests.exceptions.RequestException(response=fake_response)
with pytest.raises(SystemExit):
api._handle_error('fetch', exc)
assert '{"key": "value"}' in caplog.text
def test_handle_error__no_response(caplog):
exc = requests.exceptions.RequestException()
with pytest.raises(SystemExit):
api._handle_error('fetch', exc)
assert 'Failed to fetch resource.' in caplog.text
@mock.patch.object(api, 'index')
def test_retrieve_filter_ids_too_short(mock_index):
with pytest.raises(SystemExit):
api.retrieve_filter_ids('users', 'owner', 'f')
assert not mock_index.called
@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'index')
def test_retrieve_filter_ids_no_matches(mock_index, mock_log):
mock_index.return_value = []
ids = api.retrieve_filter_ids('users', 'owner', 'foo')
assert mock_log.warning.called
assert ids == []
@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'version')
@mock.patch.object(api, 'index')
def test_retrieve_filter_ids_multiple_matches_1_0(
mock_index, mock_version, mock_log
):
mock_index.return_value = [
{'id': 1},
{'id': 2}, # incomplete but good enough
]
mock_version.return_value = (1, 0)
ids = api.retrieve_filter_ids('users', 'owner', 'foo')
assert mock_log.warning.called
assert ids == [('owner', 1), ('owner', 2)]
@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'version')
@mock.patch.object(api, 'index')
def test_retrieve_filter_ids_multiple_matches_1_1(
mock_index, mock_version, mock_log
):
mock_index.return_value = [
{'id': 1},
{'id': 2}, # incomplete but good enough
]
mock_version.return_value = (1, 1)
ids = api.retrieve_filter_ids('users', 'owner', 'foo')
assert not mock_log.warning.called
assert ids == [('owner', 1), ('owner', 2)]
@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'index')
def test_retrieve_filter_ids(mock_index, mock_log):
mock_index.return_value = [{'id': 1}]
ids = api.retrieve_filter_ids('users', 'owner', 'foo')
assert not mock_log.warning.called
assert ids == [('owner', 1)]
|