|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.integrations.destination.snowflake.operation |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.JsonNode |
| 8 | +import io.airbyte.cdk.db.jdbc.JdbcDatabase |
| 9 | +import io.airbyte.cdk.integrations.destination.record_buffer.FileBuffer |
| 10 | +import io.airbyte.cdk.integrations.destination.s3.csv.CsvSerializedBuffer |
| 11 | +import io.airbyte.cdk.integrations.destination.s3.csv.CsvSheetGenerator |
| 12 | +import io.airbyte.commons.json.Jsons |
| 13 | +import io.airbyte.commons.string.Strings |
| 14 | +import io.airbyte.integrations.base.destination.typing_deduping.StreamId |
| 15 | +import io.airbyte.integrations.destination.snowflake.OssCloudEnvVarConsts |
| 16 | +import io.airbyte.integrations.destination.snowflake.SnowflakeDatabaseUtils |
| 17 | +import io.airbyte.protocol.models.v0.AirbyteRecordMessage |
| 18 | +import java.nio.file.Files |
| 19 | +import java.nio.file.Paths |
| 20 | +import java.time.Instant |
| 21 | +import java.util.* |
| 22 | +import org.junit.jupiter.api.AfterEach |
| 23 | +import org.junit.jupiter.api.Assertions.* |
| 24 | +import org.junit.jupiter.api.BeforeEach |
| 25 | +import org.junit.jupiter.api.Test |
| 26 | + |
| 27 | +class SnowflakeStagingClientIntegrationTest { |
| 28 | + |
| 29 | + private lateinit var stagingClient: SnowflakeStagingClient |
| 30 | + // Not using lateinit to keep spotBugs happy |
| 31 | + // since these vars are referenced within the setup |
| 32 | + // and generated bytecode as if non-null check |
| 33 | + private var namespace: String = "" |
| 34 | + private var tablename: String = "" |
| 35 | + |
| 36 | + private lateinit var stageName: String |
| 37 | + private val config = |
| 38 | + Jsons.deserialize(Files.readString(Paths.get("secrets/1s1t_internal_staging_config.json"))) |
| 39 | + private val datasource = |
| 40 | + SnowflakeDatabaseUtils.createDataSource(config, OssCloudEnvVarConsts.AIRBYTE_OSS) |
| 41 | + private val database: JdbcDatabase = SnowflakeDatabaseUtils.getDatabase(datasource) |
| 42 | + // Intentionally not using actual columns, since the staging client should be agnostic of these |
| 43 | + // and only follow the order of data. |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + fun setUp() { |
| 47 | + namespace = Strings.addRandomSuffix("staging_client_test", "_", 5).uppercase() |
| 48 | + tablename = "integration_test_raw".uppercase() |
| 49 | + val createSchemaQuery = """ |
| 50 | + CREATE SCHEMA "$namespace" |
| 51 | + """.trimIndent() |
| 52 | + val createStagingTableQuery = |
| 53 | + """ |
| 54 | + CREATE TABLE IF NOT EXISTS "$namespace"."$tablename" ( |
| 55 | + "id" VARCHAR PRIMARY KEY, |
| 56 | + "emitted_at" TIMESTAMP WITH TIME ZONE DEFAULT current_timestamp(), |
| 57 | + "data" VARIANT |
| 58 | + ) |
| 59 | + """.trimIndent() |
| 60 | + stageName = """"$namespace"."${Strings.addRandomSuffix("stage", "_", 5)}"""" |
| 61 | + stagingClient = SnowflakeStagingClient(database) |
| 62 | + database.execute(createSchemaQuery) |
| 63 | + stagingClient.createStageIfNotExists(stageName) |
| 64 | + database.execute(createStagingTableQuery) |
| 65 | + } |
| 66 | + |
| 67 | + @AfterEach |
| 68 | + fun tearDown() { |
| 69 | + stagingClient.dropStageIfExists(stageName) |
| 70 | + database.execute("DROP SCHEMA IF EXISTS \"$namespace\" CASCADE") |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun verifyUploadAndCopyToTableSuccess() { |
| 75 | + val csvSheetGenerator = |
| 76 | + object : CsvSheetGenerator { |
| 77 | + override fun getDataRow(formattedData: JsonNode): List<Any> { |
| 78 | + throw NotImplementedError("This method should not be called in this test") |
| 79 | + } |
| 80 | + |
| 81 | + override fun getDataRow(id: UUID, recordMessage: AirbyteRecordMessage): List<Any> { |
| 82 | + throw NotImplementedError("This method should not be called in this test") |
| 83 | + } |
| 84 | + |
| 85 | + override fun getDataRow( |
| 86 | + id: UUID, |
| 87 | + formattedString: String, |
| 88 | + emittedAt: Long, |
| 89 | + formattedAirbyteMetaString: String |
| 90 | + ): List<Any> { |
| 91 | + return listOf(id, Instant.ofEpochMilli(emittedAt), formattedString) |
| 92 | + } |
| 93 | + |
| 94 | + override fun getHeaderRow(): List<String> { |
| 95 | + throw NotImplementedError("This method should not be called in this test") |
| 96 | + } |
| 97 | + } |
| 98 | + val writeBuffer = |
| 99 | + CsvSerializedBuffer( |
| 100 | + FileBuffer(CsvSerializedBuffer.CSV_GZ_SUFFIX), |
| 101 | + csvSheetGenerator, |
| 102 | + true, |
| 103 | + ) |
| 104 | + val streamId = StreamId("unused", "unused", namespace, tablename, "unused", "unused") |
| 105 | + val stagingPath = "${UUID.randomUUID()}/test/" |
| 106 | + writeBuffer.use { |
| 107 | + it.accept(""" {"dummyKey": "dummyValue"} """, "", System.currentTimeMillis()) |
| 108 | + it.accept(""" {"dummyKey": "dummyValue"} """, "", System.currentTimeMillis()) |
| 109 | + it.flush() |
| 110 | + val fileName = stagingClient.uploadRecordsToStage(writeBuffer, stageName, stagingPath) |
| 111 | + stagingClient.copyIntoTableFromStage(stageName, stagingPath, listOf(fileName), streamId) |
| 112 | + } |
| 113 | + val results = |
| 114 | + database.queryJsons( |
| 115 | + "SELECT * FROM \"${streamId.rawNamespace}\".\"${streamId.rawName}\"" |
| 116 | + ) |
| 117 | + assertTrue(results.size == 2) |
| 118 | + assertNotNull(results.first().get("id")) |
| 119 | + assertNotNull(results.first().get("emitted_at")) |
| 120 | + assertNotNull(results.first().get("data")) |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun verifyUploadAndCopyToTableFailureOnMismatchedColumns() { |
| 125 | + val mismatchedColumnsSheetGenerator = |
| 126 | + object : CsvSheetGenerator { |
| 127 | + override fun getDataRow(formattedData: JsonNode): List<Any> { |
| 128 | + throw NotImplementedError("This method should not be called in this test") |
| 129 | + } |
| 130 | + |
| 131 | + override fun getDataRow(id: UUID, recordMessage: AirbyteRecordMessage): List<Any> { |
| 132 | + throw NotImplementedError("This method should not be called in this test") |
| 133 | + } |
| 134 | + |
| 135 | + override fun getDataRow( |
| 136 | + id: UUID, |
| 137 | + formattedString: String, |
| 138 | + emittedAt: Long, |
| 139 | + formattedAirbyteMetaString: String |
| 140 | + ): List<Any> { |
| 141 | + return listOf( |
| 142 | + id, |
| 143 | + Instant.ofEpochMilli(emittedAt), |
| 144 | + formattedString, |
| 145 | + "unknown_data_column" |
| 146 | + ) |
| 147 | + } |
| 148 | + |
| 149 | + override fun getHeaderRow(): List<String> { |
| 150 | + throw NotImplementedError("This method should not be called in this test") |
| 151 | + } |
| 152 | + } |
| 153 | + val writeBuffer = |
| 154 | + CsvSerializedBuffer( |
| 155 | + FileBuffer(CsvSerializedBuffer.CSV_GZ_SUFFIX), |
| 156 | + mismatchedColumnsSheetGenerator, |
| 157 | + true, |
| 158 | + ) |
| 159 | + val streamId = StreamId("unused", "unused", namespace, tablename, "unused", "unused") |
| 160 | + val stagingPath = "${UUID.randomUUID()}/test/" |
| 161 | + writeBuffer.use { |
| 162 | + it.accept(""" {"dummyKey": "dummyValue"} """, "", System.currentTimeMillis()) |
| 163 | + it.flush() |
| 164 | + val fileName = stagingClient.uploadRecordsToStage(writeBuffer, stageName, stagingPath) |
| 165 | + assertThrows(Exception::class.java) { |
| 166 | + stagingClient.copyIntoTableFromStage( |
| 167 | + stageName, |
| 168 | + stagingPath, |
| 169 | + listOf(fileName), |
| 170 | + streamId |
| 171 | + ) |
| 172 | + } |
| 173 | + } |
| 174 | + val results = |
| 175 | + database.queryJsons( |
| 176 | + "SELECT * FROM \"${streamId.rawNamespace}\".\"${streamId.rawName}\"" |
| 177 | + ) |
| 178 | + assertTrue(results.isEmpty()) |
| 179 | + } |
| 180 | +} |
0 commit comments