|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020 Airbyte |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package io.airbyte.integrations.source.mysql; |
| 26 | + |
| 27 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 28 | + |
| 29 | +import com.fasterxml.jackson.databind.JsonNode; |
| 30 | +import com.google.common.collect.ImmutableMap; |
| 31 | +import io.airbyte.commons.json.Jsons; |
| 32 | +import io.airbyte.db.Database; |
| 33 | +import io.airbyte.db.Databases; |
| 34 | +import io.airbyte.db.jdbc.JdbcUtils; |
| 35 | +import io.airbyte.protocol.models.AirbyteCatalog; |
| 36 | +import java.sql.Connection; |
| 37 | +import java.sql.DriverManager; |
| 38 | +import java.sql.SQLException; |
| 39 | +import org.apache.commons.lang3.RandomStringUtils; |
| 40 | +import org.jooq.SQLDialect; |
| 41 | +import org.junit.jupiter.api.AfterAll; |
| 42 | +import org.junit.jupiter.api.AfterEach; |
| 43 | +import org.junit.jupiter.api.BeforeAll; |
| 44 | +import org.junit.jupiter.api.BeforeEach; |
| 45 | +import org.junit.jupiter.params.ParameterizedTest; |
| 46 | +import org.junit.jupiter.params.provider.ValueSource; |
| 47 | +import org.testcontainers.containers.MySQLContainer; |
| 48 | + |
| 49 | +class MySqlTest { |
| 50 | + |
| 51 | + private static final String TEST_USER = "test"; |
| 52 | + private static final String TEST_PASSWORD = "test"; |
| 53 | + private static MySQLContainer<?> container; |
| 54 | + |
| 55 | + private static String TABLE_NAME = "id_and_name"; |
| 56 | + |
| 57 | + private JsonNode config; |
| 58 | + private Database database; |
| 59 | + |
| 60 | + @BeforeAll |
| 61 | + static void init() throws SQLException { |
| 62 | + container = new MySQLContainer<>("mysql:8.0") |
| 63 | + .withUsername(TEST_USER) |
| 64 | + .withPassword(TEST_PASSWORD) |
| 65 | + .withEnv("MYSQL_ROOT_HOST", "%") |
| 66 | + .withEnv("MYSQL_ROOT_PASSWORD", TEST_PASSWORD); |
| 67 | + container.start(); |
| 68 | + Connection connection = DriverManager.getConnection(container.getJdbcUrl(), "root", TEST_PASSWORD); |
| 69 | + connection.createStatement().execute("GRANT ALL PRIVILEGES ON *.* TO '" + TEST_USER + "'@'%';\n"); |
| 70 | + } |
| 71 | + |
| 72 | + @BeforeEach |
| 73 | + public void setup() throws Exception { |
| 74 | + config = Jsons.jsonNode(ImmutableMap.builder() |
| 75 | + .put("host", container.getHost()) |
| 76 | + .put("port", container.getFirstMappedPort()) |
| 77 | + .put("database", "db_" + RandomStringUtils.randomAlphabetic(10)) |
| 78 | + .put("username", TEST_USER) |
| 79 | + .put("password", TEST_PASSWORD) |
| 80 | + .build()); |
| 81 | + |
| 82 | + final Database masterDatabase = Databases.createDatabase( |
| 83 | + config.get("username").asText(), |
| 84 | + config.get("password").asText(), |
| 85 | + String.format("jdbc:mysql://%s:%s", |
| 86 | + config.get("host").asText(), |
| 87 | + config.get("port").asText()), |
| 88 | + MySqlSource.DRIVER_CLASS, |
| 89 | + SQLDialect.MYSQL); |
| 90 | + |
| 91 | + masterDatabase.query(ctx -> { |
| 92 | + ctx.fetch("CREATE DATABASE " + config.get("database").asText()); |
| 93 | + return null; |
| 94 | + }); |
| 95 | + |
| 96 | + masterDatabase.close(); |
| 97 | + |
| 98 | + database = Databases.createDatabase( |
| 99 | + config.get("username").asText(), |
| 100 | + config.get("password").asText(), |
| 101 | + String.format("jdbc:mysql://%s:%s/%s", |
| 102 | + config.get("host").asText(), |
| 103 | + config.get("port").asText(), |
| 104 | + config.get("database").asText()), |
| 105 | + MySqlSource.DRIVER_CLASS, |
| 106 | + SQLDialect.MYSQL); |
| 107 | + } |
| 108 | + |
| 109 | + @AfterEach |
| 110 | + void tearDownMySql() throws Exception { |
| 111 | + database.close(); |
| 112 | + } |
| 113 | + |
| 114 | + @AfterAll |
| 115 | + static void cleanUp() { |
| 116 | + container.close(); |
| 117 | + } |
| 118 | + |
| 119 | + @ParameterizedTest |
| 120 | + @ValueSource(strings = { |
| 121 | + "TINYINT", |
| 122 | + "SMALLINT", |
| 123 | + "MEDIUMINT", |
| 124 | + "INT", |
| 125 | + "BIGINT", |
| 126 | + "INT(1)", |
| 127 | + "INT(2)", |
| 128 | + "INT(3)", |
| 129 | + "INT(4)", |
| 130 | + "INT(5)", |
| 131 | + "INT(6)", |
| 132 | + "INT(7)", |
| 133 | + "INT(8)" |
| 134 | + }) |
| 135 | + void testSmallIntTypes(String type) throws Exception { |
| 136 | + database.query(ctx -> { |
| 137 | + ctx.fetch(String.format("CREATE TABLE %s(id %s)", JdbcUtils.getFullyQualifiedTableName(null, TABLE_NAME), type)); |
| 138 | + ctx.fetch(String.format("INSERT INTO %s(id) VALUES (10)", JdbcUtils.getFullyQualifiedTableName(null, TABLE_NAME))); |
| 139 | + return null; |
| 140 | + }); |
| 141 | + |
| 142 | + final AirbyteCatalog catalog = new MySqlSource().discover(config); |
| 143 | + assertEquals("number", catalog.getStreams().get(0).getJsonSchema().get("properties").get("id").get("type").asText()); |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments