def test_raise_on_mismatch(): experiment = laboratory.Experiment(raise_on_mismatch=True) experiment.control(lambda: 42) experiment.candidate(lambda: 0) with pytest.raises(laboratory.exceptions.MismatchException): experiment.conduct() experiment = laboratory.Experiment(raise_on_mismatch=True) experiment.control(lambda: 42) experiment.candidate(raise_exception) with pytest.raises(laboratory.exceptions.MismatchException): experiment.conduct()
def test_set_context(publish): experiment = laboratory.Experiment(context={'ctx': True}) with experiment.control() as e: e.record(0) assert e.context == {'ctx': True} with experiment.candidate() as e: e.record(0) e.update_context({'ctx': False}) assert e.context == {'ctx': False} with experiment.candidate(context={'additional': 1}) as e: e.record(0) assert e.context == {'ctx': True, 'additional': 1} assert experiment.context == {'ctx': True} assert experiment.run() == 0 assert publish.called result = publish.call_args[0][0] assert result.control.get_context() == {'ctx': True} assert result.observations[0].get_context() == {'ctx': False} assert result.observations[1].get_context() == { 'ctx': True, 'additional': 1 }
def test_control_raising_exception(): experiment = laboratory.Experiment() with pytest.raises(KeyError): with experiment.control() as e: e.record(raise_exception()) assert experiment._control.failure
def test_experiment_must_contain_control(): experiment = laboratory.Experiment() try: experiment.conduct() except laboratory.LaboratoryException as ex: assert ex.message == 'Your experiment must contain a control case' else: assert False, 'Expected LaboratoryException to be raised'
def test_experiment_can_not_rerecord_control(): experiment = laboratory.Experiment() experiment.control(lambda: True) try: experiment.control(lambda: True) except laboratory.LaboratoryException as ex: assert ex.message == 'You have already established a control case' else: assert False, 'Expected LaboratoryException to be raised'
def test_candidate_raising_exception_silently(): experiment = laboratory.Experiment() with experiment.control() as e: e.record(True) with experiment.candidate() as e: e.record(raise_exception()) experiment.run() assert True
def test_raise_on_mismatch(): experiment = laboratory.Experiment(raise_on_mismatch=True) with experiment.control() as e: e.record(42) with experiment.candidate() as e: e.record(0) with pytest.raises(laboratory.exceptions.MismatchException): experiment.run() experiment = laboratory.Experiment(raise_on_mismatch=True) with experiment.control() as e: e.record(42) with experiment.candidate() as e: e.record(raise_exception()) with pytest.raises(laboratory.exceptions.MismatchException): experiment.run()
def test_set_context(publish): experiment = laboratory.Experiment(context={'ctx': True}) experiment.control(lambda: 0, context={'control': True}) experiment.candidate(lambda: 0, context={'ctx': False, 'candidate': True}) experiment.conduct() assert publish.called result = publish.call_args[0][0] assert experiment.get_context() == {'ctx': True} assert result.control.get_context() == {'ctx': True, 'control': True} assert result.candidates[0].get_context() == { 'ctx': False, 'candidate': True }
def test_disable_experiment(): experiment = laboratory.Experiment() enabled_func = mock.Mock(return_value=False) experiment.enabled = enabled_func control_func = mock.Mock() cand_func = mock.Mock() experiment.control(control_func) experiment.candidate(cand_func) result = experiment.conduct() assert result is control_func.return_value assert enabled_func.called assert not cand_func.called
def run_experiment(): exp = laboratory.Experiment() counter = {'index': 0} def increment_counter(): counter['index'] += 1 def control_func(): return counter['index'] cand_func = mock.Mock(side_effect=increment_counter) exp.control(control_func) for _ in range(100): exp.candidate(cand_func) return exp.conduct(randomize=False)
def test_control_raising_exception(): experiment = laboratory.Experiment() experiment.control(raise_exception) with pytest.raises(KeyError): experiment.conduct()