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_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_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_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_send(self): """Unit tests for !send command.""" # async def send(self, ctx, cible, *, message) send = self.cog.send mock_bdd.add_campsroles(10, 10) joueurs = [ bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1", statut=bdd.Statut.vivant, _role_slug="role1", votant_village=True, votant_loups=False, role_actif=False), bdd.Joueur(discord_id=2, chan_id_=21, nom="Joueur2", chambre="ch2", statut=bdd.Statut.vivant, votant_village=True, votant_loups=False, role_actif=False), bdd.Joueur(discord_id=3, chan_id_=31, nom="Joueur3", statut=bdd.Statut.mort, _role_slug="role3", _camp_slug="camp3", votant_village=True, votant_loups=False, role_actif=True), bdd.Joueur(discord_id=4, chan_id_=41, nom="Joueur4", chambre="ch 4", statut=bdd.Statut.mort, votant_village=False, votant_loups=False, role_actif=True), bdd.Joueur(discord_id=5, chan_id_=51, nom="Joueur 5", statut=bdd.Statut.MV, _camp_slug="camp3", votant_village=False, votant_loups=False, role_actif=False), bdd.Joueur(discord_id=6, chan_id_=61, nom="Joueur6", statut=bdd.Statut.immortel, _role_slug="role3", votant_village=False, votant_loups=True, role_actif=False), ] bdd.Joueur.add(*joueurs) # cible = "all" ctx = mock_discord.get_ctx(send, "all", message="ouizz") with mock_discord.mock_members_and_chans(*joueurs): chans = [joueur.private_chan for joueur in joueurs] await ctx.invoke() ctx.assert_sent("6 trouvé", "Fini") for chan in chans: chan.send.assert_called_once_with("ouizz") # cible = "vivants" ctx = mock_discord.get_ctx(send, "vivants", message="nonkk") with mock_discord.mock_members_and_chans(*joueurs): chans = [joueur.private_chan for joueur in joueurs] await ctx.invoke() ctx.assert_sent("3 trouvé", "Fini") for i, chan in enumerate(chans): if i in [0, 1, 4]: chan.send.assert_called_once_with("nonkk") else: chan.send.assert_not_called() # cible = "morts" ctx = mock_discord.get_ctx(send, "morts", message="bzzk.!") with mock_discord.mock_members_and_chans(*joueurs): chans = [joueur.private_chan for joueur in joueurs] await ctx.invoke() ctx.assert_sent("2 trouvé", "Fini") for i, chan in enumerate(chans): if i in [2, 3]: chan.send.assert_called_once_with("bzzk.!") else: chan.send.assert_not_called() # cible = bat crit ctx = mock_discord.get_ctx(send, "biz=oui", message="hmm") with self.assertRaises(discord.ext.commands.UserInputError) as cm: await ctx.invoke() self.assertIn("critère 'biz' incorrect", cm.exception.args[0]) # cible = all crits crits = { "discord_id=3": [2], "chan_id_=21": [1], "nom=Joueur4": [3], "nom=Joueur 5": [4], "chambre=ch2": [1], "chambre=ch 4": [3], "statut=vivant": [0, 1], "statut=mort": [2, 3], "statut=MV": [4], "statut=immortel": [5], "role=role1": [0], "role=role3": [2, 5], "camp=camp3": [2, 4], "votant_village=True": [0, 1, 2], "votant_village=1": [0, 1, 2], "votant_village=faux": [3, 4, 5], "votant_loups=vrai": [5], "votant_loups=0": [0, 1, 2, 3, 4], "role_actif=True": [2, 3], "role_actif=False": [0, 1, 4, 5], } for crit, ijs in crits.items(): ctx = mock_discord.get_ctx(send, crit, message="bakka") with mock_discord.mock_members_and_chans(*joueurs): chans = [joueur.private_chan for joueur in joueurs] await ctx.invoke() ctx.assert_sent(f"{len(ijs)} trouvé", "Fini") for i, chan in enumerate(chans): if i in ijs: chan.send.assert_called_once_with("bakka") else: chan.send.assert_not_called() # cible = non-existing joueur, correct ctx = mock_discord.get_ctx(send, "gouzigouzi", message="oui") with mock_discord.interact( ("wait_for_message_here", mock_discord.message(ctx, "krr")), ("wait_for_message_here", mock_discord.message(ctx, "Joueur2")), ): with mock_discord.mock_members_and_chans(*joueurs): chans = [joueur.private_chan for joueur in joueurs] await ctx.invoke() ctx.assert_sent("Aucune entrée trouvée", "Aucune entrée trouvée", "1 trouvé", "Fini") for i, chan in enumerate(chans): if i == 1: chan.send.assert_called_once_with("oui") else: chan.send.assert_not_called() # cible = existing joueur, correct ctx = mock_discord.get_ctx(send, "Joueur 5", message="oui") with mock_discord.mock_members_and_chans(*joueurs): chans = [joueur.private_chan for joueur in joueurs] await ctx.invoke() ctx.assert_sent("1 trouvé", "Fini") for i, chan in enumerate(chans): if i == 4: chan.send.assert_called_once_with("oui") else: chan.send.assert_not_called() # cible = all, test eval ctx = mock_discord.get_ctx( send, "all", message="Salut {member.mention}, tu t'appelles {joueur.role}, rôle " "{joueur.role.nom}, camp {joueur.camp.nom}, chan {chan.name}") with mock_discord.mock_members_and_chans(*joueurs): members = [joueur.member for joueur in joueurs] chans = [joueur.private_chan for joueur in joueurs] await ctx.invoke() ctx.assert_sent("6 trouvé", "Fini") for joueur, chan, member in zip(joueurs, chans, members): chan.send.assert_called_once_with( f"Salut {member.mention}, tu t'appelles {joueur.role}, rôle " f"{joueur.role.nom}, camp {joueur.camp.nom}, chan {chan.name}")
async def test_annoncemort(self, as_patch): """Unit tests for !annoncemort command.""" # async def annoncemort(self, ctx, *, victime=None) annoncemort = self.cog.annoncemort mock_bdd.add_campsroles(10, 10) joueurs = [ bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1", statut=bdd.Statut.vivant, _role_slug="role1"), bdd.Joueur(discord_id=2, chan_id_=21, nom="Joueur2", chambre="ch2", statut=bdd.Statut.vivant), bdd.Joueur(discord_id=3, chan_id_=31, nom="Joueur3", statut=bdd.Statut.mort, _role_slug="role3", _camp_slug="camp3"), bdd.Joueur(discord_id=4, chan_id_=41, nom="Joueur4", chambre="ch 4", statut=bdd.Statut.mort), bdd.Joueur(discord_id=5, chan_id_=51, nom="Joueur5", statut=bdd.Statut.MV, _role_slug="role5"), bdd.Joueur(discord_id=6, chan_id_=61, nom="Joueur6", statut=bdd.Statut.immortel, _role_slug="role3"), ] bdd.Joueur.add(*joueurs) # victime = None, correct "Joueur3", role ok, abort ctx = mock_discord.get_ctx(annoncemort) with mock_discord.interact( ("wait_for_message_here", mock_discord.message(ctx, "bzz")), ("wait_for_message_here", mock_discord.message(ctx, "Joueur3")), ("yes_no", True), # rôle à afficher ("wait_for_message_here", mock_discord.message(ctx, "oui")), ("yes_no", False), # abort ): await ctx.invoke(cog=self.cog) ctx.assert_sent("Qui", "Aucune entrée trouvée", "Rôle à afficher", "Contexte", "Ça part", "Mission aborted") as_patch.assert_not_called() # victime = "Joueur3", role ok, proceed ctx = mock_discord.get_ctx(annoncemort, victime="Joueur3") with mock_discord.interact( ("yes_no", True), # rôle à afficher ("wait_for_message_here", mock_discord.message(ctx, "oui")), ("yes_no", True), # proceed ): await ctx.invoke(cog=self.cog) ctx.assert_sent("Rôle à afficher", "Contexte", "Ça part", "c'est parti") as_patch.assert_called_once() self.assertIn("quelque chose", as_patch.call_args.args[0]) embed = as_patch.call_args.kwargs["embed"] self.assertIn("Joueur3", embed.title) self.assertIn("Role3", embed.title) self.assertIn("oui", embed.description) self.assertEqual(discord.Embed.Empty, embed.thumbnail.url) # standard role: no emoji as_patch.reset_mock() # victime = "Joueur3", role ok, proceed + emoji ctx = mock_discord.get_ctx(annoncemort, victime="Joueur3") emoji3 = mock.NonCallableMock(discord.Emoji) emoji3.configure_mock(name="emoji3", url="bzooop") _emojis = config.guild.emojis config.guild.emojis = [emoji3] with mock_discord.interact( ("yes_no", True), # rôle à afficher ("wait_for_message_here", mock_discord.message(ctx, "oui")), ("yes_no", True), # proceed ): await ctx.invoke(cog=self.cog) config.guild.emojis = _emojis ctx.assert_sent("Rôle à afficher", "Contexte", "Ça part", "c'est parti") as_patch.assert_called_once() self.assertIn("quelque chose", as_patch.call_args.args[0]) embed = as_patch.call_args.kwargs["embed"] self.assertIn("Joueur3", embed.title) self.assertIn("Role3", embed.title) self.assertIn("oui", embed.description) self.assertEqual("bzooop", embed.thumbnail.url) as_patch.reset_mock() # victime = "Joueur5" (MV), role ok no MV, proceed ctx = mock_discord.get_ctx(annoncemort, victime="Joueur5") with mock_discord.interact( ("yes_no", True), # rôle à afficher ("yes_no", False), # afficher la MVance ("wait_for_message_here", mock_discord.message(ctx, "oui")), ("yes_no", True), # proceed ): await ctx.invoke(cog=self.cog) ctx.assert_sent("Rôle à afficher", "Annoncer la mort-vivance", "Contexte", "Ça part", "c'est parti") as_patch.assert_called_once() self.assertIn("quelque chose", as_patch.call_args.args[0]) embed = as_patch.call_args.kwargs["embed"] self.assertIn("Joueur5", embed.title) self.assertIn("Role5", embed.title) self.assertNotIn("Mort-Vivant", embed.title) self.assertIn("oui", embed.description) as_patch.reset_mock() # victime = "Joueur5" (MV), role ok no MV, proceed ctx = mock_discord.get_ctx(annoncemort, victime="Joueur5") with mock_discord.interact( ("yes_no", True), # rôle à afficher ("yes_no", True), # afficher la MVance ("wait_for_message_here", mock_discord.message(ctx, "oui")), ("yes_no", True), # proceed ): await ctx.invoke(cog=self.cog) ctx.assert_sent("Rôle à afficher", "Annoncer la mort-vivance", "Contexte", "Ça part", "c'est parti") as_patch.assert_called_once() self.assertIn("quelque chose", as_patch.call_args.args[0]) embed = as_patch.call_args.kwargs["embed"] self.assertIn("Joueur5", embed.title) self.assertIn("Role5", embed.title) self.assertIn("Mort-Vivant", embed.title) self.assertIn("oui", embed.description) as_patch.reset_mock() role_village = bdd.Role(slug="") # victime = "Joueur3", role not ok usoab, proceed ctx = mock_discord.get_ctx(annoncemort, victime="Joueur3") with mock_discord.interact( ("yes_no", False), # rôle à afficher ("wait_for_message_here", mock_discord.message(ctx, "RolZZZ")), ("wait_for_react_clic", mock.NonCallableMock(discord.Emoji, url="bzooop")), ("wait_for_message_here", mock_discord.message(ctx, "oui")), ("yes_no", True), # proceed ): await ctx.invoke(cog=self.cog) ctx.assert_sent("Rôle à afficher", "Rôle à afficher", "Camp", "Contexte", "Ça part", "c'est parti") as_patch.assert_called_once() self.assertIn("quelque chose", as_patch.call_args.args[0]) embed = as_patch.call_args.kwargs["embed"] self.assertIn("Joueur3", embed.title) self.assertIn("RolZZZ", embed.title) self.assertIn("oui", embed.description) self.assertEqual("bzooop", embed.thumbnail.url) as_patch.reset_mock()
async def test_action(self, export_patch, ca_patch): """Unit tests for !action command.""" # async def action(self, ctx, *, decision=None) action = self.cog.action mock_bdd.add_campsroles() joueur = bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1", role_actif=False) joueur.add() # role_actif False ctx = mock_discord.get_ctx(action, _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("ne peux pas utiliser", ctx.send.call_args.args[0]) export_patch.assert_not_called() ca_patch.assert_not_called() # no actions joueur.role_actif = True joueur.update() ctx = mock_discord.get_ctx(action, _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("Aucune action en cours", ctx.send.call_args.args[0]) export_patch.assert_not_called() ca_patch.assert_not_called() # 1 closed action bdd.BaseAction(slug="ouiZ").add() action1 = bdd.Action(joueur=joueur, _base_slug="ouiZ") action1.add() ctx = mock_discord.get_ctx(action, _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("Aucune action en cours", ctx.send.call_args.args[0]) export_patch.assert_not_called() ca_patch.assert_not_called() # 1 open action and decision_ action1.decision_ = "oh" action1.update() ctx = mock_discord.get_ctx(action, decision="boo", _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("boo", ctx.send.call_args.args[0]) self.assertIn("bien prise en compte", ctx.send.call_args.args[0]) self.assertIn("ouiZ", ctx.send.call_args.args[0]) self.assertEqual(action1.decision_, "boo") export_patch.assert_called_once_with("action", joueur) export_patch.reset_mock() ca_patch.assert_not_called() # 1 open action and no decision_ action1.decision_ = "oh" action1.update() ctx = mock_discord.get_ctx(action, _caller_id=1) with mock_discord.interact( ("wait_for_message_here", ctx.new_message("boo"))): await ctx.invoke() ctx.send.assert_called() self.assertIn("boo", ctx.send.call_args.args[0]) self.assertIn("bien prise en compte", ctx.send.call_args.args[0]) self.assertIn("ouiZ", ctx.send.call_args.args[0]) self.assertEqual(action1.decision_, "boo") export_patch.assert_called_once_with("action", joueur) export_patch.reset_mock() ca_patch.assert_not_called() # 1 open INSTANT action and abort action1.decision_ = "oh" action1.base.instant = True action1.update() ctx = mock_discord.get_ctx(action, decision="boo", _caller_id=1) with mock_discord.interact(("yes_no", False)): # abort (answer "no" at instant warning) await ctx.invoke() ctx.send.assert_called() calls = ctx.send.call_args_list self.assertIn("conséquence instantanée", calls[0].args[0]) self.assertIn("Ça part ?", calls[0].args[0]) self.assertIn("aborted", calls[1].args[0]) self.assertEqual(action1.decision_, "oh") export_patch.assert_not_called() ca_patch.assert_not_called() # 1 open INSTANT action and proceed ctx = mock_discord.get_ctx(action, decision="boo", _caller_id=1) with mock_discord.interact(("yes_no", True)): # proceed (answer "yes" at instant warning) await ctx.invoke() ctx.assert_sent( "Attention", [str(config.Role.mj.mention), "conséquence instantanée"]) self.assertEqual(action1.decision_, "boo") export_patch.assert_called_once_with("action", joueur) export_patch.reset_mock() ca_patch.assert_called_once_with(action1) ca_patch.reset_mock() # 1 open action and closed during decision choice action1.decision_ = "oh" action1.base.instant = False action1.update() def close_action(*args, **kwargs): action1.decision_ = None action1.update() return ctx.new_message("boo") ctx = mock_discord.get_ctx(action, _caller_id=1) with mock_discord.interact(("wait_for_message_here", close_action)): # close action and return decision await ctx.invoke() ctx.send.assert_called() self.assertIn("a fermé entre temps", ctx.send.call_args.args[0]) export_patch.assert_not_called() ca_patch.assert_not_called() # 2 open actions and decision_: ask action1.decision_ = "oh" action1.update() bdd.BaseAction(slug="nonZ").add() action2 = bdd.Action(joueur=joueur, _base_slug="nonZ", decision_="uh") action2.add() ctx = mock_discord.get_ctx(action, decision="ih", _caller_id=1) with mock_discord.interact( ("choice", 2), ("wait_for_message_here", ctx.new_message("boo"))): await ctx.invoke() ctx.send.assert_called() calls = ctx.send.call_args_list self.assertIn("ouiZ", calls[0].args[0]) self.assertIn("nonZ", calls[0].args[0]) self.assertIn("Pour laquelle", calls[0].args[0]) self.assertIn("nonZ", calls[1].args[0]) self.assertIn("veux-tu faire", calls[1].args[0]) self.assertIn("boo", calls[2].args[0]) self.assertIn("bien prise en compte", calls[2].args[0]) self.assertIn("nonZ", calls[2].args[0]) self.assertEqual(action1.decision_, "oh") self.assertEqual(action2.decision_, "boo") export_patch.assert_called_once_with("action", joueur) ca_patch.assert_not_called()
async def test_voteloups(self, export_patch): """Unit tests for !voteloups command.""" # async def voteloups(self, ctx, *, cible=None) voteloups = self.cog.voteloups mock_bdd.add_campsroles() joueur = bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1", votant_loups=False, vote_loups_="oh") joueur.add() # votant_loups False ctx = mock_discord.get_ctx(voteloups, _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("pas autorisé", ctx.send.call_args.args[0]) export_patch.assert_not_called() # no voteloups joueur.votant_loups = True joueur.vote_loups_ = None joueur.update() ctx = mock_discord.get_ctx(voteloups, _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("pas de vote", ctx.send.call_args.args[0].lower()) export_patch.assert_not_called() # closed during cible choice joueur.vote_loups_ = "oh" joueur.update() cible = bdd.Joueur(discord_id=2, chan_id_=21, nom="Joueur2") cible.add() ctx = mock_discord.get_ctx(voteloups, _caller_id=1) def close_vote(*args, **kwargs): joueur.vote_loups_ = None joueur.update() return ctx.new_message("Joueur2") with mock_discord.interact(("wait_for_message_here", close_vote)): # close voteloups and return cible await ctx.invoke() self.assertEqual(ctx.send.call_count, 2) self.assertIn("a fermé entre temps", ctx.send.call_args.args[0]) export_patch.assert_not_called() # ok joueur.vote_loups_ = "oh" # reopen voteloups joueur.update() ctx = mock_discord.get_ctx(voteloups, cible="Joueur2", _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("Joueur2", ctx.send.call_args.args[0]) self.assertIn("bien pris en compte", ctx.send.call_args.args[0]) self.assertEqual(joueur.vote_loups_, "Joueur2") export_patch.assert_called_once_with("loups", joueur) export_patch.reset_mock() # ok, cible not specified joueur.vote_loups_ = "oh" # reset voteloups joueur.update() ctx = mock_discord.get_ctx(voteloups, _caller_id=1) 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("Joueur2"))): await ctx.invoke() self.assertIn("Joueur2", ctx.send.call_args.args[0]) self.assertIn("bien pris en compte", ctx.send.call_args.args[0]) self.assertEqual(joueur.vote_loups_, "Joueur2") export_patch.assert_called_once_with("loups", joueur) export_patch.reset_mock()
async def test_votemaire(self, export_patch): """Unit tests for !votemaire command.""" # async def votemaire(self, ctx, *, cible=None) votemaire = self.cog.votemaire mock_bdd.add_campsroles() joueur = bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1", votant_village=False, vote_maire_="oh") joueur.add() export_patch.assert_not_called() # votant_village False ctx = mock_discord.get_ctx(votemaire, _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("pas autorisé", ctx.send.call_args.args[0]) export_patch.assert_not_called() # no votemaire joueur.votant_village = True joueur.vote_maire_ = None joueur.update() ctx = mock_discord.get_ctx(votemaire, _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("pas de vote", ctx.send.call_args.args[0].lower()) export_patch.assert_not_called() # not candid joueur.vote_maire_ = "oh" joueur.update() bdd.Joueur(discord_id=2, chan_id_=21, nom="Joueur2").add() ctx = mock_discord.get_ctx(votemaire, cible="Joueur2", _caller_id=1) with mock_discord.interact(("yes_no", False)): # abort (answer "no" at non-haroted warning) await ctx.invoke() calls = ctx.send.call_args_list self.assertEqual(len(calls), 2) self.assertIn("ne s'est pas (encore) présenté", calls[0].args[0]) self.assertIn("mission aborted", calls[1].args[0]) export_patch.assert_not_called() # closed during haro check def close_vote(_): joueur.vote_maire_ = None joueur.update() return True ctx = mock_discord.get_ctx(votemaire, cible="Joueur2", _caller_id=1) with mock_discord.interact(("yes_no", close_vote)): # close votemaire and answer "yes" at non-haroted warning await ctx.invoke() calls = ctx.send.call_args_list self.assertEqual(len(calls), 2) self.assertIn("a fermé entre temps", calls[1].args[0]) export_patch.assert_not_called() # ok joueur.vote_maire_ = "oh" # reopen votemaire joueur.update() bdd.CandidHaro(_joueur_id=2, type=bdd.CandidHaroType.candidature).add() ctx = mock_discord.get_ctx(votemaire, cible="Joueur2", _caller_id=1) await ctx.invoke() ctx.send.assert_called_once() self.assertIn("Joueur2", ctx.send.call_args.args[0]) self.assertIn("bien pris en compte", ctx.send.call_args.args[0]) self.assertEqual(joueur.vote_maire_, "Joueur2") export_patch.assert_called_once_with("maire", joueur) export_patch.reset_mock() # ok, cible not specified joueur.vote_maire_ = "oh" # reset votemaire joueur.update() ctx = mock_discord.get_ctx(votemaire, _caller_id=1) 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("Joueur2"))): await ctx.invoke() self.assertIn("Joueur2", ctx.send.call_args.args[0]) self.assertIn("bien pris en compte", ctx.send.call_args.args[0]) self.assertEqual(joueur.vote_maire_, "Joueur2") export_patch.assert_called_once_with("maire", joueur) export_patch.reset_mock()
async def test_addhere(self): """Unit tests for !addhere command.""" # async def addhere(self, ctx, *joueurs) addhere = self.cog.addhere mock_bdd.add_campsroles(10, 10) joueurs = [ bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1", statut=bdd.Statut.vivant, _role_slug="role1", votant_village=True, votant_loups=False, role_actif=False), bdd.Joueur(discord_id=2, chan_id_=21, nom="Joueur2", chambre="ch2", statut=bdd.Statut.vivant, votant_village=True, votant_loups=False, role_actif=False), bdd.Joueur(discord_id=3, chan_id_=31, nom="Joueur3", statut=bdd.Statut.mort, _role_slug="role3", _camp_slug="camp3", votant_village=True, votant_loups=False, role_actif=True), bdd.Joueur(discord_id=4, chan_id_=41, nom="Joueur4", chambre="ch 4", statut=bdd.Statut.mort, votant_village=False, votant_loups=False, role_actif=True), bdd.Joueur(discord_id=5, chan_id_=51, nom="Joueur 5", statut=bdd.Statut.MV, _camp_slug="camp3", votant_village=False, votant_loups=False, role_actif=False), bdd.Joueur(discord_id=6, chan_id_=61, nom="Joueur6", statut=bdd.Statut.immortel, _role_slug="role3", votant_village=False, votant_loups=True, role_actif=False), ] bdd.Joueur.add(*joueurs) # cible = bat filtre ctx = mock_discord.get_ctx(addhere, "biz=oui") with self.assertRaises(discord.ext.commands.UserInputError) as cm: await ctx.invoke() self.assertIn("critère 'biz' incorrect", cm.exception.args[0]) # cible = all filtres crits = { "discord_id=3": [2], "chan_id_=21": [1], "nom=Joueur4": [3], "nom=Joueur 5": [4], "chambre=ch2": [1], "chambre=ch 4": [3], "statut=vivant": [0, 1], "statut=mort": [2, 3], "statut=MV": [4], "statut=immortel": [5], "role=role1": [0], "role=role3": [2, 5], "camp=camp3": [2, 4], "votant_village=True": [0, 1, 2], "votant_village=1": [0, 1, 2], "votant_village=faux": [3, 4, 5], "votant_loups=vrai": [5], "votant_loups=0": [0, 1, 2, 3, 4], "role_actif=True": [2, 3], "role_actif=False": [0, 1, 4, 5], } for crit, ijs in crits.items(): ctx = mock_discord.get_ctx(addhere, crit) with mock_discord.mock_members_and_chans(*joueurs): members = [joueur.member for joueur in joueurs] with mock_discord.interact(("yes_no", False)): await ctx.invoke() ctx.assert_sent(*[joueurs[ij].nom for ij in ijs], "Fini") self.assertEqual(ctx.channel.set_permissions.call_count, len(ijs)) ctx.channel.set_permissions.assert_has_calls([ mock.call(member, read_messages=True, send_messages=True) for ij, member in enumerate(members) if ij in ijs ]) ctx.channel.purge.assert_not_called() # cible = non-existing joueur, correct ctx = mock_discord.get_ctx(addhere, "gouzigouzi") with mock_discord.interact( ("wait_for_message_here", mock_discord.message(ctx, "krr")), ("wait_for_message_here", mock_discord.message(ctx, "Joueur2")), ("yes_no", False), ): with mock_discord.mock_members_and_chans(*joueurs): members = [joueur.member for joueur in joueurs] await ctx.invoke() ctx.assert_sent("Aucune entrée trouvée", "Aucune entrée trouvée", "Joueur2", "Fini") ctx.channel.set_permissions.assert_called_once_with(members[1], read_messages=True, send_messages=True) ctx.channel.purge.assert_not_called() # cible = existing joueur ctx = mock_discord.get_ctx(addhere, "Joueur 5") with mock_discord.mock_members_and_chans(*joueurs): members = [joueur.member for joueur in joueurs] with mock_discord.interact(("yes_no", False)): await ctx.invoke() ctx.assert_sent("Joueur 5", "Fini") ctx.channel.set_permissions.assert_called_once_with(members[4], read_messages=True, send_messages=True) ctx.channel.purge.assert_not_called() # cible = several joueurs, correct some ctx = mock_discord.get_ctx(addhere, "Joueur2", "kwdzz", "Joueur 5") with mock_discord.interact( ("wait_for_message_here", mock_discord.message(ctx, "krr")), ("wait_for_message_here", mock_discord.message(ctx, "Joueur4")), ("yes_no", False), ): with mock_discord.mock_members_and_chans(*joueurs): members = [joueur.member for joueur in joueurs] await ctx.invoke() ctx.assert_sent("Aucune entrée trouvée", "Aucune entrée trouvée", "Joueur2", "Joueur4", "Joueur 5", "Fini") self.assertEqual(ctx.channel.set_permissions.call_count, 3) ctx.channel.set_permissions.assert_has_calls([ mock.call(members[1], read_messages=True, send_messages=True), mock.call(members[3], read_messages=True, send_messages=True), mock.call(members[4], read_messages=True, send_messages=True), ]) ctx.channel.purge.assert_not_called() # cible = existing joueur, purge ctx = mock_discord.get_ctx(addhere, "Joueur 5") with mock_discord.mock_members_and_chans(*joueurs): members = [joueur.member for joueur in joueurs] with mock_discord.interact(("yes_no", True)): await ctx.invoke() ctx.assert_sent("Joueur 5", "Fini") ctx.channel.set_permissions.assert_called_once_with(members[4], read_messages=True, send_messages=True) ctx.channel.purge.assert_called_once()
async def test_modifIA(self, bs_patch): """Unit tests for !modifIA command.""" # async def modifIA(self, ctx, *, trigger=None) modifIA = self.cog.modifIA reac = bdd.Reaction(reponse="s€quenZ") triggers = [ bdd.Trigger(trigger="trigz1", reaction=reac), bdd.Trigger(trigger="tr1gZ2", reaction=reac), bdd.Trigger(trigger="tr!Gz3", reaction=reac), ] bdd.Reaction.add(reac, *triggers) # nothing found ctx = mock_discord.get_ctx(modifIA) bs_patch.return_value = "s€quenZ" with mock_discord.interact( ("wait_for_message_here", mock_discord.message(ctx, "ooooo"))): await ctx.invoke() ctx.assert_sent("déclencheur", "Rien trouvé") bs_patch.assert_not_called() # modif triggers ctx = mock_discord.get_ctx(modifIA, trigger="trigz") with mock_discord.interact( ("wait_for_react_clic", 1), # modif triggers ("wait_for_react_clic", "ahbon"), # trigger 4 ("wait_for_react_clic", "3"), # delete trigger 3 ("wait_for_react_clic", "0"), # end ): await ctx.invoke() ctx.assert_sent( ["trigz1", "tr1gZ2", "tr!Gz3", "s€quenZ"], "Modifier", ["Supprimer", "ajouter", "trigz1", "tr1gZ2", "tr!Gz3"], ["Supprimer", "ajouter", "trigz1", "tr1gZ2", "tr!Gz3", "ahbon"], ["Supprimer", "ajouter", "trigz1", "tr1gZ2", "ahbon"], "Fini", ) self.assertEqual(bdd.Reaction.query.one(), reac) self.assertEqual({trig.trigger for trig in reac.triggers}, {"trigz1", "tr1gZ2", "ahbon"}) self.assertEqual(reac.reponse, "s€quenZ") bs_patch.assert_not_called() # delete all triggers ctx = mock_discord.get_ctx(modifIA, trigger="trigz") with mock_discord.interact( ("wait_for_react_clic", 1), # modif triggers ("wait_for_react_clic", "3"), # delete trigger 3 ("wait_for_react_clic", "1"), # delete trigger 1 ("wait_for_react_clic", "1"), # delete trigger 1 ("wait_for_react_clic", "0"), # end ): await ctx.invoke() ctx.assert_sent( ["trigz1", "tr1gZ2", "ahbon", "s€quenZ"], "Modifier", ["Supprimer", "ajouter", "trigz1", "tr1gZ2", "ahbon"], ["Supprimer", "ajouter", "trigz1", "tr1gZ2"], ["Supprimer", "ajouter", "tr1gZ2"], ["Supprimer", "ajouter"], "suppression", ) self.assertEqual(len(bdd.Reaction.query.all()), 0) self.assertEqual(len(bdd.Trigger.query.all()), 0) bs_patch.assert_not_called() # modif sequence (simple) reac = bdd.Reaction(reponse="s€quenZ") triggers = [ bdd.Trigger(trigger="trigz1", reaction=reac), bdd.Trigger(trigger="tr1gZ2", reaction=reac), bdd.Trigger(trigger="tr!Gz3", reaction=reac), ] bdd.Reaction.add(reac, *triggers) ctx = mock_discord.get_ctx(modifIA, trigger="trigz") bs_patch.return_value = "new_s€qu€nZ" with mock_discord.interact( ("wait_for_react_clic", 2), # modif sequence ): await ctx.invoke() ctx.assert_sent( ["trigz1", "tr1gZ2", "tr!Gz3", "s€quenZ"], "Modifier", "Fini", ) self.assertEqual(bdd.Reaction.query.one(), reac) self.assertEqual({trig.trigger for trig in reac.triggers}, {"trigz1", "tr1gZ2", "tr!Gz3"}) self.assertEqual(reac.reponse, "new_s€qu€nZ") bs_patch.assert_called_once_with(ctx) bs_patch.reset_mock() # modif sequence (complex) reac.reponse = f"s€quenZ{IA.MARK_THEN}oui" reac.update() ctx = mock_discord.get_ctx(modifIA, trigger="trigz") bs_patch.return_value = "new_s€qu€nZ" with mock_discord.interact( ("wait_for_react_clic", 2), # modif sequence ): await ctx.invoke() ctx.assert_sent( ["trigz1", "tr1gZ2", "tr!Gz3", "s€quenZ"], "Modifier", "modifiée rapidement", "Fini", ) self.assertEqual(bdd.Reaction.query.one(), reac) self.assertEqual({trig.trigger for trig in reac.triggers}, {"trigz1", "tr1gZ2", "tr!Gz3"}) self.assertEqual(reac.reponse, "new_s€qu€nZ") bs_patch.assert_called_once_with(ctx) bs_patch.reset_mock() # delete sequence ctx = mock_discord.get_ctx(modifIA, trigger="trigz") with mock_discord.interact( ("wait_for_react_clic", 0), # dekete sequence ): await ctx.invoke() ctx.assert_sent( ["trigz1", "tr1gZ2", "tr!Gz3", "new_s€qu€nZ"], "Modifier", "Fini", ) self.assertEqual(len(bdd.Reaction.query.all()), 0) self.assertEqual(len(bdd.Trigger.query.all()), 0) bs_patch.assert_not_called()
async def test__build_sequence(self): """Unit tests for IA._build_sequence function.""" # async def _build_sequence(ctx) _build_sequence = IA._build_sequence # message ctx = mock_discord.get_ctx(None) with mock_discord.interact( ("wait_for_react_clic", "mezzag"), ("wait_for_react_clic", False), ): seq = await _build_sequence(ctx) self.assertEqual(seq, "mezzag") ctx.assert_sent("Réaction du bot", "Puis") # command ctx = mock_discord.get_ctx(None) with mock_discord.interact( ("wait_for_react_clic", "!commandz"), ("wait_for_react_clic", False), ): seq = await _build_sequence(ctx) self.assertEqual(seq, f"{IA.MARK_CMD}commandz") ctx.assert_sent("Réaction du bot", "Puis") # command - other prefix ctx = mock_discord.get_ctx(None) config.bot.command_prefix = "PREFIXZ" with mock_discord.interact( ("wait_for_react_clic", "PREFIXZcommandz"), ("wait_for_react_clic", False), ): seq = await _build_sequence(ctx) self.assertEqual(seq, f"{IA.MARK_CMD}commandz") ctx.assert_sent("Réaction du bot", "Puis") config.bot.command_prefix = "!" # react ctx = mock_discord.get_ctx(None) reaczt = mock.NonCallableMock(discord.Emoji) reaczt.configure_mock(name="bon.") with mock_discord.interact( ("wait_for_react_clic", reaczt), ("wait_for_react_clic", False), ): seq = await _build_sequence(ctx) self.assertEqual(seq, f"{IA.MARK_REACT}bon.") ctx.assert_sent("Réaction du bot", "Puis") # several commands ctx = mock_discord.get_ctx(None) reaczt = mock.NonCallableMock(discord.Emoji) reaczt.configure_mock(name="bon.") with mock_discord.interact( ("wait_for_react_clic", "mezzage"), ("wait_for_react_clic", IA.MARK_THEN), ("wait_for_react_clic", reaczt), ("wait_for_react_clic", IA.MARK_OR), ("wait_for_react_clic", "oh"), ("wait_for_react_clic", IA.MARK_THEN), ("wait_for_react_clic", "!ouizz"), ("wait_for_react_clic", False), ): seq = await _build_sequence(ctx) self.assertEqual( seq, f"mezzage{IA.MARK_THEN}{IA.MARK_REACT}bon.{IA.MARK_OR}" f"oh{IA.MARK_THEN}{IA.MARK_CMD}ouizz") ctx.assert_sent("Réaction du bot", "Puis", "Réaction du bot", "Puis", "Réaction du bot", "Puis", "Réaction du bot", "Puis")
async def test_planif(self): """Unit tests for !planif command.""" # async def planif(self, ctx, quand, *, commande) planif = self.cog.planif today = datetime.date.today() today_12h = datetime.datetime.combine(today, datetime.time(12, 0)) firstmarch_12h = datetime.datetime(2021, 3, 1, 12, 0) # bad timestamp ctx = mock_discord.get_ctx(planif, "bzzt", commande="oui") with self.assertRaises(ValueError): await ctx.invoke() self.assertEqual(bdd.Tache.query.all(), []) # hh:mm after now ctx = mock_discord.get_ctx(planif, "18:10", commande="oui") with freezegun.freeze_time(today_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual( tache.timestamp, datetime.datetime.combine(today, datetime.time(18, 10))) self.assertEqual(tache.commande, "oui") tache.delete() # hh:mm:ss after now ctx = mock_discord.get_ctx(planif, "18:10:52", commande="non") with freezegun.freeze_time(today_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual( tache.timestamp, datetime.datetime.combine(today, datetime.time(18, 10, 52))) self.assertEqual(tache.commande, "non") tache.delete() # hh:mm before now, abort ctx = mock_discord.get_ctx(planif, "10:10", commande="oui") with freezegun.freeze_time(today_12h): with mock_discord.interact(("yes_no", False)): await ctx.invoke() self.assertEqual(bdd.Tache.query.all(), []) # hh:mm before now, proceed ctx = mock_discord.get_ctx(planif, "10:10", commande="oui") with freezegun.freeze_time(today_12h): with mock_discord.interact(("yes_no", True)): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual( tache.timestamp, datetime.datetime.combine(today, datetime.time(10, 10))) self.assertEqual(tache.commande, "oui") tache.delete() # jj/mm-hh:mm after now ctx = mock_discord.get_ctx(planif, "05/03-09:07", commande="non") with freezegun.freeze_time(firstmarch_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2021, 3, 5, 9, 7)) self.assertEqual(tache.commande, "non") tache.delete() # j/m-h:m after now ctx = mock_discord.get_ctx(planif, "5/3-9:7", commande="non") with freezegun.freeze_time(firstmarch_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2021, 3, 5, 9, 7)) self.assertEqual(tache.commande, "non") tache.delete() # jj/mm-hh:mm before now ctx = mock_discord.get_ctx(planif, "05/02-09:07", commande="non") with freezegun.freeze_time(firstmarch_12h): with mock_discord.interact(("yes_no", True)): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2021, 2, 5, 9, 7)) self.assertEqual(tache.commande, "non") tache.delete() # jj/mm/aaaa-hh:mm after now ctx = mock_discord.get_ctx(planif, "05/03/2023-09:07", commande="non") with freezegun.freeze_time(firstmarch_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2023, 3, 5, 9, 7)) self.assertEqual(tache.commande, "non") tache.delete() # jj/mm/aaaa-hh:mm before now ctx = mock_discord.get_ctx(planif, "05/03/2020-09:07", commande="non") with freezegun.freeze_time(firstmarch_12h): with mock_discord.interact(("yes_no", True)): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2020, 3, 5, 9, 7)) self.assertEqual(tache.commande, "non") tache.delete() # jj/mm-hh:mm after now, !open <non-existing action> ctx = mock_discord.get_ctx(planif, "05/03-09:07", commande="!open 77") with freezegun.freeze_time(firstmarch_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2021, 3, 5, 9, 7)) self.assertEqual(tache.commande, "!open 77") self.assertIsNone(tache.action) tache.delete() # jj/mm-hh:mm after now, !open <existing action> bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur1").add() bdd.BaseAction(slug="ouiz").add() action = bdd.Action(id=78, _base_slug="ouiz", _joueur_id=1) action.add() ctx = mock_discord.get_ctx(planif, "05/03-09:07", commande="!open 78") with freezegun.freeze_time(firstmarch_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2021, 3, 5, 9, 7)) self.assertEqual(tache.commande, "!open 78") self.assertEqual(tache.action, action) tache.delete() # jj/mm-hh:mm after now, !close <non-existing action> ctx = mock_discord.get_ctx(planif, "05/03-09:07", commande="!close 77") with freezegun.freeze_time(firstmarch_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2021, 3, 5, 9, 7)) self.assertEqual(tache.commande, "!close 77") self.assertIsNone(tache.action) tache.delete() # jj/mm-hh:mm after now, !close <existing action> ctx = mock_discord.get_ctx(planif, "05/03-09:07", commande="!close 78") with freezegun.freeze_time(firstmarch_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2021, 3, 5, 9, 7)) self.assertEqual(tache.commande, "!close 78") self.assertEqual(tache.action, action) tache.delete() # jj/mm-hh:mm after now, !remind <non-existing action> ctx = mock_discord.get_ctx(planif, "05/03-09:07", commande="!remind 77") with freezegun.freeze_time(firstmarch_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2021, 3, 5, 9, 7)) self.assertEqual(tache.commande, "!remind 77") self.assertIsNone(tache.action) tache.delete() # jj/mm-hh:mm after now, !remind <existing action> ctx = mock_discord.get_ctx(planif, "05/03-09:07", commande="!remind 78") with freezegun.freeze_time(firstmarch_12h): await ctx.invoke() self.assertEqual(len(bdd.Tache.query.all()), 1) tache = bdd.Tache.query.one() self.assertEqual(tache.timestamp, datetime.datetime(2021, 3, 5, 9, 7)) self.assertEqual(tache.commande, "!remind 78") self.assertEqual(tache.action, action) tache.delete()
async def test_main(self, sleep_patch, reg_patch, newchan_patch): """Unit tests for inscription.main function.""" # async def main(member) main = inscription.main mock_bdd.add_campsroles() joueur1 = bdd.Joueur(discord_id=1, chan_id_=11, nom="Joueur 1") joueur1.add() newchan_patch.return_value = mock_discord.chan("bzzt", id=798) # Joueur inscrit en base with mock_discord.mock_members_and_chans(joueur1): member = joueur1.member chan = joueur1.private_chan await main(member) chan.set_permissions.assert_called_once_with(member, read_messages=True, send_messages=True) mock_discord.assert_sent(chan, f"{member.mention} tu es déjà inscrit") reg_patch.assert_not_called() newchan_patch.assert_not_called() sleep_patch.assert_not_called() joueur1.delete() # Joueur en cours d'inscription -> abort chan = mock_discord.chan("bzz", topic="123456") config.guild.text_channels = [chan] member = mock.Mock(discord.Member, id=123456) with mock_discord.interact(("yes_no", False)): await main(member) chan.set_permissions.assert_called_once_with(member, read_messages=True, send_messages=True) mock_discord.assert_sent(chan, f"{member.mention}, par ici", f"Bienvenue {member.mention}", f"finalisons ton inscription", f"C'est bon pour toi ?", "Pas de soucis") reg_patch.assert_not_called() newchan_patch.assert_not_called() sleep_patch.assert_called() sleep_patch.reset_mock() # Joueur arrivant -> abort member = mock.Mock(discord.Member, id=145) with mock_discord.interact(("yes_no", False)): await main(member) newchan_patch.assert_called_once_with(member) chan = newchan_patch.return_value mock_discord.assert_sent(chan, f"Bienvenue {member.mention}", f"finalisons ton inscription", f"C'est bon pour toi ?", "Pas de soucis") reg_patch.assert_not_called() newchan_patch.reset_mock() sleep_patch.assert_called() sleep_patch.reset_mock() # Joueur arrivant -> ok -> name -> troll -> good name # demande_chambre False config.demande_chambre = False member = mock.Mock(discord.Member, id=145) member.top_role.__lt__ = lambda s, o: True # role < MJ member.display_name = "Pr-Z N0 N" with mock_discord.interact( ("yes_no", True), ("wait_for_message", mock.NonCallableMock(discord.Message, content="préz")), ("wait_for_message", mock.NonCallableMock(discord.Message, content="no n")), ("yes_no", False), ("wait_for_message", mock.NonCallableMock(discord.Message, content="pr-z")), ("wait_for_message", mock.NonCallableMock(discord.Message, content="n0 n")), ("yes_no", True), ): await main(member) newchan_patch.assert_called_once_with(member) chan = newchan_patch.return_value mock_discord.assert_sent( chan, f"Bienvenue {member.mention}", "finalisons ton inscription", f"C'est bon pour toi ?", ["Parfait", "prénom"], "prénom", "nom de famille", "Préz No N", "prénom", "nom de famille", "Pr-Z N0 N", "Je t'ai renommé", "Je t'inscris", "Tu es maintenant inscrit", "quelques dernières choses", "c'est tout bon") chan.edit.assert_any_call( name=f"{config.private_chan_prefix}Pr-Z N0 N") member.edit.assert_called_once_with(nick="Pr-Z N0 N") member.edit.reset_mock() self.assertEqual(len(bdd.Joueur.query.all()), 1) jr = bdd.Joueur.query.one() self.assertEqual([ jr.discord_id, jr.chan_id_, jr.nom, jr.chambre, jr.statut, jr.role, jr.camp, jr.votant_village, jr.votant_loups, jr.role_actif ], [ 145, 798, "Pr-Z N0 N", None, bdd.Statut.vivant, bdd.Role.default(), bdd.Camp.default(), True, False, False ]) member.add_roles.assert_called_once_with(config.Role.joueur_en_vie) chan.edit.assert_any_call(topic=mock.ANY) reg_patch.assert_called_once_with(jr) reg_patch.reset_mock() newchan_patch.reset_mock() sleep_patch.assert_called() sleep_patch.reset_mock() jr.delete() # Joueur arrivant -> ok -> name -> chambre -> pas à la Rez config.demande_chambre = True member = mock.Mock(discord.Member, id=145) member.top_role.__lt__ = lambda s, o: True # role < MJ member.display_name = "Pr-Z N0 N" with mock_discord.interact( ("yes_no", True), ("wait_for_message", mock.NonCallableMock(discord.Message, content="pr-z")), ("wait_for_message", mock.NonCallableMock(discord.Message, content="n0 n")), ("yes_no", True), ("yes_no", False), ): await main(member) newchan_patch.assert_called_once_with(member) chan = newchan_patch.return_value mock_discord.assert_sent( chan, f"Bienvenue {member.mention}", "finalisons ton inscription", f"C'est bon pour toi ?", ["Parfait", "prénom"], "prénom", "nom de famille", "Pr-Z N0 N", "Je t'ai renommé", "habites-tu à la Rez", "Je t'inscris", "Tu es maintenant inscrit", "quelques dernières choses", "c'est tout bon") chan.edit.assert_any_call( name=f"{config.private_chan_prefix}Pr-Z N0 N") member.edit.assert_called_once_with(nick="Pr-Z N0 N") member.edit.reset_mock() self.assertEqual(len(bdd.Joueur.query.all()), 1) jr = bdd.Joueur.query.one() self.assertEqual([ jr.discord_id, jr.chan_id_, jr.nom, jr.chambre, jr.statut, jr.role, jr.camp, jr.votant_village, jr.votant_loups, jr.role_actif ], [ 145, 798, "Pr-Z N0 N", config.chambre_mj, bdd.Statut.vivant, bdd.Role.default(), bdd.Camp.default(), True, False, False ]) member.add_roles.assert_called_once_with(config.Role.joueur_en_vie) chan.edit.assert_any_call(topic=mock.ANY) reg_patch.assert_called_once_with(jr) reg_patch.reset_mock() newchan_patch.reset_mock() sleep_patch.assert_called() sleep_patch.reset_mock() jr.delete() # Joueur arrivant -> ok -> name -> chambre -> à la Rez config.demande_chambre = True member = mock.Mock(discord.Member, id=145) member.top_role.__lt__ = lambda s, o: True # role < MJ member.display_name = "Pr-Z N0 N" with mock_discord.interact( ("yes_no", True), ("wait_for_message", mock.NonCallableMock(discord.Message, content="pr-z")), ("wait_for_message", mock.NonCallableMock(discord.Message, content="n0 n")), ("yes_no", True), ("yes_no", True), ("wait_for_message", mock.NonCallableMock(discord.Message, content="214")), ): await main(member) newchan_patch.assert_called_once_with(member) chan = newchan_patch.return_value mock_discord.assert_sent( chan, f"Bienvenue {member.mention}", "finalisons ton inscription", f"C'est bon pour toi ?", ["Parfait", "prénom"], "prénom", "nom de famille", "Pr-Z N0 N", "Je t'ai renommé", "habites-tu à la Rez", "quelle est ta chambre", "Je t'inscris", "Tu es maintenant inscrit", "quelques dernières choses", "c'est tout bon") chan.edit.assert_any_call( name=f"{config.private_chan_prefix}Pr-Z N0 N") member.edit.assert_called_once_with(nick="Pr-Z N0 N") member.edit.reset_mock() self.assertEqual(len(bdd.Joueur.query.all()), 1) jr = bdd.Joueur.query.one() self.assertEqual([ jr.discord_id, jr.chan_id_, jr.nom, jr.chambre, jr.statut, jr.role, jr.camp, jr.votant_village, jr.votant_loups, jr.role_actif ], [ 145, 798, "Pr-Z N0 N", "214", bdd.Statut.vivant, bdd.Role.default(), bdd.Camp.default(), True, False, False ]) member.add_roles.assert_called_once_with(config.Role.joueur_en_vie) chan.edit.assert_any_call(topic=mock.ANY) reg_patch.assert_called_once_with(jr) reg_patch.reset_mock() newchan_patch.reset_mock() sleep_patch.assert_called() sleep_patch.reset_mock() jr.delete()