mirror of
https://github.com/onyx-and-iris/obsws-python.git
synced 2024-11-22 21:00:53 +00:00
add callback unit tests.
callback deregister now accepts iterable.
This commit is contained in:
parent
95b1cb27da
commit
f5c2293dce
@ -42,13 +42,17 @@ class Callback:
|
|||||||
if fns not in self._callbacks:
|
if fns not in self._callbacks:
|
||||||
self._callbacks.append(fns)
|
self._callbacks.append(fns)
|
||||||
|
|
||||||
def deregister(self, callback):
|
def deregister(self, fns: Union[Iterable, Callable]):
|
||||||
"""deregisters a callback from _callbacks"""
|
"""deregisters a callback from _callbacks"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._callbacks.remove(callback)
|
iterator = iter(fns)
|
||||||
except ValueError:
|
for fn in iterator:
|
||||||
print(f"Failed to remove: {callback}")
|
if fn in self._callbacks:
|
||||||
|
self._callbacks.remove(fn)
|
||||||
|
except TypeError as e:
|
||||||
|
if fns in self._callbacks:
|
||||||
|
self._callbacks.remove(fns)
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
"""clears the _callbacks list"""
|
"""clears the _callbacks list"""
|
||||||
|
@ -41,8 +41,9 @@ class EventClient(object):
|
|||||||
self.base_client = ObsClient(**kwargs)
|
self.base_client = ObsClient(**kwargs)
|
||||||
self.base_client.authenticate()
|
self.base_client.authenticate()
|
||||||
self.callback = Callback()
|
self.callback = Callback()
|
||||||
|
self.subscribe()
|
||||||
|
|
||||||
self.running = True
|
def subscribe(self):
|
||||||
worker = Thread(target=self.trigger, daemon=True)
|
worker = Thread(target=self.trigger, daemon=True)
|
||||||
worker.start()
|
worker.start()
|
||||||
|
|
||||||
@ -52,6 +53,7 @@ class EventClient(object):
|
|||||||
|
|
||||||
Triggers a callback on event received.
|
Triggers a callback on event received.
|
||||||
"""
|
"""
|
||||||
|
self.running = True
|
||||||
while self.running:
|
while self.running:
|
||||||
self.data = json.loads(self.base_client.ws.recv())
|
self.data = json.loads(self.base_client.ws.recv())
|
||||||
event, data = (self.data["d"].get("eventType"), self.data["d"])
|
event, data = (self.data["d"].get("eventType"), self.data["d"])
|
||||||
|
59
tests/test_callback.py
Normal file
59
tests/test_callback.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import pytest
|
||||||
|
from obsstudio_sdk.callback import Callback
|
||||||
|
|
||||||
|
|
||||||
|
class TestCallbacks:
|
||||||
|
__test__ = True
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setup_class(cls):
|
||||||
|
cls.callback = Callback()
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def wraps_tests(self):
|
||||||
|
yield
|
||||||
|
self.callback.clear()
|
||||||
|
|
||||||
|
def test_register_callback(self):
|
||||||
|
def on_callback_method():
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.callback.register(on_callback_method)
|
||||||
|
assert self.callback.get() == ["CallbackMethod"]
|
||||||
|
|
||||||
|
def test_register_callbacks(self):
|
||||||
|
def on_callback_method_one():
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_callback_method_two():
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.callback.register((on_callback_method_one, on_callback_method_two))
|
||||||
|
assert self.callback.get() == ["CallbackMethodOne", "CallbackMethodTwo"]
|
||||||
|
|
||||||
|
def test_deregister_callback(self):
|
||||||
|
def on_callback_method_one():
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_callback_method_two():
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.callback.register((on_callback_method_one, on_callback_method_two))
|
||||||
|
self.callback.deregister(on_callback_method_one)
|
||||||
|
assert self.callback.get() == ["CallbackMethodTwo"]
|
||||||
|
|
||||||
|
def test_deregister_callbacks(self):
|
||||||
|
def on_callback_method_one():
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_callback_method_two():
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_callback_method_three():
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.callback.register(
|
||||||
|
(on_callback_method_one, on_callback_method_two, on_callback_method_three)
|
||||||
|
)
|
||||||
|
self.callback.deregister((on_callback_method_two, on_callback_method_three))
|
||||||
|
assert self.callback.get() == ["CallbackMethodOne"]
|
@ -1,5 +1,3 @@
|
|||||||
import time
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tests import req_cl
|
from tests import req_cl
|
||||||
|
Loading…
Reference in New Issue
Block a user