def test_pickle_error():
    exp = make_blocked_exp()
    exp.set_context_manager('block', block_context, func_module='not_a_module')
    exp.filename = 'test.pkl'
    exp.save()

    with pytest.raises(ImportError):
        call_cli('exp run test.pkl --next participant')
def test_pickle_error():
    exp = make_blocked_exp()
    exp.add_callback('block',
                     block_context,
                     func_module='not_a_module',
                     is_context=True)
    exp.filename = 'test.yaml'
    exp.save()

    with pytest.raises(ImportError):
        call_cli('exp run test.yaml --next participant')
def test_exception():
    exp = make_blocked_exp()
    exp.set_run_callback(bad_trial)
    exp.save()
    exp.save('test.pkl')
    with pytest.raises(QuitSession):
        run_experiment_section('test.pkl', participant=1)

    exp = Experiment.load('test.pkl')
    assert exp.subsection(participant=1, block=1, trial=1).has_started
    assert not exp.subsection(participant=1, block=1, trial=1).has_finished
    os.remove('test.pkl')

    e = QuitSession('message')
    assert e.__str__() == 'message'
    with pytest.raises(QuitSession):
        raise e
def test_exception():
    exp = make_blocked_exp()
    exp.add_callback('trial', bad_trial)
    exp.save()
    exp.save('test.yaml')
    with pytest.raises(QuitSession):
        run_experiment_section('test.yaml', participant=1)

    exp = Experiment.load('test.yaml')
    assert exp.subsection(participant=1, block=1, trial=1).has_started
    assert not exp.subsection(participant=1, block=1, trial=1).has_finished
    os.remove('test.yaml')

    e = QuitSession('message')
    assert e.__str__() == 'message'
    with pytest.raises(QuitSession):
        raise e
def test_cli():
    exp = make_blocked_exp()
    exp.set_context_manager('participant', participant_context)
    exp.set_context_manager('block', block_context)
    exp.filename = 'test.pkl'
    exp.save()

    call_cli('exp run test.pkl --next participant')
    exp = Experiment.load('test.pkl')
    for row in exp.dataframe.iterrows():
        if row[0][0] == 1:
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp --demo run test.pkl participant 2 block 1')
    for row in exp.dataframe.iterrows():
        if row[0][0] == 1:
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp run test.pkl participant 2 block 1')
    exp = Experiment.load('test.pkl')
    for row in exp.dataframe.iterrows():
        if row[0][0] == 1 or (row[0][0] == 2 and row[0][1] == 1):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp resume test.pkl participant')
    exp = Experiment.load('test.pkl')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2:
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp run test.pkl --next trial')
    exp = Experiment.load('test.pkl')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2 or row[0] == (3, 1, 1):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp resume test.pkl participant 3 block 1 --demo')
    exp = Experiment.load('test.pkl')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2 or row[0] == (3, 1, 1):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp --debug resume test.pkl participant 3 block 1')
    exp = Experiment.load('test.pkl')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2 or row[0][:2] == (3, 1):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp run test.pkl participant 3 block 3 --from 3')
    exp = Experiment.load('test.pkl')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2 or row[0][:2] == (3, 1) or (row[0][:2] == (3, 3) and row[0][-1] >= 3):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp run test.pkl participant 3 --from 2,4')
    exp = Experiment.load('test.pkl')
    for row in exp.dataframe.iterrows():
        if (row[0][0] <= 2
                or row[0][:2] == (3, 1)
                or (row[0][:2] == (3, 2) and row[0][-1] >= 4)
                or (row[0][:2] == (3, 3) and row[0][2] > 2)):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    for file in glob('test.pkl*'):
        os.remove(file)
def test_options():
    exp = make_blocked_exp()
    exp.set_context_manager('block', context)
    exp.filename = 'test.pkl'
    exp.save()
    call_cli('exp run test.pkl --next participant -o pass,through,option')
def test_cli():
    exp = make_blocked_exp()
    exp.add_callback('participant', participant_context, is_context=True)
    exp.add_callback('block', block_context, is_context=True)
    exp.filename = 'test.yaml'
    exp.save()

    call_cli('exp run test.yaml --next participant')
    exp = Experiment.load('test.yaml')
    for row in exp.dataframe.iterrows():
        if row[0][0] == 1:
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp --demo run test.yaml participant 2 block 1')
    for row in exp.dataframe.iterrows():
        if row[0][0] == 1:
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp run test.yaml participant 2 block 1')
    exp = Experiment.load('test.yaml')
    for row in exp.dataframe.iterrows():
        if row[0][0] == 1 or (row[0][0] == 2 and row[0][1] == 1):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp resume test.yaml participant')
    exp = Experiment.load('test.yaml')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2:
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp run test.yaml --next trial')
    exp = Experiment.load('test.yaml')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2 or row[0] == (3, 1, 1):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp resume test.yaml participant 3 block 1 --demo')
    exp = Experiment.load('test.yaml')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2 or row[0] == (3, 1, 1):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp --debug resume test.yaml participant 3 block 1')
    exp = Experiment.load('test.yaml')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2 or row[0][:2] == (3, 1):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp run test.yaml participant 3 block 3 --from 3')
    exp = Experiment.load('test.yaml')
    for row in exp.dataframe.iterrows():
        if row[0][0] <= 2 or row[0][:2] == (3, 1) or (row[0][:2] == (3, 3)
                                                      and row[0][-1] >= 3):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    call_cli('exp run test.yaml participant 3 --from 2,4')
    exp = Experiment.load('test.yaml')
    for row in exp.dataframe.iterrows():
        if (row[0][0] <= 2 or row[0][:2] == (3, 1)
                or (row[0][:2] == (3, 2) and row[0][-1] >= 4)
                or (row[0][:2] == (3, 3) and row[0][2] > 2)):
            yield check_trial, row
        else:
            assert isnan(row[1]['result'])

    for file in glob('test.yaml*'):
        os.remove(file)
def test_options():
    exp = make_blocked_exp()
    exp.add_callback('block', context, is_context=True)
    exp.filename = 'test.yaml'
    exp.save()
    call_cli('exp run test.yaml --next participant -o pass,through,option')