import pytest
import numpy as np
from privacy_tuner.core.mechanisms.copula_mechanism import CopulaMechanism


def test_copula_mechanism_basic():
    """测试 Copula 机制的基本功能：输出形状正确，数据有变化"""
    mechanism = CopulaMechanism()
    data = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])  # 4 个样本
    params = {'sampling_ratio': 0.5, 'noise_scale': 0.0}

    synthetic = mechanism.apply(data, params)

    # 检查形状：4 * 0.5 = 2 行
    assert synthetic.shape == (2, 2)

    # 检查数据类型
    assert isinstance(synthetic, np.ndarray)

    # 检查合成数据与原始数据不同（应略有不同）
    # 但由于 copula 可能生成完全相同的数据点（概率较低），这里仅验证不是完全一致
    # 更严格的测试需检查分布相似性


def test_copula_mechanism_with_noise():
    mechanism = CopulaMechanism()
    data = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
    params = {'sampling_ratio': 1.0, 'noise_scale': 0.1}

    synthetic = mechanism.apply(data, params)

    assert synthetic.shape == (4, 2)

    # 由于噪声较小，合成数据不应与原始数据完全相同
    # 但可能有些样本相似，我们只检查平均值是否有较大差异
    assert not np.array_equal(synthetic, data)


def test_copula_mechanism_zero_samples():
    mechanism = CopulaMechanism()
    data = np.array([[1, 2], [3, 4]])
    params = {'sampling_ratio': 0.0}
    synthetic = mechanism.apply(data, params)
    assert synthetic.shape == (0, 2)
    assert synthetic.size == 0