Skip to content

Commit

Permalink
#42 - Prefer Instant to LocalDateTime for timestamp fields
Browse files Browse the repository at this point in the history
Believe this is more canonical usage anyway.
  • Loading branch information
chadlwilson committed Oct 25, 2021
1 parent 36f6db0 commit b7edc85
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/recce/server/dataset/MigrationRepositories.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.micronaut.data.r2dbc.annotation.R2dbcRepository
import io.micronaut.data.repository.reactive.ReactorCrudRepository
import reactor.core.publisher.Flux
import java.io.Serializable
import java.time.LocalDateTime
import java.time.Instant
import javax.persistence.*

@R2dbcRepository(dialect = Dialect.POSTGRES)
Expand All @@ -23,11 +23,11 @@ interface MigrationRecordRepository : ReactorCrudRepository<MigrationRecord, Mig
data class MigrationRun(
@Id @GeneratedValue val id: Int?,
val datasetId: String,
@DateCreated val createdTime: LocalDateTime?,
@DateCreated val createdTime: Instant?,
) {
@DateUpdated
var updatedTime: LocalDateTime? = null
var completedTime: LocalDateTime? = null
var updatedTime: Instant? = null
var completedTime: Instant? = null

constructor(datasetId: String) : this(null, datasetId, null)

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/recce/server/dataset/MigrationRunService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package recce.server.dataset
import jakarta.inject.Singleton
import mu.KotlinLogging
import reactor.core.publisher.Mono
import java.time.LocalDateTime
import java.time.Instant

private val logger = KotlinLogging.logger {}

Expand All @@ -15,6 +15,6 @@ open class MigrationRunService(private val runRepository: MigrationRunRepository
.cache()

fun complete(run: MigrationRun): Mono<MigrationRun> =
runRepository.update(run.apply { completedTime = LocalDateTime.now() })
runRepository.update(run.apply { completedTime = Instant.now() })
.doOnNext { logger.info { "Run completed for $it" } }
}
16 changes: 11 additions & 5 deletions src/test/kotlin/recce/server/api/ReconciliationControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import recce.server.dataset.MigrationRun
import recce.server.dataset.ReconciliationRunner
import recce.server.dataset.ReconciliationService
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

internal class ReconciliationControllerTest {
Expand All @@ -50,9 +51,14 @@ internal class ReconciliationControllerTest {
@MicronautTest
internal class ReconciliationControllerApiTest {
private val testDataset = "testDataset"
private val testResults = MigrationRun(12, testDataset, LocalDateTime.of(2021, 10, 25, 16, 16, 16)).apply {
private val testResults = MigrationRun(
12, testDataset,
LocalDateTime.of(2021, 10, 25, 16, 16, 16).toInstant(
ZoneOffset.UTC
)
).apply {

completedTime = createdTime?.plusMinutes(10)
completedTime = createdTime?.plusSeconds(180)
updatedTime = completedTime?.plusSeconds(10)
results = DatasetResults(2000, 3000)
}
Expand Down Expand Up @@ -81,9 +87,9 @@ internal class ReconciliationControllerApiTest {
statusCode(HttpStatus.SC_OK)
body("datasetId", equalTo(testDataset))
body("id", equalTo(testResults.id))
body("createdTime", equalTo(testResults.createdTime?.format(DateTimeFormatter.ISO_INSTANT)))
body("completedTime", equalTo(testResults.completedTime?.format(DateTimeFormatter.ISO_INSTANT)))
body("updatedTime", equalTo(testResults.updatedTime?.format(DateTimeFormatter.ISO_INSTANT)))
body("createdTime", equalTo(DateTimeFormatter.ISO_INSTANT.format(testResults.createdTime)))
body("completedTime", equalTo(DateTimeFormatter.ISO_INSTANT.format(testResults.completedTime)))
body("updatedTime", equalTo(DateTimeFormatter.ISO_INSTANT.format(testResults.updatedTime)))
body("results.sourceRows", equalTo(testResults.results?.sourceRows?.toInt()))
body("results.targetRows", equalTo(testResults.results?.targetRows?.toInt()))
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/recce/server/dataset/HashedRowTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.mockito.kotlin.mock
import recce.server.config.DataLoadDefinition.Companion.migrationKeyColumnName
import java.math.BigDecimal
import java.nio.ByteBuffer
import java.time.LocalDateTime
import java.time.Instant

internal class HashedRowTest {

Expand Down Expand Up @@ -50,10 +50,10 @@ internal class HashedRowTest {

@Test
fun `should throw on unrecognized type`() {
val row = mockSingleColumnRowReturning(LocalDateTime.now())
val row = mockSingleColumnRowReturning(Instant.now())
assertThatThrownBy { HashedRow.fromRow(row, meta) }
.isExactlyInstanceOf(IllegalArgumentException::class.java)
.hasMessageContaining("LocalDateTime")
.hasMessageContaining("Instant")
.hasMessageContaining("test")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import reactor.core.publisher.Mono
import reactor.test.StepVerifier
import java.time.LocalDateTime
import java.time.Instant

internal class MigrationRunServiceTest {

private val startedRun = MigrationRun(1, datasetId, LocalDateTime.now())
private val startedRun = MigrationRun(1, datasetId, Instant.now())

@Test
fun `should return start results`() {
Expand Down

0 comments on commit b7edc85

Please sign in to comment.