From f06b782d9d4e7c79cd16da6ba3c134d411aa87c9 Mon Sep 17 00:00:00 2001 From: Frisk Date: Wed, 12 Jan 2022 16:45:07 +0100 Subject: [PATCH] Added new test --- test/data/rc_objects.json | 17 ++++++++++++ test/data/rc_results.json | 3 ++ test/test_formatters.py | 58 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 test/data/rc_objects.json create mode 100644 test/data/rc_results.json create mode 100644 test/test_formatters.py diff --git a/test/data/rc_objects.json b/test/data/rc_objects.json new file mode 100644 index 0000000..c6863ec --- /dev/null +++ b/test/data/rc_objects.json @@ -0,0 +1,17 @@ +{ + "edit": { + "type": "edit", + "ns": 0, + "title": "Some page", + "pageid": 9327, + "revid": 2075232, + "old_revid": 232555, + "rcid": 2793437, + "user": "User3", + "oldlen": 32882, + "newlen": 328, + "timestamp": "2022-03-26T11:41:22Z", + "parsedcomment": "Work on new as", + "tags": ["VisualEdit"] + } +} \ No newline at end of file diff --git a/test/data/rc_results.json b/test/data/rc_results.json new file mode 100644 index 0000000..14825b2 --- /dev/null +++ b/test/data/rc_results.json @@ -0,0 +1,3 @@ +{ + "edit": {"allowed_mentions": {"parse": []}, "avatar_url": "", "embeds": [{"color": 16711680, "url": "https://minecraft.fandom.com/index.php?title=Some_page&curid=9327&diff=2075232&oldid=232555", "title": "Some page (-32554)", "fields": [{"name": "Usuni\u0119to", "value": "__Tylko znaki niedrukowane__", "inline": true}, {"name": "Dodano", "value": "__Tylko znaki niedrukowane__", "inline": true}, {"name": "Tagi", "value": "VisualEdit", "inline": false}], "author": {"name": "User3", "url": "https://minecraft.fandom.com/wiki/User:User3", "icon_url": ""}, "timestamp": "2022-03-26T11:41:22Z", "description": "Work on new as"}]} +} \ No newline at end of file diff --git a/test/test_formatters.py b/test/test_formatters.py new file mode 100644 index 0000000..2aa5a39 --- /dev/null +++ b/test/test_formatters.py @@ -0,0 +1,58 @@ +# 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 importlib +import json +from src.configloader import settings +from src.api.context import Context +from src.api.client import Client +from src.api.util import default_message +from src.api.hooks import formatter_hooks +from src.misc import WIKI_SCRIPT_PATH +from test.test_utilities import inject_settings +from unittest.mock import Mock, PropertyMock +import unittest + + +def no_formatter(ctx: Context, change: dict) -> None: + raise NameError + + +inject_settings("appearance.mode", "embed") +importlib.import_module(settings.get('extensions_dir', 'extensions'), 'extensions') +formatter_hooks["no_formatter"] = no_formatter +with open("test/data/rc_objects.json", "r") as ob: + jsons = json.loads(ob.read()) +with open("test/data/rc_results.json", "r") as ob: + results = json.loads(ob.read()) + + +def get_objects(name: str): + return jsons.get(name), json.dumps(results.get(name)) + +class TestMWFormatter(unittest.TestCase): + def test_edit_embed(self): + test = default_message("edit", formatter_hooks) + ctx = PropertyMock() + ctx.message_type = "embed" + ctx.event_type = "edit" + ctx.event = "edit" + ctx.parsedcomment = "Work on new as" + ctx.client.WIKI_SCRIPT_PATH = WIKI_SCRIPT_PATH + ctx.webhook_url = "https://example.com" + # ctx.client.return_value = Mock(spec=Client) + edit_c, results = get_objects("edit") + result = repr(test(ctx, edit_c)) + self.assertEqual(results, result)