Files
core/tests/test_orm_coverage.py

421 lines
15 KiB
Python
Raw Normal View History

2025-07-31 18:55:59 +03:00
"""
Тесты для покрытия модуля orm
"""
import pytest
from unittest.mock import Mock, patch, MagicMock
from datetime import datetime
from sqlalchemy import inspect
# Импортируем модули orm для покрытия
import orm.base
import orm.community
import orm.shout
import orm.reaction
import orm.collection
import orm.draft
import orm.topic
import orm.invite
import orm.notification
class TestOrmBase:
"""Тесты для orm.base"""
def test_base_import(self):
"""Тест импорта base"""
from orm.base import BaseModel, REGISTRY, FILTERED_FIELDS
assert BaseModel is not None
assert isinstance(REGISTRY, dict)
assert isinstance(FILTERED_FIELDS, list)
def test_base_model_attributes(self):
"""Тест атрибутов BaseModel"""
from orm.base import BaseModel
assert hasattr(BaseModel, 'dict')
assert hasattr(BaseModel, 'update')
# BaseModel не является абстрактным, но используется как базовый класс
assert hasattr(BaseModel, '__init_subclass__')
def test_base_model_dict_method(self):
"""Тест метода dict"""
from orm.base import BaseModel
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import mapped_column, Mapped
# Создаем мок объекта с правильной структурой
class MockModel(BaseModel):
__tablename__ = 'mock_model'
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String)
def __init__(self, id=1, name="test"):
self.id = id
self.name = name
# Создаем экземпляр мок-модели
mock_obj = MockModel()
# Вызываем метод dict
result = mock_obj.dict()
# Проверяем, что результат - словарь
assert isinstance(result, dict)
# Проверяем, что словарь содержит ожидаемые ключи
assert 'id' in result
assert 'name' in result
assert result['id'] == 1
assert result['name'] == "test"
class TestOrmCommunity:
"""Тесты для orm.community"""
def test_community_import(self):
"""Тест импорта community"""
from orm.community import Community, CommunityFollower, CommunityAuthor
assert Community is not None
assert CommunityFollower is not None
assert CommunityAuthor is not None
def test_community_attributes(self):
"""Тест атрибутов Community"""
from orm.community import Community
assert hasattr(Community, 'name')
assert hasattr(Community, 'slug')
assert hasattr(Community, 'desc')
assert hasattr(Community, 'pic')
assert hasattr(Community, 'created_at')
assert hasattr(Community, 'created_by')
assert hasattr(Community, 'settings')
assert hasattr(Community, 'updated_at')
assert hasattr(Community, 'deleted_at')
assert hasattr(Community, 'private')
def test_community_follower_attributes(self):
"""Тест атрибутов CommunityFollower"""
from orm.community import CommunityFollower
assert hasattr(CommunityFollower, 'community')
assert hasattr(CommunityFollower, 'follower')
assert hasattr(CommunityFollower, 'created_at')
def test_community_author_attributes(self):
"""Тест атрибутов CommunityAuthor"""
from orm.community import CommunityAuthor
assert hasattr(CommunityAuthor, 'community_id')
assert hasattr(CommunityAuthor, 'author_id')
assert hasattr(CommunityAuthor, 'roles')
assert hasattr(CommunityAuthor, 'joined_at')
def test_community_methods(self):
"""Тест методов Community"""
from orm.community import Community
assert hasattr(Community, 'is_followed_by')
assert hasattr(Community, 'get_user_roles')
assert hasattr(Community, 'has_user_role')
assert hasattr(Community, 'add_user_role')
assert hasattr(Community, 'remove_user_role')
assert hasattr(Community, 'set_user_roles')
assert hasattr(Community, 'get_community_members')
assert hasattr(Community, 'assign_default_roles_to_user')
assert hasattr(Community, 'get_default_roles')
assert hasattr(Community, 'set_default_roles')
assert hasattr(Community, 'initialize_role_permissions')
assert hasattr(Community, 'get_available_roles')
assert hasattr(Community, 'set_available_roles')
assert hasattr(Community, 'set_slug')
assert hasattr(Community, 'get_followers')
assert hasattr(Community, 'add_community_creator')
def test_community_author_methods(self):
"""Тест методов CommunityAuthor"""
from orm.community import CommunityAuthor
assert hasattr(CommunityAuthor, 'role_list')
assert hasattr(CommunityAuthor, 'add_role')
assert hasattr(CommunityAuthor, 'remove_role')
assert hasattr(CommunityAuthor, 'has_role')
assert hasattr(CommunityAuthor, 'set_roles')
assert hasattr(CommunityAuthor, 'get_permissions')
assert hasattr(CommunityAuthor, 'has_permission')
def test_community_functions(self):
"""Тест функций community"""
from orm.community import (
get_user_roles_in_community,
check_user_permission_in_community,
assign_role_to_user,
remove_role_from_user,
migrate_old_roles_to_community_author,
get_all_community_members_with_roles,
bulk_assign_roles
)
assert all([
get_user_roles_in_community,
check_user_permission_in_community,
assign_role_to_user,
remove_role_from_user,
migrate_old_roles_to_community_author,
get_all_community_members_with_roles,
bulk_assign_roles
])
class TestOrmShout:
"""Тесты для orm.shout"""
def test_shout_import(self):
"""Тест импорта shout"""
from orm.shout import Shout
assert Shout is not None
def test_shout_attributes(self):
"""Тест атрибутов Shout"""
from orm.shout import Shout
# Получаем инспектор для модели
mapper = inspect(Shout)
# Список ожидаемых атрибутов
expected_attrs = [
'title', 'body', 'created_by', 'community',
'created_at', 'updated_at', 'deleted_at',
'published_at', 'slug', 'layout'
]
# Проверяем наличие каждого атрибута
for attr in expected_attrs:
assert any(col.name == attr for col in mapper.columns), f"Атрибут {attr} не найден"
class TestOrmReaction:
"""Тесты для orm.reaction"""
def test_reaction_import(self):
"""Тест импорта reaction"""
from orm.reaction import Reaction
assert Reaction is not None
def test_reaction_attributes(self):
"""Тест атрибутов Reaction"""
from orm.reaction import Reaction
# Получаем инспектор для модели
mapper = inspect(Reaction)
# Список ожидаемых атрибутов
expected_attrs = [
'body', 'created_at', 'updated_at', 'deleted_at',
'deleted_by', 'reply_to', 'quote', 'shout',
'created_by', 'kind', 'oid'
]
# Проверяем наличие каждого атрибута
for attr in expected_attrs:
assert any(col.name == attr for col in mapper.columns), f"Атрибут {attr} не найден"
class TestOrmCollection:
"""Тесты для orm.collection"""
def test_collection_import(self):
"""Тест импорта collection"""
from orm.collection import Collection
assert Collection is not None
def test_collection_attributes(self):
"""Тест атрибутов Collection"""
from orm.collection import Collection
# Получаем инспектор для модели
mapper = inspect(Collection)
# Список ожидаемых атрибутов
expected_attrs = [
'slug', 'title', 'body', 'pic',
'created_at', 'created_by', 'published_at'
]
# Проверяем наличие каждого атрибута
for attr in expected_attrs:
assert any(col.name == attr for col in mapper.columns), f"Атрибут {attr} не найден"
class TestOrmDraft:
"""Тесты для orm.draft"""
def test_draft_import(self):
"""Тест импорта draft"""
from orm.draft import Draft
assert Draft is not None
def test_draft_attributes(self):
"""Тест атрибутов Draft"""
from orm.draft import Draft
assert hasattr(Draft, 'title')
assert hasattr(Draft, 'body')
assert hasattr(Draft, 'created_by')
assert hasattr(Draft, 'community')
assert hasattr(Draft, 'created_at')
assert hasattr(Draft, 'updated_at')
assert hasattr(Draft, 'deleted_at')
class TestOrmTopic:
"""Тесты для orm.topic"""
def test_topic_import(self):
"""Тест импорта topic"""
from orm.topic import Topic
assert Topic is not None
def test_topic_attributes(self):
"""Тест атрибутов Topic"""
from orm.topic import Topic
# Получаем инспектор для модели
mapper = inspect(Topic)
# Список ожидаемых атрибутов
expected_attrs = [
'slug', 'title', 'body', 'pic',
'community', 'oid', 'parent_ids'
]
# Проверяем наличие каждого атрибута
for attr in expected_attrs:
assert any(col.name == attr for col in mapper.columns), f"Атрибут {attr} не найден"
class TestOrmInvite:
"""Тесты для orm.invite"""
def test_invite_import(self):
"""Тест импорта invite"""
from orm.invite import Invite
assert Invite is not None
def test_invite_attributes(self):
"""Тест атрибутов Invite"""
from orm.invite import Invite
# Получаем инспектор для модели
mapper = inspect(Invite)
# Список ожидаемых атрибутов
expected_attrs = [
'inviter_id', 'author_id', 'shout_id', 'status'
]
# Проверяем наличие каждого атрибута
for attr in expected_attrs:
assert any(col.name == attr for col in mapper.columns), f"Атрибут {attr} не найден"
class TestOrmRating:
"""Тесты для orm.rating"""
def test_rating_import(self):
"""Тест импорта rating"""
from orm.rating import is_negative, is_positive, RATING_REACTIONS
assert is_negative is not None
assert is_positive is not None
assert RATING_REACTIONS is not None
def test_rating_functions(self):
"""Тест функций rating"""
from orm.rating import is_negative, is_positive, ReactionKind
# Тест is_negative
assert is_negative(ReactionKind.DISLIKE) is True
assert is_negative(ReactionKind.DISPROOF) is True
assert is_negative(ReactionKind.REJECT) is True
assert is_negative(ReactionKind.LIKE) is False
# Тест is_positive
assert is_positive(ReactionKind.ACCEPT) is True
assert is_positive(ReactionKind.LIKE) is True
assert is_positive(ReactionKind.PROOF) is True
assert is_positive(ReactionKind.DISLIKE) is False
class TestOrmNotification:
"""Тесты для orm.notification"""
def test_notification_import(self):
"""Тест импорта notification"""
from orm.notification import Notification
assert Notification is not None
def test_notification_attributes(self):
"""Тест атрибутов Notification"""
from orm.notification import Notification
# Получаем инспектор для модели
mapper = inspect(Notification)
# Список ожидаемых атрибутов
expected_attrs = [
'id', 'created_at', 'updated_at',
'entity', 'action', 'payload',
'status', 'kind'
]
# Проверяем наличие каждого атрибута
for attr in expected_attrs:
assert any(col.name == attr for col in mapper.columns), f"Атрибут {attr} не найден"
class TestOrmRelationships:
"""Тесты для отношений между моделями"""
def test_community_shouts_relationship(self):
"""Тест отношения community-shouts"""
from orm.community import Community
from orm.shout import Shout
# Проверяем, что модели могут быть импортированы вместе
assert Community is not None
assert Shout is not None
def test_shout_reactions_relationship(self):
"""Тест отношения shout-reactions"""
from orm.shout import Shout
from orm.reaction import Reaction
# Проверяем, что модели могут быть импортированы вместе
assert Shout is not None
assert Reaction is not None
def test_topic_hierarchy_relationship(self):
"""Тест иерархии топиков"""
from orm.topic import Topic
# Проверяем, что модель может быть импортирована
assert Topic is not None
class TestOrmModelMethods:
"""Тесты методов моделей"""
def test_base_model_repr(self):
"""Тест __repr__ базовой модели"""
from orm.base import BaseModel
# Создаем мок объект
mock_obj = Mock(spec=BaseModel)
mock_obj.__class__.__name__ = 'TestModel'
mock_obj.id = 1
# Тест доступности метода dict
assert hasattr(mock_obj, 'dict')
def test_community_str(self):
"""Тест __str__ для Community"""
from orm.community import Community
# Проверяем, что модель имеет необходимые атрибуты
assert hasattr(Community, 'name')
assert hasattr(Community, 'slug')
def test_shout_str(self):
"""Тест __str__ для Shout"""
from orm.shout import Shout
# Проверяем, что модель имеет необходимые атрибуты
assert hasattr(Shout, 'title')
assert hasattr(Shout, 'slug')