|
| 1 | +/* Copyright (c) 2024 Airbyte, Inc., all rights reserved. */ |
| 2 | +package io.airbyte.integrations.source.mysql |
| 3 | + |
| 4 | +import io.airbyte.cdk.ConfigErrorException |
| 5 | +import io.airbyte.cdk.check.JdbcCheckQueries |
| 6 | +import io.airbyte.cdk.discover.MetadataQuerier |
| 7 | +import io.airbyte.cdk.jdbc.DefaultJdbcConstants |
| 8 | +import io.airbyte.cdk.jdbc.JdbcConnectionFactory |
| 9 | +import java.sql.Connection |
| 10 | +import java.sql.Statement |
| 11 | +import org.junit.jupiter.api.Assertions |
| 12 | +import org.junit.jupiter.api.BeforeAll |
| 13 | +import org.junit.jupiter.api.Test |
| 14 | +import org.junit.jupiter.api.Timeout |
| 15 | +import org.junit.jupiter.api.assertThrows |
| 16 | +import org.testcontainers.containers.MySQLContainer |
| 17 | + |
| 18 | +class MySqlSourceMetadataQuerierTableFilterTest { |
| 19 | + companion object { |
| 20 | + val dbContainer: MySQLContainer<*> = MySqlContainerFactory.shared(imageName = "mysql:9.2.0") |
| 21 | + |
| 22 | + // In MySQL, database == schema. We test within 'test' database |
| 23 | + val databaseName = "test" |
| 24 | + val tableNames = listOf("orders", "customers", "products", "invoices") |
| 25 | + |
| 26 | + val factory = |
| 27 | + MySqlSourceMetadataQuerier.Factory( |
| 28 | + constants = DefaultJdbcConstants(), |
| 29 | + selectQueryGenerator = MySqlSourceOperations(), |
| 30 | + fieldTypeMapper = MySqlSourceOperations(), |
| 31 | + checkQueries = JdbcCheckQueries(), |
| 32 | + ) |
| 33 | + |
| 34 | + @JvmStatic |
| 35 | + @BeforeAll |
| 36 | + @Timeout(value = 300) |
| 37 | + fun setupDatabase() { |
| 38 | + val config: MySqlSourceConfiguration = |
| 39 | + MySqlSourceConfigurationFactory().make(MySqlContainerFactory.config(dbContainer)) |
| 40 | + |
| 41 | + val connectionFactory = JdbcConnectionFactory(config) |
| 42 | + |
| 43 | + connectionFactory.get().use { connection: Connection -> |
| 44 | + connection.isReadOnly = false |
| 45 | + connection.createStatement().use { |
| 46 | + it.execute("CREATE DATABASE IF NOT EXISTS test") |
| 47 | + } |
| 48 | + connection.createStatement().use { it.execute("USE test") } |
| 49 | + |
| 50 | + // Create test tables |
| 51 | + tableNames.forEach { tableName -> |
| 52 | + connection.createStatement().use { stmt: Statement -> |
| 53 | + stmt.execute( |
| 54 | + "CREATE TABLE IF NOT EXISTS $tableName (id INT PRIMARY KEY, name VARCHAR(255))" |
| 55 | + ) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun testNoFilter() { |
| 64 | + // When no filters are provided, all tables should be returned |
| 65 | + val configPojo = |
| 66 | + MySqlContainerFactory.config(dbContainer).apply { |
| 67 | + database = databaseName |
| 68 | + tableFilters = null |
| 69 | + } |
| 70 | + |
| 71 | + val config: MySqlSourceConfiguration = MySqlSourceConfigurationFactory().make(configPojo) |
| 72 | + |
| 73 | + factory.session(config).use { mdq: MetadataQuerier -> |
| 74 | + // Verify all tables are returned |
| 75 | + val streamNames = mdq.streamNames(databaseName).map { it.name }.sorted() |
| 76 | + Assertions.assertEquals(tableNames.sorted(), streamNames) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun testTableFilters() { |
| 82 | + val tableFilter = |
| 83 | + TableFilter().apply { |
| 84 | + databaseName = "test" |
| 85 | + patterns = listOf("customers", "orders") |
| 86 | + } |
| 87 | + |
| 88 | + val configPojo = |
| 89 | + MySqlContainerFactory.config(dbContainer).apply { |
| 90 | + database = databaseName |
| 91 | + tableFilters = listOf(tableFilter) |
| 92 | + } |
| 93 | + |
| 94 | + val config: MySqlSourceConfiguration = MySqlSourceConfigurationFactory().make(configPojo) |
| 95 | + |
| 96 | + factory.session(config).use { mdq: MetadataQuerier -> |
| 97 | + val querier = mdq as MySqlSourceMetadataQuerier |
| 98 | + |
| 99 | + // Check that memoizedTableNames only contains filtered tables |
| 100 | + val filteredTables = |
| 101 | + querier.base.memoizedTableNames |
| 102 | + .filter { (it.schema ?: it.catalog) == databaseName } |
| 103 | + .map { it.name } |
| 104 | + .toSet() |
| 105 | + |
| 106 | + Assertions.assertEquals(setOf("customers", "orders"), filteredTables) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun testNoMatchingTables() { |
| 112 | + // Test pattern that doesn't match any tables |
| 113 | + val tableFilter = |
| 114 | + TableFilter().apply { |
| 115 | + databaseName = "test" |
| 116 | + patterns = listOf("nonexistent%") |
| 117 | + } |
| 118 | + |
| 119 | + val configPojo = |
| 120 | + MySqlContainerFactory.config(dbContainer).apply { |
| 121 | + database = databaseName |
| 122 | + tableFilters = listOf(tableFilter) |
| 123 | + } |
| 124 | + |
| 125 | + val config: MySqlSourceConfiguration = MySqlSourceConfigurationFactory().make(configPojo) |
| 126 | + |
| 127 | + factory.session(config).use { mdq: MetadataQuerier -> |
| 128 | + val querier = mdq as MySqlSourceMetadataQuerier |
| 129 | + val filteredTables = |
| 130 | + querier.base.memoizedTableNames |
| 131 | + .filter { (it.schema ?: it.catalog) == databaseName } |
| 132 | + .map { it.name } |
| 133 | + .toSet() |
| 134 | + |
| 135 | + Assertions.assertEquals(emptySet<String>(), filteredTables) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + fun testFilterSchemaNotInConfiguredSchemas() { |
| 141 | + // Filter references a schema that is not in the configured schemas list |
| 142 | + val tableFilter = |
| 143 | + TableFilter().apply { |
| 144 | + databaseName = "nonexistent_schema" |
| 145 | + patterns = listOf("orders", "customers") |
| 146 | + } |
| 147 | + |
| 148 | + val configPojo = |
| 149 | + MySqlContainerFactory.config(dbContainer).apply { |
| 150 | + database = databaseName |
| 151 | + tableFilters = listOf(tableFilter) |
| 152 | + } |
| 153 | + |
| 154 | + assertThrows<ConfigErrorException> { |
| 155 | + MySqlSourceConfigurationFactory().makeWithoutExceptionHandling(configPojo) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + fun testWildcardPatterns() { |
| 161 | + // Test SQL LIKE wildcards with MySQL |
| 162 | + val tableFilter = |
| 163 | + TableFilter().apply { |
| 164 | + databaseName = "test" |
| 165 | + patterns = listOf("c%") // Should match 'customers' |
| 166 | + } |
| 167 | + |
| 168 | + val configPojo = |
| 169 | + MySqlContainerFactory.config(dbContainer).apply { |
| 170 | + database = databaseName |
| 171 | + tableFilters = listOf(tableFilter) |
| 172 | + } |
| 173 | + |
| 174 | + val config: MySqlSourceConfiguration = MySqlSourceConfigurationFactory().make(configPojo) |
| 175 | + |
| 176 | + factory.session(config).use { mdq: MetadataQuerier -> |
| 177 | + val querier = mdq as MySqlSourceMetadataQuerier |
| 178 | + val filteredTables = |
| 179 | + querier.base.memoizedTableNames |
| 180 | + .filter { (it.schema ?: it.catalog) == databaseName } |
| 181 | + .map { it.name } |
| 182 | + .toSet() |
| 183 | + |
| 184 | + Assertions.assertEquals(setOf("customers"), filteredTables) |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + fun testMultiplePatternsInOneFilter() { |
| 190 | + // Test multiple patterns for the same schema |
| 191 | + val tableFilter = |
| 192 | + TableFilter().apply { |
| 193 | + databaseName = "test" |
| 194 | + patterns = listOf("orders", "products", "invoices") |
| 195 | + } |
| 196 | + |
| 197 | + val configPojo = |
| 198 | + MySqlContainerFactory.config(dbContainer).apply { |
| 199 | + database = databaseName |
| 200 | + tableFilters = listOf(tableFilter) |
| 201 | + } |
| 202 | + |
| 203 | + val config: MySqlSourceConfiguration = MySqlSourceConfigurationFactory().make(configPojo) |
| 204 | + |
| 205 | + factory.session(config).use { mdq: MetadataQuerier -> |
| 206 | + val querier = mdq as MySqlSourceMetadataQuerier |
| 207 | + val filteredTables = |
| 208 | + querier.base.memoizedTableNames |
| 209 | + .filter { (it.schema ?: it.catalog) == databaseName } |
| 210 | + .map { it.name } |
| 211 | + .toSet() |
| 212 | + |
| 213 | + Assertions.assertEquals(setOf("orders", "products", "invoices"), filteredTables) |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments