async def test_taches(self): """Unit tests for !taches command.""" # async def taches(self, ctx) taches_cmd = self.cog.taches # no taches ctx = mock_discord.get_ctx(taches_cmd) await ctx.invoke() ctx.assert_sent("Aucune tâche") # some taches bdd.Tache(timestamp=datetime.datetime.now(), commande="bloup").add() bdd.BaseAction(slug="baaz").add() bdd.Joueur(discord_id=123, chan_id_=2, nom="Joueur1").add() bdd.Action(id=13, _base_slug="baaz", _joueur_id=123).add() bdd.Tache(timestamp=datetime.datetime(2021, 5, 15, 18, 0, 0), commande="bliip", _action_id=13).add() ctx = mock_discord.get_ctx(taches_cmd) await ctx.invoke() ctx.assert_sent("Tâches en attente") self.assertIn("bloup", ctx.send.call_args.args[0]) self.assertIn("bliip", ctx.send.call_args.args[0]) self.assertIn("baaz", ctx.send.call_args.args[0]) self.assertIn("Joueur1", ctx.send.call_args.args[0]) # a lot of taches bdd.Tache.add(*( bdd.Tache(timestamp=datetime.datetime.now(), commande=f"bloup{n}") for n in range(1000))) ctx = mock_discord.get_ctx(taches_cmd) await ctx.invoke() sent = "\n".join(call.args[0] for call in ctx.send.call_args_list) for n in range(1000): self.assertIn(f"bloup{n}", sent)
async def test_do(self): """Unit tests for !do command.""" # async def do(self, ctx, *, code) do = self.cog.do samples = { "1": 1, "1 + 2": 3, "37*(5-2)": 37 * (5 - 2), "config.debut_saison": config.debut_saison, "blocs.tools.montre()": blocs.tools.montre(), "tools.montre()": blocs.tools.montre(), "features.sync.transtype": features.sync.transtype, "bdd.Joueur": bdd.Joueur, "Joueur": bdd.Joueur, } # Vérification bases + accès à tous modules for sample, result in samples.items(): ctx = mock_discord.get_ctx(do, code=sample) await ctx.invoke() ctx.assert_sent(str(result)) # Vérification coroutines ctx = mock_discord.get_ctx(do, code="ctx.send('oh' + 'ah')") ctx.send.return_value = "bookz" await ctx.invoke() ctx.assert_sent("ohah", "bookz") # Vérification erreurs ctx = mock_discord.get_ctx(do, code="blbllb") with self.assertRaises(NameError): await ctx.invoke() ctx.assert_sent()
async def test_purge(self): """Unit tests for !purge command.""" # async def purge(self, ctx, N=None) purge = self.cog.purge # purge all, abort ctx = mock_discord.get_ctx(purge) with mock_discord.interact(("yes_no", False)): await ctx.invoke() ctx.assert_sent("Supprimer tous") ctx.channel.purge.assert_not_called() # purge all, proceed ctx = mock_discord.get_ctx(purge) with mock_discord.interact(("yes_no", True)): await ctx.invoke() ctx.assert_sent("Supprimer tous") ctx.channel.purge.assert_called_once_with(limit=None) # purge 15, abort ctx = mock_discord.get_ctx(purge, "15") with mock_discord.interact(("yes_no", False)): await ctx.invoke() ctx.assert_sent("Supprimer les 15") ctx.channel.purge.assert_not_called() # purge 15, proceed ctx = mock_discord.get_ctx(purge, "15") with mock_discord.interact(("yes_no", True)): await ctx.invoke() ctx.assert_sent("Supprimer les 15") ctx.channel.purge.assert_called_once_with(limit=17)
async def test_trigger_at_mj(self): """Unit tests for IA.trigger_at_mj function.""" # async def trigger_at_mj(message) trigger_at_mj = IA.trigger_at_mj # no mention ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, "oo", role_mentions=[]) rep = await trigger_at_mj(message) self.assertIs(rep, False) ctx.assert_sent() # other role mention ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, "oo", role_mentions=[config.Role.joueur_mort]) rep = await trigger_at_mj(message) self.assertIs(rep, False) ctx.assert_sent() # @MJ mention ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, "oo", role_mentions=[config.Role.mj]) rep = await trigger_at_mj(message) self.assertIs(rep, True) ctx.assert_sent("ils sont en route")
async def test_react(self, pia_patch): """Unit tests for !react command.""" # async def react(self, ctx, *, trigger) react = self.cog.react class SaveContent: content = None @classmethod def save(cls, msg, debug=None): cls.content = msg.content pia_patch.side_effect = SaveContent.save # non-MJ ctx = mock_discord.get_ctx(react, trigger="booz") ctx.author.top_role = config.Role.joueur_en_vie ctx.message.content = "original" await ctx.invoke() pia_patch.assert_called_once_with(ctx.message, debug=False) self.assertEqual(SaveContent.content, "booz") self.assertEqual(ctx.message.content, "original") ctx.assert_sent() pia_patch.reset_mock() # MJ ctx = mock_discord.get_ctx(react, trigger="booz") ctx.author.top_role = config.Role.mj ctx.message.content = "original" await ctx.invoke() pia_patch.assert_called_once_with(ctx.message, debug=True) self.assertEqual(SaveContent.content, "booz") self.assertEqual(ctx.message.content, "original") ctx.assert_sent()
async def test_co(self, im_patch, mb_patch): """Unit tests for !co command.""" # async def co(self, ctx, cible=None) co = self.cog.co # no cible ctx = mock_discord.get_ctx(co) await ctx.invoke() im_patch.assert_called_once_with(ctx.author) mb_patch.assert_not_called() im_patch.reset_mock() # cible = non-existing player mb_patch.side_effect = ValueError ctx = mock_discord.get_ctx(co, cible="booo") await ctx.invoke() ctx.assert_sent("introuvable") im_patch.assert_not_called() mb_patch.reset_mock(side_effect=True) # cible = existing player ctx = mock_discord.get_ctx(co, cible="noice") await ctx.invoke() mb_patch.assert_called_once_with("noice") im_patch.assert_called_once_with(mb_patch.return_value)
async def test_roll(self): """Unit tests for !roll command.""" # async def roll(self, ctx, *, XdY) roll = self.cog.roll samples = { "": False, "grzz": False, "1d6": True, "17d6": True, "17d6+3": True, "17d6-3": True, "17d6 + 3": True, "17d6 - 3": True, "17d6 + 3d15 + 2": True, "17d6-3d15-2": True, "17d0-3d15-2": False, "0d6-3d15-2": False, "17d6-3d15-0": True, } for sample, result in samples.items(): ctx = mock_discord.get_ctx(roll, XdY=sample) if result: await ctx.invoke() ctx.assert_sent("=") else: with self.assertRaises(discord.ext.commands.UserInputError): await ctx.invoke()
async def test_trigger_mot_unique(self): """Unit tests for IA.trigger_mot_unique function.""" # async def trigger_mot_unique(message) trigger_mot_unique = IA.trigger_mot_unique samples = { "": False, "bla bla": False, "boozabaka": "Boozabaka ?", "booza-baka": "Booza-baka ?", "@!!!": "@!!! ?", "http://oui": False, } # all samples for sample, result in samples.items(): ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, sample) rep = await trigger_mot_unique(message) if result is False: self.assertIs(rep, False) ctx.assert_sent() else: self.assertIs(rep, True) ctx.assert_sent(result)
async def test_process_IA(self, *patches): """Unit tests for IA.process_IA function.""" # async def process_IA(message, debug=False) process_IA = IA.process_IA # patches = reverse order of triggers # test if none triggered except default, then mot_unique, then... for i in range(len(patches)): # made reaction = patch[i] for ii, patch in enumerate(patches): if ii <= i: # i and lower priority: return True patch.return_value = True else: # higher priority: return False patch.return_value = False ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, "") rep = await process_IA(message) for ii, patch in enumerate(patches): if ii >= i: # i and higher priority: called patch.assert_called_once() self.assertEqual(patch.call_args.args[0], message) else: # lower priority: not called patch.assert_not_called() patch.reset_mock()
async def test_trigger_a_ou_b(self): """Unit tests for IA.trigger_a_ou_b function.""" # async def trigger_a_ou_b(message) trigger_a_ou_b = IA.trigger_a_ou_b samples = { "": False, "bla bla": False, "boozabaka": False, "booza ou baka": "Baka.", "a v à @ ou b. !??,!;;!": "B.", "a v à @ ou b. !??,!;;!a": "B. !??,!;;!a.", } # all samples for sample, result in samples.items(): ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, sample) rep = await trigger_a_ou_b(message) if result is False: self.assertIs(rep, False) ctx.assert_sent() else: self.assertIs(rep, True) ctx.assert_sent(result)
async def test_trigger_di(self): """Unit tests for IA.trigger_di function.""" # async def trigger_di(message) trigger_di = IA.trigger_di diprefs = ["di", "dy", "dis ", "dit ", "dis-", "dit-"] criprefs = ["cri", "cry", "kri", "kry"] samples = { "": False, "boozabaka": False, **{f"{pref}kaboom": "kaboom" for pref in diprefs}, **{f"{pref}kaé@@m": "KAÉ@@M" for pref in criprefs}, **{f"aa{pref}ka{pref}oh": f"ka{pref}oh" for pref in diprefs}, } # all samples for sample, result in samples.items(): ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, sample) rep = await trigger_di(message) if result is False: self.assertIs(rep, False) ctx.assert_sent() else: self.assertIs(rep, True) ctx.assert_sent(result)
async def test_stop(self): """Unit tests for !stop command.""" # async def stop(self, ctx) stop = self.cog.stop config.bot.in_command = [11, 12] # not in bot.in_command ctx = mock_discord.get_ctx(stop) ctx.channel.id = 13 await ctx.invoke() self.assertEqual(config.bot.in_command, [11, 12]) # in bot.in_command ctx = mock_discord.get_ctx(stop) ctx.channel.id = 12 await ctx.invoke() self.assertEqual(config.bot.in_command, [11])
async def test_panik(self): """Unit tests for !panik command.""" # async def panik(self, ctx) panik = self.cog.panik ctx = mock_discord.get_ctx(panik) with mock.patch("sys.exit") as exit_patch: await ctx.invoke() exit_patch.assert_called_once()
async def test_roles(self): """Unit tests for !roles command.""" # async def roles(self, ctx, *, filtre=None) roles = self.cog.roles # !roles with no roles ctx = mock_discord.get_ctx(roles) await ctx.invoke() ctx.assert_sent("Rôles trouvés :") mock_bdd.add_campsroles(300, 300) # several panes # !roles with a lot of roles emoji1 = mock.Mock(name="<:emoji1:>") emoji1.configure_mock(name="emoji1") ctx = mock_discord.get_ctx(roles) with mock.patch("lgrez.config.guild.emojis", [emoji1]): await ctx.invoke() sent = "\n".join(call.args[0] for call in ctx.send.call_args_list) self.assertIn("Role1", sent) self.assertIn("emoji1", sent) self.assertNotIn("emoji2", sent) bdd.Role(slug="chz", nom="ChienZ", description_courte="zoo", description_longue="zpp" * 12, camp=bdd.Camp.query.get("camp42")).add() # !roles <camp> ctx = mock_discord.get_ctx(roles, filtre="camp42") await ctx.invoke() ctx.send.assert_called_once() self.assertIn("Role42", ctx.send.call_args.args[0]) self.assertIn("ChienZ", ctx.send.call_args.args[0]) # !roles <role> ctx = mock_discord.get_ctx(roles, filtre="chz") await ctx.invoke() ctx.send.assert_called_once() self.assertIn("ChienZ", ctx.send.call_args.args[0]) self.assertIn("Camp42", ctx.send.call_args.args[0]) self.assertIn("zoo", ctx.send.call_args.args[0]) self.assertIn("zppzpp", ctx.send.call_args.args[0])
async def test_infos(self): """Unit tests for !infos command.""" # async def infos(self, ctx) infos = self.cog.infos mock_bdd.add_campsroles(10, 10) joueur = bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1", _role_slug="role7") joueur.add() # no actions ctx = mock_discord.get_ctx(infos, _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("Role7", ctx.send.call_args.args[0]) self.assertIn("role7", ctx.send.call_args.args[0]) self.assertIn("Aucune action disponible", ctx.send.call_args.args[0]) # one action bdd.BaseAction(slug="ouiZ", trigger_debut=bdd.ActionTrigger.perma, trigger_fin=bdd.ActionTrigger.perma).add() bdd.Action(joueur=joueur, _base_slug="ouiZ").add() ctx = mock_discord.get_ctx(infos, _caller_id=1) await ctx.invoke() ctx.assert_sent("ouiZ") # test every triggers names = [] for trigger in bdd.ActionTrigger: name = f"test{trigger.name}" bdd.BaseAction(slug=name, trigger_debut=trigger, trigger_fin=trigger).add() bdd.Action(joueur=joueur, _base_slug=name).add() names.append(name) ctx = mock_discord.get_ctx(infos, _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() for name in names: self.assertIn(name, ctx.send.call_args.args[0])
async def test_listIA(self): """Unit tests for !listIA command.""" # async def listIA(self, ctx, trigger=None, sensi=0.5) listIA = self.cog.listIA reacs = [ bdd.Reaction(reponse="s1quenZ"), bdd.Reaction(reponse="s€quenZ2"), bdd.Reaction(reponse="s€qu3enZ"), bdd.Reaction(reponse="s€4quenZ"), bdd.Reaction(reponse="s€que5nZ" * 1000), ] triggers = [ bdd.Trigger(trigger="trigz1", reaction=reacs[0]), bdd.Trigger(trigger="tr1gZ2", reaction=reacs[0]), bdd.Trigger(trigger="tr!Gz3", reaction=reacs[0]), bdd.Trigger(trigger="tr!Gz4", reaction=reacs[1]), bdd.Trigger(trigger="tr!Gz5", reaction=reacs[1]), bdd.Trigger(trigger="tr!Gz6", reaction=reacs[2]), bdd.Trigger(trigger="tr!Gz7", reaction=reacs[3]), bdd.Trigger(trigger="tr!G88", reaction=reacs[4]), ] bdd.Reaction.add(*reacs, *triggers) # list all ctx = mock_discord.get_ctx(listIA) await ctx.invoke() ctx.assert_sent([reac.reponse[:10] for reac in reacs] + [trig.trigger for trig in triggers] + ["[...]"]) ctx.assert_not_sent("s€que5nZ" * 1000) # filter ctx = mock_discord.get_ctx(listIA, "1gZ2") await ctx.invoke() ctx.assert_sent(["tr1gZ2", "s1quenZ"]) ctx.assert_not_sent(["s€quenZ2", "s€qu3enZ", "s€4quenZ", "s€que5nZ"]) # filter & sensi ctx = mock_discord.get_ctx(listIA, "1gZ2", sensi=0.3) await ctx.invoke() ctx.assert_sent(["s1quenZ", "s€quenZ2", "s€qu3enZ", "s€4quenZ"]) ctx.assert_not_sent("s€que5nZ")
async def test_reactfals(self, ft_patch): """Unit tests for !reactfals command.""" # async def reactfals(self, ctx, *, trigger) reactfals = self.cog.reactfals # no GIF ctx = mock_discord.get_ctx(reactfals, trigger="booz") ft_patch.return_value = None await ctx.invoke() ft_patch.assert_called_once_with("booz") ctx.assert_sent("Palaref") ft_patch.reset_mock() # MJ ctx = mock_discord.get_ctx(reactfals, trigger="booz") ft_patch.return_value = "giiiif" await ctx.invoke() ft_patch.assert_called_once_with("booz") ctx.assert_sent("giiiif") ft_patch.reset_mock()
async def test_secret(self, ocb_patch): """Unit tests for !secret command.""" # async def secret(self, ctx, *, quoi) secret = self.cog.secret # proceed ctx = mock_discord.get_ctx(secret, quoi="krkrkrk") await ctx.invoke() ctx.message.delete.assert_called_once() ocb_patch.assert_called_once_with(ctx) config.bot.process_commands.assert_called_once_with(ctx.message) self.assertEqual(ctx.message.content, "krkrkrk")
async def test_quiest(self): """Unit tests for !quiest command.""" # async def quiest(self, ctx, *, nomrole) quiest = self.cog.quiest mock_bdd.add_campsroles(10, 10) bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1", _role_slug="role7").add() bdd.Joueur(discord_id=2, chan_id_=21, nom="Joueur2", _role_slug="role8").add() bdd.Joueur(discord_id=3, chan_id_=31, nom="Joueur3", _role_slug="role8").add() bdd.Joueur(discord_id=4, chan_id_=41, nom="Joueur4", _role_slug="role8", statut=bdd.Statut.mort).add() # !quiest with non-existing nomrole ctx = mock_discord.get_ctx(quiest, nomrole="zzzzz") await ctx.invoke() ctx.assert_sent("") # !quiest with existing nomrole ctx = mock_discord.get_ctx(quiest, nomrole="role7") await ctx.invoke() ctx.assert_sent("Joueur1") # check several players & dead ctx = mock_discord.get_ctx(quiest, nomrole="role8") await ctx.invoke() ctx.send.assert_called_once() self.assertIn("Joueur2", ctx.send.call_args.args[0]) self.assertIn("Joueur3", ctx.send.call_args.args[0]) self.assertNotIn("Joueur4", ctx.send.call_args.args[0])
async def test_post(self): """Unit tests for !post command.""" # async def post(self, ctx, chan, *, message) post = self.cog.post joueurs = [ bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1"), bdd.Joueur(discord_id=2, chan_id_=21, nom="Joueur2"), bdd.Joueur(discord_id=3, chan_id_=31, nom="Joueur3"), bdd.Joueur(discord_id=4, chan_id_=41, nom="Joueur4"), bdd.Joueur(discord_id=5, chan_id_=51, nom="Joueur5"), bdd.Joueur(discord_id=6, chan_id_=61, nom="Joueur6"), ] bdd.Joueur.add(*joueurs) chans = [ mock_discord.chan("logs", id=153447897153447897), mock_discord.chan("rôles", id=564894149416486431) ] # cible = "logs" ctx = mock_discord.get_ctx(post, "logs", message="ouizz") _chans = config.guild.channels config.guild.channels = chans await ctx.invoke() config.guild.channels = _chans ctx.assert_sent("Fait") chans[0].send.assert_called_once_with("ouizz") chans[0].send.reset_mock() chans[1].send.assert_not_called() # cible = mention de #logs ctx = mock_discord.get_ctx(post, "<#153447897153447897>", message="oz") _chans = config.guild.channels config.guild.channels = chans await ctx.invoke() config.guild.channels = _chans ctx.assert_sent("Fait") chans[0].send.assert_called_once_with("oz") chans[0].send.reset_mock() chans[1].send.assert_not_called()
async def test_trigger_roles(self): """Unit tests for IA.trigger_roles function.""" # async def trigger_roles(message, sensi=0.8) trigger_roles = IA.trigger_roles mock_bdd.add_campsroles(10, 10) # nothing ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, "ooooo") rep = await trigger_roles(message) self.assertIs(rep, False) ctx.assert_sent() # one exact role found ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, "Role3") role3 = bdd.Role.query.get("role3") role3.prefixe = "PREF@x" role3.description_courte = "D€$k00rt" role3.description_longue = "D€$k_longue" role3.camp = bdd.Camp.query.get("camp3") rep = await trigger_roles(message) self.assertIs(rep, True) ctx.assert_sent( ["Role3", "PREF@x", "D€$k00rt", "D€$k_longue", "Camp3"]) # high sensi ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, "Rol") rep = await trigger_roles(message) self.assertIs(rep, False) ctx.assert_sent() # low sensi - more roles found ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, "Rol") rep = await trigger_roles(message, sensi=0.5) self.assertIs(rep, True) ctx.assert_sent("Role") # one selected
async def test_trigger_gif(self, ft_patch): """Unit tests for IA.trigger_gif function.""" # async def trigger_gif(message) trigger_gif = IA.trigger_gif config.bot.in_fals = [12, 13] # not in FALS ctx = mock_discord.get_ctx(None) ctx.channel.id = 2 message = mock_discord.message(ctx, "booza") rep = await trigger_gif(message) self.assertIs(rep, False) ft_patch.assert_not_called() ctx.assert_sent() # in FALS, return None ctx = mock_discord.get_ctx(None) ctx.channel.id = 12 message = mock_discord.message(ctx, "booza") ft_patch.return_value = None rep = await trigger_gif(message) self.assertIs(rep, False) ft_patch.assert_called_once_with("booza") ctx.assert_sent() ft_patch.reset_mock() # in FALS, return a gif ctx = mock_discord.get_ctx(None) ctx.channel.id = 12 message = mock_discord.message(ctx, "booza") ft_patch.return_value = "gif://cgénial" rep = await trigger_gif(message) self.assertIs(rep, True) ft_patch.assert_called_once_with("booza") ctx.assert_sent("gif://cgénial") ft_patch.reset_mock() config.bot.in_fals = []
async def test_rolede(self): """Unit tests for !rolede command.""" # async def rolede(self, ctx, *, cible=None) rolede = self.cog.rolede mock_bdd.add_campsroles(10, 10) bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1", _role_slug="role7").add() # !rolede with existing cible ctx = mock_discord.get_ctx(rolede, cible="Joueur1") await ctx.invoke() ctx.assert_sent("Role7") # !rolede with no cible ctx = mock_discord.get_ctx(rolede) with mock_discord.interact( ("wait_for_message_here", ctx.new_message("zzz")), ("wait_for_message_here", ctx.new_message("zzz")), ("wait_for_message_here", ctx.new_message("Joueur1"))): await ctx.invoke() ctx.assert_sent("", "", "", "Role7")
async def test_shell(self, rs_patch): """Unit tests for !shell command.""" # async def shell(self, ctx) shell = self.cog.shell rs_patch().interact = mock.AsyncMock() rs_patch.reset_mock() # Okay ctx = mock_discord.get_ctx(shell) await ctx.invoke() rs_patch.assert_called_once_with(ctx.channel, mock.ANY) self.assertEqual(rs_patch.call_args.args[1]["ctx"], ctx) ctx.assert_sent() rs_patch.reset_mock() # Exit rs_patch().interact.side_effect = blocs.realshell.RealShellExit rs_patch.reset_mock() ctx = mock_discord.get_ctx(shell) with self.assertRaises(blocs.tools.CommandExit): await ctx.invoke() rs_patch.assert_called_once_with(ctx.channel, mock.ANY) self.assertEqual(rs_patch.call_args.args[1]["ctx"], ctx) ctx.assert_sent()
async def test_addIA(self, bs_patch): """Unit tests for !addIA command.""" # async def addIA(self, ctx, *, triggers=None) addIA = self.cog.addIA # no triggers, simple gave ctx = mock_discord.get_ctx(addIA) bs_patch.return_value = "s€quenZ" with mock_discord.interact( ("wait_for_message_here", mock_discord.message(ctx, "trigzz"))): await ctx.invoke() bs_patch.assert_called_once_with(ctx) ctx.assert_sent("déclencheurs", "trigzz", "s€quenZ", "ajoutée en base") trig = bdd.Trigger.query.one() reac = bdd.Reaction.query.one() self.assertEqual(trig.trigger, "trigzz") self.assertEqual(trig.reaction, reac) self.assertEqual(reac.reponse, "s€quenZ") bdd.Trigger.delete(trig, reac) bs_patch.reset_mock() # complex triggers triggers = "trigger\n\nTR1; trégèrçù; trÉgÈrÇÙ ;; ❤\nah!" formated_trgz = ["trigger", "tr1", "tregercu", "tregercu", "❤", "ah!"] ctx = mock_discord.get_ctx(addIA, triggers=triggers) bs_patch.return_value = "s€quenZ" await ctx.invoke() bs_patch.assert_called_once_with(ctx) ctx.assert_sent(formated_trgz, "s€quenZ", "ajoutée en base") trigs = bdd.Trigger.query.all() reac = bdd.Reaction.query.one() self.assertEqual(len(trigs), 5) self.assertEqual({trig.trigger for trig in trigs}, set(formated_trgz)) self.assertEqual([trig.reaction for trig in trigs], [reac] * 5) self.assertEqual(reac.reponse, "s€quenZ") bs_patch.reset_mock()
async def test_doas(self, ocb_patch, bqj_patch): """Unit tests for !doas command.""" # async def doas(self, ctx, *, qui_quoi) doas = self.cog.doas # bad argument ctx = mock_discord.get_ctx(doas, qui_quoi="brbbrbr") with self.assertRaises(discord.ext.commands.UserInputError): await ctx.invoke() ctx = mock_discord.get_ctx(doas, qui_quoi="brbbr!br") with self.assertRaises(discord.ext.commands.UserInputError): await ctx.invoke() ocb_patch.assert_not_called() bqj_patch.assert_not_called() config.bot.process_commands.assert_not_called() # proceed ctx = mock_discord.get_ctx(doas, qui_quoi="bizz !kmd argz") await ctx.invoke() bqj_patch.assert_called_once_with(ctx, "bizz") ocb_patch.assert_called_once_with(ctx) config.bot.process_commands.assert_called_once_with(ctx.message) self.assertEqual(ctx.message.author, bqj_patch.return_value.member) self.assertEqual(ctx.message.content, "!kmd argz")
async def test_apropos(self): """Unit tests for !apropos command.""" # async def apropos(self, ctx) apropos = self.cog.apropos config.bot.description = "deskrzz" config.bot.user.avatar_url = "uzavurlz" ctx = mock_discord.get_ctx(apropos) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("embed", ctx.send.call_args.kwargs) embed = ctx.send.call_args.kwargs["embed"] self.assertIn(f"v{__version__}", embed.title) self.assertEqual("deskrzz", embed.description) self.assertEqual("uzavurlz", embed.author.icon_url) self.assertIn("logo_espci.png", embed.image.url)
async def test_cancel(self): """Unit tests for !cancel command.""" # async def cancel(self, ctx, *ids) cancel = self.cog.cancel # no arg ctx = mock_discord.get_ctx(cancel) await ctx.invoke() ctx.assert_sent("Aucune tâche trouvée") # meaningless args ctx = mock_discord.get_ctx(cancel, "oh", "ezz", "O@!") await ctx.invoke() ctx.assert_sent("Aucune tâche trouvée") # nonexisting tasks ctx = mock_discord.get_ctx(cancel, "1", "2", "3") await ctx.invoke() ctx.assert_sent("Aucune tâche trouvée") # one existing task, abort bdd.Tache(id=9, timestamp=datetime.datetime.now(), commande="oz").add() ctx = mock_discord.get_ctx(cancel, "9") with mock_discord.interact(("yes_no", False)): await ctx.invoke() ctx.assert_sent("oz", "aborted") self.assertEqual(len(bdd.Tache.query.all()), 1) # one existing task, proceed ctx = mock_discord.get_ctx(cancel, "9") with mock_discord.interact(("yes_no", True)): await ctx.invoke() ctx.assert_sent("oz", "annulée") self.assertEqual(len(bdd.Tache.query.all()), 0) # two tasks and junk, abort bdd.Tache(id=3, timestamp=datetime.datetime.now(), commande="oz").add() bdd.Tache(id=5, timestamp=datetime.datetime.now(), commande="uz").add() ctx = mock_discord.get_ctx(cancel, "9", "az", "3", "5") with mock_discord.interact(("yes_no", False)): await ctx.invoke() ctx.assert_sent(["oz", "uz"], "aborted") self.assertEqual(len(bdd.Tache.query.all()), 2) # two tasks and junk, proceed ctx = mock_discord.get_ctx(cancel, "9", "az", "3", "5") with mock_discord.interact(("yes_no", True)): await ctx.invoke() ctx.assert_sent(["oz", "uz"], "annulée") self.assertEqual(len(bdd.Tache.query.all()), 0)
async def test_coinflip(self): """Unit tests for !coinflip command.""" # async def coinflip(self, ctx) coinflip = self.cog.coinflip ctx = mock_discord.get_ctx(coinflip) p, c = 0, 0 for i in range(1000): await ctx.invoke() if ctx.send.call_args.args[0] == "Pile": p += 1 elif ctx.send.call_args.args[0] == "Face": c += 1 else: raise AssertionError("returned wtf thing") self.assertGreater(p, 400) self.assertGreater(c, 400)
async def test_default(self): """Unit tests for IA.default function.""" # async def default(message) default = IA.default samples = { "": False, "bla bla": False, "boozabaka": False, "booza ou baka": "Baka.", "a v à @ ou b. !??,!;;!": "B.", "a v à @ ou b. !??,!;;!a": "B. !??,!;;!a.", } # all samples for sample, result in samples.items(): ctx = mock_discord.get_ctx(None) message = mock_discord.message(ctx, sample) rep = await default(message) self.assertIs(rep, True)