-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReservationTest.kt
132 lines (111 loc) · 5.1 KB
/
ReservationTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package pl.touk.krush
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.within
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.SqlExpressionBuilder
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.batchInsert
import org.jetbrains.exposed.sql.deleteAll
import org.jetbrains.exposed.sql.replace
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
typealias WhereEx = SqlExpressionBuilder.() -> Op<Boolean>
class ReservationTest : BaseDatabaseTest() {
@AfterEach
internal fun tearDown() {
transaction {
ReservationTable.deleteAll()
}
}
@Test
fun shouldUpdateUsingExposed() {
transaction {
SchemaUtils.create(ReservationTable)
// given
val reservation = Reservation().reserve().let(ReservationTable::insert)
val byUid: WhereEx = { ReservationTable.uid eq reservation.uid }
val persistedReservation = ReservationTable.select(byUid).singleOrNull()?.toReservation()
assertThat(persistedReservation?.status).isEqualTo(Status.RESERVED)
assertThat(persistedReservation?.reservedAt).isCloseTo(reservation.reservedAt, within(10, ChronoUnit.MILLIS))
assertThat(persistedReservation?.freedAt).isNull()
// when
val freedAt = LocalDateTime.now()
val updated = ReservationTable.update({
(ReservationTable.uid eq reservation.uid) and (ReservationTable.status eq Status.RESERVED) }) {
it[ReservationTable.status] = Status.FREE
it[ReservationTable.freedAt] = freedAt
}
if (updated < 1) {
throw IllegalStateException("Wrong status!")
}
// then
val updatedReservation = ReservationTable.select(byUid).singleOrNull()?.toReservation()
assertThat(updatedReservation?.status).isEqualTo(Status.FREE)
assertThat(updatedReservation?.reservedAt).isCloseTo(reservation.reservedAt, within(10, ChronoUnit.MILLIS))
assertThat(updatedReservation?.freedAt).isCloseTo(freedAt, within(10, ChronoUnit.MILLIS))
}
}
@Test
fun shouldUpdateWithFrom() {
transaction {
SchemaUtils.create(ReservationTable)
// given
val reservation = Reservation().reserve().let(ReservationTable::insert)
val byUid: WhereEx = { ReservationTable.uid eq reservation.uid }
val persistedReservation = ReservationTable.select(byUid).singleOrNull()?.toReservation()
assertThat(persistedReservation?.status).isEqualTo(Status.RESERVED)
assertThat(persistedReservation?.reservedAt).isCloseTo(reservation.reservedAt, within(10, ChronoUnit.MILLIS))
assertThat(persistedReservation?.freedAt).isNull()
// when
val freedReservation = reservation.free()
ReservationTable.update(byUid) { it.from(freedReservation) }
// then
val updatedReservation = ReservationTable.select(byUid).singleOrNull()?.toReservation()
assertThat(updatedReservation?.status).isEqualTo(Status.FREE)
assertThat(updatedReservation?.reservedAt).isCloseTo(reservation.reservedAt, within(10, ChronoUnit.MILLIS))
assertThat(updatedReservation?.freedAt).isCloseTo(freedReservation.freedAt, within(10, ChronoUnit.MILLIS))
}
}
@Test
fun shouldReplace() {
transaction {
SchemaUtils.create(ReservationTable)
// given
val reservation = Reservation().reserve()
// when
ReservationTable.replace { it.from(reservation) }
val freedReservation = reservation.free()
ReservationTable.replace { it.from(freedReservation) }
// then
val allReservations = ReservationTable.selectAll().toReservationList()
assertThat(allReservations)
.usingRecursiveFieldByFieldElementComparatorIgnoringFields("reservedAt", "freedAt")
.containsExactly(freedReservation)
}
}
@Test
fun shouldBatchInsert() {
transaction {
SchemaUtils.create(ReservationTable)
// given
val reservation1 = Reservation().reserve()
val reservation2 = Reservation().reserve()
// when
ReservationTable.batchInsert(
listOf(reservation1, reservation2), body = { this.from(it) }
)
// then
val allReservations = ReservationTable.selectAll().toReservationList()
assertThat(allReservations)
.usingRecursiveFieldByFieldElementComparatorIgnoringFields("reservedAt")
.containsExactly(reservation1, reservation2)
}
}
}