async def watch(self, ctx: commands.Context, is_on: str = "on"): is_on = is_on.lower() author_id = ctx.author.id author_display_name = ctx.author.display_name if is_on == "on": if author_id not in self.user_ids: try: with Session() as session: fword_user.save(session, author_id) session.commit() # discord_id가 이미 있을 때만 무시함. except IntegrityError as e: if "UNIQUE constraint failed: fword_user.discord_id" not in repr( e): raise self.user_ids.add(author_id) await ctx.send(f"`{author_display_name}`에 대한 비속어 탐지 켜짐") elif is_on == "off": if author_id in self.user_ids: with Session() as session: user = fword_user.find_by_discord_id(session, author_id) if user is not None: session.delete(user) session.commit() self.user_ids.discard(author_id) await ctx.send(f"`{author_display_name}`에 대한 비속어 탐지 꺼짐")
def test_delete(Session): discord_id = 123 with Session() as session: # !fword watch off 명령어에서 discord_id를 쿼리 후 삭제하는 걸 테스트하기 위해 save()의 반환값을 쓰지않음. fword_user.save(session, discord_id) session.commit() user = fword_user.find_by_discord_id(session, discord_id) session.delete(user) session.commit() assert fword_user.find_by_discord_id(session, discord_id) is None
def test_save_duplicated_discord_id(Session): discord_id = 123456 with pytest.raises( IntegrityError, match="UNIQUE constraint failed: fword_user.discord_id", ): with Session() as session: fword_user.save(session, discord_id) fword_user.save(session, discord_id) session.commit()
def test_id_autoincrement(Session): with Session() as session: fword_user.save(session, 123) fword_user.save(session, 456) fword_user.save(session, 789) user_list = fword_user.find_all(session) for i, user in enumerate(user_list, start=1): assert i == user.id
def test_save(Session): discord_id = 123456 with Session() as session: fword_user.save(session, discord_id) result = fword_user.find_by_discord_id(session, discord_id) assert result.discord_id == discord_id