44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
"""
|
|
tests/services/admin/test_announcement_service.py
|
|
|
|
用途:announcement_service CRUD 与活跃列表单元测试。
|
|
"""
|
|
import pytest
|
|
|
|
from app import db
|
|
from app.core.errors import NotFoundError, ValidationError
|
|
from app.models import Announcement
|
|
from app.services.admin import announcement_service
|
|
|
|
|
|
def test_announcement_crud(app):
|
|
with app.app_context():
|
|
created = announcement_service.create_announcement(
|
|
{'title': 'Hello', 'content': 'World'}
|
|
)
|
|
ann_id = created['id']
|
|
|
|
active = announcement_service.list_active_announcements()
|
|
assert len(active) == 1
|
|
assert active[0]['title'] == 'Hello'
|
|
|
|
admin_list = announcement_service.list_announcements()
|
|
assert len(admin_list) == 1
|
|
assert admin_list[0]['is_active'] is True
|
|
|
|
announcement_service.update_announcement(ann_id, {'title': 'Hi', 'content': 'There'})
|
|
announcement_service.delete_announcement(ann_id)
|
|
assert db.session.get(Announcement, ann_id) is None
|
|
|
|
|
|
def test_create_announcement_validation(app):
|
|
with app.app_context():
|
|
with pytest.raises(ValidationError):
|
|
announcement_service.create_announcement({'title': 'only title'})
|
|
|
|
|
|
def test_update_announcement_not_found(app):
|
|
with app.app_context():
|
|
with pytest.raises(NotFoundError):
|
|
announcement_service.update_announcement(99999, {'title': 'x', 'content': 'y'})
|