##// END OF EJS Templates
events: use context manager for testing fired events
dan -
r376:ce1a3e78 default
parent child Browse files
Show More
@@ -38,7 +38,9 b' def trigger(event):'
38
38
39
39
40 from rhodecode.events.user import (
40 from rhodecode.events.user import (
41 UserPreCreate, UserPreUpdate, UserRegistered
41 UserPreCreate,
42 UserPreUpdate,
43 UserRegistered
42 )
44 )
43
45
44 from rhodecode.events.repo import (
46 from rhodecode.events.repo import (
@@ -46,4 +48,4 b' from rhodecode.events.repo import ('
46 RepoPreDeleteEvent, RepoDeletedEvent,
48 RepoPreDeleteEvent, RepoDeletedEvent,
47 RepoPrePushEvent, RepoPushEvent,
49 RepoPrePushEvent, RepoPushEvent,
48 RepoPrePullEvent, RepoPullEvent,
50 RepoPrePullEvent, RepoPullEvent,
49 ) No newline at end of file
51 )
@@ -34,8 +34,6 b' class RepoPreCreateEvent(RepoEvent):'
34 """
34 """
35 An instance of this class is emitted as an :term:`event` before a repo is
35 An instance of this class is emitted as an :term:`event` before a repo is
36 created.
36 created.
37
38 :param repo_name: repository name
39 """
37 """
40 name = 'repo-pre-create'
38 name = 'repo-pre-create'
41
39
@@ -20,19 +20,22 b' import mock'
20 import decorator
20 import decorator
21
21
22
22
23 def assert_fires_events(*expected_events):
23 class EventCatcher(object):
24 """ Testing decorator to check if the function fires events in order """
24 """ Testing context manager to check if events are fired """
25 def deco(func):
25
26 def wrapper(func, *args, **kwargs):
26 def __init__(self):
27 with mock.patch('rhodecode.events.trigger') as mock_trigger:
27 self.events = [] # the actual events captured
28 result = func(*args, **kwargs)
28 self.event_types = [] # the types of events captured
29
29
30 captured_events = []
30 def __enter__(self):
31 for call in mock_trigger.call_args_list:
31 self.event_trigger_patch = mock.patch('rhodecode.events.trigger')
32 event = call[0][0]
32 self.mocked_event_trigger = self.event_trigger_patch.start()
33 captured_events.append(type(event))
33 return self
34
34
35 assert set(captured_events) == set(expected_events)
35 def __exit__(self, type_, value, traceback):
36 return result
36 self.event_trigger_patch.stop()
37 return decorator.decorator(wrapper, func)
37
38 return deco No newline at end of file
38 for call in self.mocked_event_trigger.call_args_list:
39 event = call[0][0]
40 self.events.append(event)
41 self.event_types.append(type(event))
@@ -20,7 +20,7 b''
20
20
21 import pytest
21 import pytest
22
22
23 from rhodecode.tests.events.conftest import assert_fires_events
23 from rhodecode.tests.events.conftest import EventCatcher
24
24
25 from rhodecode.lib import hooks_base, utils2
25 from rhodecode.lib import hooks_base, utils2
26 from rhodecode.model.repo import RepoModel
26 from rhodecode.model.repo import RepoModel
@@ -49,20 +49,31 b' def scm_extras(user_regular, repo_stub):'
49 return extras
49 return extras
50
50
51
51
52 @assert_fires_events(
53 RepoPreCreateEvent, RepoCreatedEvent, RepoPreDeleteEvent, RepoDeletedEvent)
54 def test_create_delete_repo_fires_events(backend):
52 def test_create_delete_repo_fires_events(backend):
55 repo = backend.create_repo()
53 with EventCatcher() as event_catcher:
56 RepoModel().delete(repo)
54 repo = backend.create_repo()
55 assert event_catcher.event_types == [RepoPreCreateEvent, RepoCreatedEvent]
56
57 with EventCatcher() as event_catcher:
58 RepoModel().delete(repo)
59 assert event_catcher.event_types == [RepoPreDeleteEvent, RepoDeletedEvent]
57
60
58
61
59 @assert_fires_events(RepoPrePushEvent, RepoPushEvent)
60 def test_pull_fires_events(scm_extras):
62 def test_pull_fires_events(scm_extras):
61 hooks_base.pre_push(scm_extras)
63 with EventCatcher() as event_catcher:
62 hooks_base.post_push(scm_extras)
64 hooks_base.pre_push(scm_extras)
65 assert event_catcher.event_types == [RepoPrePushEvent]
66
67 with EventCatcher() as event_catcher:
68 hooks_base.post_push(scm_extras)
69 assert event_catcher.event_types == [RepoPushEvent]
63
70
64
71
65 @assert_fires_events(RepoPrePullEvent, RepoPullEvent)
66 def test_push_fires_events(scm_extras):
72 def test_push_fires_events(scm_extras):
67 hooks_base.pre_pull(scm_extras)
73 with EventCatcher() as event_catcher:
68 hooks_base.post_pull(scm_extras)
74 hooks_base.pre_pull(scm_extras)
75 assert event_catcher.event_types == [RepoPrePullEvent]
76
77 with EventCatcher() as event_catcher:
78 hooks_base.post_pull(scm_extras)
79 assert event_catcher.event_types == [RepoPullEvent]
General Comments 0
You need to be logged in to leave comments. Login now