Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swvanbuuren/add decorator kwargs tests #23

Merged
merged 2 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pqthreads/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Decorator: # pylint: disable=too-few-public-methods
def __init__(self, decorator_class: DecoratorCore):
self.decorator_class = decorator_class

def __call__(self, wrapped, **kwargs):
def __call__(self, wrapped=None, **kwargs):
""" Enables usage with and without decorator keyword arguments """
if wrapped is None:
return functools.partial(self.__call__, **kwargs)
Expand Down
20 changes: 16 additions & 4 deletions pqthreads/examples/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

def raise_worker_exception(self):
""" Method to raise custom exception for testing purposes """
raise FigureWorkerException('Custom exception')

Check failure on line 24 in pqthreads/examples/worker.py

View workflow job for this annotation

GitHub Actions / lint-and-test / Lint and test (3.8)

Custom exception

Check failure on line 24 in pqthreads/examples/worker.py

View workflow job for this annotation

GitHub Actions / lint-and-test / Lint and test (3.9)

Custom exception

Check failure on line 24 in pqthreads/examples/worker.py

View workflow job for this annotation

GitHub Actions / lint-and-test / Lint and test (3.10)

Custom exception

Check failure on line 24 in pqthreads/examples/worker.py

View workflow job for this annotation

GitHub Actions / lint-and-test / Lint and test (3.11)

Custom exception


class GraphWorker(containers.WorkerItem):
Expand Down Expand Up @@ -50,8 +50,20 @@
return graph_worker


global_kwargs = {}


# Configure decorators
DecoratorCore = decorator.DecoratorCore
DecoratorCore.add_agent('figure', window.FigureWindow, FigureWorker)
DecoratorCore.add_agent('graph', window.GraphWindow, GraphWorker)
decorator_example = decorator.Decorator(DecoratorCore)
class ExampleDecoratorCore(decorator.DecoratorCore):
""" Exemplary decorator core class, with extra testing functionality for
global keyword arguments """

def __init__(self, **dec_kwargs):
""" Reimplement this to make use of decorator's keyword arguments """
global_kwargs.update(dec_kwargs)
super().__init__(**dec_kwargs)


ExampleDecoratorCore.add_agent('figure', window.FigureWindow, FigureWorker)
ExampleDecoratorCore.add_agent('graph', window.GraphWindow, GraphWorker)
decorator_example = decorator.Decorator(ExampleDecoratorCore)
27 changes: 27 additions & 0 deletions tests/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,30 @@ def main():

result = main()
assert result == 'Test successful'


def test_decorator_keyword_argument():
""" Test functionality of single decorator keyword argument """

@worker.decorator_example(keyword_argument='test')
def main():
""" Helper function """
fig = worker.figure()
fig.close()

main()
assert worker.global_kwargs['keyword_argument'] == 'test'


def test_multiple_decorator_keyword_arguments():
""" Test functionality of multiple decorator keyword arguments """

@worker.decorator_example(first_kwarg='first_arg', second_kw='second_arg')
def main():
""" Helper function """
fig = worker.figure()
fig.close()

main()
assert worker.global_kwargs['first_kwarg'] == 'first_arg'
assert worker.global_kwargs['second_kw'] == 'second_arg'
Loading