From 8378625694db9340a89c2b234b14695c5de8028c Mon Sep 17 00:00:00 2001 From: ilyazheprog Date: Sun, 26 Nov 2023 20:23:39 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9=D1=81=20=D0=B2?= =?UTF-8?q?=D0=B7=D0=B0=D0=B8=D0=BC=D0=BE=D0=B4=D0=B5=D0=B9=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=B8=D1=8F=20=D1=81=20=D0=91=D0=94=20=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BA=D1=80=D1=8B=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/__init__.py | 0 src/core/__init__.py | 0 src/core/database/__init__.py | 3 + src/core/database/api.py | 92 ++++++++++++++++++++++++++++ src/core/database/models/__init__.py | 1 + src/core/database/models/base.py | 8 +++ src/core/database/models/chats.py | 9 +++ src/core/database/models/messages.py | 9 +++ src/core/database/models/users.py | 11 ++++ src/modules/__init__.py | 1 + tests/__init__.py | 0 tests/database/file | 1 + tests/test_db.py | 65 ++++++++++++++++++++ 14 files changed, 201 insertions(+) create mode 100644 src/__init__.py create mode 100644 src/core/__init__.py create mode 100644 src/core/database/__init__.py create mode 100644 src/core/database/api.py create mode 100644 src/core/database/models/__init__.py create mode 100644 src/core/database/models/base.py create mode 100644 src/core/database/models/chats.py create mode 100644 src/core/database/models/messages.py create mode 100644 src/core/database/models/users.py create mode 100644 src/modules/__init__.py create mode 100644 tests/__init__.py create mode 100644 tests/database/file create mode 100644 tests/test_db.py diff --git a/.gitignore b/.gitignore index bb23e66..7cb22bc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ env venv __pycache__ +OCAB.db \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/core/__init__.py b/src/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/core/database/__init__.py b/src/core/database/__init__.py new file mode 100644 index 0000000..7dc6621 --- /dev/null +++ b/src/core/database/__init__.py @@ -0,0 +1,3 @@ +from .api import * +from .models import * +from .models.base import database diff --git a/src/core/database/api.py b/src/core/database/api.py new file mode 100644 index 0000000..1ba63e3 --- /dev/null +++ b/src/core/database/api.py @@ -0,0 +1,92 @@ +from src.core.database.models.chats import Chats +from src.core.database.models.messages import Messages +from src.core.database.models.users import Users + +import peewee as pw + + +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() diff --git a/src/core/database/models/__init__.py b/src/core/database/models/__init__.py new file mode 100644 index 0000000..7a14cda --- /dev/null +++ b/src/core/database/models/__init__.py @@ -0,0 +1 @@ +from .base import BaseModel diff --git a/src/core/database/models/base.py b/src/core/database/models/base.py new file mode 100644 index 0000000..d0e4e40 --- /dev/null +++ b/src/core/database/models/base.py @@ -0,0 +1,8 @@ +import peewee as pw + +database = pw.SqliteDatabase("database/OCAB.db") + + +class BaseModel(pw.Model): + class Meta: + database = database diff --git a/src/core/database/models/chats.py b/src/core/database/models/chats.py new file mode 100644 index 0000000..2d8da3b --- /dev/null +++ b/src/core/database/models/chats.py @@ -0,0 +1,9 @@ +from .base import BaseModel + +import peewee as pw + + +class Chats(BaseModel): + chat_role = pw.IntegerField(null=False) + chat_stats = pw.IntegerField(null=False) + chat_federation = pw.IntegerField(null=False) diff --git a/src/core/database/models/messages.py b/src/core/database/models/messages.py new file mode 100644 index 0000000..da18879 --- /dev/null +++ b/src/core/database/models/messages.py @@ -0,0 +1,9 @@ +from .base import BaseModel + +import peewee as pw + + +class Messages(BaseModel): + message_text = pw.TextField(null=False) + message_id_sender = pw.IntegerField(null=False) + answer_to_id = pw.IntegerField(null=True) diff --git a/src/core/database/models/users.py b/src/core/database/models/users.py new file mode 100644 index 0000000..802d73e --- /dev/null +++ b/src/core/database/models/users.py @@ -0,0 +1,11 @@ +from .base import BaseModel + +import peewee as pw + + +class Users(BaseModel): + 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) diff --git a/src/modules/__init__.py b/src/modules/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/modules/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/database/file b/tests/database/file new file mode 100644 index 0000000..ef0b553 --- /dev/null +++ b/tests/database/file @@ -0,0 +1 @@ +Эта директория для тестовой БД \ No newline at end of file diff --git a/tests/test_db.py b/tests/test_db.py new file mode 100644 index 0000000..1969dbb --- /dev/null +++ b/tests/test_db.py @@ -0,0 +1,65 @@ +import unittest + +from src.core.database.api import * +from src.core.database.models.base import database + +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) + + 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") + + 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") + + 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) + + 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") + + # Дополнительные тесты для других функций обновления... + + 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()