From 56fd7aefb781d9552f24aa6b9297410201fe2f1f Mon Sep 17 00:00:00 2001 From: Frisk Date: Sat, 8 Jan 2022 13:37:33 +0100 Subject: [PATCH] Added tests for registering formatters and hooks --- src/api/formatter.py | 1 + test/test_hooks.py | 76 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 test/test_hooks.py diff --git a/src/api/formatter.py b/src/api/formatter.py index c9946a1..35926cc 100644 --- a/src/api/formatter.py +++ b/src/api/formatter.py @@ -21,6 +21,7 @@ from typing import Optional, Callable logger = logging.getLogger("src.api.formatter") + def _register_formatter(func, kwargs, formatter_type: str, action_type=None): """ Registers a formatter inside of src.rcgcdw.formatter_hooks diff --git a/test/test_hooks.py b/test/test_hooks.py new file mode 100644 index 0000000..4969e5d --- /dev/null +++ b/test/test_hooks.py @@ -0,0 +1,76 @@ +# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw). +# +# RcGcDw is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# RcGcDw is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with RcGcDw. If not, see . + +import unittest + +from api.context import Context +from discord.message import DiscordMessage, DiscordMessageMetadata +from src.api import formatter +from src.api.hook import pre_hook, post_hook +from src.api.hooks import formatter_hooks, pre_hooks, post_hooks + + +class ApiTesting(unittest.TestCase): + def test_embed_formatter_registration(self): + formatter_hooks.clear() + + @formatter.embed(event="test", mode="embed") + def test_formatter_registration(ctx: Context, change: dict) -> DiscordMessage: + pass + self.assertEqual(formatter_hooks["test"], test_formatter_registration) + + def test_compact_formatter_registration(self): + formatter_hooks.clear() + + @formatter.embed(event="test", mode="compact") + def test_formatter_registration(ctx: Context, change: dict) -> DiscordMessage: + pass + self.assertEqual(formatter_hooks["test"], test_formatter_registration) + + def test_overwrite_formatter_registration_warning(self): + formatter_hooks.clear() + + @formatter.embed(event="test", mode="compact") + def test_formatter_registration(ctx: Context, change: dict) -> DiscordMessage: + pass + + with self.assertLogs("src.api.formatter", level="WARNING"): + @formatter.embed(event="test", mode="compact") + def test_other_formatter_registration(ctx: Context, change: dict) -> DiscordMessage: + pass + + def test_formatter_aliasing(self): + formatter_hooks.clear() + + @formatter.embed(event="test", mode="compact", aliases=["test2", "test3"]) + def test_formatter_registration(ctx: Context, change: dict) -> DiscordMessage: + pass + self.assertEqual(formatter_hooks["test2"], test_formatter_registration) + + def test_pre_hook_registration(self): + pre_hooks.clear() + + @pre_hook + def test_prehook(some_data: Context, change: dict): + pass + self.assertEqual(pre_hooks[0], test_prehook) + + def test_post_hook_registration(self): + post_hooks.clear() + + @post_hook + def test_posthook(message: DiscordMessage, metadata: DiscordMessageMetadata, context: Context, change: dict): + pass + self.assertEqual(post_hooks[0], test_posthook)