| 1 |
|
package guru.mikelue.foxglove.jdbc; |
| 2 |
|
|
| 3 |
|
import java.sql.*; |
| 4 |
|
import java.util.EnumSet; |
| 5 |
|
import java.util.List; |
| 6 |
|
import java.util.function.Consumer; |
| 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.api.Test; |
| 12 |
|
import org.junit.jupiter.params.ParameterizedTest; |
| 13 |
|
import org.junit.jupiter.params.provider.Arguments; |
| 14 |
|
import org.junit.jupiter.params.provider.CsvSource; |
| 15 |
|
import org.junit.jupiter.params.provider.MethodSource; |
| 16 |
|
|
| 17 |
|
import guru.mikelue.foxglove.ColumnMeta; |
| 18 |
|
import guru.mikelue.foxglove.setting.DataSetting; |
| 19 |
|
import guru.mikelue.misc.testlib.AbstractTestBase; |
| 20 |
|
|
| 21 |
|
import mockit.Expectations; |
| 22 |
|
import mockit.Mocked; |
| 23 |
|
|
| 24 |
|
import static guru.mikelue.foxglove.ColumnMeta.Property.*; |
| 25 |
|
import static guru.mikelue.foxglove.ColumnMetaTestUtils.newColumnMeta; |
| 26 |
|
import static org.assertj.core.api.Assertions.assertThat; |
| 27 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (85) |
Complexity: 21 |
Complexity Density: 0.33 |
|
| 28 |
|
public class MetaUtilsTest extends AbstractTestBase { |
| 29 |
|
@Mocked |
| 30 |
|
private DatabaseMetaData mockDbMeta; |
| 31 |
|
@Mocked |
| 32 |
|
private ResultSet mockRs; |
| 33 |
|
|
| 34 |
|
private final static String TEST_TABLE = "ap_table"; |
| 35 |
|
private final static String SAMPLE_COLUMN_NAME = "rb_any_column"; |
| 36 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 37 |
16 |
public MetaUtilsTest() {}... |
| 38 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 39 |
16 |
@BeforeEach... |
| 40 |
|
void setup() {} |
| 41 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 42 |
16 |
@AfterEach... |
| 43 |
|
void tearDown() {} |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
@link |
| 47 |
|
@link |
| 48 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
4-
|
|
| 49 |
3 |
@ParameterizedTest... |
| 50 |
|
@MethodSource |
| 51 |
|
void filterColumns( |
| 52 |
|
Consumer<JdbcTableFacet.Builder> setupBuilder, |
| 53 |
|
List<String> expectedInclusions |
| 54 |
|
) { |
| 55 |
3 |
var builder = JdbcTableFacet.builder("kc_person"); |
| 56 |
3 |
setupBuilder.accept(builder); |
| 57 |
3 |
var sampleTable = builder.build(); |
| 58 |
|
|
| 59 |
3 |
var sampleSetting = new DataSetting() |
| 60 |
|
.excludeWhen(columnMeta -> columnMeta.name().equals("kc_excluded_by_setting")); |
| 61 |
|
|
| 62 |
3 |
var testedResult = MetaUtils.filterColumns( |
| 63 |
|
List.of( |
| 64 |
|
newColumnMeta("kc_included_1"), |
| 65 |
|
newColumnMeta("kc_included_2"), |
| 66 |
|
newColumnMeta("kc_excluded_1"), |
| 67 |
|
newColumnMeta("kc_excluded_2"), |
| 68 |
|
newColumnMeta("kc_excluded_by_setting") |
| 69 |
|
), |
| 70 |
|
sampleSetting, sampleTable |
| 71 |
|
); |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
3 |
assertThat(testedResult) |
| 77 |
|
.extracting(ColumnMeta::name) |
| 78 |
|
.containsExactlyInAnyOrderElementsOf(expectedInclusions); |
| 79 |
|
|
| 80 |
|
} |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 81 |
1 |
static Stream<Arguments> filterColumns()... |
| 82 |
|
{ |
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 83 |
|
record TestCase( |
| 84 |
|
Consumer<JdbcTableFacet.Builder> builder, |
| 85 |
|
List<String> expectedInclusions |
| 86 |
|
) {} |
| 87 |
|
|
| 88 |
1 |
return Stream.of( |
| 89 |
|
new TestCase( |
| 90 |
|
builder -> builder |
| 91 |
|
.includeColumns("kc_included_1", "kc_included_2"), |
| 92 |
|
List.of( |
| 93 |
|
"kc_included_1", "kc_included_2" |
| 94 |
|
) |
| 95 |
|
), |
| 96 |
|
new TestCase( |
| 97 |
|
builder -> builder |
| 98 |
|
.excludeColumns("kc_excluded_1", "kc_excluded_2"), |
| 99 |
|
List.of("kc_included_1", "kc_included_2") |
| 100 |
|
), |
| 101 |
|
new TestCase( |
| 102 |
|
builder -> builder |
| 103 |
|
.column("kc_included_1").fixed(20), |
| 104 |
|
List.of("kc_included_1", "kc_included_2", "kc_excluded_1", "kc_excluded_2") |
| 105 |
|
) |
| 106 |
|
) |
| 107 |
|
.map(testCase -> Arguments.of( |
| 108 |
|
testCase.builder, |
| 109 |
|
testCase.expectedInclusions |
| 110 |
|
)); |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
4-
|
|
| 116 |
1 |
@Test... |
| 117 |
|
void descriptiveMeta() throws SQLException |
| 118 |
|
{ |
| 119 |
1 |
final String sampleTypeName = "VARCHAR"; |
| 120 |
1 |
final int sampleColumnSize = 32; |
| 121 |
1 |
final int sampleDecimalDigits = 10; |
| 122 |
|
|
| 123 |
1 |
mockOneRow(); |
| 124 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
| 125 |
1 |
new Expectations() {{... |
| 126 |
1 |
mockRs.getString("TYPE_NAME"); |
| 127 |
1 |
result = sampleTypeName; |
| 128 |
|
|
| 129 |
1 |
mockRs.getInt("DATA_TYPE"); |
| 130 |
1 |
result = Types.VARCHAR; |
| 131 |
|
|
| 132 |
1 |
mockRs.getInt("COLUMN_SIZE"); |
| 133 |
1 |
result = sampleColumnSize; |
| 134 |
|
|
| 135 |
1 |
mockRs.getInt("DECIMAL_DIGITS"); |
| 136 |
1 |
result = sampleDecimalDigits; |
| 137 |
|
}}; |
| 138 |
|
|
| 139 |
1 |
var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE); |
| 140 |
|
|
| 141 |
1 |
assertThat(testedMeta.get(0)) |
| 142 |
|
.hasFieldOrPropertyWithValue("name", SAMPLE_COLUMN_NAME) |
| 143 |
|
.hasFieldOrPropertyWithValue("typeName", sampleTypeName) |
| 144 |
|
.hasFieldOrPropertyWithValue("jdbcType", JDBCType.VARCHAR) |
| 145 |
|
.hasFieldOrPropertyWithValue("size", sampleColumnSize) |
| 146 |
|
.hasFieldOrPropertyWithValue("decimalDigits", sampleDecimalDigits); |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
| 152 |
3 |
@ParameterizedTest... |
| 153 |
|
@CsvSource({ |
| 154 |
|
"0, false", |
| 155 |
|
"1, true", |
| 156 |
|
"2, true" |
| 157 |
|
}) |
| 158 |
|
void isNullable(int nullableValue, boolean expectedNullable) throws SQLException |
| 159 |
|
{ |
| 160 |
3 |
mockOneRow(); |
| 161 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 162 |
3 |
new Expectations() {{... |
| 163 |
3 |
mockRs.getInt("NULLABLE"); |
| 164 |
3 |
result = nullableValue; |
| 165 |
|
}}; |
| 166 |
|
|
| 167 |
3 |
var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE); |
| 168 |
3 |
assertContains(testedMeta.get(0).properties(), |
| 169 |
|
NULLABLE, expectedNullable |
| 170 |
|
); |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
| 176 |
2 |
@ParameterizedTest... |
| 177 |
|
@CsvSource( |
| 178 |
|
value={ |
| 179 |
|
"NULL, false", |
| 180 |
|
"10, true" |
| 181 |
|
}, |
| 182 |
|
nullValues={"NULL"} |
| 183 |
|
) |
| 184 |
|
void hasDefaultValue(Integer defaultValue, boolean expectedHasDefault) throws SQLException |
| 185 |
|
{ |
| 186 |
2 |
mockOneRow(); |
| 187 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 188 |
2 |
new Expectations() {{... |
| 189 |
2 |
mockRs.getString("COLUMN_DEF"); |
| 190 |
2 |
result = defaultValue; |
| 191 |
|
}}; |
| 192 |
|
|
| 193 |
2 |
var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE); |
| 194 |
2 |
assertContains(testedMeta.get(0).properties(), DEFAULT_VALUE, expectedHasDefault); |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
| 200 |
3 |
@ParameterizedTest... |
| 201 |
|
@CsvSource({ |
| 202 |
|
"YES, true", |
| 203 |
|
"NO, false", |
| 204 |
|
", false" |
| 205 |
|
}) |
| 206 |
|
void isAutoIncrement(String isAutoIncrementValue, boolean expectedIsAutoIncrement) throws SQLException |
| 207 |
|
{ |
| 208 |
3 |
mockOneRow(); |
| 209 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 210 |
3 |
new Expectations() {{... |
| 211 |
3 |
mockRs.getString("IS_AUTOINCREMENT"); |
| 212 |
3 |
result = isAutoIncrementValue; |
| 213 |
|
|
| 214 |
|
}}; |
| 215 |
|
|
| 216 |
3 |
var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE); |
| 217 |
3 |
assertContains(testedMeta.get(0).properties(), |
| 218 |
|
AUTO_INCREMENT, expectedIsAutoIncrement |
| 219 |
|
); |
| 220 |
|
} |
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
| 225 |
3 |
@ParameterizedTest... |
| 226 |
|
@CsvSource({ |
| 227 |
|
"YES, true", |
| 228 |
|
"NO, false", |
| 229 |
|
", false" |
| 230 |
|
}) |
| 231 |
|
void isGeneratedColumn(String isGeneratedColumnValue, boolean expectedIsGeneratedColumn) throws SQLException |
| 232 |
|
{ |
| 233 |
3 |
mockOneRow(); |
| 234 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 235 |
3 |
new Expectations() {{... |
| 236 |
3 |
mockRs.getString("IS_GENERATEDCOLUMN"); |
| 237 |
3 |
result = isGeneratedColumnValue; |
| 238 |
|
}}; |
| 239 |
|
|
| 240 |
3 |
var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE); |
| 241 |
3 |
assertContains(testedMeta.get(0).properties(), |
| 242 |
|
GENERATED, expectedIsGeneratedColumn |
| 243 |
|
); |
| 244 |
|
} |
| 245 |
|
|
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
| 249 |
1 |
@Test... |
| 250 |
|
void buildInsertSql() throws SQLException |
| 251 |
|
{ |
| 252 |
1 |
var sampleColumns = List.of( |
| 253 |
|
newColumnMeta("kc_name", JDBCType.VARCHAR), |
| 254 |
|
newColumnMeta("kc_age", JDBCType.INTEGER), |
| 255 |
|
newColumnMeta("kc_address", JDBCType.VARCHAR) |
| 256 |
|
); |
| 257 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 258 |
1 |
new Expectations() {{... |
| 259 |
1 |
mockDbMeta.getIdentifierQuoteString(); |
| 260 |
1 |
result = "`"; |
| 261 |
|
}}; |
| 262 |
|
|
| 263 |
1 |
var testedSql = MetaUtils.buildInsertSql(mockDbMeta, "kc_person", sampleColumns); |
| 264 |
|
|
| 265 |
1 |
getLogger().debug("Generated SQL:\n{}", testedSql); |
| 266 |
|
|
| 267 |
1 |
assertThat(testedSql) |
| 268 |
|
.contains("INSERT INTO kc_person (kc_name, kc_age, kc_address)") |
| 269 |
|
.contains("?, ?, ?"); |
| 270 |
|
} |
| 271 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 272 |
12 |
private void mockOneRow() throws SQLException... |
| 273 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 274 |
12 |
new Expectations() {{... |
| 275 |
12 |
mockDbMeta.getColumns(null, null, TEST_TABLE, null); |
| 276 |
12 |
result = mockRs; |
| 277 |
|
|
| 278 |
12 |
mockRs.next(); |
| 279 |
12 |
returns(true, false); |
| 280 |
|
|
| 281 |
12 |
mockRs.getString("COLUMN_NAME"); |
| 282 |
12 |
result = SAMPLE_COLUMN_NAME; |
| 283 |
|
}}; |
| 284 |
|
} |
| 285 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 286 |
11 |
private static void assertContains(... |
| 287 |
|
EnumSet<ColumnMeta.Property> testedProperties, ColumnMeta.Property checkedProperty, |
| 288 |
|
boolean expectedContains |
| 289 |
|
) { |
| 290 |
11 |
if (expectedContains) { |
| 291 |
5 |
assertThat(testedProperties) |
| 292 |
|
.contains(checkedProperty); |
| 293 |
|
} else { |
| 294 |
6 |
assertThat(testedProperties) |
| 295 |
|
.doesNotContain(checkedProperty); |
| 296 |
|
} |
| 297 |
|
} |
| 298 |
|
} |