-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#6 Allow ability to start streaming a dataset off the source DB by it…
…s name - Currently automatically starts a hard-coded stream of a dataset at start-up - Each row is just toStringed right now; obviously not a real implementation - Doesn't go anywhere; just logged
- Loading branch information
1 parent
94d2f7e
commit a9e91a5
Showing
3 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 33 additions & 1 deletion
34
src/test/kotlin/com/thoughtworks/recce/server/dataset/DataSetService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,35 @@ | ||
package com.thoughtworks.recce.server.dataset | ||
|
||
class DataSetService | ||
import com.thoughtworks.recce.server.config.ReconciliationConfiguration | ||
import io.micronaut.discovery.event.ServiceReadyEvent | ||
import io.micronaut.runtime.event.annotation.EventListener | ||
import io.micronaut.scheduling.annotation.Async | ||
import jakarta.inject.Inject | ||
import jakarta.inject.Singleton | ||
import mu.KotlinLogging | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
@Singleton | ||
open class DataSetService(@Inject val config: ReconciliationConfiguration) { | ||
fun start(dataSetName: String): Flux<String> { | ||
|
||
val source = config.datasets[dataSetName]?.source | ||
?: throw IllegalArgumentException("[$dataSetName] not found!") | ||
|
||
logger.info { "Streaming [$dataSetName] from [source]..." } | ||
|
||
return Mono.from(source.dbOperations.connectionFactory().create()) | ||
.flatMapMany { it.createStatement(source.query).execute() } | ||
.flatMap { result -> result.map { row, _ -> row.toString() } } | ||
} | ||
|
||
@EventListener | ||
@Async | ||
open fun doOnStart(event: ServiceReadyEvent): Flux<String> { | ||
return start("test-dataset") | ||
.doOnEach { logger.info { it } } | ||
} | ||
} |
40 changes: 39 additions & 1 deletion
40
src/test/kotlin/com/thoughtworks/recce/server/dataset/DataSetServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,47 @@ | ||
package com.thoughtworks.recce.server.dataset | ||
|
||
import com.thoughtworks.recce.server.config.DataSourceTest | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import io.r2dbc.spi.* | ||
import jakarta.inject.Inject | ||
import org.assertj.core.api.Assertions | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.kotlin.mock | ||
import reactor.test.StepVerifier | ||
|
||
internal class DataSetServiceTest { | ||
@Test | ||
fun `should load configurations`() { | ||
fun `start should throw on missing dataset`() { | ||
Assertions.assertThatThrownBy { DataSetService(mock()).start("test-dataset") } | ||
.isExactlyInstanceOf(IllegalArgumentException::class.java) | ||
.hasMessageContaining("test-dataset") | ||
} | ||
} | ||
|
||
@MicronautTest( | ||
environments = arrayOf("test-integration"), | ||
propertySources = arrayOf("classpath:config/application-test-dataset.yml") | ||
) | ||
class DataSetServiceIntegrationTest : DataSourceTest() { | ||
@Inject | ||
lateinit var service: DataSetService | ||
|
||
@Test | ||
fun `start can stream a source dataset`() { | ||
StepVerifier.create(service.start("test-dataset")) | ||
.assertNext { assertThat(it).contains("name='COUNT(*)', value=3") } | ||
.verifyComplete() | ||
} | ||
|
||
@Test | ||
fun `should emit error on bad query`() { | ||
transaction(sourceDb) { SchemaUtils.drop(TestData) } | ||
|
||
StepVerifier.create(service.start("test-dataset")) | ||
.expectError(R2dbcBadGrammarException::class.java) | ||
.verify() | ||
} | ||
} |