import unittest import dotenv import os import discord import requests from discord.ext import commands import Shitpost class TestShitpost(unittest.TestCase): def setUp(self): dotenv.load_dotenv() intents = discord.Intents.default() intents.message_content = True intents.members = True bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), description='Stroopwafel. Shitpost bot extraordinaire.', pm_help=True, intents=intents) self.cog = Shitpost.Shitpost(bot) def test_review(self): TOPIC = "qpwoeirut12" topic_review = self.cog.get_review_string(TOPIC) with self.subTest(msg=f"Topic review does not return a string"): self.assertIsInstance(topic_review, str) with self.subTest(msg=f"Topic review does not contain the topic"): self.assertIn(TOPIC, topic_review) basic_review = self.cog.get_review_string() with self.subTest(msg=f"Review does not return a string"): self.assertIsInstance(basic_review, str) def test_watspelenwe_sass(self): once = self.cog.get_watspelenwe_sass() twice = self.cog.get_watspelenwe_sass() with self.subTest(msg="Watspelenwe does not seed correctly"): self.assertEqual(once, twice) with self.subTest(msg="Watspelenwe does not return a string"): self.assertIsInstance(once, str) def test_watspelenwe_cal(self): data = requests.get(os.getenv('STROOP_CALDAV_CAL')) if not data: self.skipTest(f"Server at {os.getenv('STROOP_CALDAV_HOST')} has an error, or is unavailable") (activiteit, reden) = self.cog.get_watspelenwe_cal() with self.subTest(msg="Watspelenwe activiteit does not return a string"): self.assertIsInstance(activiteit, str) with self.subTest(msg="Watspelenwe reden does not return a string"): self.assertIsInstance(reden, str) def test_ktdm(self): with self.subTest(): wit = ["qwerty", "yuiop"] kaart = self.cog.get_ktdm(wit) self.assertIn(wit[0], kaart) self.assertIn(wit[1], kaart) with self.subTest(): wit = ["a", "b", "c", "d"] kaart = self.cog.get_ktdm(wit) self.assertEqual(kaart, None) with self.subTest(): wit = ["a", "b", "c"] self.cog.zwarte_lijst = ["___"] kaart = self.cog.get_ktdm(wit) self.assertEqual(kaart, None) def test_weer(self): data = requests.get("http://weerlive.nl/api/json-data-10min.php") if not data: self.skipTest(f"Weerlive has an error, or is unavailable") weerJson = self.cog.get_weer_json() with self.subTest("Weer does not correctly convert from json"): self.assertIsInstance(weerJson, dict) icon = self.cog.getWeatherIconTranslation(weerJson["d2weer"]) with self.subTest("Weericon not available"): self.assertNotEqual(icon, ":park:") with self.subTest("Weericon incorrect"): self.assertRegex(icon, ":[A-z]+:") with self.subTest("No weericon for unknown weather"): icon = self.cog.getWeatherIconTranslation("sniezelig") # Bestaat niet natuurlijk self.assertRegex(icon, ":[A-z]+:") bbq = self.cog.getBbqGrade(weerJson) with self.subTest("BBQ cijfer incorrect"): self.assertIsInstance(bbq, float) self.assertGreaterEqual(bbq, 1.0) self.assertLessEqual(bbq, 10.0) with self.subTest("False positive alarm"): weerMock = { "alarm": 0} alertCode, alertMessage = self.cog.getWeerAlarm(weerMock) self.assertEqual(alertCode, None) self.assertEqual(alertMessage, "") with self.subTest("False negative rood alarm"): weerMock = { "alarm": "1", "alarmtxt": "qwerty code rood yuiop"} alertCode, alertMessage = self.cog.getWeerAlarm(weerMock) self.assertEqual(alertCode, "rood") self.assertIsInstance(alertMessage, str) with self.subTest("False negative oranje alarm"): weerMock = { "alarm": "1", "alarmtxt": "qwerty code oranje yuiop"} alertCode, alertMessage = self.cog.getWeerAlarm(weerMock) self.assertEqual(alertCode, "oranje") self.assertIsInstance(alertMessage, str) with self.subTest("False negative geel alarm"): weerMock = { "alarm": "1", "alarmtxt": "lorem ipsum dolor sit amet"} alertCode, alertMessage = self.cog.getWeerAlarm(weerMock) self.assertEqual(alertCode, "geel") self.assertIsInstance(alertMessage, str) wb = self.cog.get_weerbericht(weerJson) wb_v = self.cog.get_weerbericht(weerJson, "verbose pls") with self.subTest("Can't render message from json"): self.assertIsInstance(wb, str) self.assertIsInstance(wb_v, str) with self.subTest("Verbose isn't actually verbose"): self.assertTrue(len(wb_v) > len(wb)) def test_roll(self): with self.subTest("Incorrect acceptance of malformed string"): self.assertIsNone(self.cog.get_dice_parameters("1 d 20 ")) with self.subTest("Incorrect parsing of capital D"): self.assertEqual((1, 20, None), self.cog.get_dice_parameters("1D20")) with self.subTest("Incorrect parsing of die-string"): self.assertEqual((1, 20, None), self.cog.get_dice_parameters("1d20")) with self.subTest("Incorrect parsing of die modifier"): self.assertEqual((2, 20, 3), self.cog.get_dice_parameters("2d20+3")) with self.subTest("Incorrect parsing of spacing in die-string"): self.assertEqual((1, 20, 5), self.cog.get_dice_parameters("1d20 + 5")) with self.subTest("Incorrect parsing of negative modifier"): self.assertEqual((1, 12, -3), self.cog.get_dice_parameters("1d12 - 3")) with self.subTest("Incorrect parsing of unbalanced spacing"): self.assertEqual((1, 12, -3), self.cog.get_dice_parameters("1d12 -3")) with self.subTest("Incorrect parsing of largest values"): self.assertEqual((99, 9999, 9999), self.cog.get_dice_parameters("99d9999 + 9999")) with self.subTest("Incorrect acceptance of too many dice"): self.assertIsNone(self.cog.get_dice_roll(100, 20, None)) with self.subTest("Incorrect acceptance of too big die"): self.assertIsNone(self.cog.get_dice_roll(4, 10000, None)) with self.subTest("Incorrect acceptance of too large modifier"): self.assertIsNone(self.cog.get_dice_roll(1, 20, 999999)) with self.subTest("Incorrect acceptance of too large negative modifier"): self.assertIsNone(self.cog.get_dice_roll(1, 20, -999999)) with self.subTest("Critical fail/success not marked"): self.assertIn("**10**", self.cog.get_dice_roll(99, 10, None)) self.assertIn("**1**", self.cog.get_dice_roll(99, 10, None)) self.assertTrue(True) def test_imgflips(self): eriku = self.cog.get_imgflip_embed(self.cog.imgflips['eriku'], "a", "b") tegeltje = self.cog.get_tegeltje("Man who put penis in peanut butter is fucking nuts", "Confucius") self.assertIsInstance(eriku, discord.Embed) self.assertIsInstance(tegeltje, discord.Embed) def test_inspire(self): inspo = self.cog.get_inspiration() self.assertIn("https://", inspo) self.assertIn(".jpg", inspo) def test_giethetaloan(self): self.assertIn("nee", self.cog.get_giet_het_oan().lower()) def test_corona(self): message = self.cog.get_corona_message() self.assertIsInstance(message, str) def test_zalgo(self): inmsg = "lorem ipsum" message = self.cog.zalgofy(inmsg) self.assertIsInstance(message, str) self.assertEqual(len(message), len(inmsg)*5)