-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not set timestamps if already set
GitOrigin-RevId: 07c5fc448c43ae922af1f4c32c1f479fa5b59ba7
- Loading branch information
1 parent
680bca2
commit bac1676
Showing
6 changed files
with
204 additions
and
19 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
4 changes: 2 additions & 2 deletions
4
misk-jooq/src/main/kotlin/misk/jooq/listeners/AvoidUsingSelectStarListener.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
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
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
50 changes: 50 additions & 0 deletions
50
misk-jooq/src/test/kotlin/misk/jooq/listeners/AvoidUsingSelectStarListenerTest.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package misk.jooq.listeners | ||
|
||
import jakarta.inject.Inject | ||
import misk.jooq.JooqTransacter | ||
import misk.jooq.config.ClientJooqTestingModule | ||
import misk.jooq.config.JooqDBIdentifier | ||
import misk.jooq.model.Genre | ||
import misk.jooq.testgen.tables.references.MOVIE | ||
import misk.testing.MiskTest | ||
import misk.testing.MiskTestModule | ||
import org.assertj.core.api.Assertions.assertThatExceptionOfType | ||
import org.assertj.core.api.Assertions.assertThatNoException | ||
import org.junit.jupiter.api.Test | ||
|
||
@MiskTest(startService = true) | ||
class AvoidUsingSelectStarListenerTest { | ||
@SuppressWarnings("unused") @MiskTestModule private var module = ClientJooqTestingModule() | ||
@Inject | ||
@JooqDBIdentifier private lateinit var transacter: JooqTransacter | ||
|
||
@Test fun `using select star throws an exception`() { | ||
transacter.transaction { (ctx) -> | ||
ctx.newRecord(MOVIE).apply { | ||
this.genre = Genre.COMEDY.name | ||
this.name = "Enter the dragon" | ||
}.also { it.store() } | ||
} | ||
|
||
assertThatExceptionOfType(AvoidUsingSelectStarException::class.java).isThrownBy { | ||
transacter.transaction { | ||
(ctx) -> ctx.select(MOVIE.asterisk()).from(MOVIE).fetchOne() | ||
} | ||
} | ||
} | ||
|
||
@Test fun `selecting all the fields does not throw an exception`() { | ||
transacter.transaction { (ctx) -> | ||
ctx.newRecord(MOVIE).apply { | ||
this.genre = Genre.COMEDY.name | ||
this.name = "Enter the dragon" | ||
}.also { it.store() } | ||
} | ||
|
||
assertThatNoException().isThrownBy { | ||
transacter.transaction { | ||
(ctx) -> ctx.select(*MOVIE.fields()).from(MOVIE).fetchOne() | ||
} | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
misk-jooq/src/test/kotlin/misk/jooq/listeners/JooqTimestampRecordListenerTest.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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package misk.jooq.listeners | ||
|
||
import jakarta.inject.Inject | ||
import misk.jooq.JooqTransacter | ||
import misk.jooq.config.ClientJooqTestingModule | ||
import misk.jooq.config.JooqDBIdentifier | ||
import misk.jooq.model.Genre | ||
import misk.jooq.testgen.tables.references.MOVIE | ||
import misk.jooq.toLocalDateTime | ||
import misk.testing.MiskTest | ||
import misk.testing.MiskTestModule | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import wisp.time.FakeClock | ||
import java.time.LocalDateTime | ||
import java.time.temporal.ChronoUnit | ||
|
||
@MiskTest(startService = true) | ||
class JooqTimestampRecordListenerTest { | ||
@SuppressWarnings("unused") @MiskTestModule private var module = ClientJooqTestingModule() | ||
@Inject | ||
@JooqDBIdentifier private lateinit var transacter: JooqTransacter | ||
@Inject | ||
private lateinit var clock: FakeClock | ||
|
||
private val anHourBefore: LocalDateTime by lazy { | ||
clock.instant().minus(1, ChronoUnit.HOURS).toLocalDateTime() | ||
} | ||
|
||
private val aMinuteBefore: LocalDateTime by lazy { | ||
clock.instant().minus(1, ChronoUnit.MINUTES).toLocalDateTime() | ||
} | ||
|
||
@Test fun `should set the created and updated times if not already set`() { | ||
val movie = transacter.transaction { (ctx) -> | ||
ctx.newRecord(MOVIE).apply { | ||
this.genre = Genre.COMEDY.name | ||
this.name = "Enter the dragon" | ||
}.also { it.store() } | ||
} | ||
|
||
assertThat(movie.createdAt).isEqualTo(clock.instant().toLocalDateTime()) | ||
assertThat(movie.updatedAt).isEqualTo(clock.instant().toLocalDateTime()) | ||
|
||
val fetchedMovie = transacter.transaction { (ctx) -> | ||
ctx.selectFrom(MOVIE).where(MOVIE.NAME.eq("Enter the dragon")).fetchOne()!! | ||
} | ||
|
||
assertThat(fetchedMovie.createdAt).isEqualTo(clock.instant().toLocalDateTime()) | ||
assertThat(fetchedMovie.updatedAt).isEqualTo(clock.instant().toLocalDateTime()) | ||
} | ||
|
||
@Test fun `should not set the created and updated times if already set`() { | ||
val movie = transacter.transaction { (ctx) -> | ||
ctx.newRecord(MOVIE).apply { | ||
this.genre = Genre.COMEDY.name | ||
this.name = "Enter the dragon" | ||
this.createdAt = anHourBefore | ||
this.updatedAt = anHourBefore | ||
}.also { it.store() } | ||
} | ||
|
||
assertThat(movie.createdAt).isEqualTo(anHourBefore) | ||
assertThat(movie.updatedAt).isEqualTo(anHourBefore) | ||
|
||
val fetchedMovie = transacter.transaction { (ctx) -> | ||
ctx.selectFrom(MOVIE).where(MOVIE.NAME.eq("Enter the dragon")).fetchOne()!! | ||
} | ||
|
||
assertThat(fetchedMovie.createdAt).isEqualTo(anHourBefore) | ||
assertThat(fetchedMovie.updatedAt).isEqualTo(anHourBefore) | ||
} | ||
|
||
@Test fun `updates the updated at column during an update`() { | ||
transacter.transaction { (ctx) -> | ||
val record = ctx.newRecord(MOVIE).apply { | ||
this.genre = Genre.COMEDY.name | ||
this.name = "Enter the dragon" | ||
this.createdAt = anHourBefore | ||
this.updatedAt = anHourBefore | ||
}.also { it.store() } | ||
|
||
// force an update | ||
record.store() | ||
} | ||
|
||
val movie = transacter.transaction { (ctx) -> | ||
ctx.selectFrom(MOVIE).where(MOVIE.NAME.eq("Enter the dragon")).fetchOne()!! | ||
} | ||
assertThat(movie.updatedAt).isEqualTo(clock.instant().toLocalDateTime()) | ||
} | ||
|
||
@Test fun `updated at will not be set if it has been changed before store is called`() { | ||
transacter.transaction { (ctx) -> | ||
val record = ctx.newRecord(MOVIE).apply { | ||
this.genre = Genre.COMEDY.name | ||
this.name = "Enter the dragon" | ||
this.createdAt = anHourBefore | ||
this.updatedAt = anHourBefore | ||
}.also { it.store() } | ||
|
||
// force an update | ||
record.updatedAt = aMinuteBefore | ||
record.store() | ||
} | ||
|
||
val movie = transacter.transaction { (ctx) -> | ||
ctx.selectFrom(MOVIE).where(MOVIE.NAME.eq("Enter the dragon")).fetchOne()!! | ||
} | ||
assertThat(movie.updatedAt).isEqualTo(aMinuteBefore) | ||
} | ||
} |