1. Project Clover database Wed Nov 12 2025 05:07:35 UTC
  2. Package guru.mikelue.foxglove.jdbc

File CartesianProductBuilderTest.java

 

Code metrics

2
15
6
1
140
121
7
0.47
2.5
6
1.17

Classes

Class Line # Actions
CartesianProductBuilderTest 20 15 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    package guru.mikelue.foxglove.jdbc;
2   
3    import java.util.HashMap;
4    import java.util.LinkedHashMap;
5    import java.util.List;
6    import java.util.Map;
7    import java.util.stream.Stream;
8   
9    import org.junit.jupiter.api.AfterEach;
10    import org.junit.jupiter.api.BeforeEach;
11    import org.junit.jupiter.params.ParameterizedTest;
12    import org.junit.jupiter.params.provider.Arguments;
13    import org.junit.jupiter.params.provider.MethodSource;
14   
15    import guru.mikelue.misc.testlib.AbstractTestBase;
16   
17    import static org.assertj.core.api.Assertions.assertThat;
18    import static org.junit.jupiter.params.provider.Arguments.arguments;
19   
 
20    public class CartesianProductBuilderTest extends AbstractTestBase {
 
21  4 toggle public CartesianProductBuilderTest() {}
22   
 
23  4 toggle @BeforeEach
24    void setup() {}
25   
 
26  4 toggle @AfterEach
27    void tearDown() {}
28   
29    /**
30    * Tests the building of Cartesian product values.
31    */
 
32  4 toggle @ParameterizedTest
33    @MethodSource
34    void buildSupplierFactory(
35    Map<String, List<?>> domains,
36    Map<String, List<?>> expectedExpandedValues
37    ) {
38  4 var testedBuilder = new CartesianProductBuilder();
39  4 for (var entry: domains.entrySet()) {
40  10 testedBuilder.putDomain(entry.getKey(), () -> entry.getValue());
41    }
42   
43  4 var columnNames = testedBuilder.getColumnNames();
44  4 var testedResult = new HashMap<>(columnNames.size());
45  4 columnNames.stream()
46    .forEach(name -> {
47  10 var supplier = testedBuilder.buildLazySupplier(name);
48   
49  10 testedResult.put(
50    name,
51    Stream.generate(supplier)
52    .limit(testedBuilder.getNumberOfRows())
53    .toList()
54    );
55    });
56   
57  4 assertThat(testedResult)
58    .isEqualTo(expectedExpandedValues);
59    }
60   
 
61  1 toggle static Arguments[] buildSupplierFactory()
62    {
63  1 return new Arguments[] {
64    arguments( // Single column
65    ofMap(
66    "A", List.of(1, 2)
67    ),
68    ofMap(
69    "A", List.of(1, 2)
70    )
71    ),
72    arguments( // Different size, two columns
73    ofMap(
74    "A", List.of(1, 2),
75    "B", List.of(1, 2, 3)
76    ),
77    ofMap(
78    "A", List.of(1, 1, 1, 2, 2, 2),
79    "B", List.of(1, 2, 3, 1, 2, 3)
80    )
81    ),
82    arguments( // Different size, three columns
83    ofMap(
84    "A", List.of(1, 2),
85    "B", List.of(1, 2, 3),
86    "C", List.of(7, 8)
87    ),
88    ofMap(
89    "A", List.of(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
90    "B", List.of(1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3),
91    "C", List.of(7, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 8)
92    )
93    ),
94    arguments( // Same size, more columns
95    ofMap(
96    "A", List.of("A1", "A2"),
97    "B", List.of("B1", "B2"),
98    "C", List.of("C1", "C2"),
99    "D", List.of("D1", "D2")
100    ),
101    ofMap(
102    "A", List.of(
103    "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1",
104    "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2"
105    ),
106    "B", List.of(
107    "B1", "B1", "B1", "B1", "B2", "B2", "B2", "B2",
108    "B1", "B1", "B1", "B1", "B2", "B2", "B2", "B2"
109    ),
110    "C", List.of(
111    "C1", "C1", "C2", "C2", "C1", "C1", "C2", "C2",
112    "C1", "C1", "C2", "C2", "C1", "C1", "C2", "C2"
113    ),
114    "D", List.of(
115    "D1", "D2", "D1", "D2", "D1", "D2", "D1", "D2",
116    "D1", "D2", "D1", "D2", "D1", "D2", "D1", "D2"
117    )
118    )
119    ),
120    };
121    }
122   
 
123  8 toggle private static Map<String, List<?>> ofMap(
124    String key, List<?> values,
125    Object... keyValues
126    ) {
127  8 var result = new LinkedHashMap<String, List<?>>();
128   
129  8 result.put(key, values);
130   
131  20 for (int i = 0; i < keyValues.length; i += 2) {
132  12 result.put(
133    (String)keyValues[i],
134    (List<?>)keyValues[i + 1]
135    );
136    }
137   
138  8 return result;
139    }
140    }