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

File ManyToManyTest.java

 

Code metrics

0
21
7
1
173
103
7
0.33
3
7
1

Classes

Class Line # Actions
ManyToManyTest 15 21 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    package guru.mikelue.foxglove.examples;
2   
3    import java.util.stream.LongStream;
4   
5    import org.junit.jupiter.api.AfterEach;
6    import org.junit.jupiter.api.BeforeEach;
7    import org.junit.jupiter.api.Test;
8   
9    import guru.mikelue.foxglove.jdbc.JdbcDataGenerator;
10    import guru.mikelue.foxglove.jdbc.JdbcTableFacet;
11    import guru.mikelue.foxglove.test.AbstractJdbcTestBase;
12   
13    import static guru.mikelue.foxglove.test.SampleSchema.*;
14   
 
15    public class ManyToManyTest extends AbstractJdbcTestBase {
 
16  3 toggle public ManyToManyTest() {}
17   
 
18  3 toggle @BeforeEach
19    void setup() {}
20   
 
21  3 toggle @AfterEach
22    void tearDown()
23    {
24  3 deleteAll(TABLE_RENT, TABLE_CAR, TABLE_MEMBER);
25    }
26   
27    /**
28    * Example for fixed domain of a many-to-many relationship.
29    */
 
30  1 toggle @Test
31    void fixedDomain()
32    {
33    // tag::fixedDomain[]
34    // The domain of ids of cars
35  1 var idsOfCars = LongStream.range(11, 21).boxed()
36    .toList();
37    // The domain of ids of members
38  1 var idsOfMembers = LongStream.range(101, 111).boxed()
39    .toList();
40   
41    /*
42    * Sets up the facets for car and member tables by their domain of ids.
43    */
44  1 var carFacet = JdbcTableFacet.builder(TABLE_CAR)
45    .keyOfInt("cr_id").domain(idsOfCars)
46    .build();
47  1 var memberFacet = JdbcTableFacet.builder(TABLE_MEMBER)
48    .keyOfInt("mb_id").domain(idsOfMembers)
49    .build();
50    // :~)
51   
52    /*
53    * Uses Cartesian product to set up the many-to-many relationship
54    */
55  1 var rentFacet = JdbcTableFacet.builder(TABLE_RENT)
56    .cartesianProduct("rt_cr_id").domain(idsOfCars)
57    .cartesianProduct("rt_mb_id").domain(idsOfMembers)
58    .build();
59    // :~)
60   
61  1 dataGenerator()
62    .generate(carFacet, memberFacet, rentFacet);
63    // end::fixedDomain[]
64   
65  1 assertNumberOfRows(
66    TABLE_RENT,
67    " rt_cr_id BETWEEN 11 AND 20 AND" +
68    " rt_mb_id BETWEEN 101 AND 110"
69    )
70    .isEqualTo(idsOfCars.size() * idsOfMembers.size());
71    }
72   
73    /**
74    * Example for referencing column of one side in a many-to-many relationship.
75    */
 
76  1 toggle @Test
77    void oneSideReferencing()
78    {
79    // tag::oneSideReferencing[]
80  1 final int numberOfCars = 10;
81   
82    // The domain of ids of members
83  1 var idsOfMembers = LongStream.range(101, 111).boxed()
84    .toList();
85   
86    /*
87    * Can be any way to generate the ids of cars
88    */
89  1 var carFacet = JdbcTableFacet.builder(TABLE_CAR)
90    .keyOfInt("cr_id").limit(1020, numberOfCars)
91    .build();
92    // :~)
93    /*
94    * Fixed domain of ids of members
95    */
96  1 var memberFacet = JdbcTableFacet.builder(TABLE_MEMBER)
97    .keyOfInt("mb_id").domain(idsOfMembers)
98    .build();
99    // :~)
100   
101    /*
102    * Uses Cartesian product to set up the many-to-many relationship
103    */
104  1 var rentFacet = JdbcTableFacet.builder(TABLE_RENT)
105    // References to the ids of cars
106    .referencing("rt_cr_id").parent(carFacet, "cr_id")
107    // The cardinality is the number of members
108    .cardinality(idsOfMembers.size())
109    // The domain comes from the ids of members
110    .column("rt_mb_id").from(memberFacet, "mb_id")
111    // Uses round-robin to cover all members
112    .roundRobin()
113    .build();
114    // :~)
115   
116  1 dataGenerator()
117    .generate(carFacet, memberFacet, rentFacet);
118    // end::oneSideReferencing[]
119   
120  1 assertNumberOfRows(
121    TABLE_RENT,
122    " rt_cr_id >= 1020 AND" +
123    " rt_mb_id BETWEEN 101 AND 110"
124    )
125    .isEqualTo(10 * idsOfMembers.size());
126    }
127   
128    /**
129    * Example for Cartesian product with referencing column.
130    */
 
131  1 toggle @Test
132    void cartesianProductByReferencing()
133    {
134    // tag::cartesianProductByReferencing[]
135    // Any way to generate data for these two tables
136  1 var carFacet = JdbcTableFacet.builder(TABLE_CAR)
137    .keyOfInt("cr_id").limit(1000, 3)
138    .build();
139  1 var memberFacet = JdbcTableFacet.builder(TABLE_MEMBER)
140    .keyOfInt("mb_id").limit(2000, 5)
141    .build();
142    // :~)
143   
144  1 var rentFacet = JdbcTableFacet.builder(TABLE_RENT)
145    // References to ids of cars
146    .cartesianProduct("rt_cr_id")
147    .referencing(carFacet, "cr_id")
148    // References to ids of members
149    .cartesianProduct("rt_mb_id")
150    .referencing(memberFacet, "mb_id")
151    .build();
152    // end::cartesianProductByReferencing[]
153   
154  1 dataGenerator()
155    .generate(carFacet, memberFacet, rentFacet);
156   
157  1 assertNumberOfRows(
158    TABLE_RENT,
159    String.format(
160    """ rt_cr_id BETWEEN 1000 AND %d AND rt_mb_id BETWEEN 2000 AND %d """,
161    1002, 2004
162    )
163    )
164    .isEqualTo(15);
165    }
166   
167    private JdbcDataGenerator dataGenerator()
168    {
 
169  3 toggle return new JdbcDataGenerator(getDataSource());
170    }
171  3 }