0
0
mirror of https://gitflic.ru/project/maks1ms/ocab.git synced 2024-12-23 16:23:01 +03:00

добавлены модули config, role, исправлена бд

This commit is contained in:
ilyazheprog 2023-12-07 18:02:04 +07:00
parent bc5de691a7
commit 88db66c360
15 changed files with 201 additions and 45 deletions

View File

@ -1,2 +1,7 @@
TELEGRAM:
TOKEN: xxxxxxxxxxxxxxxxxxxx
TOKEN: xxxxxxxxxxxxxxxxxxxx
ROLES:
ADMIN: 0
MODERATOR: 1
USER: 2
BOT: 3

View File

@ -1,4 +1,4 @@
from .config import yaml_load
from src.modules.standart.config.config import yaml_load
from asyncio import run
from aiogram import Bot, Dispatcher

View File

View File

@ -1,4 +1,5 @@
import yaml
with open('config.yaml', 'r') as file:
yaml_load = yaml.full_load(file)

View File

@ -0,0 +1,6 @@
{
"name": "Config YAML",
"description": "Модуль для работы с конфигурационным файлом бота (YAML)",
"author": "OCAB Team",
"version": "1.0"
}

View File

@ -0,0 +1,7 @@
TELEGRAM:
TOKEN: xxxxxxxxxxxxxxxxxxxx
ROLES:
ADMIN: 0
MODERATOR: 1
USER: 2
BOT: 3

View File

@ -0,0 +1,35 @@
from src.modules.standart.config.config import yaml_load
import unittest
class TestConfig(unittest.TestCase):
def test_yaml_load_correctness(self):
self.assertIsNotNone(yaml_load)
self.assertIn("TELEGRAM", yaml_load)
self.assertIn("TOKEN", yaml_load["TELEGRAM"])
self.assertIn("ROLES", yaml_load)
self.assertIn("ADMIN", yaml_load["ROLES"])
self.assertIn("MODERATOR", yaml_load["ROLES"])
self.assertIn("USER", yaml_load["ROLES"])
self.assertIn("BOT", yaml_load["ROLES"])
def test_yaml_keys_existence(self):
self.assertTrue(all(key in yaml_load for key in ["TELEGRAM", "ROLES"]))
self.assertIn("TOKEN", yaml_load["TELEGRAM"])
self.assertTrue(all(role in yaml_load["ROLES"] for role in ["ADMIN", "MODERATOR", "USER"]))
def test_yaml_yaml_load_types(self):
self.assertIsInstance(yaml_load["TELEGRAM"]["TOKEN"], str)
self.assertTrue(all(isinstance(yaml_load["ROLES"][role], int) for role in ["ADMIN", "MODERATOR", "USER"]))
def test_yaml_values(self):
expected_token = 'xxxxxxxxxxxxxxxxxxxx'
expected_role_values = {'ADMIN': 0, 'MODERATOR': 1, 'USER': 2, 'BOT': 3}
self.assertEqual(yaml_load["TELEGRAM"]["TOKEN"], expected_token)
for role, value in expected_role_values.items():
self.assertEqual(yaml_load["ROLES"][role], value)
if __name__ == '__main__':
unittest.main()

View File

@ -4,6 +4,6 @@ import peewee as pw
class Chats(BaseModel):
chat_role = pw.IntegerField(null=False)
chat_role = pw.IntegerField(null=False, default=1)
chat_stats = pw.IntegerField(null=False)
chat_federation = pw.IntegerField(null=False)

View File

@ -7,59 +7,70 @@ create_tables(database)
class TestDatabaseAPI(unittest.TestCase):
def test_add_and_get_chat(self):
add_chat(chat_id=21, chat_role=1, chat_stats=0, chat_federation=0)
chat = get_chat(21)
self.assertIsNotNone(chat)
self.assertEqual(chat.id, 21)
self.assertEqual(chat.chat_role, 1)
add_chat(chat_id=21, chat_role=0, chat_stats=0, chat_federation=0)
add_chat(chat_id=22, chat_role=1, chat_stats=100, chat_federation=1)
chat1 = get_chat(21)
self.assertIsNotNone(chat1)
self.assertEqual(chat1.id, 21)
self.assertEqual(chat1.chat_role, 0)
chat2 = get_chat(22)
self.assertIsNotNone(chat2)
self.assertEqual(chat2.id, 22)
self.assertEqual(chat2.chat_role, 1)
def test_add_and_get_message(self):
add_message(message_id=1, message_text="Test Message", message_sender=1, answer_id=2)
message = get_message(1)
self.assertIsNotNone(message)
self.assertEqual(message.id, 1)
self.assertEqual(message.message_text, "Test Message")
add_message(message_id=1, message_text="Test Message 1", message_sender=1, answer_id=2)
add_message(message_id=2, message_text="Test Message 2", message_sender=2, answer_id=1)
message1 = get_message(1)
self.assertIsNotNone(message1)
self.assertEqual(message1.id, 1)
self.assertEqual(message1.message_text, "Test Message 1")
message2 = get_message(2)
self.assertIsNotNone(message2)
self.assertEqual(message2.id, 2)
self.assertEqual(message2.message_text, "Test Message 2")
def test_add_and_get_user(self):
add_user(user_id=100, user_name="TestUser", user_tag="TestTag", user_role=2, user_stats=10, user_rep=5)
user = get_user(100)
self.assertIsNotNone(user)
self.assertEqual(user.id, 100)
self.assertEqual(user.user_name, "TestUser")
add_user(user_id=100, user_name="TestUser1", user_tag="TestTag1", user_role=0, user_stats=10, user_rep=5)
add_user(user_id=101, user_name="TestUser2", user_tag="TestTag2", user_role=1, user_stats=20, user_rep=10)
user1 = get_user(100)
self.assertIsNotNone(user1)
self.assertEqual(user1.id, 100)
self.assertEqual(user1.user_name, "TestUser1")
user2 = get_user(101)
self.assertIsNotNone(user2)
self.assertEqual(user2.id, 101)
self.assertEqual(user2.user_name, "TestUser2")
def test_get_user_role(self):
add_user(user_id=101, user_name="TestUser2", user_tag="TestTag2", user_role=3, user_stats=20, user_rep=10)
user_role = get_user_role(101)
self.assertEqual(user_role, 3)
add_user(user_id=102, user_name="TestUser3", user_tag="TestTag3", user_role=0, user_stats=30, user_rep=15)
add_user(user_id=103, user_name="TestUser4", user_tag="TestTag4", user_role=1, user_stats=40, user_rep=20)
user_role1 = get_user_role(102)
self.assertEqual(user_role1, 0)
user_role2 = get_user_role(103)
self.assertEqual(user_role2, 1)
def test_change_user_name(self):
add_user(user_id=102, user_name="OldName", user_tag="TestTag3", user_role=4, user_stats=30, user_rep=15)
change_user_name(102, "NewName")
updated_user = get_user(102)
self.assertEqual(updated_user.user_name, "NewName")
add_user(user_id=104, user_name="OldName1", user_tag="TestTag5", user_role=0, user_stats=50, user_rep=25)
change_user_name(104, "NewName1")
updated_user1 = get_user(104)
self.assertEqual(updated_user1.user_name, "NewName1")
# Дополнительные тесты для других функций обновления...
add_user(user_id=105, user_name="OldName2", user_tag="TestTag6", user_role=1, user_stats=60, user_rep=30)
change_user_name(105, "NewName2")
updated_user2 = get_user(105)
self.assertEqual(updated_user2.user_name, "NewName2")
def test_update_user_stats(self):
add_user(user_id=103, user_name="UserStats", user_tag="TestTag4", user_role=5, user_stats=40, user_rep=20)
update_user_stats()
updated_user = get_user(103)
self.assertEqual(updated_user.user_stats, 41)
def test_update_user_rep(self):
add_user(user_id=104, user_name="UserRep", user_tag="TestTag5", user_role=6, user_stats=50, user_rep=25)
update_user_rep()
updated_user = get_user(104)
self.assertEqual(updated_user.user_rep, 26)
def test_update_chat_stats(self):
add_chat(chat_id=22, chat_role=2, chat_stats=100, chat_federation=0)
update_chat_stats()
updated_chat = get_chat(22)
self.assertEqual(updated_chat.chat_stats, 101)
# Дополнительные тесты для других функций...
if __name__ == '__main__':
unittest.main()

View File

View File

@ -0,0 +1,43 @@
from src.modules.standart.database import get_user_role
from src.modules.standart.config.config import yaml_load
class Roles:
user = "USER"
moderator = "MODERATOR"
admin = "ADMIN"
bot = "BOT"
__roles = yaml_load["ROLES"]
def __init__(self):
self.user_role_id = self.__roles[self.user]
self.moderator_role_id = self.__roles[self.moderator]
self.admin_role_id = self.__roles[self.admin]
self.bot_role_id = self.__roles[self.bot]
async def check_admin_permission(self, user_id):
match get_user_role(user_id):
case self.admin_role_id:
return True
case _:
return False
async def check_moderator_permission(self, user_id):
match get_user_role(user_id):
case self.moderator_role_id:
return True
case _:
return False
async def get_role_name(self, role_id):
match role_id:
case self.admin_role_id:
return self.admin
case self.moderator_role_id:
return self.moderator
case self.user_role_id:
return self.user
case self.bot_role_id:
return self.bot
case _:
raise ValueError(f"Нет роли с id={role_id}")

View File

@ -0,0 +1,6 @@
{
"name": "Roles",
"description": "Модуль для работы с ролями",
"author": "OCAB Team",
"version": "1.0"
}

View File

@ -0,0 +1,7 @@
TELEGRAM:
TOKEN: xxxxxxxxxxxxxxxxxxxx
ROLES:
ADMIN: 0
MODERATOR: 1
USER: 2
BOT: 3

View File

@ -0,0 +1,35 @@
import unittest
from src.modules.standart.database.api import create_tables, add_user
from src.modules.standart.database.models.base import database
from src.modules.standart.roles.api import Roles
import asyncio
create_tables(database)
class TestRoles(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.roles = Roles()
async def test_check_admin_permission(self):
self.assertTrue(await self.roles.check_admin_permission(1))
self.assertFalse(await self.roles.check_admin_permission(2))
self.assertFalse(await self.roles.check_admin_permission(3))
self.assertFalse(await self.roles.check_admin_permission(4))
async def test_check_moderator_permission(self):
self.assertTrue(await self.roles.check_moderator_permission(2))
self.assertFalse(await self.roles.check_moderator_permission(0))
self.assertFalse(await self.roles.check_moderator_permission(1))
self.assertFalse(await self.roles.check_moderator_permission(3))
async def test_get_role_name(self):
self.assertEqual(await self.roles.get_role_name(self.roles.admin_role_id), "ADMIN")
self.assertEqual(await self.roles.get_role_name(self.roles.moderator_role_id), "MODERATOR")
self.assertEqual(await self.roles.get_role_name(self.roles.user_role_id), "USER")
self.assertEqual(await self.roles.get_role_name(self.roles.bot_role_id), "BOT")
with self.assertRaises(ValueError):
await self.roles.get_role_name(999) # Несуществующий ID роли
if __name__ == '__main__':
unittest.main()