0
0
mirror of https://gitflic.ru/project/maks1ms/ocab.git synced 2025-04-04 08:33:47 +03:00
ocab/src/modules/standard/database/tests/test_db.py

94 lines
3.4 KiB
Python

import unittest
import os
from ..db_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()