Added more tests and removed broken codequality test

This commit is contained in:
Frisk 2022-01-12 13:55:08 +01:00
parent 54bf7716c9
commit 4450d7e642
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC
7 changed files with 206 additions and 4 deletions

View file

@ -1,8 +1,5 @@
image: python:3.7-alpine
include:
- template: Code-Quality.gitlab-ci.yml
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

View file

@ -21,7 +21,7 @@
"show_updown_messages": true,
"ignored_namespaces": [],
"extensions_dir": "extensions",
"error_tolerance": 0,
"error_tolerance": 2,
"overview": true,
"overview_time": "00:00",
"send_empty_overview": true,

View file

@ -0,0 +1,31 @@
# 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 <http://www.gnu.org/licenses/>.
import unittest
from test.test_utilities import inject_settings
from src.wiki import Wiki
class login_Testing(unittest.TestCase):
wiki = Wiki(None, None)
def test_connection_checker(self):
self.assertTrue(self.wiki.check_connection(looped=True))
def test_connection_tracker1(self): # expands this test
inject_settings("show_updown_messages", True)
self.wiki.downtimecredibility = 0
self.wiki.downtime_controller(True)
self.assertTrue(self.wiki.downtimecredibility > 0)

30
test/test_i18n.py Normal file
View file

@ -0,0 +1,30 @@
# 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 <http://www.gnu.org/licenses/>.
from test.test_utilities import inject_settings
import src.i18n
import unittest
class i18nTesting(unittest.TestCase):
def test_language_output_polish(self):
inject_settings("lang", "pl")
src.i18n.load_languages() # reload languages with new language
self.assertEqual(src.i18n.rcgcdw.gettext("Daily overview"), "Podsumowanie dnia")
def test_language_output_english(self):
inject_settings("lang", "en")
src.i18n.load_languages() # reload languages with new language
self.assertEqual(src.i18n.rcgcdw.gettext("Daily overview"), "Daily overview")

66
test/test_queue.py Normal file
View file

@ -0,0 +1,66 @@
# 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 <http://www.gnu.org/licenses/>.
import unittest
from typing import Tuple
from src.discord.queue import MessageQueue
from src.discord.message import DiscordMessage, DiscordMessageMetadata
def create_dummy(id: int = 0, **kwargs) -> Tuple[DiscordMessage, DiscordMessageMetadata]:
dm = DiscordMessage(event_type="log/{}".format(id), message_type="embed", webhook_url="https://example.com/")
dmm = DiscordMessageMetadata("POST", log_id=kwargs.get("log_id", int(id)*10), page_id=kwargs.get("page_id", int(id)),
rev_id=kwargs.get("rev_id", int(id)*10), webhook_url=kwargs.get("webhook_url", "https://example.com/"))
return dm, dmm
class TestQueue(unittest.TestCase):
def test_add_message(self):
queue = MessageQueue()
for _ in range(100):
queue.add_message(create_dummy())
self.assertEqual(len(queue), 100)
def test_cut_messages(self):
queue = MessageQueue()
for num in range(100):
queue.add_message(create_dummy(id=num))
queue.cut_messages(10)
self.assertEqual(list(queue)[0][1].page_id, 10)
def test_compare_message_to_dict(self):
queue = MessageQueue()
passing = [create_dummy(id=103, page_id=3928, rev_id=228848), create_dummy(id=108, page_id=3928, rev_id=228853)]
failing = [create_dummy(id=105, page_id=39, rev_id=2288), create_dummy(id=110, page_id=392, rev_id=228)]
for msg in passing:
with self.subTest():
self.assertTrue(queue.compare_message_to_dict(msg[1], {"page_id": 3928}))
for msg in failing:
with self.subTest():
self.assertFalse(queue.compare_message_to_dict(msg[1], {"page_id": 3928}))
def test_delete_all_with_matching_metadata(self):
queue = MessageQueue()
queue.add_message(create_dummy(id=103, page_id=500, rev_id=228844))
for num in range(100):
queue.add_message(create_dummy(id=num))
queue.add_message(create_dummy(id=105, page_id=3923, rev_id=228848))
queue_correct = MessageQueue()
for num in range(100):
queue_correct.add_message(create_dummy(id=num))
queue_correct.add_message(create_dummy(id=105, page_id=3923, rev_id=228848))
queue.delete_all_with_matching_metadata(page_id=500)
self.assertEqual(len(queue), len(queue_correct)) # Could be better but I'm running out of time

25
test/test_utilities.py Normal file
View file

@ -0,0 +1,25 @@
# 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 <http://www.gnu.org/licenses/>.
from typing import Any
import src.configloader
def inject_settings(setting: str, value: Any):
path = setting.split(".")
dic = src.configloader.settings
for key in path[:-1]:
dic = dic[key]
dic[path[-1]] = value

53
test/test_wiki_login.py Normal file
View file

@ -0,0 +1,53 @@
# 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 <http://www.gnu.org/licenses/>.
import unittest
from src.configloader import settings
from test.test_utilities import inject_settings
from src.wiki import Wiki
def cleanup_func(func):
def wrap(*args, **kwargs):
login = settings["wiki_bot_login"]
password = settings["wiki_bot_password"]
func(*args, **kwargs)
inject_settings("wiki_bot_login", login)
inject_settings("wiki_bot_password", password)
return func
return wrap
class login_Testing(unittest.TestCase):
wiki = Wiki(None, None)
def test_success(self):
self.wiki.logged_in = False
self.wiki.log_in()
self.assertTrue(self.wiki.logged_in)
@cleanup_func
def test_failure1(self):
self.wiki.logged_in = False
inject_settings("wiki_bot_login", "asdkaodhasofaufbasf")
with self.assertLogs("rcgcdw.rc", level="ERROR"):
self.wiki.log_in()
@cleanup_func
def test_failure2(self):
self.wiki.logged_in = False
inject_settings("wiki_bot_password", "lkkkkk")
with self.assertLogs("rcgcdw.rc", level="ERROR"):
self.wiki.log_in()