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

File DataSettingTest.java

 

Code metrics

0
18
10
1
159
118
10
0.56
1.8
10
1

Classes

Class Line # Actions
DataSettingTest 24 18 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    package guru.mikelue.foxglove.setting;
2   
3    import java.sql.JDBCType;
4   
5    import org.junit.jupiter.api.AfterEach;
6    import org.junit.jupiter.api.BeforeEach;
7    import org.junit.jupiter.api.Test;
8    import org.junit.jupiter.params.ParameterizedTest;
9    import org.junit.jupiter.params.provider.Arguments;
10    import org.junit.jupiter.params.provider.MethodSource;
11   
12    import guru.mikelue.misc.testlib.AbstractTestBase;
13   
14    import guru.mikelue.foxglove.ColumnMeta;
15    import guru.mikelue.foxglove.ColumnMeta.Property;
16   
17    import static guru.mikelue.foxglove.ColumnMeta.Property.*;
18    import static guru.mikelue.foxglove.ColumnMetaTestUtils.newColumnMeta;
19    import static java.util.concurrent.TimeUnit.SECONDS;
20    import static org.assertj.core.api.Assertions.assertThat;
21    import static org.awaitility.Awaitility.await;
22    import static org.junit.jupiter.params.provider.Arguments.arguments;
23   
 
24    public class DataSettingTest extends AbstractTestBase {
 
25  11 toggle public DataSettingTest() {}
26   
 
27  11 toggle @BeforeEach
28    void setup() {}
29   
 
30  11 toggle @AfterEach
31    void tearDown() {}
32   
33    /**
34    * Tests the setting-up for properties of auto-generating.
35    */
 
36  7 toggle @ParameterizedTest
37    @MethodSource
38    void isAutoGenerating(
39    ColumnMeta sampleColumn,
40    boolean expectedGenerating
41    ) {
42  7 var testedSetting = DataSetting.defaults()
43    .givenType("status")
44    .useSupplier(() -> "ACTIVE")
45    .columnMatcher(columnMeta ->
46    columnMeta.jdbcType() == JDBCType.ARRAY
47    )
48    .decideSupplier(columnMeta -> () -> new Object[0])
49    .excludeWhen(columnMeta ->
50    columnMeta.name().startsWith("audit_")
51    );
52   
53  7 assertThat(testedSetting.isAutoGenerating(sampleColumn))
54    .isEqualTo(expectedGenerating);
55    }
 
56  1 toggle static Arguments[] isAutoGenerating()
57    {
58  1 return new Arguments[] {
59    // Excluded by custom rule
60    arguments(newColumnMeta("audit_not1"), false),
61    // supported types by custom matcher
62    arguments(sampleColumn(JDBCType.ARRAY, GENERATED), true),
63    /*
64    * Not auto-generated by default
65    */
66    arguments(sampleColumn(AUTO_INCREMENT), false),
67    arguments(sampleColumn(GENERATED), false),
68    // :~)
69    // Supported types
70    arguments(sampleColumn(JDBCType.VARCHAR, NULLABLE), true),
71    // Supported type names
72    arguments(newColumnMeta("status", NULLABLE), true),
73    // Not supported types
74    arguments(sampleColumn(JDBCType.ROWID, GENERATED), false)
75    };
76    }
77   
78    /**
79    * Tests the resolving of suppliers by different column properties.
80    */
 
81  3 toggle @ParameterizedTest
82    @MethodSource
83    void resolveSupplier(
84    ColumnMeta sampleColumnMeta,
85    String expectedValue
86    )
87    {
88  3 var testedSetting = new DataSetting();
89   
90  3 testedSetting.givenType(JDBCType.VARCHAR)
91    .useSupplier(() -> "test-string-1");
92  3 testedSetting.givenType(JDBCType.OTHER) // Should have lower priority of all
93    .useSupplier(() -> "test-string-error");
94   
95  3 testedSetting.givenType("LINT CHAR") // Should have middle priority(for name of "cl_special")
96    .useSupplier(() -> "test-string-2");
97   
98  3 testedSetting.columnMatcher(c -> c.name().equals("cl_special"))
99    .useSupplier(() -> "test-string-3");
100   
101  3 var testedSupplier = testedSetting.resolveSupplier(sampleColumnMeta)
102    .get();
103   
104  3 assertThat(testedSupplier.get())
105    .isEqualTo(expectedValue);
106    }
107   
 
108  1 toggle static Arguments[] resolveSupplier()
109    {
110  1 return new Arguments[] {
111    arguments( // By column matcher
112    newColumnMeta("cl_special", JDBCType.OTHER),
113    "test-string-3"
114    ),
115    arguments( // By type name
116    newColumnMeta("cl_diff", "LINT CHAR", JDBCType.OTHER),
117    "test-string-2"
118    ),
119    arguments( // By JDBC type
120    newColumnMeta("cl_another", JDBCType.VARCHAR),
121    "test-string-1"
122    ),
123    };
124    }
125   
126    /**
127    * Tests the nullable value generation.
128    */
 
129  1 toggle @Test
130    void generateNullable()
131    {
132  1 var testedSetting = new DataSetting()
133    .givenType(JDBCType.VARCHAR)
134    .useSupplier(() -> "not-null-value")
135    .generateNull(2);
136   
137  1 var sampleColumn = sampleColumn(NULLABLE);
138   
139  1 var supplier = testedSetting.resolveSupplier(sampleColumn)
140    .get();
141   
142  1 await()
143    .atMost(5, SECONDS)
144    .untilAsserted(() -> {
145  1 assertThat(supplier.get())
146    .isNull();
147    });
148    }
149   
 
150  3 toggle private static ColumnMeta sampleColumn(Property... properties)
151    {
152  3 return newColumnMeta("any_column", JDBCType.VARCHAR, properties);
153    }
154   
 
155  3 toggle private static ColumnMeta sampleColumn(JDBCType jdbcType, Property... properties)
156    {
157  3 return newColumnMeta("any_column", jdbcType, properties);
158    }
159    }