|
| 1 | +package com.dragos.kafkacsvloader.cli |
| 2 | + |
| 3 | +import com.dragos.kafkacsvloader.avro.AvroRecordMapper |
| 4 | +import com.dragos.kafkacsvloader.avro.AvroSchemaLoader |
| 5 | +import com.dragos.kafkacsvloader.avro.RowMappingResult |
| 6 | +import com.dragos.kafkacsvloader.csv.CsvParser |
| 7 | +import io.kotest.core.spec.style.FunSpec |
| 8 | +import io.kotest.matchers.collections.shouldBeEmpty |
| 9 | +import io.kotest.matchers.collections.shouldHaveSize |
| 10 | +import io.kotest.matchers.shouldBe |
| 11 | +import java.io.File |
| 12 | +import java.nio.file.Files |
| 13 | + |
| 14 | +class DryRunTest : FunSpec({ |
| 15 | + |
| 16 | + test("dry run validates CSV and schema without errors for valid data") { |
| 17 | + val tempDir = Files.createTempDirectory("dry-run-test").toFile() |
| 18 | + try { |
| 19 | + // Create test CSV |
| 20 | + val csvFile = File(tempDir, "test.csv") |
| 21 | + csvFile.writeText( |
| 22 | + """ |
| 23 | + id,name,email,age |
| 24 | + 1,Alice,alice@example.com,30 |
| 25 | + 2,Bob,bob@example.com,25 |
| 26 | + """.trimIndent(), |
| 27 | + ) |
| 28 | + |
| 29 | + // Create test schema |
| 30 | + val schemaFile = File(tempDir, "schema.avsc") |
| 31 | + schemaFile.writeText( |
| 32 | + """ |
| 33 | + { |
| 34 | + "type": "record", |
| 35 | + "name": "User", |
| 36 | + "namespace": "com.example", |
| 37 | + "fields": [ |
| 38 | + {"name": "id", "type": "string"}, |
| 39 | + {"name": "name", "type": "string"}, |
| 40 | + {"name": "email", "type": "string"}, |
| 41 | + {"name": "age", "type": "int"} |
| 42 | + ] |
| 43 | + } |
| 44 | + """.trimIndent(), |
| 45 | + ) |
| 46 | + |
| 47 | + // Load schema |
| 48 | + val schema = AvroSchemaLoader.loadFromFile(schemaFile.absolutePath) |
| 49 | + |
| 50 | + // Parse CSV |
| 51 | + val csvData = CsvParser.parse(csvFile.absolutePath) |
| 52 | + |
| 53 | + // Validate headers |
| 54 | + val schemaFields = schema.fields.map { it.name() } |
| 55 | + val headersValid = CsvParser.validateHeaders(csvData.headers, schemaFields) |
| 56 | + headersValid shouldBe true |
| 57 | + |
| 58 | + // Validate all rows can be mapped (dry run logic) |
| 59 | + val failures = mutableListOf<Pair<Int, String>>() |
| 60 | + var validCount = 0 |
| 61 | + |
| 62 | + csvData.rows.forEachIndexed { index, row -> |
| 63 | + val rowNumber = index + 1 |
| 64 | + when (val result = AvroRecordMapper.mapRow(schema, row)) { |
| 65 | + is RowMappingResult.Success -> validCount++ |
| 66 | + is RowMappingResult.Failure -> { |
| 67 | + failures.add(rowNumber to result.errors.joinToString("; ")) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Assert no failures |
| 73 | + validCount shouldBe 2 |
| 74 | + failures.shouldBeEmpty() |
| 75 | + } finally { |
| 76 | + tempDir.deleteRecursively() |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + test("dry run detects invalid rows") { |
| 81 | + val tempDir = Files.createTempDirectory("dry-run-test").toFile() |
| 82 | + try { |
| 83 | + // Create test CSV with invalid data |
| 84 | + val csvFile = File(tempDir, "test.csv") |
| 85 | + csvFile.writeText( |
| 86 | + """ |
| 87 | + id,name,email,age |
| 88 | + 1,Alice,alice@example.com,30 |
| 89 | + 2,Bob,bob@example.com,invalid_age |
| 90 | + """.trimIndent(), |
| 91 | + ) |
| 92 | + |
| 93 | + // Create test schema |
| 94 | + val schemaFile = File(tempDir, "schema.avsc") |
| 95 | + schemaFile.writeText( |
| 96 | + """ |
| 97 | + { |
| 98 | + "type": "record", |
| 99 | + "name": "User", |
| 100 | + "namespace": "com.example", |
| 101 | + "fields": [ |
| 102 | + {"name": "id", "type": "string"}, |
| 103 | + {"name": "name", "type": "string"}, |
| 104 | + {"name": "email", "type": "string"}, |
| 105 | + {"name": "age", "type": "int"} |
| 106 | + ] |
| 107 | + } |
| 108 | + """.trimIndent(), |
| 109 | + ) |
| 110 | + |
| 111 | + // Load schema |
| 112 | + val schema = AvroSchemaLoader.loadFromFile(schemaFile.absolutePath) |
| 113 | + |
| 114 | + // Parse CSV |
| 115 | + val csvData = CsvParser.parse(csvFile.absolutePath) |
| 116 | + |
| 117 | + // Validate all rows (dry run logic) |
| 118 | + val failures = mutableListOf<Pair<Int, String>>() |
| 119 | + var validCount = 0 |
| 120 | + |
| 121 | + csvData.rows.forEachIndexed { index, row -> |
| 122 | + val rowNumber = index + 1 |
| 123 | + when (val result = AvroRecordMapper.mapRow(schema, row)) { |
| 124 | + is RowMappingResult.Success -> validCount++ |
| 125 | + is RowMappingResult.Failure -> { |
| 126 | + failures.add(rowNumber to result.errors.joinToString("; ")) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Assert there's one failure |
| 132 | + validCount shouldBe 1 |
| 133 | + failures shouldHaveSize 1 |
| 134 | + failures[0].first shouldBe 2 |
| 135 | + failures[0].second shouldBe "Field 'age' conversion error: For input string: \"invalid_age\"" |
| 136 | + } finally { |
| 137 | + tempDir.deleteRecursively() |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + test("dry run detects missing required fields") { |
| 142 | + val tempDir = Files.createTempDirectory("dry-run-test").toFile() |
| 143 | + try { |
| 144 | + // Create test CSV missing 'age' column |
| 145 | + val csvFile = File(tempDir, "test.csv") |
| 146 | + csvFile.writeText( |
| 147 | + """ |
| 148 | + id,name,email |
| 149 | + 1,Alice,alice@example.com |
| 150 | + 2,Bob,bob@example.com |
| 151 | + """.trimIndent(), |
| 152 | + ) |
| 153 | + |
| 154 | + // Create test schema |
| 155 | + val schemaFile = File(tempDir, "schema.avsc") |
| 156 | + schemaFile.writeText( |
| 157 | + """ |
| 158 | + { |
| 159 | + "type": "record", |
| 160 | + "name": "User", |
| 161 | + "namespace": "com.example", |
| 162 | + "fields": [ |
| 163 | + {"name": "id", "type": "string"}, |
| 164 | + {"name": "name", "type": "string"}, |
| 165 | + {"name": "email", "type": "string"}, |
| 166 | + {"name": "age", "type": "int"} |
| 167 | + ] |
| 168 | + } |
| 169 | + """.trimIndent(), |
| 170 | + ) |
| 171 | + |
| 172 | + // Load schema |
| 173 | + val schema = AvroSchemaLoader.loadFromFile(schemaFile.absolutePath) |
| 174 | + |
| 175 | + // Parse CSV |
| 176 | + val csvData = CsvParser.parse(csvFile.absolutePath) |
| 177 | + |
| 178 | + // Validate headers |
| 179 | + val schemaFields = schema.fields.map { it.name() } |
| 180 | + val headersValid = CsvParser.validateHeaders(csvData.headers, schemaFields) |
| 181 | + |
| 182 | + // Should fail validation |
| 183 | + headersValid shouldBe false |
| 184 | + |
| 185 | + // Check missing fields |
| 186 | + val missingFields = CsvParser.getMissingFields(csvData.headers, schemaFields) |
| 187 | + missingFields shouldBe listOf("age") |
| 188 | + } finally { |
| 189 | + tempDir.deleteRecursively() |
| 190 | + } |
| 191 | + } |
| 192 | +}) |
0 commit comments