def test_pipeline(): @returns(str) def say_hello(): return "hello" @returns(offset=int) def meta_offset(s): assert s == "hello" return {'offset': 1} @returns(int) def count(s, offset): return len(s) + offset def with_world(offset): return offset, "world" @returns(str) def second_item(tup): return tup[1] pipeline = Pipeline(say_hello, meta_offset) assert pipeline() is None pipeline = Pipeline(meta_offset, say_hello, count) assert pipeline("hello") == 6 pipeline = Pipeline(meta_offset, say_hello, count, with_world) assert pipeline("hello") == (6, "world") pipeline = Pipeline(with_world, second_item) assert pipeline(offset=1) == "world"
def test_downcast_pipeline(): def missing_x(x): return "should not work" # pragma: no cover def say_hello(y): return "hello and y = " + y @returns(bool, x=int, y=str) def blah(): return True, {"x": 1, "y": "abc"} inner_pipeline = Pipeline(blah) assert inner_pipeline() is True downcasted_pipeline = returns(y=str)(inner_pipeline) pipeline = Pipeline(downcasted_pipeline, say_hello) assert pipeline() == "hello and y = abc" pipeline = Pipeline(downcasted_pipeline, missing_x) with pytest.raises(Exception) as e: pipeline() assert type(e.value.__cause__) == RuntimeError assert str( e.value.__cause__).startswith("'x' is required but not available.") downcasted_pipeline = returns()(inner_pipeline) pipeline = Pipeline(downcasted_pipeline, lambda: "hello") assert pipeline() == "hello" downcasted_pipeline = returns(bool)(inner_pipeline) pipeline = Pipeline(downcasted_pipeline, lambda x: not x) assert pipeline() == False
def test_nested_pipeline_step_exception(): def throw(): raise Exception("something is wrong") pipeline = Pipeline(Pipeline(throw)) with pytest.raises(PakkrError) as e: pipeline() assert str(e.value).startswith("something is wrong")
def test_nested_pipeline(): inner_step = returns(str, a=bool)(lambda i: ("inner_" + str(i), {'a': True})) inner_pipeline = Pipeline(inner_step, _name="inner_pipeline") def outer_step(s, a, x=0): return (not a, s[::-1], x) outer_pipeline = Pipeline(inner_pipeline, outer_step, _name="outer_pipeline") assert outer_pipeline(100, x=-1) == (False, "001_renni", -1)
def test_add_arguments(): @cmd_args(argument('--config')) def test(config): return f'config: {config}' pipeline = Pipeline(test) mock_parser = MagicMock() parser = pipeline.add_arguments(mock_parser) assert parser is mock_parser mock_parser.add_argument.assert_called_once_with('--config') result = pipeline(config='some_file') assert result == 'config: some_file'
def test_calling_pipeline_inside_a_step(): @returns(int, x=str) def inner_step(): return 1, {'x': 'hello'} callable = Pipeline(inner_step) @returns(int) def use_callable(): result = callable() assert result == 1 return result pipeline = Pipeline(use_callable) assert pipeline() == 1
def test_pipeline_run_step(): def say_hello(): return "hello" pipeline = Pipeline(say_hello) with patch.object(pipeline, '_run_step', wraps=pipeline._run_step) as spy: pipeline() spy.assert_called_with(((), {}), say_hello, indent=1)
def test_step_instance_method(): @returns(str, a=bool) class Step: def __call__(self, x): return str(x), {'a': False} pipeline = Pipeline(Step()) assert pipeline(x=100) == "100" assert pipeline(100) == "100" class Step: @returns(str, a=bool) def some_func(self, x): return str(x), {'a': False} pipeline = Pipeline(Step().some_func) assert pipeline(x=100) == "100" assert pipeline(100) == "100"
def test_step_static_method(): class Step: @staticmethod @returns(str, a=bool) def some_func(x): return str(x), {'a': False} pipeline = Pipeline(Step.some_func) assert pipeline(x=100) == "100" assert pipeline(100) == "100"
def exec_pipeline(config_yaml_file: str): pipeline = Pipeline( read_benchmark_data, identify_pii_entities, calculate_precisions_and_recalls, log_predictions_and_ground_truths, calculate_aggregate_metrics, report_results, name="pii_validation_pipeline", ) config = load_yaml_file(config_yaml_file) if config: # conversions to meet requirements on type checks config["grouped_targeted_labels"] = [ set(item) for item in config["grouped_targeted_labels"] ] config["nontargeted_labels"] = set(config["nontargeted_labels"]) return pipeline(**config) else: raise ValueError("Config YAML is empty.")
def execute_evaluation_pipeline(config_yaml: str): eval_pipeline = Pipeline( enable_tracker, log_config_yaml_path, get_tokeniser, get_detokeniser, get_recogniser, get_evaluator, load_test_data, evaluate, disable_tracker, name="pii_evaluation_pipeline", _suppress_timing_logs=False, ) config = load_yaml_file(config_yaml) if config: config["config_yaml_path"] = config_yaml return eval_pipeline(**config) else: raise ValueError("Config YAML is empty.")
def test_identifier_attr(): pipeline = Pipeline(lambda: 1, _name="cool_pipeline") assert _identifier(pipeline) == '"cool_pipeline"<Pipeline>'
def test_no_return(): pipeline = returns()(Pipeline(lambda: "hello")) assert pipeline() is None pipeline = returns()(Pipeline(returns(x=int)(lambda: {'x': 1}))) assert pipeline() is None