|
16 | 16 |
|
17 | 17 | """Unit tests for the named_product function in lambkin.common.""" |
18 | 18 |
|
19 | | -from lambkin.common.named_product import named_product |
20 | | - |
21 | | - |
22 | | -def test_two_parameters_correct_length(): |
23 | | - """Tests if named_product generates the correct number of combinations. |
24 | | -
|
25 | | - It verifies that two parameters with 2 values each produce |
26 | | - 4 combinations (2 × 2). |
27 | | - """ |
28 | | - result = named_product( |
29 | | - sensor_model=["likelihood_field", "beam"], num_particles=[1, 10] |
30 | | - ) |
31 | | - assert len(result) == 4 |
32 | | - |
33 | | - |
34 | | -def test_two_parameters_correct_combinations(): |
35 | | - """Tests if named_product generates all expected combinations. |
36 | | -
|
37 | | - It verifies that every possible combination of the given |
38 | | - parameters is present in the result. |
39 | | - """ |
40 | | - result = named_product( |
41 | | - sensor_model=["likelihood_field", "beam"], num_particles=[1, 10] |
42 | | - ) |
43 | | - assert {"sensor_model": "likelihood_field", "num_particles": 1} in result |
44 | | - assert {"sensor_model": "likelihood_field", "num_particles": 10} in result |
45 | | - assert {"sensor_model": "beam", "num_particles": 1} in result |
46 | | - assert {"sensor_model": "beam", "num_particles": 10} in result |
47 | | - |
48 | | - |
49 | | -def test_three_parameters_correct_length(): |
50 | | - """Tests if named_product scales correctly with three parameters. |
51 | | -
|
52 | | - It verifies that three parameters with 2 values each produce |
53 | | - 8 combinations (2³). |
54 | | - """ |
55 | | - result = named_product( |
56 | | - sensor_model=["likelihood_field", "beam"], |
57 | | - num_particles=[1, 10], |
58 | | - rate=[10, 100], |
59 | | - ) |
60 | | - assert len(result) == 8 |
61 | | - |
| 19 | +import pytest |
62 | 20 |
|
63 | | -def test_single_parameter(): |
64 | | - """Tests if named_product works correctly with a single parameter. |
65 | | -
|
66 | | - It verifies that a single parameter returns one dict per value. |
67 | | - """ |
68 | | - result = named_product(sensor_model=["likelihood_field", "beam"]) |
69 | | - assert result == [{"sensor_model": "likelihood_field"}, {"sensor_model": "beam"}] |
70 | | - |
71 | | - |
72 | | -def test_returns_list_of_dicts(): |
73 | | - """Tests if named_product always returns a list of dictionaries. |
74 | | -
|
75 | | - It verifies the return type is always a list and each |
76 | | - element is a dictionary regardless of input size. |
77 | | - """ |
78 | | - result = named_product(sensor_model=["likelihood_field"]) |
79 | | - assert isinstance(result, list) |
80 | | - assert isinstance(result[0], dict) |
81 | | - |
82 | | - |
83 | | -def test_empty_list_returns_no_combinations(): |
84 | | - """Tests if named_product handles an empty parameter list correctly. |
85 | | -
|
86 | | - It verifies that an empty list in any parameter produces |
87 | | - no combinations at all. |
88 | | - """ |
89 | | - result = named_product(sensor_model=[], num_particles=[1]) |
90 | | - assert result == [] |
91 | | - |
92 | | - |
93 | | -def test_no_parameters_returns_one_empty_dict(): |
94 | | - """Tests if named_product handles no parameters gracefully. |
| 21 | +from lambkin.common.named_product import named_product |
95 | 22 |
|
96 | | - It verifies that calling named_product with no arguments |
97 | | - returns a list with a single empty dictionary. |
98 | | - """ |
99 | | - result = named_product() |
100 | | - assert result == [{}] |
| 23 | +# Membership test |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize( |
| 27 | + "kwargs, expected_combinations", |
| 28 | + [ |
| 29 | + ( |
| 30 | + {"sensor_model": ["likelihood_field", "beam"], "num_particles": [1, 10]}, |
| 31 | + [ |
| 32 | + {"sensor_model": "likelihood_field", "num_particles": 1}, |
| 33 | + {"sensor_model": "likelihood_field", "num_particles": 10}, |
| 34 | + {"sensor_model": "beam", "num_particles": 1}, |
| 35 | + {"sensor_model": "beam", "num_particles": 10}, |
| 36 | + ], |
| 37 | + ), |
| 38 | + ( |
| 39 | + {"sensor_model": ["likelihood_field", "beam"]}, |
| 40 | + [ |
| 41 | + {"sensor_model": "likelihood_field"}, |
| 42 | + {"sensor_model": "beam"}, |
| 43 | + ], |
| 44 | + ), |
| 45 | + ( |
| 46 | + {}, |
| 47 | + [{}], |
| 48 | + ), |
| 49 | + ( |
| 50 | + {"sensor_model": [], "num_particles": [1]}, |
| 51 | + [], |
| 52 | + ), |
| 53 | + ], |
| 54 | +) |
| 55 | +def test_named_product_combinations(kwargs, expected_combinations): |
| 56 | + """named_product contains exactly the expected combination dicts.""" |
| 57 | + result = named_product(**kwargs) |
| 58 | + assert result == expected_combinations |
| 59 | + |
| 60 | + |
| 61 | +# Length test |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "kwargs, expected_len", |
| 66 | + [ |
| 67 | + ( |
| 68 | + {"sensor_model": ["likelihood_field", "beam"], "num_particles": [1, 10]}, |
| 69 | + 4, |
| 70 | + ), |
| 71 | + ( |
| 72 | + { |
| 73 | + "sensor_model": ["likelihood_field", "beam"], |
| 74 | + "num_particles": [1, 10], |
| 75 | + "rate": [10, 100], |
| 76 | + }, |
| 77 | + 8, |
| 78 | + ), |
| 79 | + ( |
| 80 | + {"sensor_model": ["likelihood_field", "beam"]}, |
| 81 | + 2, |
| 82 | + ), |
| 83 | + ( |
| 84 | + {"sensor_model": [], "num_particles": [1]}, |
| 85 | + 0, |
| 86 | + ), |
| 87 | + ( |
| 88 | + {}, |
| 89 | + 1, |
| 90 | + ), |
| 91 | + ], |
| 92 | +) |
| 93 | +def test_named_product_length(kwargs, expected_len): |
| 94 | + """named_product returns the correct number of combinations (cartesian product).""" |
| 95 | + result = named_product(**kwargs) |
| 96 | + assert len(result) == expected_len |
0 commit comments