# conftest.py
# ==============================================
# pytest 全局夹具（Fixture）配置文件
# 核心特点：
# 1. 文件名固定为 conftest.py，pytest 会自动识别加载
# 2. 本文件中定义的 fixture 全局共享，当前目录及子目录下所有测试文件均可直接使用
# 3. 无需在测试代码中手动 import，通过函数形参名即可自动注入对象
# ==============================================

# 导入 pytest 框架
import pytest
# 导入项目内的帖子模型类
from app.models import Post


@pytest.fixture
def post():
    """
    测试夹具：生成【单个帖子】测试实例
    用途：供需要使用单个 Post 对象的测试用例复用
    """
    # 实例化帖子对象并返回，供测试函数使用
    return Post("测试帖子", "test_user", "内容")


@pytest.fixture
def post_list():
    """
    测试夹具：生成【帖子对象列表】
    用途：供需要批量帖子数据、遍历列表、批量操作的测试用例复用
    """
    # 创建包含多个帖子实例的列表并返回
    return [
        Post("帖子A", "user1", "内容A"),
        Post("帖子B", "user2", "内容B"),
    ]

# test_post.py （普通测试文件）
def test_check_single_post(post):
    """测试单个帖子的属性"""
    assert post.title == "测试帖子"
    assert post.author == "test_user"

def test_check_post_list(post_list):
    """测试帖子列表长度与内容"""
    # 校验列表总数
    assert len(post_list) == 2
    # 校验第一个帖子信息
    assert post_list[0].title == "帖子A"
    # 校验第二个帖子作者
    assert post_list[1].author == "user2"