From 795dea374e581d7e227971311a05439ca7bada2a Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Mon, 27 Jan 2020 19:10:38 +0100 Subject: Update tests to use requests_mock pytest plugin --- test/test_obs_build_api.py | 260 ++++++++++++++++++++++----------------------- 1 file changed, 125 insertions(+), 135 deletions(-) (limited to 'test') diff --git a/test/test_obs_build_api.py b/test/test_obs_build_api.py index 69370b3..194abf3 100644 --- a/test/test_obs_build_api.py +++ b/test/test_obs_build_api.py @@ -1,7 +1,6 @@ import os import pytest from obsapi.buildapi import ObsBuildApi -from unittest.mock import MagicMock prj = 'home:sbahling:obsapi:test' pkg = 'suse-hello-1.0' @@ -13,115 +12,105 @@ buildapi = ObsBuildApi(apiurl='https://api.opensuse.org') test_dir = os.path.split(os.path.abspath(__file__))[0] -@pytest.fixture(autouse=True) -def setup_mock_get(monkeypatch): - mock_response = MagicMock() - mock_response.status_code = 200 - mock_get = MagicMock(return_value=mock_response) - monkeypatch.setattr('requests.get', mock_get) - return(mock_get, mock_response) - - # GET /build/ # Result: directory listing as xml -def test_build_get(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get(requests_mock): + uri = '{}/build/'.format(buildapi.apiurl) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get() - assert mock_get.call_args[0][0] == '{}/build/'.format(buildapi.apiurl) - assert '' in xml + assert xml == text # GET /build/ # Result: directory listing as xml -def test_build_get_prj(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_prj(requests_mock): + uri = '{}/build/{}'.format(buildapi.apiurl, prj) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get(prj) - assert mock_get.call_args[0][0] == '{}/build/{}'.format(buildapi.apiurl, prj) - assert '' in xml + assert xml == text # GET /build// # Result: directory listing as xml -def test_build_get_repo(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_repo(requests_mock): + uri = '{}/build/{}/{}'.format(buildapi.apiurl, prj, repo) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get(prj, repo) - assert mock_get.call_args[0][0] == '{}/build/{}/{}'.format(buildapi.apiurl, prj, repo) - assert '' in xml + assert xml == text # GET /build/// # Result: directory listing as xml -def test_build_get_arch(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_arch(requests_mock): + uri = '{}/build/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get(prj, repo, arch) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch) - assert '' in xml + assert xml == text # GET /build//// # Result: binarylist as xml -def test_build_get_binary_list(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_binary_list(requests_mock): + uri = '{}/build/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get(prj, repo, arch, pkg) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg) - assert '' in xml + assert xml == text # Not official api call # GET /build////_statistics # Result: buildstatistics as xml -def test_build_get_binary(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.content = '' +def test_build_get_binary(requests_mock): bname = '_statistics' - binary = buildapi.get(prj, repo, arch, pkg, bname) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname) - assert '' in str(binary) + uri = '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname) + content = b'' + requests_mock.get(uri, content=content) + file_content = buildapi.get(prj, repo, arch, pkg, bname) + assert file_content == content # GET /build////?view=fileinfo # Result: fileinfo as xml -def test_build_get_binary_fileinfo(setup_mock_get): - mock_get, mock_response = setup_mock_get - bname = 'suse-hello-1.0-1.1.src.rpm' - mock_response.content = ''.format(bname) +def test_build_get_binary_fileinfo(requests_mock): + bname = b'suse-hello-1.0-1.1.src.rpm' + uri = '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname) + content = b'' % bname + requests_mock.get(uri, content=content) xml = buildapi.get(prj, repo, arch, pkg, bname, view='fileinfo') - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname) - assert mock_get.call_args[1]['params']['view'] == 'fileinfo' - assert ''.format(bname) in str(xml) + assert requests_mock.request_history[0].qs['view'][0] == 'fileinfo' + assert xml == content # GET /build////?view=fileinfo_ext # Result: fileinfo as xml -def test_build_get_binary_fileinfo_ext(setup_mock_get): - mock_get, mock_response = setup_mock_get - bname = 'suse-hello-1.0-1.1.src.rpm' - mock_response.content = ''.format(bname) +def test_build_get_binary_fileinfo_ext(requests_mock): + bname = b'suse-hello-1.0-1.1.src.rpm' + uri = '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname) + content = b'' % bname + requests_mock.get(uri, content=content) xml = buildapi.get(prj, repo, arch, pkg, bname, view='fileinfo_ext') - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname) - assert mock_get.call_args[1]['params']['view'] == 'fileinfo_ext' - assert ''.format(bname) in str(xml) + assert requests_mock.request_history[0].qs['view'][0] == 'fileinfo_ext' + assert xml == content # GET /build////_builddepinfo # Result: builddepinfo as xml -def test_build_get_builddepinfo(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_builddepinfo(requests_mock): + uri = '{}/build/{}/{}/{}/_builddepinfo'.format(buildapi.apiurl, prj, repo, arch) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_builddepinfo(prj, pkg, repo, arch) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_builddepinfo'.format(buildapi.apiurl, prj, repo, arch) - assert mock_get.call_args[1]['params']['package'] == pkg - assert '' in xml + assert xml == text # GET /build////_buildconfig # Result: buildconfig as text file -def test_build_get_buildconfig(setup_mock_get): +def test_build_get_buildconfig(requests_mock): mock_buildconfig = """%define _project home:sbahling:obsapi:test ### from SUSE:SLE-12-SP3:GA @@ -132,140 +121,141 @@ Macros: %_download_url http://download.opensuse.org/repositories %_project home:sbahling:obsapi:test """ - mock_get, mock_response = setup_mock_get - mock_response.text = mock_buildconfig + text = mock_buildconfig + uri = '{}/build/{}/{}/_buildconfig'.format(buildapi.apiurl, prj, repo) + requests_mock.get(uri, text=text) buildconfig = buildapi.get_buildconfig(prj, repo) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/_buildconfig'.format(buildapi.apiurl, prj, repo) - assert '%define _project home:sbahling:obsapi:test' in buildconfig + assert buildconfig == text # GET /build/////_buildinfo # Result: buildinfo as xml -def test_build_get_buildinfo(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_buildinfo(requests_mock): + uri = '{}/build/{}/{}/{}/{}/_buildinfo'.format(buildapi.apiurl, prj, repo, arch, pkg) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_buildinfo(prj, pkg, repo, arch) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_buildinfo'.format(buildapi.apiurl, prj, repo, arch, pkg) - assert '///_jobhistory # Result: jobhistlist as xml -def test_build_get_jobhistory(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_jobhistory(requests_mock): + uri = '{}/build/{}/{}/{}/_jobhistory'.format(buildapi.apiurl, prj, repo, arch) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_jobhistory(prj, repo, arch) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_jobhistory'.format(buildapi.apiurl, prj, repo, arch) - assert '' in xml + assert xml == text # GET /build////_jobhistory?package= # Result: jobhistlist as xml -def test_build_get_jobhistory_pkg(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_jobhistory_pkg(requests_mock): + uri = '{}/build/{}/{}/{}/_jobhistory'.format(buildapi.apiurl, prj, repo, arch) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_jobhistory(prj, repo, arch, pkg) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_jobhistory'.format(buildapi.apiurl, prj, repo, arch) - assert mock_get.call_args[1]['params']['package'] == pkg - assert '' in xml + assert len(requests_mock.request_history[0].qs['package']) == 1 + assert requests_mock.request_history[0].qs['package'][0] == pkg + assert xml == text # Undocumented api ?? -def test_build_get_result(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_result(requests_mock): + uri = '{}/build/{}/_result'.format(buildapi.apiurl, prj) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_result(prj) - assert mock_get.call_args[0][0] == '{}/build/{}/_result'.format(buildapi.apiurl, prj) - assert '////_history # Result: buildhistory as xml -def test_build_get_history(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_history(requests_mock): + uri = '{}/build/{}/{}/{}/{}/_history'.format(buildapi.apiurl, prj, repo, arch, pkg) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_history(prj, repo, arch, pkg) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_history'.format(buildapi.apiurl, prj, repo, arch, pkg) - assert '' in xml + assert xml == text # GET /build/////_reason # Result: build reason as xml -def test_build_get_reason(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_reason(requests_mock): + uri = '{}/build/{}/{}/{}/{}/_reason'.format(buildapi.apiurl, prj, repo, arch, pkg) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_reason(prj, repo, arch, pkg) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_reason'.format(buildapi.apiurl, prj, repo, arch, pkg) - assert '' in xml + assert xml == text # GET /build/////_jobstatus # Result: jobstatus as xml -def test_build_get_jobstatus(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_jobstatus(requests_mock): + uri = '{}/build/{}/{}/{}/{}/_jobstatus'.format(buildapi.apiurl, prj, repo, arch, pkg) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_jobstatus(prj, repo, arch, pkg) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_jobstatus'.format(buildapi.apiurl, prj, repo, arch, pkg) - assert '' in xml + assert xml == text # GET /build/////_status # Result: build status as xml -def test_build_get_status(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_status(requests_mock): + uri = '{}/build/{}/{}/{}/{}/_status'.format(buildapi.apiurl, prj, repo, arch, pkg) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_status(prj, repo, arch, pkg) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_status'.format(buildapi.apiurl, prj, repo, arch, pkg) - assert '////_log # Result: build log as text file # params: nostream, last, lastsucceeded, start, end, view=entry @pytest.mark.parametrize('params', ({}, - {'nostream': 1}, - {'view': 'entry', 'last': 1, - 'start': 100, 'end': 500}, - {'view': 'entry', 'lastsucceeded': 1, - 'start': 100, 'end': 500}, + {'nostream': '1'}, + {'view': 'entry', 'last': '1', + 'start': '100', 'end': '500'}, + {'view': 'entry', 'lastsucceeded': '1', + 'start': '100', 'end': '500'}, )) -def test_build_get_log(setup_mock_get, params): - mock_get, mock_response = setup_mock_get - mock_response.text = '[ 0s] Start of log' +def test_build_get_log(requests_mock, params): + uri = '{}/build/{}/{}/{}/{}/_log'.format(buildapi.apiurl, prj, repo, arch, pkg) + text = '[ 0s] Start of log' + requests_mock.get(uri, text=text) log = buildapi.get_log(prj, repo, arch, pkg, **params) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_log'.format(buildapi.apiurl, prj, repo, arch, pkg) - assert mock_get.call_args[1]['params'] == params - assert '[ 0s]' in log + for key, value in params.items(): + assert len(requests_mock.request_history[0].qs[key]) == 1 + assert value == requests_mock.request_history[0].qs[key][0] + assert log == text # GET /build////_repository # Result: binarylist -def test_build_get_repository(setup_mock_get): - mock_get, mock_response = setup_mock_get - mock_response.text = '' +def test_build_get_repository(requests_mock): + uri = '{}/build/{}/{}/{}/_repository'.format(buildapi.apiurl, prj, repo, arch) + text = '' + requests_mock.get(uri, text=text) xml = buildapi.get_repository(prj, repo, arch) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_repository'.format(buildapi.apiurl, prj, repo, arch) - assert mock_get.call_args[1]['params']['view'] is None - assert '///_repository/ # Result: binary file -def test_build_get_repository_binary(setup_mock_get): - mock_get, mock_response = setup_mock_get +def test_build_get_repository_binary(requests_mock): bname = 'suse-hello-kmp-default.rpm' + uri = '{}/build/{}/{}/{}/_repository/{}'.format(buildapi.apiurl, prj, repo, arch, bname) with open(os.path.join(test_dir, bname), 'rb') as f: - mock_response.content = f.read() + data = f.read() + requests_mock.get(uri, content=data) binary = buildapi.get_repository(prj, repo, arch, bname) - assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_repository/{}'.format(buildapi.apiurl, prj, repo, arch, bname) - assert mock_get.call_args[1]['params']['view'] is None - assert binary == mock_response.content + assert binary == data -- cgit v1.2.3