mirror of
https://gitflic.ru/project/alt-gnome/karkas.git
synced 2025-09-26 18:29:06 +03:00
Исправлены импорты внутри модулей, добавлены: модуль для исключений, скрипт инициализации путей, переделано подключение к бд
This commit is contained in:
1
src/modules/standard/database/__init__.py
Normal file
1
src/modules/standard/database/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import api, models
|
109
src/modules/standard/database/api.py
Normal file
109
src/modules/standard/database/api.py
Normal file
@@ -0,0 +1,109 @@
|
||||
from .models.chats import Chats
|
||||
from .models.messages import Messages
|
||||
from .models.users import Users
|
||||
from service import paths
|
||||
from ..exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
|
||||
import peewee as pw
|
||||
|
||||
def connect_database(is_test: bool = False, module: str | None = None):
|
||||
if is_test:
|
||||
if not module:
|
||||
raise MissingModuleName()
|
||||
db_path = f"{paths.modules_standard}/{module}/tests/database"
|
||||
else:
|
||||
if module:
|
||||
raise NotExpectedModuleName()
|
||||
db_path = f"{paths.core}/database"
|
||||
|
||||
_database = pw.SqliteDatabase(f"{db_path}/OCAB.db")
|
||||
Chats._meta.database = _database
|
||||
Messages._meta.database = _database
|
||||
Users._meta.database = _database
|
||||
|
||||
return _database, f"{db_path}/OCAB.db"
|
||||
|
||||
def create_tables(db: pw.SqliteDatabase):
|
||||
"""Создание таблиц"""
|
||||
for table in Chats, Messages, Users:
|
||||
if not table.table_exists():
|
||||
db.create_tables([table])
|
||||
|
||||
|
||||
def add_chat(chat_id, chat_role, chat_stats=0, chat_federation=0):
|
||||
chat, created = Chats.get_or_create(id=chat_id, defaults={
|
||||
'chat_role': chat_role,
|
||||
'chat_stats': chat_stats,
|
||||
'chat_federation': chat_federation
|
||||
})
|
||||
if not created:
|
||||
# Обновить существующий чат, если он уже существует
|
||||
chat.chat_role = chat_role
|
||||
chat.chat_stats = chat_stats
|
||||
chat.chat_federation = chat_federation
|
||||
chat.save()
|
||||
|
||||
|
||||
def add_user(user_id, user_name, user_tag=None, user_role=0, user_stats=0, user_rep=0):
|
||||
user, created = Users.get_or_create(id=user_id, defaults={
|
||||
'user_name': user_name,
|
||||
'user_tag': user_tag,
|
||||
'user_role': user_role,
|
||||
'user_stats': user_stats,
|
||||
'user_rep': user_rep
|
||||
})
|
||||
if not created:
|
||||
# Обновить существующего пользователя, если он уже существует
|
||||
user.user_name = user_name
|
||||
user.user_tag = user_tag
|
||||
user.user_role = user_role
|
||||
user.user_stats = user_stats
|
||||
user.user_rep = user_rep
|
||||
user.save()
|
||||
|
||||
|
||||
def add_message(message_id, message_text, message_sender, answer_id):
|
||||
Messages.create(
|
||||
id=message_id,
|
||||
message_text=message_text,
|
||||
message_id_sender=message_sender,
|
||||
answer_to_id=answer_id
|
||||
)
|
||||
|
||||
|
||||
def get_chat(chat_id):
|
||||
return Chats.get_or_none(Chats.id == chat_id)
|
||||
|
||||
|
||||
def get_user(user_id):
|
||||
return Users.get_or_none(Users.id == user_id)
|
||||
|
||||
|
||||
def get_user_role(user_id):
|
||||
user = Users.get_or_none(Users.id == user_id)
|
||||
return user.user_role if user else None
|
||||
|
||||
|
||||
def get_message(message_id):
|
||||
return Messages.get_or_none(Messages.id == message_id)
|
||||
|
||||
|
||||
def change_user_name(user_id, new_user_name):
|
||||
query = Users.update(user_name=new_user_name).where(Users.id == user_id)
|
||||
query.execute()
|
||||
|
||||
|
||||
# Аналогично для других функций обновления...
|
||||
|
||||
def update_user_stats():
|
||||
query = Users.update(user_stats=Users.user_stats + 1)
|
||||
query.execute()
|
||||
|
||||
|
||||
def update_user_rep():
|
||||
query = Users.update(user_rep=Users.user_rep + 1)
|
||||
query.execute()
|
||||
|
||||
|
||||
def update_chat_stats():
|
||||
query = Chats.update(chat_stats=Chats.chat_stats + 1)
|
||||
query.execute()
|
6
src/modules/standard/database/info.json
Normal file
6
src/modules/standard/database/info.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "Database",
|
||||
"description": "Модуль для работы с БД",
|
||||
"author": "OCAB Team",
|
||||
"version": "1.0"
|
||||
}
|
1
src/modules/standard/database/models/__init__.py
Normal file
1
src/modules/standard/database/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
9
src/modules/standard/database/models/chats.py
Normal file
9
src/modules/standard/database/models/chats.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import peewee as pw
|
||||
|
||||
|
||||
class Chats(pw.Model):
|
||||
class Meta:
|
||||
...
|
||||
chat_role = pw.IntegerField(null=False, default=1)
|
||||
chat_stats = pw.IntegerField(null=False)
|
||||
chat_federation = pw.IntegerField(null=False)
|
9
src/modules/standard/database/models/messages.py
Normal file
9
src/modules/standard/database/models/messages.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import peewee as pw
|
||||
|
||||
|
||||
class Messages(pw.Model):
|
||||
class Meta:
|
||||
...
|
||||
message_text = pw.TextField(null=False)
|
||||
message_id_sender = pw.IntegerField(null=False)
|
||||
answer_to_id = pw.IntegerField(null=True)
|
11
src/modules/standard/database/models/users.py
Normal file
11
src/modules/standard/database/models/users.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import peewee as pw
|
||||
|
||||
|
||||
class Users(pw.Model):
|
||||
class Meta:
|
||||
...
|
||||
user_name = pw.CharField(null=False) # до 255 символов
|
||||
user_tag = pw.CharField(null=True)
|
||||
user_role = pw.IntegerField(null=True)
|
||||
user_stats = pw.IntegerField(null=True)
|
||||
user_rep = pw.IntegerField(null=True)
|
0
src/modules/standard/database/tests/__init__.py
Normal file
0
src/modules/standard/database/tests/__init__.py
Normal file
1
src/modules/standard/database/tests/database/file
Normal file
1
src/modules/standard/database/tests/database/file
Normal file
@@ -0,0 +1 @@
|
||||
Эта директория для тестовой БД
|
93
src/modules/standard/database/tests/test_db.py
Normal file
93
src/modules/standard/database/tests/test_db.py
Normal file
@@ -0,0 +1,93 @@
|
||||
import unittest
|
||||
import os
|
||||
|
||||
from ..api import *
|
||||
from ...exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
|
||||
|
||||
|
||||
class TestDatabaseAPI(unittest.TestCase):
|
||||
database = None
|
||||
path = None
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.database, cls.path = connect_database(is_test=True, module="database")
|
||||
create_tables(cls.database)
|
||||
def test_fail_connect(cls):
|
||||
with cls.assertRaises(MissingModuleName):
|
||||
cls.database, cls.path = connect_database(is_test=True)
|
||||
|
||||
with cls.assertRaises(NotExpectedModuleName):
|
||||
cls.database, cls.path = connect_database(module="database")
|
||||
|
||||
def test_add_and_get_chat(self):
|
||||
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 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="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=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=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")
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.database.close()
|
||||
os.system(f"rm {cls.path}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in New Issue
Block a user