From 86fb4c38ea9132194e1e3a6f1875355b29f8189f Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Mon, 1 Jun 2020 11:33:42 +0200 Subject: [PATCH] [tests] add pytest for events see #642 --- .coveragerc | 1 + pytests/__init__.py | 4 +++ pytests/core/__init__.py | 0 pytests/core/test_event.py | 73 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 pytests/__init__.py create mode 100644 pytests/core/__init__.py create mode 100644 pytests/core/test_event.py diff --git a/.coveragerc b/.coveragerc index ec0aef3..e714d9c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -4,6 +4,7 @@ omit = bumblebee_status/_version.* setup.* tests/* + pytests/* *mock* *funcsigs* *pbr* diff --git a/pytests/__init__.py b/pytests/__init__.py new file mode 100644 index 0000000..afddb20 --- /dev/null +++ b/pytests/__init__.py @@ -0,0 +1,4 @@ +import bumblebee_status.discover + +bumblebee_status.discover.discover() + diff --git a/pytests/core/__init__.py b/pytests/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pytests/core/test_event.py b/pytests/core/test_event.py new file mode 100644 index 0000000..b64478f --- /dev/null +++ b/pytests/core/test_event.py @@ -0,0 +1,73 @@ +import pytest + +import core.event + +@pytest.fixture +def someEvent(): + class Event(): + def __init__(self): + core.event.clear() + self.id = "some event" + self.called = 0 + self.call_args = [] + self.call_kwargs = [] + + def callback(self, *args, **kwargs): + self.called += 1 + if args: + self.call_args.append(list(args)) + if kwargs: + self.call_kwargs.append(kwargs) + + return Event() + + +def test_simple_callback(someEvent): + assert someEvent.called == 0 + + core.event.register(someEvent.id, someEvent.callback) + core.event.register(someEvent.id, someEvent.callback) + + core.event.trigger(someEvent.id) + + assert someEvent.called == 2 + +def test_args_callback(someEvent): + core.event.register(someEvent.id, someEvent.callback, "a", "b") + core.event.trigger(someEvent.id) + + assert someEvent.called == 1 + assert len(someEvent.call_args) == 1 + assert someEvent.call_args[0] == ["a", "b"] + +def test_kwargs_callback(someEvent): + core.event.register( + someEvent.id, someEvent.callback, "a", "b", key1="test", key2="another" + ) + core.event.trigger(someEvent.id) + + assert someEvent.called == 1 + assert len(someEvent.call_args) == 1 + assert someEvent.call_args[0] == ["a", "b"] + assert len(someEvent.call_kwargs) == 1 + assert someEvent.call_kwargs[0] == { "key1": "test", "key2": "another" } + +def test_arg_trigger(someEvent): + core.event.register(someEvent.id, someEvent.callback) + core.event.trigger(someEvent.id, "a", "b") + + assert someEvent.called == 1 + assert len(someEvent.call_args) == 1 + assert someEvent.call_args[0] == ["a", "b"] + +def test_kwargs_trigger(someEvent): + core.event.register(someEvent.id, someEvent.callback) + core.event.trigger(someEvent.id, "a", "c", key1="test", key2="something") + + assert someEvent.called == 1 + assert len(someEvent.call_args) == 1 + assert someEvent.call_args[0] == ["a", "c"] + assert len(someEvent.call_kwargs) == 1 + assert someEvent.call_kwargs[0] == { "key1": "test", "key2": "something" } + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4