From f445aefe0060b8b2f82fba3aa783c49726a66ec1 Mon Sep 17 00:00:00 2001 From: t1ngson <74460709+t1ngson@users.noreply.github.com> Date: Mon, 16 Nov 2020 21:07:24 +0000 Subject: [PATCH 01/18] Start of new cog: Insight --- cogs/Insight.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 cogs/Insight.py diff --git a/cogs/Insight.py b/cogs/Insight.py new file mode 100644 index 00000000..e69de29b From 1a9090258edfe9212b95b798eba26cd9979aaeb3 Mon Sep 17 00:00:00 2001 From: t1ngson <74460709+t1ngson@users.noreply.github.com> Date: Mon, 16 Nov 2020 22:00:45 +0000 Subject: [PATCH 02/18] Code for initialisation added --- cogs/Insight.py | 0 cogs/Insights.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) delete mode 100644 cogs/Insight.py create mode 100644 cogs/Insights.py diff --git a/cogs/Insight.py b/cogs/Insight.py deleted file mode 100644 index e69de29b..00000000 diff --git a/cogs/Insights.py b/cogs/Insights.py new file mode 100644 index 00000000..ab1becbf --- /dev/null +++ b/cogs/Insights.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +""" +Koala Bot Insights Code +Created by: Samuel Tiongson +""" +from discord.ext import commands + +import KoalaBot + + +def insights_is_enabled(ctx): + """ + A command used to check if the guild has enabled insights + e.g. @commands.check(KoalaBot.is_admin) + :param ctx: The context of the message + :return: True if admin or test, False otherwise + """ + try: + result = KoalaBot.check_guild_has_ext(ctx, "Insights") + except PermissionError: + result = False + + return result or (str(ctx.author) == KoalaBot.TEST_USER and KoalaBot.is_dpytest) + + +class Insights(commands.Cog): + """ + A discord.py cog pertaining to showing the insights of the KoalaBot + """ + def __init__(self, bot, database_manager=None): + self.bot = bot + + @commands.check(KoalaBot.is_admin) + @commands.check(insights_is_enabled) + @commands.command(name="insightServer", aliases=["insight_server"]) + async def insight_server(self, ctx): + print("h") + + + + + + + + + +def setup(bot: KoalaBot) -> None: + """ + Load this cog to the KoalaBot. + :param bot: the bot client for KoalaBot + """ + bot.add_cog(Insights(bot)) + print("Insights is ready.") \ No newline at end of file From f5fb971b7f5c51507db55f19f9ba972066425dcb Mon Sep 17 00:00:00 2001 From: t1ngson <74460709+t1ngson@users.noreply.github.com> Date: Mon, 14 Dec 2020 18:35:21 +0000 Subject: [PATCH 03/18] Added functionality of k!insights and k!servers --- cogs/Insights.py | 82 +++++++++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 28 deletions(-) diff --git a/cogs/Insights.py b/cogs/Insights.py index ab1becbf..144a6493 100644 --- a/cogs/Insights.py +++ b/cogs/Insights.py @@ -9,40 +9,66 @@ import KoalaBot -def insights_is_enabled(ctx): - """ - A command used to check if the guild has enabled insights - e.g. @commands.check(KoalaBot.is_admin) - :param ctx: The context of the message - :return: True if admin or test, False otherwise - """ - try: - result = KoalaBot.check_guild_has_ext(ctx, "Insights") - except PermissionError: - result = False - - return result or (str(ctx.author) == KoalaBot.TEST_USER and KoalaBot.is_dpytest) - - class Insights(commands.Cog): """ A discord.py cog pertaining to showing the insights of the KoalaBot + """ + def __init__(self, bot, database_manager=None): self.bot = bot - @commands.check(KoalaBot.is_admin) - @commands.check(insights_is_enabled) - @commands.command(name="insightServer", aliases=["insight_server"]) - async def insight_server(self, ctx): - print("h") - - - - - - - + @commands.check(KoalaBot.is_owner) + @commands.command(name="insights", aliases=[]) + async def insights(self, ctx): + """ + Returns the number of servers KoalaBot is in and the total number of members of those servers. + + :param ctx: The discord context + """ + guilds = self.bot.guilds + number_of_servers = len(guilds) + number_of_members = 0 + + for guild in guilds: + number_of_members += guild.member_count + + await ctx.send(f"KoalaBot is in {number_of_servers} servers with a member total of {number_of_members}.") + + @commands.check(KoalaBot.is_owner) + @commands.command(name="servers", aliases=[]) + async def servers(self, ctx, *, arg=None): + """ + Returns the names of the servers KoalaBot is in + + :param ctx: The discord context + :param arg: Searches for guilds with argument provided + """ + guild_list = self.bot.fetch_guilds() + guild_list_names = [] + + async for guild in guild_list: + if arg is not None: + if arg.upper() in guild.name.upper().split(" "): + guild_list_names.append(guild.name) + else: + guild_list_names.append(guild.name) + + if len(guild_list_names) == 0 and arg is None: + await ctx.send("KoalaBot is in no servers!") + elif len(guild_list_names) == 0 and arg is not None: + await ctx.send(f"No servers with {arg} in their name!") + else: + string_to_send = '' + while len(guild_list_names) != 0: + length = len(guild_list_names[0]) + if len(string_to_send) + length + 2 > 2000: + await ctx.send(string_to_send) + string_to_send = '' + else: + guild = guild_list_names.pop(0) + string_to_send += guild + ", " + await ctx.send(string_to_send[:-2]) def setup(bot: KoalaBot) -> None: @@ -51,4 +77,4 @@ def setup(bot: KoalaBot) -> None: :param bot: the bot client for KoalaBot """ bot.add_cog(Insights(bot)) - print("Insights is ready.") \ No newline at end of file + print("Insights is ready.") From 1413378a2196a5f8b7feec343d31738f6bb98466 Mon Sep 17 00:00:00 2001 From: t1ngson <74460709+t1ngson@users.noreply.github.com> Date: Mon, 14 Dec 2020 18:49:52 +0000 Subject: [PATCH 04/18] Added comments to Insights --- cogs/Insights.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cogs/Insights.py b/cogs/Insights.py index 144a6493..26e19688 100644 --- a/cogs/Insights.py +++ b/cogs/Insights.py @@ -5,14 +5,12 @@ Created by: Samuel Tiongson """ from discord.ext import commands - import KoalaBot class Insights(commands.Cog): """ A discord.py cog pertaining to showing the insights of the KoalaBot - """ def __init__(self, bot, database_manager=None): @@ -44,30 +42,46 @@ async def servers(self, ctx, *, arg=None): :param ctx: The discord context :param arg: Searches for guilds with argument provided """ + # Retrieves AsyncIterator guild_list = self.bot.fetch_guilds() guild_list_names = [] + # Cycle through iterator, check if there is an arg, if there is check against the arg, if not just append the + # guild name async for guild in guild_list: if arg is not None: + # Change arg and guild name to uppercase, and split guild name by spaces if arg.upper() in guild.name.upper().split(" "): guild_list_names.append(guild.name) else: guild_list_names.append(guild.name) + # If there are no guilds and no arguments if len(guild_list_names) == 0 and arg is None: await ctx.send("KoalaBot is in no servers!") + + # If there are no guilds but there are arguments elif len(guild_list_names) == 0 and arg is not None: await ctx.send(f"No servers with {arg} in their name!") + + # There must be guilds in the list else: string_to_send = '' + # While there are guilds in the list, run code while len(guild_list_names) != 0: + # Get the length of the first server name length = len(guild_list_names[0]) + # If this length + the current string length + 2 for comma and space is greater than 2000 if len(string_to_send) + length + 2 > 2000: + # Print the string and reset it to nothing await ctx.send(string_to_send) string_to_send = '' else: + # If the above is not true, then pop the server name from the list and add it to the string guild = guild_list_names.pop(0) string_to_send += guild + ", " + + # Remove the comma and space at the end of the string await ctx.send(string_to_send[:-2]) From 1c8ace8a36f428a56b3b87b534366e8f693f0031 Mon Sep 17 00:00:00 2001 From: t1ngson <74460709+t1ngson@users.noreply.github.com> Date: Sun, 27 Dec 2020 22:21:45 +0000 Subject: [PATCH 05/18] Added to CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2f97aa8..9c67307c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ A lot of these commands will only be available to administrators - Add regex validation to ensure valid regex only - Fix mod channel not saving correctly +### Insights +#### Added +- `insights` Displays the insights for KoalaBot +- `servers ` Displays the servers that the bot is in with the given text in their name. (If left blank, displays all servers) + ## [0.2.0] - 15-10-2020 ### Text Filter ##### Added From ca97f3229ec533050dded3f38b1cb29544dc37c6 Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 5 Apr 2021 07:01:46 +0100 Subject: [PATCH 06/18] Add tests for insights and servers commands --- tests/test_Insights.py | 114 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tests/test_Insights.py diff --git a/tests/test_Insights.py b/tests/test_Insights.py new file mode 100644 index 00000000..35ebeaa4 --- /dev/null +++ b/tests/test_Insights.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python + +""" +Testing KoalaBot Insights Cog + +Commented using reStructuredText (reST) +""" +# Futures + +# Built-in/Generic Imports +import random +from typing import * + +# Libs +import discord +import discord.ext.test as dpytest +import mock +import pytest +from discord.ext import commands +from discord.ext.test import factories as dpyfactory + +# Own modules +import KoalaBot +from cogs import Insights +from cogs.Insights import InsightsDBManager +from tests.utils import TestUtils as utils +from tests.utils import TestUtilsCog +from utils.KoalaDBManager import KoalaDBManager + +# Constants + +# Variables +insights_cog: Insights.Insights = None +utils_cog: TestUtilsCog.TestUtilsCog = None +DBManager = InsightsDBManager(KoalaBot.database_manager) +DBManager.create_tables() + + +def setup_function(): + """ setup any state specific to the execution of the given module.""" + global insights_cog + global utils_cog + bot: commands.Bot = commands.Bot(command_prefix=KoalaBot.COMMAND_PREFIX) + insights_cog = Insights.Insights(bot) + utils_cog = TestUtilsCog.TestUtilsCog(bot) + bot.add_cog(insights_cog) + bot.add_cog(utils_cog) + dpytest.configure(bot) + print("Tests starting") + +@pytest.mark.asyncio +@pytest.mark.parametrize("num_guilds, num_users", [(1,1),(1,2),(1,5),(2,1),(2,2),(5,4),(5,100)]) +async def test_insights(num_guilds, num_users): + test_config = dpytest.get_config() + client = test_config.client + for i in range(num_guilds - 1): + guild = dpytest.back.make_guild(f"Test Guild {i}") + test_config.guilds.append(guild) + + for i in range(len(test_config.guilds)): + for j in range(num_users - 1): + await dpytest.member_join(i, client.user) + await dpytest.member_join(i) + + await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") + dpytest.verify_message(f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {num_guilds * num_users}.") + +@pytest.mark.asyncio +async def test_servers_no_args(): + test_config = dpytest.get_config() + client = test_config.client + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") + dpytest.verify_message("Test Guild 0") + await dpytest.kick_callback(test_config.guilds[0], client.user) + expected = "" + for i in range(10): + guild = dpytest.back.make_guild(f"Test Guild {i}") + expected += f"Test Guild {i}, " + test_config.guilds.append(guild) + await dpytest.member_join(i,client.user) + await dpytest.message(KoalaBot.COMMAND_PREFIX+"servers",i) + await dpytest.verify_message(expected[:-2]) + + +@pytest.mark.asyncio +async def test_servers_fail_args(): + test_config = dpytest.get_config() + client = test_config.client + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") + dpytest.verify_message("Test Guild 0") + await dpytest.kick_callback(test_config.guilds[0], client.user) + arg = "fail_pls" + for i in range(10): + guild = dpytest.back.make_guild(f"Test Guild {i}") + test_config.guilds.append(guild) + await dpytest.member_join(i, client.user) + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers", i) + await dpytest.verify_message(f"No servers with {arg} in their name") + +@pytest.mark.asyncio +async def test_servers_with_args(): + test_config = dpytest.get_config() + client = test_config.client + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") + dpytest.verify_message("Test Guild 0") + await dpytest.kick_callback(test_config.guilds[0], client.user) + arg = "0" + for i in range(10): + guild = dpytest.back.make_guild(f"Test Guild {i}") + test_config.guilds.append(guild) + await dpytest.member_join(i, client.user) + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers", i) + await dpytest.verify_message("Test Guild 0") + From bd2ae8f0ba039683ab67772bd080ed23dd063730 Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 5 Apr 2021 07:01:46 +0100 Subject: [PATCH 07/18] Add tests for insights and servers commands --- tests/test_Insights.py | 114 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tests/test_Insights.py diff --git a/tests/test_Insights.py b/tests/test_Insights.py new file mode 100644 index 00000000..7df9a95e --- /dev/null +++ b/tests/test_Insights.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python + +""" +Testing KoalaBot Insights Cog + +Commented using reStructuredText (reST) +""" +# Futures + +# Built-in/Generic Imports +import random +from typing import * + +# Libs +import discord +import discord.ext.test as dpytest +import mock +import pytest +from discord.ext import commands +from discord.ext.test import factories as dpyfactory + +# Own modules +import KoalaBot +from cogs import Insights +from cogs.Insights import InsightsDBManager +from tests.utils import TestUtils as utils +from tests.utils import TestUtilsCog +from utils.KoalaDBManager import KoalaDBManager + +# Constants + +# Variables +insights_cog: Insights.Insights = None +utils_cog: TestUtilsCog.TestUtilsCog = None +DBManager = InsightsDBManager(KoalaBot.database_manager) +DBManager.create_tables() + + +def setup_function(): + """ setup any state specific to the execution of the given module.""" + global insights_cog + global utils_cog + bot: commands.Bot = commands.Bot(command_prefix=KoalaBot.COMMAND_PREFIX) + insights_cog = Insights.Insights(bot) + utils_cog = TestUtilsCog.TestUtilsCog(bot) + bot.add_cog(insights_cog) + bot.add_cog(utils_cog) + dpytest.configure(bot) + print("Tests starting") + +@pytest.mark.asyncio +@pytest.mark.parametrize("num_guilds, num_users", [(1,1),(1,2),(1,5),(2,1),(2,2),(5,4),(5,100)]) +async def test_insights(num_guilds, num_users): + test_config = dpytest.get_config() + client = test_config.client + for i in range(num_guilds - 1): + guild = dpytest.back.make_guild(f"Test Guild {i}") + test_config.guilds.append(guild) + + for i in range(len(test_config.guilds)): + for j in range(num_users - 1): + await dpytest.member_join(i, client.user) + await dpytest.member_join(i) + + await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") + dpytest.verify_message(f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {num_guilds * num_users}.") + +@pytest.mark.asyncio +async def test_servers_no_args(): + test_config = dpytest.get_config() + client = test_config.client + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") + dpytest.verify_message("Test Guild 0") + await dpytest.kick_callback(test_config.guilds[0], client.user) + expected = "" + for i in range(10): + guild = dpytest.back.make_guild(f"Test Guild {i}") + expected += f"Test Guild {i}, " + test_config.guilds.append(guild) + await dpytest.member_join(i,client.user) + await dpytest.message(KoalaBot.COMMAND_PREFIX+"servers",i) + await dpytest.verify_message(expected[:-2]) + + +@pytest.mark.asyncio +async def test_servers_fail_args(): + test_config = dpytest.get_config() + client = test_config.client + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") + dpytest.verify_message("Test Guild 0") + await dpytest.kick_callback(test_config.guilds[0], client.user) + arg = "fail_pls" + for i in range(10): + guild = dpytest.back.make_guild(f"Test Guild {i}") + test_config.guilds.append(guild) + await dpytest.member_join(i, client.user) + await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) + await dpytest.verify_message(f"No servers with {arg} in their name") + +@pytest.mark.asyncio +async def test_servers_with_args(): + test_config = dpytest.get_config() + client = test_config.client + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") + dpytest.verify_message("Test Guild 0") + await dpytest.kick_callback(test_config.guilds[0], client.user) + arg = "0" + for i in range(10): + guild = dpytest.back.make_guild(f"Test Guild {i}") + test_config.guilds.append(guild) + await dpytest.member_join(i, client.user) + await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) + await dpytest.verify_message("Test Guild 0") + From d0350d3b8fc984dcd5d54ecb59e2351a8e9126f1 Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 5 Apr 2021 07:06:16 +0100 Subject: [PATCH 08/18] Fix issues with test_Insights.py caused by random merge conflicts --- tests/test_Insights.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/tests/test_Insights.py b/tests/test_Insights.py index 7df9a95e..6c306360 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -8,32 +8,22 @@ # Futures # Built-in/Generic Imports -import random -from typing import * # Libs -import discord import discord.ext.test as dpytest -import mock import pytest from discord.ext import commands -from discord.ext.test import factories as dpyfactory # Own modules import KoalaBot from cogs import Insights -from cogs.Insights import InsightsDBManager -from tests.utils import TestUtils as utils from tests.utils import TestUtilsCog -from utils.KoalaDBManager import KoalaDBManager # Constants # Variables insights_cog: Insights.Insights = None utils_cog: TestUtilsCog.TestUtilsCog = None -DBManager = InsightsDBManager(KoalaBot.database_manager) -DBManager.create_tables() def setup_function(): @@ -48,8 +38,9 @@ def setup_function(): dpytest.configure(bot) print("Tests starting") + @pytest.mark.asyncio -@pytest.mark.parametrize("num_guilds, num_users", [(1,1),(1,2),(1,5),(2,1),(2,2),(5,4),(5,100)]) +@pytest.mark.parametrize("num_guilds, num_users", [(1, 1), (1, 2), (1, 5), (2, 1), (2, 2), (5, 4), (5, 100)]) async def test_insights(num_guilds, num_users): test_config = dpytest.get_config() client = test_config.client @@ -63,7 +54,9 @@ async def test_insights(num_guilds, num_users): await dpytest.member_join(i) await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") - dpytest.verify_message(f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {num_guilds * num_users}.") + dpytest.verify_message( + f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {num_guilds * num_users}.") + @pytest.mark.asyncio async def test_servers_no_args(): @@ -77,8 +70,8 @@ async def test_servers_no_args(): guild = dpytest.back.make_guild(f"Test Guild {i}") expected += f"Test Guild {i}, " test_config.guilds.append(guild) - await dpytest.member_join(i,client.user) - await dpytest.message(KoalaBot.COMMAND_PREFIX+"servers",i) + await dpytest.member_join(i, client.user) + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers", i) await dpytest.verify_message(expected[:-2]) @@ -97,6 +90,7 @@ async def test_servers_fail_args(): await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) await dpytest.verify_message(f"No servers with {arg} in their name") + @pytest.mark.asyncio async def test_servers_with_args(): test_config = dpytest.get_config() @@ -111,4 +105,3 @@ async def test_servers_with_args(): await dpytest.member_join(i, client.user) await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) await dpytest.verify_message("Test Guild 0") - From 68b29a902f74b8564bd204c16f3929826264b48e Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 5 Apr 2021 07:15:40 +0100 Subject: [PATCH 09/18] Account for fake bot user in test_insights() --- tests/test_Insights.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_Insights.py b/tests/test_Insights.py index 6c306360..911b90f6 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -40,7 +40,7 @@ def setup_function(): @pytest.mark.asyncio -@pytest.mark.parametrize("num_guilds, num_users", [(1, 1), (1, 2), (1, 5), (2, 1), (2, 2), (5, 4), (5, 100)]) +@pytest.mark.parametrize("num_guilds, num_users", [(1, 1), (1, 2), (1, 5), (2, 1), (2, 2), (5, 4), (5,10)]) async def test_insights(num_guilds, num_users): test_config = dpytest.get_config() client = test_config.client @@ -55,7 +55,7 @@ async def test_insights(num_guilds, num_users): await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") dpytest.verify_message( - f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {num_guilds * num_users}.") + f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {2 * num_guilds * num_users}.") @pytest.mark.asyncio From 502cf7c3b128e5dce6d58cd4a88ff025c2b746dc Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 5 Apr 2021 07:23:55 +0100 Subject: [PATCH 10/18] Remove bot user addition from test_insights() --- tests/test_Insights.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_Insights.py b/tests/test_Insights.py index 911b90f6..c712ea80 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -10,6 +10,8 @@ # Built-in/Generic Imports # Libs +import asyncio + import discord.ext.test as dpytest import pytest from discord.ext import commands @@ -50,12 +52,10 @@ async def test_insights(num_guilds, num_users): for i in range(len(test_config.guilds)): for j in range(num_users - 1): - await dpytest.member_join(i, client.user) await dpytest.member_join(i) - await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") dpytest.verify_message( - f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {2 * num_guilds * num_users}.") + f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {num_guilds * num_users}.") @pytest.mark.asyncio From 0456e45cc6fec92a592bce92ccbe12faf9809301 Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 5 Apr 2021 08:02:39 +0100 Subject: [PATCH 11/18] Attempt moving configure to inside test method --- tests/test_Insights.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/test_Insights.py b/tests/test_Insights.py index c712ea80..956363ec 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -26,25 +26,24 @@ # Variables insights_cog: Insights.Insights = None utils_cog: TestUtilsCog.TestUtilsCog = None - +bot: commands.Bot = None def setup_function(): """ setup any state specific to the execution of the given module.""" global insights_cog global utils_cog - bot: commands.Bot = commands.Bot(command_prefix=KoalaBot.COMMAND_PREFIX) + bot = commands.Bot(command_prefix=KoalaBot.COMMAND_PREFIX) insights_cog = Insights.Insights(bot) utils_cog = TestUtilsCog.TestUtilsCog(bot) bot.add_cog(insights_cog) bot.add_cog(utils_cog) - dpytest.configure(bot) print("Tests starting") @pytest.mark.asyncio @pytest.mark.parametrize("num_guilds, num_users", [(1, 1), (1, 2), (1, 5), (2, 1), (2, 2), (5, 4), (5,10)]) async def test_insights(num_guilds, num_users): - test_config = dpytest.get_config() + test_config = dpytest.configure(bot,num_guilds,1,num_users) client = test_config.client for i in range(num_guilds - 1): guild = dpytest.back.make_guild(f"Test Guild {i}") @@ -60,7 +59,7 @@ async def test_insights(num_guilds, num_users): @pytest.mark.asyncio async def test_servers_no_args(): - test_config = dpytest.get_config() + test_config = dpytest.configure(bot) client = test_config.client await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") dpytest.verify_message("Test Guild 0") @@ -77,7 +76,7 @@ async def test_servers_no_args(): @pytest.mark.asyncio async def test_servers_fail_args(): - test_config = dpytest.get_config() + test_config = dpytest.configure(bot) client = test_config.client await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") dpytest.verify_message("Test Guild 0") @@ -93,7 +92,7 @@ async def test_servers_fail_args(): @pytest.mark.asyncio async def test_servers_with_args(): - test_config = dpytest.get_config() + test_config = dpytest.configure(bot) client = test_config.client await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") dpytest.verify_message("Test Guild 0") From c144bfacf389a185ebc82b1c397bed1ca61c97e4 Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 5 Apr 2021 08:09:08 +0100 Subject: [PATCH 12/18] Attempt moving configure to inside test method --- tests/test_Insights.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/test_Insights.py b/tests/test_Insights.py index 956363ec..002d972f 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -10,8 +10,6 @@ # Built-in/Generic Imports # Libs -import asyncio - import discord.ext.test as dpytest import pytest from discord.ext import commands @@ -26,24 +24,29 @@ # Variables insights_cog: Insights.Insights = None utils_cog: TestUtilsCog.TestUtilsCog = None -bot: commands.Bot = None + def setup_function(): """ setup any state specific to the execution of the given module.""" global insights_cog global utils_cog - bot = commands.Bot(command_prefix=KoalaBot.COMMAND_PREFIX) + bot: commands.Bot = commands.Bot(command_prefix=KoalaBot.COMMAND_PREFIX) insights_cog = Insights.Insights(bot) utils_cog = TestUtilsCog.TestUtilsCog(bot) bot.add_cog(insights_cog) bot.add_cog(utils_cog) + dpytest.configure(bot) print("Tests starting") @pytest.mark.asyncio @pytest.mark.parametrize("num_guilds, num_users", [(1, 1), (1, 2), (1, 5), (2, 1), (2, 2), (5, 4), (5,10)]) async def test_insights(num_guilds, num_users): - test_config = dpytest.configure(bot,num_guilds,1,num_users) + bot: commands.Bot = commands.Bot(command_prefix=KoalaBot.COMMAND_PREFIX) + bot.add_cog(insights_cog) + bot.add_cog(utils_cog) + dpytest.configure(bot,num_guilds,1,num_users) + test_config = dpytest.get_config() client = test_config.client for i in range(num_guilds - 1): guild = dpytest.back.make_guild(f"Test Guild {i}") @@ -51,15 +54,17 @@ async def test_insights(num_guilds, num_users): for i in range(len(test_config.guilds)): for j in range(num_users - 1): + await dpytest.member_join(i, client.user) await dpytest.member_join(i) + await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") dpytest.verify_message( - f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {num_guilds * num_users}.") + f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {2 * num_guilds * num_users}.") @pytest.mark.asyncio async def test_servers_no_args(): - test_config = dpytest.configure(bot) + test_config = dpytest.get_config() client = test_config.client await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") dpytest.verify_message("Test Guild 0") @@ -76,7 +81,7 @@ async def test_servers_no_args(): @pytest.mark.asyncio async def test_servers_fail_args(): - test_config = dpytest.configure(bot) + test_config = dpytest.get_config() client = test_config.client await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") dpytest.verify_message("Test Guild 0") @@ -92,7 +97,7 @@ async def test_servers_fail_args(): @pytest.mark.asyncio async def test_servers_with_args(): - test_config = dpytest.configure(bot) + test_config = dpytest.get_config() client = test_config.client await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") dpytest.verify_message("Test Guild 0") From 82ac0c5ddc1a02c3135f55a3e5c3137303b2801c Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 5 Apr 2021 08:23:14 +0100 Subject: [PATCH 13/18] Refactor test_insights() --- tests/test_Insights.py | 49 +++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/tests/test_Insights.py b/tests/test_Insights.py index 002d972f..1417df0b 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -8,22 +8,32 @@ # Futures # Built-in/Generic Imports +import random +from typing import * # Libs +import discord import discord.ext.test as dpytest +import mock import pytest from discord.ext import commands +from discord.ext.test import factories as dpyfactory # Own modules import KoalaBot from cogs import Insights +from cogs.Insights import InsightsDBManager +from tests.utils import TestUtils as utils from tests.utils import TestUtilsCog +from utils.KoalaDBManager import KoalaDBManager # Constants # Variables insights_cog: Insights.Insights = None utils_cog: TestUtilsCog.TestUtilsCog = None +DBManager = InsightsDBManager(KoalaBot.database_manager) +DBManager.create_tables() def setup_function(): @@ -38,29 +48,24 @@ def setup_function(): dpytest.configure(bot) print("Tests starting") - @pytest.mark.asyncio -@pytest.mark.parametrize("num_guilds, num_users", [(1, 1), (1, 2), (1, 5), (2, 1), (2, 2), (5, 4), (5,10)]) -async def test_insights(num_guilds, num_users): - bot: commands.Bot = commands.Bot(command_prefix=KoalaBot.COMMAND_PREFIX) - bot.add_cog(insights_cog) - bot.add_cog(utils_cog) - dpytest.configure(bot,num_guilds,1,num_users) +async def test_insights(): test_config = dpytest.get_config() - client = test_config.client - for i in range(num_guilds - 1): + expected_users = 1 + for i in range(10): guild = dpytest.back.make_guild(f"Test Guild {i}") test_config.guilds.append(guild) - - for i in range(len(test_config.guilds)): - for j in range(num_users - 1): - await dpytest.member_join(i, client.user) - await dpytest.member_join(i) - - await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") - dpytest.verify_message( - f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {2 * num_guilds * num_users}.") - + expected_users += 1 + await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") + dpytest.verify_message( + f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {expected_users}.") + import random + for i in range(100): + await dpytest.member_join(random.randint(0,len(test_config.guilds) - 1)) + expected_users += 1 + await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") + dpytest.verify_message( + f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {expected_users}.") @pytest.mark.asyncio async def test_servers_no_args(): @@ -74,8 +79,8 @@ async def test_servers_no_args(): guild = dpytest.back.make_guild(f"Test Guild {i}") expected += f"Test Guild {i}, " test_config.guilds.append(guild) - await dpytest.member_join(i, client.user) - await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers", i) + await dpytest.member_join(i,client.user) + await dpytest.message(KoalaBot.COMMAND_PREFIX+"servers",i) await dpytest.verify_message(expected[:-2]) @@ -94,7 +99,6 @@ async def test_servers_fail_args(): await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) await dpytest.verify_message(f"No servers with {arg} in their name") - @pytest.mark.asyncio async def test_servers_with_args(): test_config = dpytest.get_config() @@ -109,3 +113,4 @@ async def test_servers_with_args(): await dpytest.member_join(i, client.user) await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) await dpytest.verify_message("Test Guild 0") + From 64b269af00b0871763023d8ded13f0c6c61c9185 Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 5 Apr 2021 08:29:47 +0100 Subject: [PATCH 14/18] Remove DBManager stuff from another conflict --- tests/test_Insights.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/test_Insights.py b/tests/test_Insights.py index 1417df0b..c7a8c510 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -12,29 +12,20 @@ from typing import * # Libs -import discord import discord.ext.test as dpytest -import mock import pytest from discord.ext import commands -from discord.ext.test import factories as dpyfactory # Own modules import KoalaBot from cogs import Insights -from cogs.Insights import InsightsDBManager -from tests.utils import TestUtils as utils from tests.utils import TestUtilsCog -from utils.KoalaDBManager import KoalaDBManager # Constants # Variables insights_cog: Insights.Insights = None utils_cog: TestUtilsCog.TestUtilsCog = None -DBManager = InsightsDBManager(KoalaBot.database_manager) -DBManager.create_tables() - def setup_function(): """ setup any state specific to the execution of the given module.""" From 21c2d9c12abcd9972f3e4211903331c1627449e4 Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 28 Jun 2021 12:43:54 +0100 Subject: [PATCH 15/18] Implement k!insights test --- tests/test_Insights.py | 85 ++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/tests/test_Insights.py b/tests/test_Insights.py index c7a8c510..610c0d9b 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -8,55 +8,74 @@ # Futures # Built-in/Generic Imports + import random -from typing import * # Libs import discord.ext.test as dpytest import pytest -from discord.ext import commands # Own modules import KoalaBot -from cogs import Insights -from tests.utils import TestUtilsCog +from cogs import Insights, BaseCog +from tests.utils_testing import LastCtxCog # Constants # Variables -insights_cog: Insights.Insights = None -utils_cog: TestUtilsCog.TestUtilsCog = None - -def setup_function(): - """ setup any state specific to the execution of the given module.""" - global insights_cog - global utils_cog - bot: commands.Bot = commands.Bot(command_prefix=KoalaBot.COMMAND_PREFIX) + +@pytest.fixture(scope="function", autouse=True) +def utils_cog(bot): + utils_cog = LastCtxCog.LastCtxCog(bot) + bot.add_cog(utils_cog) + dpytest.configure(bot) + print("Tests starting") + return utils_cog + + +@pytest.fixture(scope="function", autouse=True) +def base_cog(bot): + base_cog = BaseCog.BaseCog(bot) + bot.add_cog(base_cog) + dpytest.configure(bot) + print("Tests starting") + return base_cog + + +@pytest.fixture(scope="function", autouse=True) +async def insights_cog(bot): insights_cog = Insights.Insights(bot) - utils_cog = TestUtilsCog.TestUtilsCog(bot) bot.add_cog(insights_cog) - bot.add_cog(utils_cog) dpytest.configure(bot) print("Tests starting") + return insights_cog + @pytest.mark.asyncio -async def test_insights(): +@pytest.mark.parametrize("num_guilds, num_members", + [(1, 1), (1, 2), (1, 10), (2, 2), (2, 5), (2, 20), (5, 100), (100, 10000), (20, 20000)]) +async def test_insights(num_guilds, num_members): test_config = dpytest.get_config() - expected_users = 1 - for i in range(10): - guild = dpytest.back.make_guild(f"Test Guild {i}") - test_config.guilds.append(guild) - expected_users += 1 - await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") - dpytest.verify_message( - f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {expected_users}.") - import random - for i in range(100): - await dpytest.member_join(random.randint(0,len(test_config.guilds) - 1)) - expected_users += 1 - await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") - dpytest.verify_message( - f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {expected_users}.") + + for i in range(num_guilds): + g = dpytest.back.make_guild(f"TestGuild {i}") + if test_config.guilds[i] is None: + test_config.guilds[i] = g + else: + test_config.guilds.append(g) + + for i in range(1, num_members): + m = dpytest.back.make_user(f"TestUser {i}", random.randint(1, 9999)) + dpytest.back.make_member(m, random.choice(test_config.guilds)) + + expected_users = 0 + for g in test_config.guilds: + expected_users += g.member_count + + await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") + assert dpytest.verify().message().content( + f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {expected_users}.") + @pytest.mark.asyncio async def test_servers_no_args(): @@ -70,8 +89,8 @@ async def test_servers_no_args(): guild = dpytest.back.make_guild(f"Test Guild {i}") expected += f"Test Guild {i}, " test_config.guilds.append(guild) - await dpytest.member_join(i,client.user) - await dpytest.message(KoalaBot.COMMAND_PREFIX+"servers",i) + await dpytest.member_join(i, client.user) + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers", i) await dpytest.verify_message(expected[:-2]) @@ -90,6 +109,7 @@ async def test_servers_fail_args(): await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) await dpytest.verify_message(f"No servers with {arg} in their name") + @pytest.mark.asyncio async def test_servers_with_args(): test_config = dpytest.get_config() @@ -104,4 +124,3 @@ async def test_servers_with_args(): await dpytest.member_join(i, client.user) await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) await dpytest.verify_message("Test Guild 0") - From 1d33ac7f7dd56688ccb444e955679e07a0f5b1f1 Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 26 Jul 2021 12:59:06 +0100 Subject: [PATCH 16/18] Implement k!insights test --- cogs/Insights.py | 23 ++++++++++++++--------- tests/test_Insights.py | 42 +++++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/cogs/Insights.py b/cogs/Insights.py index 26e19688..6f9d4e68 100644 --- a/cogs/Insights.py +++ b/cogs/Insights.py @@ -5,6 +5,7 @@ Created by: Samuel Tiongson """ from discord.ext import commands + import KoalaBot @@ -33,15 +34,7 @@ async def insights(self, ctx): await ctx.send(f"KoalaBot is in {number_of_servers} servers with a member total of {number_of_members}.") - @commands.check(KoalaBot.is_owner) - @commands.command(name="servers", aliases=[]) - async def servers(self, ctx, *, arg=None): - """ - Returns the names of the servers KoalaBot is in - - :param ctx: The discord context - :param arg: Searches for guilds with argument provided - """ + async def get_bot_guilds(self, arg=None): # Retrieves AsyncIterator guild_list = self.bot.fetch_guilds() guild_list_names = [] @@ -55,6 +48,18 @@ async def servers(self, ctx, *, arg=None): guild_list_names.append(guild.name) else: guild_list_names.append(guild.name) + return guild_list_names + + @commands.check(KoalaBot.is_owner) + @commands.command(name="servers", aliases=[]) + async def servers(self, ctx, *, arg=None): + """ + Returns the names of the servers KoalaBot is in + + :param ctx: The discord context + :param arg: Searches for guilds with argument provided + """ + guild_list_names = await self.get_bot_guilds(arg) # If there are no guilds and no arguments if len(guild_list_names) == 0 and arg is None: diff --git a/tests/test_Insights.py b/tests/test_Insights.py index 610c0d9b..a922745c 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -13,6 +13,7 @@ # Libs import discord.ext.test as dpytest +import mock import pytest # Own modules @@ -20,6 +21,7 @@ from cogs import Insights, BaseCog from tests.utils_testing import LastCtxCog + # Constants # Variables @@ -78,20 +80,34 @@ async def test_insights(num_guilds, num_members): @pytest.mark.asyncio -async def test_servers_no_args(): +@pytest.mark.parametrize("num_guilds", [1, 2, 5, 10, 50, 100, 1000, 20000]) +async def test_servers_no_args(num_guilds): test_config = dpytest.get_config() - client = test_config.client - await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") - dpytest.verify_message("Test Guild 0") - await dpytest.kick_callback(test_config.guilds[0], client.user) - expected = "" - for i in range(10): - guild = dpytest.back.make_guild(f"Test Guild {i}") - expected += f"Test Guild {i}, " - test_config.guilds.append(guild) - await dpytest.member_join(i, client.user) - await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers", i) - await dpytest.verify_message(expected[:-2]) + guild_list_names = [] + message_content_list = [] + for i in range(num_guilds): + g = dpytest.back.make_guild(f"TestGuild {i}") + if test_config.guilds[i] is None: + test_config.guilds[i] = g + else: + test_config.guilds.append(g) + guild_list_names.append(g.name) + string_to_send = '' + while len(guild_list_names) != 0: + length = len(guild_list_names[0]) + if len(string_to_send) + length + 2 > 2000: + message_content_list.append(string_to_send) + string_to_send = '' + else: + guild = guild_list_names.pop(0) + string_to_send += guild + ", " + message_content_list.append(string_to_send[:-2]) + with mock.patch("cogs.Insights.Insights.get_bot_guilds", mock.AsyncMock(return_value=guild_list_names)): + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") + for msg in message_content_list: + print(dpytest.get_message(True).content) + print(msg) + assert dpytest.verify().message().content(msg) @pytest.mark.asyncio From 760d7a72f7d59c138f3bacd71f0ceca756a28a56 Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 26 Jul 2021 13:01:20 +0100 Subject: [PATCH 17/18] Remove broken k!servers tests --- tests/test_Insights.py | 63 ------------------------------------------ 1 file changed, 63 deletions(-) diff --git a/tests/test_Insights.py b/tests/test_Insights.py index a922745c..1b4ecf5a 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -77,66 +77,3 @@ async def test_insights(num_guilds, num_members): await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") assert dpytest.verify().message().content( f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {expected_users}.") - - -@pytest.mark.asyncio -@pytest.mark.parametrize("num_guilds", [1, 2, 5, 10, 50, 100, 1000, 20000]) -async def test_servers_no_args(num_guilds): - test_config = dpytest.get_config() - guild_list_names = [] - message_content_list = [] - for i in range(num_guilds): - g = dpytest.back.make_guild(f"TestGuild {i}") - if test_config.guilds[i] is None: - test_config.guilds[i] = g - else: - test_config.guilds.append(g) - guild_list_names.append(g.name) - string_to_send = '' - while len(guild_list_names) != 0: - length = len(guild_list_names[0]) - if len(string_to_send) + length + 2 > 2000: - message_content_list.append(string_to_send) - string_to_send = '' - else: - guild = guild_list_names.pop(0) - string_to_send += guild + ", " - message_content_list.append(string_to_send[:-2]) - with mock.patch("cogs.Insights.Insights.get_bot_guilds", mock.AsyncMock(return_value=guild_list_names)): - await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") - for msg in message_content_list: - print(dpytest.get_message(True).content) - print(msg) - assert dpytest.verify().message().content(msg) - - -@pytest.mark.asyncio -async def test_servers_fail_args(): - test_config = dpytest.get_config() - client = test_config.client - await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") - dpytest.verify_message("Test Guild 0") - await dpytest.kick_callback(test_config.guilds[0], client.user) - arg = "fail_pls" - for i in range(10): - guild = dpytest.back.make_guild(f"Test Guild {i}") - test_config.guilds.append(guild) - await dpytest.member_join(i, client.user) - await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) - await dpytest.verify_message(f"No servers with {arg} in their name") - - -@pytest.mark.asyncio -async def test_servers_with_args(): - test_config = dpytest.get_config() - client = test_config.client - await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") - dpytest.verify_message("Test Guild 0") - await dpytest.kick_callback(test_config.guilds[0], client.user) - arg = "0" - for i in range(10): - guild = dpytest.back.make_guild(f"Test Guild {i}") - test_config.guilds.append(guild) - await dpytest.member_join(i, client.user) - await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}", i) - await dpytest.verify_message("Test Guild 0") From df35c50e0e3e181d3d3378bd070c76b6822dba1b Mon Sep 17 00:00:00 2001 From: Anan Venkatesh Date: Mon, 11 Oct 2021 16:06:35 +0100 Subject: [PATCH 18/18] Fix previous broken commits with patched dpytest --- conftest.py | 20 ++++++- tests/test_Insights.py | 126 +++++++++++++++++++++++++++++++++-------- 2 files changed, 119 insertions(+), 27 deletions(-) diff --git a/conftest.py b/conftest.py index a53b8ce5..15d2c086 100644 --- a/conftest.py +++ b/conftest.py @@ -5,16 +5,16 @@ # Built-in/Generic Imports -# Libs -import pytest import discord import discord.ext.commands as commands import discord.ext.test as dpytest -import asyncio +# Libs +import pytest # Own modules import KoalaBot + # Constants @pytest.fixture @@ -29,6 +29,20 @@ async def bot(event_loop): return b +@pytest.fixture(autouse=False) +async def bot_no_configure(event_loop): + """ + The bot conftest method but with no dpytest.configure() method call + """ + intents = discord.Intents.default() + intents.members = True + intents.guilds = True + intents.messages = True + b = commands.Bot(KoalaBot.COMMAND_PREFIX, loop=event_loop, intents=intents) + await dpytest.empty_queue() + return b + + @pytest.fixture(autouse=True) def setup_is_dpytest(): KoalaBot.is_dpytest = True diff --git a/tests/test_Insights.py b/tests/test_Insights.py index 1b4ecf5a..96c9a846 100644 --- a/tests/test_Insights.py +++ b/tests/test_Insights.py @@ -8,55 +8,47 @@ # Futures # Built-in/Generic Imports - import random # Libs import discord.ext.test as dpytest -import mock import pytest # Own modules import KoalaBot -from cogs import Insights, BaseCog -from tests.utils_testing import LastCtxCog +from cogs import Insights +from utils_testing import LastCtxCog # Constants # Variables -@pytest.fixture(scope="function", autouse=True) -def utils_cog(bot): +@pytest.fixture(scope="function", autouse=False) +def setup(bot): utils_cog = LastCtxCog.LastCtxCog(bot) + insights_cog = Insights.Insights(bot) bot.add_cog(utils_cog) - dpytest.configure(bot) - print("Tests starting") - return utils_cog - - -@pytest.fixture(scope="function", autouse=True) -def base_cog(bot): - base_cog = BaseCog.BaseCog(bot) - bot.add_cog(base_cog) - dpytest.configure(bot) - print("Tests starting") - return base_cog + bot.add_cog(insights_cog) + print("Tests starting (setup)") + return bot -@pytest.fixture(scope="function", autouse=True) -async def insights_cog(bot): +@pytest.fixture(scope="function", autouse=False) +def setup_no_conf(bot_no_configure): + bot = bot_no_configure + utils_cog = LastCtxCog.LastCtxCog(bot) insights_cog = Insights.Insights(bot) + bot.add_cog(utils_cog) bot.add_cog(insights_cog) - dpytest.configure(bot) - print("Tests starting") - return insights_cog + print("Tests starting (setup)") + return bot @pytest.mark.asyncio @pytest.mark.parametrize("num_guilds, num_members", [(1, 1), (1, 2), (1, 10), (2, 2), (2, 5), (2, 20), (5, 100), (100, 10000), (20, 20000)]) -async def test_insights(num_guilds, num_members): +async def test_insights(num_guilds, num_members, setup): test_config = dpytest.get_config() for i in range(num_guilds): @@ -77,3 +69,89 @@ async def test_insights(num_guilds, num_members): await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights") assert dpytest.verify().message().content( f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {expected_users}.") + + +@pytest.mark.asyncio +@pytest.mark.parametrize("num_guilds", + [1, 2, 5, 10, 100, 200, 500, 1000, 2000, 5000, 10000]) +async def test_servers_no_args(num_guilds, setup_no_conf): + dpytest.configure(setup_no_conf, num_guilds, 1, 1) + test_config = dpytest.get_config() + + for i in range(num_guilds): + g = dpytest.back.make_guild(f"Test Guild {i}") + if test_config.guilds[i] is None or g.name in [gg.name for gg in test_config.guilds]: + test_config.guilds[i] = g + else: + test_config.guilds.append(g) + + strings_expected = [] + msg = '' + len_msg = 0 + guild_list_names = [g.name for g in test_config.guilds] + while len(guild_list_names) != 0: + length = len(guild_list_names[0]) + if len(msg) + length + 2 > 2000 or len_msg == 100: + strings_expected.append(msg) + msg = '' + len_msg = 0 + else: + guild = guild_list_names.pop(0) + msg += guild + ", " + len_msg += 1 + strings_expected.append(msg[:-2]) + await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers") + while not dpytest.sent_queue.empty(): + x = (await dpytest.sent_queue.get()).content + assert x in strings_expected, print(f"content = {x}") + assert dpytest.verify().message().nothing() + + +@pytest.mark.asyncio +@pytest.mark.parametrize("num_guilds", + [1, 2, 5, 10, 100, 1000, 10000]) +async def test_servers_args(num_guilds, setup_no_conf): + dpytest.configure(setup_no_conf, num_guilds, 1, 1) + test_config = dpytest.get_config() + + for i in range(num_guilds): + g = dpytest.back.make_guild(f"Test Guild {i}") + if test_config.guilds[i] is None or g.name in [gg.name for gg in test_config.guilds]: + test_config.guilds[i] = g + else: + test_config.guilds.append(g) + + arg = '1' + print(f"arg={arg}") + + strings_expected = [] + msg = '' + len_msg = 0 + guild_list_names = [] + async for guild in test_config.client.fetch_guilds(): + if arg is not None: + if arg.upper() in guild.name.upper().split(" "): + guild_list_names.append(guild.name) + else: + guild_list_names.append(guild.name) + + if len(guild_list_names) == 0: + strings_expected = [f"No servers with {arg} in their name!"] + else: + while len(guild_list_names) != 0: + length = len(guild_list_names[0]) + if len(msg) + length + 2 > 2000 or len_msg == 100: + strings_expected.append(msg) + msg = '' + len_msg = 0 + else: + guild = guild_list_names.pop(0) + msg += guild + ", " + len_msg += 1 + strings_expected.append(msg[:-2]) + print(strings_expected) + await dpytest.message(KoalaBot.COMMAND_PREFIX + f"servers {arg}") + while not dpytest.sent_queue.empty(): + x = (await dpytest.sent_queue.get()).content + assert x in strings_expected, print(f"content = {x}") + assert dpytest.verify().message().nothing()