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

File JdbcDataGeneratorTest.java

 

Code metrics

0
29
11
3
260
197
11
0.38
2.64
3.67
1

Classes

Class Line # Actions
JdbcDataGeneratorTest 22 29 0% 11 0
1.0100%
JdbcDataGeneratorTest.SampleEnum 163 0 - 0 0
-1.0 -
JdbcDataGeneratorTest.TestCase 169 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    package guru.mikelue.foxglove.jdbc;
2   
3    import java.util.List;
4    import java.util.function.Consumer;
5    import java.util.function.Supplier;
6    import java.util.stream.Collectors;
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.CsvSource;
14    import org.junit.jupiter.params.provider.MethodSource;
15   
16    import guru.mikelue.foxglove.test.AbstractJdbcTestBase;
17   
18    import static guru.mikelue.foxglove.test.SampleSchema.*;
19    import static org.assertj.core.api.Assertions.assertThat;
20    import static org.junit.jupiter.params.provider.Arguments.arguments;
21   
 
22    public class JdbcDataGeneratorTest extends AbstractJdbcTestBase {
 
23  12 toggle public JdbcDataGeneratorTest() {}
24   
 
25  12 toggle @BeforeEach
26    void setup()
27    {
28  12 deleteAll(TABLE_DATA_TYPES, TABLE_RENT, TABLE_CAR_FEATURE, TABLE_CAR, TABLE_CAR_ARCHIVED, TABLE_MEMBER);
29    }
30   
 
31  12 toggle @AfterEach
32    void tearDown() {}
33   
34    /**
35    * Tests the generating of data for referencing columns.
36    */
 
37  2 toggle @ParameterizedTest
38    @MethodSource
39    void referencing(
40    int parentRows, int childRowsPerParent,
41    int expectedNumberOfChildRows
42    ) {
43  2 var parentTable = JdbcTableFacet.builder(TABLE_CAR)
44    .numberOfRows(parentRows)
45    .build();
46  2 var childTable = JdbcTableFacet.builder(TABLE_CAR_FEATURE)
47    .referencing("cf_cr_id")
48    .parent(parentTable, "cr_id")
49    .cardinality(childRowsPerParent)
50    .column("cf_feature_name")
51    .forRow(rowIndex -> "Feature-" + (rowIndex + 1))
52    .build();
53   
54  2 int testedNumber = getDataGenerator()
55    .generate(parentTable, childTable);
56   
57  2 assertThat(testedNumber)
58    .isEqualTo(expectedNumberOfChildRows + parentRows);
59   
60  2 assertNumberOfRows(
61    TABLE_CAR_FEATURE, "cf_feature_name LIKE 'Feature-%'"
62    )
63    .isEqualTo(expectedNumberOfChildRows);
64    }
65   
 
66  1 toggle static Arguments[] referencing()
67    {
68  1 return new Arguments[] {
69    // 3 parents, 2 children per parent
70    Arguments.arguments(3, 2, 6),
71    // 5 parents, 1 children per parent
72    Arguments.arguments(5, 1, 5),
73    };
74    }
75   
76    /**
77    * Tests the value domain from another table.
78    */
 
79  3 toggle @ParameterizedTest
80    @MethodSource
81    void from(
82    int parentRows, List<String> sampleColors
83    ) {
84  3 final int numberOfRows = gen().ints().range(10, 30).get();
85   
86    /*
87    * Sets up the table facets.
88    */
89  3 var parentTable = JdbcTableFacet.builder(TABLE_CAR)
90    .numberOfRows(parentRows)
91    .column("cr_color")
92    .roundRobin(sampleColors)
93    .column("cr_model")
94    .fixed("GM-001")
95    .build();
96   
97  3 var childTable = JdbcTableFacet.builder(TABLE_CAR_ARCHIVED)
98    .numberOfRows(numberOfRows)
99    .column("ca_color")
100    .from(parentTable, "cr_color")
101    .roundRobin()
102    .column("ca_model")
103    .from(parentTable, "cr_model")
104    .random()
105    .build();
106   
107  3 var colorNamesForQuery = sampleColors.stream()
108    .map(color -> "'" + color + "'")
109    .collect(Collectors.joining(", "));
110   
111  3 getDataGenerator()
112    .generate(parentTable, childTable);
113   
114    /*
115    * Asserts the number of rows in referencing table,
116    * which has expected colors.
117    */
118  3 assertNumberOfRows(
119    TABLE_CAR_ARCHIVED,
120    " ca_color IN (" + colorNamesForQuery + ") AND" +
121    " ca_model = 'GM-001'"
122    )
123    .isEqualTo(numberOfRows);
124    // :~)
125    }
 
126  1 toggle static Arguments[] from()
127    {
128  1 return new Arguments[] {
129    arguments(10, List.of("Purple", "Grass Green")),
130    arguments(10, List.of("Red", "Green", "Blue")),
131    arguments(20, List.of("Yellow", "Black", "White", "Silver")),
132    };
133    }
134   
135    /**
136    * Tests the generating of data.
137    */
 
138  3 toggle @ParameterizedTest
139    @MethodSource
140    void generate(
141    Consumer<JdbcTableFacet.Builder> facetCustomizer,
142    String whereCondition, int expectedNumberOfRows
143    ) {
144  3 var builder = JdbcTableFacet.builder(TABLE_DATA_TYPES);
145  3 facetCustomizer.accept(builder);
146  3 var facet = builder.build();
147   
148  3 int testedNumber = getDataGenerator()
149    .generate(facet);
150   
151    /*
152    * Asserts the:
153    * 1) returned value from generate
154    * 2) actual number of rows in the table
155    */
156  3 assertThat(testedNumber)
157    .isEqualTo(expectedNumberOfRows);
158  3 assertNumberOfRows(TABLE_DATA_TYPES, whereCondition)
159    .isEqualTo(expectedNumberOfRows);
160    // :~)
161    }
162   
 
163    enum SampleEnum {
164    VAL_A, VAL_B, VAL_C
165    }
166   
 
167  1 toggle static Stream<Arguments> generate()
168    {
 
169    record TestCase(
170    Consumer<JdbcTableFacet.Builder> facetCustomizer,
171    String whereClause, int expectedNumberOfRows
172    ) {}
173   
174  1 var enumSupplier = gen().enumOf(SampleEnum.class);
175  1 Supplier<String> enumAsTextSupplier = () -> enumSupplier.get().name();
176   
177  1 return Stream.<TestCase>of(
178    // Default case
179    new TestCase(
180    builder -> builder
181    .numberOfRows(10)
182    .column("st_big_int")
183    .fixed(null)
184    .column("st_enum")
185    .useSupplier(enumAsTextSupplier)
186    .excludeColumns("st_double"),
187    "st_big_int IS NULL AND st_double IS NULL", 10
188    ),
189    // By key column
190    new TestCase(
191    builder -> builder
192    .keyOfInt("st_id").limit(1000, 7),
193    "st_id BETWEEN 1000 AND 1006", 7
194    ),
195    // By Cartesian product
196    new TestCase(
197    builder -> builder
198    .cartesianProduct("st_int")
199    .domain(1, 2, 3)
200    .cartesianProduct("st_varchar")
201    .domain("A", "B", "C")
202    .includeColumns("st_float", "st_large_text"),
203    "st_int IN (1, 2, 3) AND st_varchar IN ('A', 'B', 'C')", 9
204    )
205    )
206    .map(tc -> Arguments.arguments(
207    tc.facetCustomizer(),
208    tc.whereClause(),
209    tc.expectedNumberOfRows()
210    ));
211    }
212   
213    /**
214    * Tests the generating of data by Cartesian product,
215    * which the domains come from other tables.
216    */
 
217  4 toggle @ParameterizedTest
218    @CsvSource({
219    "1,1", "1,2", "3,2", "5,5",
220    })
221    void cartesianProductByReferencing(
222    int numberOfRowsForCar, int numberOfRowsForMember
223    ) {
224  4 var dataOfCars = JdbcTableFacet.builder(TABLE_CAR)
225    .keyOfInt("cr_id")
226    .limit(1000, numberOfRowsForCar)
227    .build();
228  4 var dataOfMembers = JdbcTableFacet.builder(TABLE_MEMBER)
229    .keyOfInt("mb_id")
230    .limit(2000, numberOfRowsForMember)
231    .build();
232   
233  4 var rentData = JdbcTableFacet.builder(TABLE_RENT)
234    .cartesianProduct("rt_cr_id")
235    .referencing(dataOfCars, "cr_id")
236    .cartesianProduct("rt_mb_id")
237    .referencing(dataOfMembers, "mb_id")
238    .build();
239   
240  4 getDataGenerator()
241    .generate(dataOfCars, dataOfMembers, rentData);
242   
243  4 assertNumberOfRows(
244    TABLE_RENT,
245    String.format(
246    """ rt_cr_id BETWEEN 1000 AND %d AND rt_mb_id BETWEEN 2000 AND %d """,
247    1000 + numberOfRowsForCar - 1,
248    2000 + numberOfRowsForMember - 1
249    )
250    )
251    .isEqualTo(numberOfRowsForCar * numberOfRowsForMember);
252    }
253   
254    private JdbcDataGenerator getDataGenerator()
255    {
 
256  12 toggle return new JdbcDataGenerator(getDataSource());
257    }
258  12 }