events: use context manager for testing fired events
This commit is contained in:
parent
9727a3880f
commit
b6bf036756
4 changed files with 43 additions and 29 deletions
|
|
@ -38,7 +38,9 @@ def trigger(event):
|
|||
|
||||
|
||||
from rhodecode.events.user import (
|
||||
UserPreCreate, UserPreUpdate, UserRegistered
|
||||
UserPreCreate,
|
||||
UserPreUpdate,
|
||||
UserRegistered
|
||||
)
|
||||
|
||||
from rhodecode.events.repo import (
|
||||
|
|
@ -46,4 +48,4 @@ from rhodecode.events.repo import (
|
|||
RepoPreDeleteEvent, RepoDeletedEvent,
|
||||
RepoPrePushEvent, RepoPushEvent,
|
||||
RepoPrePullEvent, RepoPullEvent,
|
||||
)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -34,8 +34,6 @@ class RepoPreCreateEvent(RepoEvent):
|
|||
"""
|
||||
An instance of this class is emitted as an :term:`event` before a repo is
|
||||
created.
|
||||
|
||||
:param repo_name: repository name
|
||||
"""
|
||||
name = 'repo-pre-create'
|
||||
|
||||
|
|
|
|||
|
|
@ -20,19 +20,22 @@ import mock
|
|||
import decorator
|
||||
|
||||
|
||||
def assert_fires_events(*expected_events):
|
||||
""" Testing decorator to check if the function fires events in order """
|
||||
def deco(func):
|
||||
def wrapper(func, *args, **kwargs):
|
||||
with mock.patch('rhodecode.events.trigger') as mock_trigger:
|
||||
result = func(*args, **kwargs)
|
||||
class EventCatcher(object):
|
||||
""" Testing context manager to check if events are fired """
|
||||
|
||||
captured_events = []
|
||||
for call in mock_trigger.call_args_list:
|
||||
event = call[0][0]
|
||||
captured_events.append(type(event))
|
||||
def __init__(self):
|
||||
self.events = [] # the actual events captured
|
||||
self.event_types = [] # the types of events captured
|
||||
|
||||
assert set(captured_events) == set(expected_events)
|
||||
return result
|
||||
return decorator.decorator(wrapper, func)
|
||||
return deco
|
||||
def __enter__(self):
|
||||
self.event_trigger_patch = mock.patch('rhodecode.events.trigger')
|
||||
self.mocked_event_trigger = self.event_trigger_patch.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, type_, value, traceback):
|
||||
self.event_trigger_patch.stop()
|
||||
|
||||
for call in self.mocked_event_trigger.call_args_list:
|
||||
event = call[0][0]
|
||||
self.events.append(event)
|
||||
self.event_types.append(type(event))
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
import pytest
|
||||
|
||||
from rhodecode.tests.events.conftest import assert_fires_events
|
||||
from rhodecode.tests.events.conftest import EventCatcher
|
||||
|
||||
from rhodecode.lib import hooks_base, utils2
|
||||
from rhodecode.model.repo import RepoModel
|
||||
|
|
@ -49,20 +49,31 @@ def scm_extras(user_regular, repo_stub):
|
|||
return extras
|
||||
|
||||
|
||||
@assert_fires_events(
|
||||
RepoPreCreateEvent, RepoCreatedEvent, RepoPreDeleteEvent, RepoDeletedEvent)
|
||||
def test_create_delete_repo_fires_events(backend):
|
||||
repo = backend.create_repo()
|
||||
RepoModel().delete(repo)
|
||||
with EventCatcher() as event_catcher:
|
||||
repo = backend.create_repo()
|
||||
assert event_catcher.event_types == [RepoPreCreateEvent, RepoCreatedEvent]
|
||||
|
||||
with EventCatcher() as event_catcher:
|
||||
RepoModel().delete(repo)
|
||||
assert event_catcher.event_types == [RepoPreDeleteEvent, RepoDeletedEvent]
|
||||
|
||||
|
||||
@assert_fires_events(RepoPrePushEvent, RepoPushEvent)
|
||||
def test_pull_fires_events(scm_extras):
|
||||
hooks_base.pre_push(scm_extras)
|
||||
hooks_base.post_push(scm_extras)
|
||||
with EventCatcher() as event_catcher:
|
||||
hooks_base.pre_push(scm_extras)
|
||||
assert event_catcher.event_types == [RepoPrePushEvent]
|
||||
|
||||
with EventCatcher() as event_catcher:
|
||||
hooks_base.post_push(scm_extras)
|
||||
assert event_catcher.event_types == [RepoPushEvent]
|
||||
|
||||
|
||||
@assert_fires_events(RepoPrePullEvent, RepoPullEvent)
|
||||
def test_push_fires_events(scm_extras):
|
||||
hooks_base.pre_pull(scm_extras)
|
||||
hooks_base.post_pull(scm_extras)
|
||||
with EventCatcher() as event_catcher:
|
||||
hooks_base.pre_pull(scm_extras)
|
||||
assert event_catcher.event_types == [RepoPrePullEvent]
|
||||
|
||||
with EventCatcher() as event_catcher:
|
||||
hooks_base.post_pull(scm_extras)
|
||||
assert event_catcher.event_types == [RepoPullEvent]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue