|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.integrations.standardtest.source.performancetest; |
| 6 | + |
| 7 | +import io.airbyte.db.Database; |
| 8 | +import java.util.StringJoiner; |
| 9 | +import java.util.stream.Stream; |
| 10 | +import org.junit.jupiter.api.Disabled; |
| 11 | +import org.junit.jupiter.params.ParameterizedTest; |
| 12 | +import org.junit.jupiter.params.provider.Arguments; |
| 13 | +import org.junit.jupiter.params.provider.MethodSource; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +/** |
| 18 | + * This abstract class contains common methods for Fill Db scripts. |
| 19 | + */ |
| 20 | +public abstract class AbstractSourceFillDbWithTestData extends AbstractSourceBasePerformanceTest { |
| 21 | + |
| 22 | + private static final String CREATE_DB_TABLE_TEMPLATE = "CREATE TABLE %s.%s(id INTEGER PRIMARY KEY, %s)"; |
| 23 | + private static final String INSERT_INTO_DB_TABLE_QUERY_TEMPLATE = "INSERT INTO %s.%s (%s) VALUES %s"; |
| 24 | + private static final String TEST_DB_FIELD_TYPE = "varchar(10)"; |
| 25 | + |
| 26 | + protected static final Logger c = LoggerFactory.getLogger(AbstractSourceFillDbWithTestData.class); |
| 27 | + private static final String TEST_VALUE_TEMPLATE_POSTGRES = "\'Value id_placeholder\'"; |
| 28 | + protected static Stream testArgs; |
| 29 | + |
| 30 | + /** |
| 31 | + * Setup the test database. All tables and data described in the registered tests will be put there. |
| 32 | + * |
| 33 | + * @return configured test database |
| 34 | + * @throws Exception - might throw any exception during initialization. |
| 35 | + */ |
| 36 | + protected abstract Database setupDatabase(String dbName) throws Exception; |
| 37 | + |
| 38 | + /** |
| 39 | + * The test added test data to a new DB. 1. Set DB creds in static variables above 2. Set desired |
| 40 | + * number for streams, coolumns and records 3. Run the test |
| 41 | + */ |
| 42 | + @Disabled |
| 43 | + @ParameterizedTest |
| 44 | + @MethodSource("provideParameters") |
| 45 | + public void addTestData(String dbName, |
| 46 | + String schemaName, |
| 47 | + int numberOfDummyRecords, |
| 48 | + int numberOfBatches, |
| 49 | + int numberOfColumns, |
| 50 | + int numberOfStreams) |
| 51 | + throws Exception { |
| 52 | + |
| 53 | + final Database database = setupDatabase(dbName); |
| 54 | + |
| 55 | + database.query(ctx -> { |
| 56 | + for (int currentSteamNumber = 0; currentSteamNumber < numberOfStreams; currentSteamNumber++) { |
| 57 | + |
| 58 | + String currentTableName = String.format(getTestStreamNameTemplate(), currentSteamNumber); |
| 59 | + |
| 60 | + ctx.fetch(prepareCreateTableQuery(schemaName, numberOfColumns, currentTableName)); |
| 61 | + for (int i = 0; i < numberOfBatches; i++) { |
| 62 | + String insertQueryTemplate = prepareInsertQueryTemplate(schemaName, i, |
| 63 | + numberOfColumns, |
| 64 | + numberOfDummyRecords); |
| 65 | + ctx.fetch(String.format(insertQueryTemplate, currentTableName)); |
| 66 | + } |
| 67 | + |
| 68 | + c.info("Finished processing for stream " + currentSteamNumber); |
| 69 | + } |
| 70 | + return null; |
| 71 | + }); |
| 72 | + |
| 73 | + database.close(); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * This is a data provider for fill DB script,, Each argument's group would be ran as a separate |
| 79 | + * test. Set the "testArgs" in test class of your DB in @BeforeTest method. |
| 80 | + * |
| 81 | + * 1st arg - a name of DB that will be used in jdbc connection string. 2nd arg - a schemaName that |
| 82 | + * will be ised as a NameSpace in Configured Airbyte Catalog. 3rd arg - a number of expected records |
| 83 | + * retrieved in each stream. 4th arg - a number of columns in each stream\table that will be use for |
| 84 | + * Airbyte Cataloq configuration 5th arg - a number of streams to read in configured airbyte |
| 85 | + * Catalog. Each stream\table in DB should be names like "test_0", "test_1",..., test_n. |
| 86 | + * |
| 87 | + * Stream.of( Arguments.of("your_db_name", "your_schema_name", 100, 2, 240, 1000) ); |
| 88 | + */ |
| 89 | + private static Stream<Arguments> provideParameters() { |
| 90 | + return testArgs; |
| 91 | + } |
| 92 | + |
| 93 | + protected String prepareCreateTableQuery(final String dbSchemaName, |
| 94 | + final int numberOfColumns, |
| 95 | + final String currentTableName) { |
| 96 | + |
| 97 | + StringJoiner sj = new StringJoiner(","); |
| 98 | + for (int i = 0; i < numberOfColumns; i++) { |
| 99 | + sj.add(String.format(" %s%s %s", getTestColumnName(), i, TEST_DB_FIELD_TYPE)); |
| 100 | + } |
| 101 | + |
| 102 | + return String.format(CREATE_DB_TABLE_TEMPLATE, dbSchemaName, currentTableName, sj.toString()); |
| 103 | + } |
| 104 | + |
| 105 | + protected String prepareInsertQueryTemplate(final String dbSchemaName, |
| 106 | + final int batchNumber, |
| 107 | + final int numberOfColumns, |
| 108 | + final int recordsNumber) { |
| 109 | + |
| 110 | + StringJoiner fieldsNames = new StringJoiner(","); |
| 111 | + fieldsNames.add("id"); |
| 112 | + |
| 113 | + StringJoiner baseInsertQuery = new StringJoiner(","); |
| 114 | + baseInsertQuery.add("id_placeholder"); |
| 115 | + |
| 116 | + for (int i = 0; i < numberOfColumns; i++) { |
| 117 | + fieldsNames.add(getTestColumnName() + i); |
| 118 | + baseInsertQuery.add(TEST_VALUE_TEMPLATE_POSTGRES); |
| 119 | + } |
| 120 | + |
| 121 | + StringJoiner insertGroupValuesJoiner = new StringJoiner(","); |
| 122 | + |
| 123 | + int batchMessages = batchNumber * 100; |
| 124 | + |
| 125 | + for (int currentRecordNumber = batchMessages; |
| 126 | + currentRecordNumber < recordsNumber + batchMessages; |
| 127 | + currentRecordNumber++) { |
| 128 | + insertGroupValuesJoiner |
| 129 | + .add("(" + baseInsertQuery.toString() |
| 130 | + .replaceAll("id_placeholder", String.valueOf(currentRecordNumber)) + ")"); |
| 131 | + } |
| 132 | + |
| 133 | + return String |
| 134 | + .format(INSERT_INTO_DB_TABLE_QUERY_TEMPLATE, dbSchemaName, "%s", fieldsNames.toString(), |
| 135 | + insertGroupValuesJoiner.toString()); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments