Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joelee2012 committed Aug 14, 2023
1 parent c46dfdc commit 24124e1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
8 changes: 5 additions & 3 deletions api4jenkins/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class WaitingItem(QueueItem):
class AsyncQueue(AsyncItem):

async def get(self, id):
async for item in self.api_json(tree='items[id,url]')['items']:
for item in (await self.api_json(tree='items[id,url]'))['items']:
if item['id'] == int(id):
return AsyncQueueItem(self.jenkins,
f"{self.jenkins.url}{item['url']}")
Expand Down Expand Up @@ -125,8 +125,10 @@ def __init__(self, jenkins, url):
self._build = None

async def get_job(self):
if self._class.endswith('$BuildableItem'):
return (await self.get_build()).get_job()
_class = await self._class
if _class.endswith('$BuildableItem'):
build = await self.get_build()
return await build.get_job()
data = await self.api_json(tree='task[url]')
return self._new_item('api4jenkins.job', data['task'])

Expand Down
7 changes: 1 addition & 6 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _api_json(self, tree='', depth=0):
return load_json('report/coverage_report.json')
elif isinstance(self, CoverageResult):
return load_json('report/coverage_result.json')
elif isinstance(self, QueueItem):
elif isinstance(self, (QueueItem, AsyncQueueItem)):
return load_json('queue/waitingitem.json')
raise TypeError(f'unknow item: {type(self)}')

Expand Down Expand Up @@ -97,11 +97,6 @@ def async_folder(async_jenkins):
return AsyncFolder(async_jenkins, f'{async_jenkins.url}job/folder/')


@pytest.fixture()
def async_folder(async_jenkins):
return AsyncFolder(async_jenkins, f'{async_jenkins.url}job/folder/')


@pytest.fixture(scope='module')
def job(jenkins):
return WorkflowJob(jenkins, f'{jenkins.url}job/folder/job/pipeline/')
Expand Down
74 changes: 67 additions & 7 deletions tests/unit/test_queue.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# encoding: utf-8

import pytest
from api4jenkins.build import WorkflowRun
from api4jenkins.job import WorkflowJob
from api4jenkins.queue import QueueItem
from api4jenkins.build import AsyncWorkflowRun, WorkflowRun
from api4jenkins.job import AsyncWorkflowJob, WorkflowJob
from api4jenkins.queue import AsyncQueueItem, QueueItem

from .conftest import load_json

Expand All @@ -19,6 +19,11 @@ def test_iter(self, jenkins):
items = list(jenkins.queue)
assert len(items) == 2

def test_cancel(self, jenkins, respx_mock):
req_url = f'{jenkins.queue.url}cancelItem?id=0'
respx_mock.post(req_url)
jenkins.queue.cancel(0)
assert respx_mock.calls[0].request.url == req_url

class TestQueueItem:

Expand All @@ -34,16 +39,14 @@ def _api_json(tree='', depth=0):
return load_json(f'queue/{type_}.json')

monkeypatch.setattr(item, 'api_json', _api_json)
job = item.get_job()
assert isinstance(job, WorkflowJob)
assert job == job
assert job == item.get_job()

@pytest.mark.parametrize('id_, type_, obj',
[(669, 'blockeditem', type(None)),
(599, 'leftitem', WorkflowRun),
(668, 'waitingitem', WorkflowRun),
(668, 'buildableitem', WorkflowRun)])
def test_get_build(self, jenkins, build, id_, type_, obj, monkeypatch):
def test_get_build(self, jenkins, id_, type_, obj, monkeypatch):
item = QueueItem(jenkins, f'{jenkins.url}queue/item/{id_}/')

def _api_json(tree='', depth=0):
Expand All @@ -62,3 +65,60 @@ def test_get_causes(self, jenkins):
item = QueueItem(jenkins, f'{jenkins.url}queue/item/668/')
causes = item.get_causes()
assert causes[0]['shortDescription'] == 'Triggered by'

class TestAsyncQueue:

@pytest.mark.parametrize('id_, obj',
[(1, type(None)), (669, AsyncQueueItem)])
async def test_get(self, async_jenkins, id_, obj):
assert isinstance(await async_jenkins.queue.get(id_), obj)

async def test_iter(self, async_jenkins):
items = [ i async for i in async_jenkins.queue]
assert len(items) == 2
async def test_cancel(self, async_jenkins, respx_mock):
req_url = f'{async_jenkins.queue.url}cancelItem?id=0'
respx_mock.post(req_url)
await async_jenkins.queue.cancel(0)
assert respx_mock.calls[0].request.url == req_url
class TestAsyncQueueItem:

@pytest.mark.parametrize('id_, type_',
[(669, 'blockeditem'),
(599, 'leftitem'),
(700, 'waitingitem'),
(668, 'buildableitem')])
async def test_get_job(self, async_jenkins, async_job, id_, type_, monkeypatch):
item = AsyncQueueItem(async_jenkins, f'{async_jenkins.url}queue/item/{id_}/')

async def _api_json(tree='', depth=0):
return load_json(f'queue/{type_}.json')

monkeypatch.setattr(item, 'api_json', _api_json)
job = await item.get_job()
assert isinstance(job, AsyncWorkflowJob)
assert job == job

@pytest.mark.parametrize('id_, type_, obj',
[(669, 'blockeditem', type(None)),
(599, 'leftitem', AsyncWorkflowRun),
(668, 'waitingitem', AsyncWorkflowRun),
(668, 'buildableitem', AsyncWorkflowRun)])
async def test_get_build(self, async_jenkins, id_, type_, obj, monkeypatch):
item = AsyncQueueItem(async_jenkins, f'{async_jenkins.url}queue/item/{id_}/')

async def _api_json(tree='', depth=0):
return load_json(f'queue/{type_}.json')

monkeypatch.setattr(item, 'api_json', _api_json)
assert isinstance(await item.get_build(), obj)

async def test_get_parameters(self, async_jenkins):
item = AsyncQueueItem(async_jenkins, f'{async_jenkins.url}queue/item/668/')
params = await item.get_parameters()
assert len(params) == 0

async def test_get_causes(self, async_jenkins):
item = AsyncQueueItem(async_jenkins, f'{async_jenkins.url}queue/item/668/')
causes = await item.get_causes()
assert causes[0]['shortDescription'] == 'Triggered by'

0 comments on commit 24124e1

Please sign in to comment.