import pytest
from abra import Experiment, HypothesisTest


def test_large_proportions_delta_expermiment(proportions_data_large):
    exp = Experiment(proportions_data_large, name='proportions-test')

    # run 'A/A' test
    test_aa = HypothesisTest(
        metric='metric',
        control='A', variation='A',
        hypothesis='larger',
        inference_method='proportions_delta'
    )
    results_aa = exp.run_test(test_aa)

    assert results_aa.test_statistic == 'z'
    assert not results_aa.accept_hypothesis

    # run A/B test
    test_ab = HypothesisTest(
        metric='metric',
        control='A', variation='B',
        hypothesis='larger',
        inference_method='proportions_delta'
    )
    results_ab = exp.run_test(test_ab)

    assert results_ab.test_statistic == 'z'
    assert results_ab.accept_hypothesis


def test_proportions_delta_ab_unequal(proportions_data_small):
    exp = Experiment(proportions_data_small, name='proportions-test')

    # run A/B test
    test_ab = HypothesisTest(
        metric='metric',
        control='A', variation='F',
        hypothesis='unequal',
        inference_method='proportions_delta'
    )
    results_ab = exp.run_test(test_ab)

    assert results_ab.test_statistic == 'z'
    assert results_ab.accept_hypothesis

def test_proportions_delta_ab_larger(proportions_data_small):
    exp = Experiment(proportions_data_small, name='proportions-test')

    # run A/B test
    test_ab = HypothesisTest(
        metric='metric',
        control='A', variation='F',
        hypothesis='larger',
        inference_method='proportions_delta'
    )
    results_ab = exp.run_test(test_ab)
    assert results_ab.accept_hypothesis

def test_proportions_delta_ab_smaller(proportions_data_small):
    exp = Experiment(proportions_data_small, name='proportions-test')

    # run A/B test
    test_ab = HypothesisTest(
        metric='metric',
        control='A', variation='F',
        hypothesis='smaller',
        inference_method='proportions_delta'
    )
    results_ab = exp.run_test(test_ab)
    assert not results_ab.accept_hypothesis


def test_proportions_delta_aa(proportions_data_small):
    exp = Experiment(proportions_data_small, name='proportions-test')

    # run A/A test
    test_aa = HypothesisTest(
        metric='metric',
        control='A', variation='A',
        hypothesis='larger',
        inference_method='proportions_delta'
    )
    results_aa = exp.run_test(test_aa)
    assert not results_aa.accept_hypothesis


def test_proportions_delta_experiment_t(proportions_data_small):
    """Small sample sizes defautl to t-tests"""
    exp = Experiment(proportions_data_small.sample(29), name='proportions-test')

    test_aa = HypothesisTest(
        metric='metric',
        control='A', variation='A',
        hypothesis='unequal',
        inference_method='means_delta'
    )
    results_aa = exp.run_test(test_aa)
    assert results_aa.test_statistic == 't'
[((140, 199), 'abra.Experiment', 'Experiment', (['proportions_data_large'], {'name': '"""proportions-test"""'}), "(proportions_data_large, name='proportions-test')\n", (150, 199), False, 'from abra import Experiment, HypothesisTest\n'), ((236, 359), 'abra.HypothesisTest', 'HypothesisTest', ([], {'metric': '"""metric"""', 'control': '"""A"""', 'variation': '"""A"""', 'hypothesis': '"""larger"""', 'inference_method': '"""proportions_delta"""'}), "(metric='metric', control='A', variation='A', hypothesis=\n    'larger', inference_method='proportions_delta')\n", (250, 359), False, 'from abra import Experiment, HypothesisTest\n'), ((555, 678), 'abra.HypothesisTest', 'HypothesisTest', ([], {'metric': '"""metric"""', 'control': '"""A"""', 'variation': '"""B"""', 'hypothesis': '"""larger"""', 'inference_method': '"""proportions_delta"""'}), "(metric='metric', control='A', variation='B', hypothesis=\n    'larger', inference_method='proportions_delta')\n", (569, 678), False, 'from abra import Experiment, HypothesisTest\n'), ((911, 970), 'abra.Experiment', 'Experiment', (['proportions_data_small'], {'name': '"""proportions-test"""'}), "(proportions_data_small, name='proportions-test')\n", (921, 970), False, 'from abra import Experiment, HypothesisTest\n'), ((1005, 1129), 'abra.HypothesisTest', 'HypothesisTest', ([], {'metric': '"""metric"""', 'control': '"""A"""', 'variation': '"""F"""', 'hypothesis': '"""unequal"""', 'inference_method': '"""proportions_delta"""'}), "(metric='metric', control='A', variation='F', hypothesis=\n    'unequal', inference_method='proportions_delta')\n", (1019, 1129), False, 'from abra import Experiment, HypothesisTest\n'), ((1360, 1419), 'abra.Experiment', 'Experiment', (['proportions_data_small'], {'name': '"""proportions-test"""'}), "(proportions_data_small, name='proportions-test')\n", (1370, 1419), False, 'from abra import Experiment, HypothesisTest\n'), ((1454, 1577), 'abra.HypothesisTest', 'HypothesisTest', ([], {'metric': '"""metric"""', 'control': '"""A"""', 'variation': '"""F"""', 'hypothesis': '"""larger"""', 'inference_method': '"""proportions_delta"""'}), "(metric='metric', control='A', variation='F', hypothesis=\n    'larger', inference_method='proportions_delta')\n", (1468, 1577), False, 'from abra import Experiment, HypothesisTest\n'), ((1764, 1823), 'abra.Experiment', 'Experiment', (['proportions_data_small'], {'name': '"""proportions-test"""'}), "(proportions_data_small, name='proportions-test')\n", (1774, 1823), False, 'from abra import Experiment, HypothesisTest\n'), ((1858, 1982), 'abra.HypothesisTest', 'HypothesisTest', ([], {'metric': '"""metric"""', 'control': '"""A"""', 'variation': '"""F"""', 'hypothesis': '"""smaller"""', 'inference_method': '"""proportions_delta"""'}), "(metric='metric', control='A', variation='F', hypothesis=\n    'smaller', inference_method='proportions_delta')\n", (1872, 1982), False, 'from abra import Experiment, HypothesisTest\n'), ((2166, 2225), 'abra.Experiment', 'Experiment', (['proportions_data_small'], {'name': '"""proportions-test"""'}), "(proportions_data_small, name='proportions-test')\n", (2176, 2225), False, 'from abra import Experiment, HypothesisTest\n'), ((2260, 2383), 'abra.HypothesisTest', 'HypothesisTest', ([], {'metric': '"""metric"""', 'control': '"""A"""', 'variation': '"""A"""', 'hypothesis': '"""larger"""', 'inference_method': '"""proportions_delta"""'}), "(metric='metric', control='A', variation='A', hypothesis=\n    'larger', inference_method='proportions_delta')\n", (2274, 2383), False, 'from abra import Experiment, HypothesisTest\n'), ((2711, 2829), 'abra.HypothesisTest', 'HypothesisTest', ([], {'metric': '"""metric"""', 'control': '"""A"""', 'variation': '"""A"""', 'hypothesis': '"""unequal"""', 'inference_method': '"""means_delta"""'}), "(metric='metric', control='A', variation='A', hypothesis=\n    'unequal', inference_method='means_delta')\n", (2725, 2829), False, 'from abra import Experiment, HypothesisTest\n')]