def test_configset_getitem(): """ Verify that a parameter set and a covering array with no "don't care" values present is correctly converted to a data frame with the correct column headings and value names. """ p1 = Parameter("Colour", [RED, GREEN, BLUE]) p2 = Parameter("Pet", [BIRD, CAT, DOG]) p3 = Parameter("Speed", [FAST, MEDIUM, SLOW]) p4 = Parameter("Music", [SEVENTIES, EIGHTIES, TWENTIES]) parameter_set = ParameterSet([p1, p2, p3, p4]) # Covering array from... # generator = RecursiveGenerator(parameter_set, 2) # covering_array = generator.generate_covering_array() covering_array = np.array([[1, 1, 1, 1], [1, 2, 2, 2], [1, 3, 3, 3], [2, 1, 2, 3], [2, 2, 3, 1], [2, 3, 1, 2], [3, 1, 3, 2], [3, 2, 1, 3], [3, 3, 2, 1]]) configurations = ConfigurationSet(parameter_set=parameter_set, covering_array=covering_array) expected = pd.Series({ "Colour": GREEN, "Pet": BIRD, "Speed": MEDIUM, "Music": TWENTIES, }) actual = configurations[3] assert pd.Series.equals(actual, expected)
def test_equals(): """ Verify that a parameter set and a covering array that contains a "don't care" value is correctly converted to a data frame, where the "don't care" value becomes Pandas' pd.NA value. """ p1 = Parameter("Colour", [RED, GREEN]) p2 = Parameter("Pet", [BIRD, CAT, DOG, FISH]) p3 = Parameter("Speed", [FAST, SLOW]) p4 = Parameter("Music", [EIGHTIES, TWENTIES]) parameter_set = ParameterSet([p1, p2, p3, p4]) # Covering array from... # generator = RecursiveGenerator(parameter_set, 2) # covering_array = generator.generate_covering_array() covering_array = np.array([[1, 1, 1, 1], [1, 2, 2, 1], [2, 1, 2, 1], [2, 2, 1, 0], [2, 2, 2, 2], [1, 1, 1, 2], [1, 3, 1, 1], [2, 3, 2, 2], [1, 4, 1, 1], [2, 4, 2, 2]]) # configs1 = ConfigurationSet(parameter_set, covering_array) # configs2 = ConfigurationSet(parameter_set, covering_array) configs1 = ConfigurationSet(parameter_set=parameter_set, covering_array=covering_array) configs2 = ConfigurationSet(parameter_set=parameter_set, covering_array=covering_array) assert configs1 is not configs2 assert configs1 == configs2
def test_dataframe_to_covering_array(): p1 = Parameter("Colour", [RED, GREEN]) p2 = Parameter("Pet", [BIRD, CAT, DOG, FISH]) p3 = Parameter("Speed", [FAST, SLOW]) p4 = Parameter("Music", [EIGHTIES, TWENTIES]) parameter_set = ParameterSet([p1, p2, p3, p4]) dataframe = pd.DataFrame([ [RED, BIRD, FAST, EIGHTIES], [RED, CAT, SLOW, EIGHTIES], [GREEN, BIRD, SLOW, EIGHTIES], [GREEN, CAT, FAST, pd.NA], [GREEN, CAT, SLOW, TWENTIES], [RED, BIRD, FAST, TWENTIES], [RED, DOG, FAST, EIGHTIES], [GREEN, DOG, SLOW, TWENTIES], [RED, FISH, FAST, EIGHTIES], [GREEN, FISH, SLOW, TWENTIES], ], columns=["Colour", "Pet", "Speed", "Music"]) covering_array = ConfigurationSet.dataframe_to_covering_array( dataframe, parameter_set) expected = np.array( [[1, 1, 1, 1], [1, 2, 2, 1], [2, 1, 2, 1], [2, 2, 1, 0], [2, 2, 2, 2], [1, 1, 1, 2], [1, 3, 1, 1], [2, 3, 2, 2], [1, 4, 1, 1], [2, 4, 2, 2]], dtype=DEFAULT_NDARRAY_TYPE) assert np.array_equal(covering_array, expected)
def test_set_name(): p = Parameter("Z", [VAL_1, VAL_2, VAL_3, VAL_4]) p.name = "X" p_expected = Parameter("X", [VAL_1, VAL_2, VAL_3, VAL_4], uid=p.uid) assert p == p_expected
def test_series(): p = Parameter("Z", [VAL_1, VAL_2]) s = p.to_series() assert s.name == "Z" assert s.to_dict() == {0: VAL_1, 1: VAL_2}
def test_init_name_values(): plist = [VAL_1, VAL_2] p = Parameter("Z", plist) assert p.name == "Z" assert p.values == plist
def test_clear(): p = Parameter("Z", [VAL_1, VAL_2, VAL_3, VAL_4]) p.clear() assert p.name == "Z" assert p.values == []
def test_pop_index(): p = Parameter("Z", [VAL_1, VAL_2, VAL_3, VAL_4]) popped_val = p.pop(2) assert popped_val == VAL_3 assert p.values == [VAL_1, VAL_2, VAL_4]
def test_init_name_list_str(): p = Parameter("Z", ["A", "B"]) assert p.name == "Z" v1 = Value("A", uid=p.values[0].uid) v2 = Value("B", uid=p.values[1].uid) assert p.values == [v1, v2]
def test_dict_round_trip(): p_orig = Parameter("Z", [VAL_1, VAL_2, VAL_3, VAL_4], uid="337f7234-85a1-45a0-be77-0934ec232f21") parameter_dict = p_orig.to_dict() p_new = Parameter.from_dict(parameter_dict) assert p_new == p_orig
def test_iter(): p = Parameter("Z", [VAL_1, VAL_2, VAL_3, VAL_4]) result = [] for value in p: result.append(value) assert result == [VAL_1, VAL_2, VAL_3, VAL_4]
def test_configset_iterate(): """ Verify that a parameter set and a covering array with no "don't care" values present is correctly converted to a data frame with the correct column headings and value names. """ p1 = Parameter("Colour", [RED, GREEN]) p2 = Parameter("Pet", [CAT, DOG]) p3 = Parameter("Speed", [FAST, SLOW]) parameter_set = ParameterSet([p1, p2, p3]) # Covering array from... # generator = RecursiveGenerator(parameter_set, 2) # covering_array = generator.generate_covering_array() covering_array = np.array([[1, 1, 1], [1, 2, 2], [2, 1, 2], [2, 2, 1]]) configurations = ConfigurationSet(parameter_set=parameter_set, covering_array=covering_array) expected = [ pd.Series({ "Colour": RED, "Pet": CAT, "Speed": FAST, }), pd.Series({ "Colour": RED, "Pet": DOG, "Speed": SLOW, }), pd.Series({ "Colour": GREEN, "Pet": CAT, "Speed": SLOW, }), pd.Series({ "Colour": GREEN, "Pet": DOG, "Speed": FAST, }) ] for index, actual in enumerate(configurations): assert pd.Series.equals(actual, expected[index])
def test_to_dict(): p = Parameter("Z", [VAL_1, VAL_2, VAL_3, VAL_4], uid="337f7234-85a1-45a0-be77-0934ec232f21") expected = { "name": "Z", "uid": "337f7234-85a1-45a0-be77-0934ec232f21", "values": [v.to_dict() for v in [VAL_1, VAL_2, VAL_3, VAL_4]], } assert p.to_dict() == expected
def test_generate_configurations_with_dont_care(): """ Verify that a parameter set and a covering array that contains a "don't care" value is correctly converted to a data frame, where the "don't care" value becomes Pandas' pd.NA value. """ p1 = Parameter("Colour", [RED, GREEN]) p2 = Parameter("Pet", [BIRD, CAT, DOG, FISH]) p3 = Parameter("Speed", [FAST, SLOW]) p4 = Parameter("Music", [EIGHTIES, TWENTIES]) parameter_set = ParameterSet([p1, p2, p3, p4]) # Covering array from... # generator = RecursiveGenerator(parameter_set, 2) # covering_array = generator.generate_covering_array() covering_array = np.array([[1, 1, 1, 1], [1, 2, 2, 1], [2, 1, 2, 1], [2, 2, 1, 0], [2, 2, 2, 2], [1, 1, 1, 2], [1, 3, 1, 1], [2, 3, 2, 2], [1, 4, 1, 1], [2, 4, 2, 2]]) configurations = ConfigurationSet(parameter_set=parameter_set, covering_array=covering_array) actual_configurations = configurations.configs expected_configurations = pd.DataFrame( [ [RED, BIRD, FAST, EIGHTIES], [RED, CAT, SLOW, EIGHTIES], [GREEN, BIRD, SLOW, EIGHTIES], [GREEN, CAT, FAST, pd.NA], [GREEN, CAT, SLOW, TWENTIES], [RED, BIRD, FAST, TWENTIES], [RED, DOG, FAST, EIGHTIES], [GREEN, DOG, SLOW, TWENTIES], [RED, FISH, FAST, EIGHTIES], [GREEN, FISH, SLOW, TWENTIES], ], columns=["Colour", "Pet", "Speed", "Music"]) assert actual_configurations.equals(expected_configurations)
def test_to_dict(): p1 = Parameter("Colour", [RED, GREEN]) p2 = Parameter("Pet", [CAT, DOG]) p3 = Parameter("Speed", [FAST, SLOW]) parameter_set = ParameterSet([p1, p2, p3]) # Covering array from... # generator = RecursiveGenerator(parameter_set, 2) # covering_array = generator.generate_covering_array() covering_array = np.array([[1, 1, 1], [1, 2, 2], [2, 1, 2], [2, 2, 1]]) configurations = ConfigurationSet(parameter_set=parameter_set, covering_array=covering_array) expected = { "configurations": [ { "Colour": "Red", "Pet": "Cat", "Speed": "Fast" }, { "Colour": "Red", "Pet": "Dog", "Speed": "Slow" }, { "Colour": "Green", "Pet": "Cat", "Speed": "Slow" }, { "Colour": "Green", "Pet": "Dog", "Speed": "Fast" }, ] } actual = configurations.to_dict() assert actual == expected
def test_generate_configurations(): """ Verify that a parameter set and a covering array with no "don't care" values present is correctly converted to a data frame with the correct column headings and value names. """ p1 = Parameter("Colour", [RED, GREEN, BLUE]) p2 = Parameter("Pet", [BIRD, CAT, DOG]) p3 = Parameter("Speed", [FAST, MEDIUM, SLOW]) p4 = Parameter("Music", [SEVENTIES, EIGHTIES, TWENTIES]) parameter_set = ParameterSet([p1, p2, p3, p4]) # Covering array from... # generator = RecursiveGenerator(parameter_set, 2) # covering_array = generator.generate_covering_array() covering_array = np.array([[1, 1, 1, 1], [1, 2, 2, 2], [1, 3, 3, 3], [2, 1, 2, 3], [2, 2, 3, 1], [2, 3, 1, 2], [3, 1, 3, 2], [3, 2, 1, 3], [3, 3, 2, 1]]) configurations = ConfigurationSet(parameter_set=parameter_set, covering_array=covering_array) actual_configurations = configurations.configs expected_configurations = pd.DataFrame( [ [RED, BIRD, FAST, SEVENTIES], [RED, CAT, MEDIUM, EIGHTIES], [RED, DOG, SLOW, TWENTIES], [GREEN, BIRD, MEDIUM, TWENTIES], [GREEN, CAT, SLOW, SEVENTIES], [GREEN, DOG, FAST, EIGHTIES], [BLUE, BIRD, SLOW, EIGHTIES], [BLUE, CAT, FAST, TWENTIES], [BLUE, DOG, MEDIUM, SEVENTIES], ], columns=["Colour", "Pet", "Speed", "Music"]) assert actual_configurations.equals(expected_configurations)
def test_index(): p = Parameter("Z", [VAL_1, VAL_2, VAL_3, VAL_4]) index = p.index(VAL_3) assert index == 2
def test_index_start_stop(): p = Parameter("Z", [VAL_1, VAL_2, VAL_3, VAL_4]) error_message = re.escape(f"{repr(VAL_4)} is not in list") with pytest.raises(ValueError, match=error_message): p.index(VAL_4, 0, 2)
def parm_e(a1, e1, e2): return Parameter("E", [e1, e2])
def parm_c(c1, c2, c3): return Parameter("C", [c1, c2, c3])
def parm_d(d1, d2, d3, d4): return Parameter("D", [d1, d2, d3, d4])
def test_len(): p = Parameter("Z", [VAL_1, VAL_2, VAL_3, VAL_4]) assert len(p) == 4
def speed(orm, fast, medium, slow): return Parameter("Speed", values=[fast, medium, slow])
def colour(orm, red, green, blue): return Parameter("Colour", values=[red, green, blue])
def test_eq_diff_values(): p1 = Parameter("Z", [VAL_1, VAL_2]) p2 = Parameter("Z", [VAL_3, VAL_2]) assert not p1 == p2 # pylint: disable=unneeded-not
def test_eq_true(): p1 = Parameter("Z", [VAL_1, VAL_2]) p2 = Parameter("Z", [VAL_1, VAL_2], uid=p1.uid) assert p1 == p2
def pet(orm, bird, cat, dog): return Parameter("Pet", values=[bird, cat, dog])
def parm_a(a1, a2, a3): return Parameter("A", [a1, a2, a3])
def music(orm, seventies, eighties, twenties): return Parameter("Music", values=[seventies, eighties, twenties])
def parm_b(b1, b2, b3): return Parameter("B", [b1, b2, b3])