def test_contamination_config_for_consignment_no_default():
    """Check that consignment has only its unique config (defaults not requested)"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Liatris", origin="Netherlands")
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config == {"arrangement": "random_box"}
def test_program_rejected():
    """Check that program which does not exist is rejected"""
    with pytest.raises(RuntimeError) as error:
        get_inspection_needed_function(
            load_configuration_yaml_from_text(DOES_NOT_EXIST_PROGRAM_CONFIG)
        )
    assert "does_not_exist" in str(error)
def test_consignment_matches_contamination_rule():
    """Check that consignment is selected based on a rule"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Sedum", origin="Colombia")
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config == {"contamination_unit": "item", "arrangement": "random"}
def test_contamination_config_for_consignment_with_default():
    """Check that consignment has has combination of defaults and its own config"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Rose", origin="Mexico")
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config == {"contamination_unit": "box", "arrangement": "random"}
def test_consignment_with_no_contamination():
    """Check that consignment is not selected based on a rule"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Rosa", origin="Colombia")
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config is None
def test_contamination_config_for_consignment_implicit_default():
    """Check that consignment inherits the top-level config"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Hyacinthus", origin="Israel")
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config == {"contamination_unit": "item", "arrangement": "random"}
def test_contamination_rate_dict_config(config):
    """Contamination rate function accepts list and mapping as params"""
    config = load_configuration_yaml_from_text(config)
    rate = get_contamination_rate(
        config["contamination"]["contamination_rate"])
    assert rate >= 0
    assert rate <= 1
def test_contamination_config_for_consignment_no_default_explicitly():
    """Check that consignment has only its unique config (defaults disabled)"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Rose", origin="Netherlands")
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config == {"contamination_unit": "box"}
def test_consignment_between_two_date_rules(date):
    """Check that consignment is not selected in presence of start and end date rules"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Gerbera",
                                     origin="Netherlands",
                                     date=date)
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config is None
def test_consignment_matches_contamination_rule_after_start_date(date):
    """Check that consignment is selected based on a rule with start date"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Gerbera",
                                     origin="Netherlands",
                                     date=date)
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config == {"contamination_unit": "item"}
def test_consignment_matches_contamination_rule_before_end_date(date):
    """Check that consignment is selected based on a rule with end date"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Gerbera",
                                     origin="Netherlands",
                                     date=date)
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config == {"arrangement": "random_box"}
def test_consignment_matches_contamination_rule_outside_of_date_interval(date):
    """Check that consignment is not selected based on a rule with dates"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Tulipa",
                                     origin="Netherlands",
                                     date=date)
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config is None
def test_consignment_matches_contamination_rule_within_date_interval(date):
    """Check that consignment is selected based on a rule with start and end dates"""
    main_config = load_configuration_yaml_from_text(CONFIG)
    consignment = simple_consignment(flower="Tulipa",
                                     origin="Netherlands",
                                     date=date)
    config = get_contamination_config_for_consignment(
        main_config["contamination"], consignment)
    assert config == {"contamination_unit": "box", "arrangement": "random_box"}
def test_naive_cfrp():
    """Check that naive CFRP program is accepted and gives expected results"""
    consignment_generator = get_consignment_generator(
        load_configuration_yaml_from_text(BASE_CONSIGNMENT_CONFIG)
    )
    is_needed_function = get_inspection_needed_function(
        load_configuration_yaml_from_text(NAIVE_CFRP_CONFIG)
    )
    # The following assumes what is the default returned by the get function,
    # i.e., it relies on its internals, not the interface.
    # pylint: disable=comparison-with-callable
    assert is_needed_function != inspect_always
    for seed in range(10):
        # We run with different, but fixed seeded so we can know which seed fails.
        random_seed(seed)
        consignment = consignment_generator.generate_consignment()
        inspect, program = is_needed_function(consignment, consignment.date)
        assert isinstance(inspect, bool)
        assert program == "naive_cfrp" or program is None
Esempio n. 15
0
def test_random_clusters():
    """Test contamination rate of clustered arrangement with random distribution"""
    random_seed(42)
    config = load_configuration_yaml_from_text(RANDOM_CONFIG)["contamination"]
    num_items = 550
    consignment = get_consignment(num_items)
    add_contaminant_clusters(config, consignment)
    contamination_rate = 0.12
    contaminated_items = int(num_items * contamination_rate)
    assert np.count_nonzero(consignment.items) == contaminated_items
def test_inspect_not_in_program():
    """Check inspection is requested when consignment is not in the program"""
    program = FixedComplianceLevelSkipLot(
        load_configuration_yaml_from_text(CONFIG)["release_programs"]["fixed_skip_lot"]
    )
    consignment = simple_consignment(flower="Rosa", origin="Netherlands")
    for seed in range(10):
        random_seed(seed)
        inspect, program_name = program(consignment, consignment.date)
        assert inspect
        assert program_name == "Skip Lot"
def test_simulation_runs():
    """Check that the simulation runs

    This should contain parameters which at one point failed the simulation.
    """
    for seed in range(10):
        run_simulation(
            config=load_configuration_yaml_from_text(CONFIG),
            num_simulations=1,
            num_consignments=10,
            seed=seed,
        )
Esempio n. 18
0
def test_cfrp(tmp_path):
    """Check that CFRP program is accepted and gives expected results"""
    schedule_file = tmp_path / "schedule_file.csv"
    schedule_file.write_text(SCHEDULE_CSV_TEXT)
    consignment_generator = get_consignment_generator(
        load_configuration_yaml_from_text(BASE_CONSIGNMENT_CONFIG))
    is_needed_function = get_inspection_needed_function(
        load_configuration_yaml_from_text(
            CFRP_CONFIG.format(schedule_file=schedule_file)))
    # The following assumes what is the default returned by the get function,
    # i.e., it relies on its internals, not the interface.
    # pylint: disable=comparison-with-callable
    assert is_needed_function != inspect_always
    for seed in range(10):
        # We run with different, but fixed seeded so we can know which seed fails.
        random_seed(seed)
        consignment = consignment_generator.generate_consignment()
        inspect, program = is_needed_function(consignment, consignment.date)
        assert isinstance(inspect, bool)
        # Testing custom name
        assert program == "CFRP" or program is None
def test_never_inspect_in_program():
    """Inspection is not requested when consignment is in a zero inspections level"""
    program = FixedComplianceLevelSkipLot(
        load_configuration_yaml_from_text(CONFIG)["release_programs"]["fixed_skip_lot"]
    )
    consignment = simple_consignment(flower="Gerbera", origin="Mexico")
    for seed in range(10):
        random_seed(seed)
        inspect, program_name = program(consignment, consignment.date)
        assert (
            not inspect
        ), "We disabled inspections completely for this inspection level"
        assert program_name == "Skip Lot"
def test_sometimes_inspect_in_program():
    """Inspection is requested at least sometimes when consignment is in the program"""
    program = FixedComplianceLevelSkipLot(
        load_configuration_yaml_from_text(CONFIG)["release_programs"]["fixed_skip_lot"]
    )
    consignment = simple_consignment(flower="Hyacinthus", origin="Netherlands")
    inspected = 0
    for seed in range(10):
        random_seed(seed)
        inspect, program_name = program(consignment, consignment.date)
        inspected += int(inspect)
        assert program_name == "Skip Lot"
    assert inspected, "With the seeds, we expect at least one inspection to happen"
def test_gives_reasonable_result(num_simulations):
    """Check that the result from the simulation is in the expected range"""
    num_consignments = 100
    # We modify the existing configuration rather than defining a completely
    # new one as a separate YAML.
    min_boxes = 30
    max_boxes = 150
    config = load_configuration_yaml_from_text(CONFIG)
    config["consignment"]["parameter_based"]["boxes"]["min"] = min_boxes
    config["consignment"]["parameter_based"]["boxes"]["max"] = max_boxes
    for seed in range(10):
        result = run_simulation(
            config=config, num_simulations=1, num_consignments=100, seed=seed
        )
        test_min_boxes = min_boxes * num_consignments
        test_max_boxes = max_boxes * num_consignments
        assert test_min_boxes <= result.num_boxes <= test_max_boxes
        assert 0 <= result.pct_boxes_opened_completion <= 100
        assert 0 <= result.pct_boxes_opened_detection <= 100
        assert 0 <= result.pct_items_inspected_completion <= 100
        assert 0 <= result.pct_items_inspected_detection <= 100
        assert 0 <= result.pct_contaminant_unreported_if_detection <= 100
def test_level(consignment, level):
    """Correct level is returned for a shipment"""
    program = FixedComplianceLevelSkipLot(
        load_configuration_yaml_from_text(CONFIG)["release_programs"]["fixed_skip_lot"]
    )
    assert program.compliance_level_for_consignment(consignment) == level
def test_fraction(level, fraction):
    """Correct fraction is returned for a level"""
    program = FixedComplianceLevelSkipLot(
        load_configuration_yaml_from_text(CONFIG)["release_programs"]["fixed_skip_lot"]
    )
    assert program.sampling_fraction_for_level(level) == fraction