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

Написал интерфейс взаимодействия с БД и покрыл тестами

This commit is contained in:
ilyazheprog 2023-11-26 20:23:39 +07:00
parent 9dde0530d0
commit 8378625694
14 changed files with 201 additions and 0 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
env
venv
__pycache__
OCAB.db

0
src/__init__.py Normal file
View File

0
src/core/__init__.py Normal file
View File

View File

@ -0,0 +1,3 @@
from .api import *
from .models import *
from .models.base import database

92
src/core/database/api.py Normal file
View File

@ -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()

View File

@ -0,0 +1 @@
from .base import BaseModel

View File

@ -0,0 +1,8 @@
import peewee as pw
database = pw.SqliteDatabase("database/OCAB.db")
class BaseModel(pw.Model):
class Meta:
database = database

View File

@ -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)

View File

@ -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)

View File

@ -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)

1
src/modules/__init__.py Normal file
View File

@ -0,0 +1 @@

0
tests/__init__.py Normal file
View File

1
tests/database/file Normal file
View File

@ -0,0 +1 @@
Эта директория для тестовой БД

65
tests/test_db.py Normal file
View File

@ -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()