|
| 1 | +package ru.astrainteractive.astramarket.api.market.impl |
| 2 | + |
| 3 | +import kotlinx.coroutines.test.runTest |
| 4 | +import kotlin.test.Test |
| 5 | +import kotlin.test.assertEquals |
| 6 | +import kotlin.test.assertNotNull |
| 7 | +import kotlin.test.assertNull |
| 8 | +import kotlin.test.assertTrue |
| 9 | + |
| 10 | +class ExposedMarketApiTest { |
| 11 | + private suspend fun withTestContext(block: suspend TestContext.() -> Unit) { |
| 12 | + val ctx = TestContext() |
| 13 | + ctx.onStart() |
| 14 | + try { |
| 15 | + ctx.block() |
| 16 | + } finally { |
| 17 | + ctx.onDisable() |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + fun GIVEN_emptyDatabase_WHEN_insertSlot_THEN_returnsNonNullId() { |
| 23 | + runTest { |
| 24 | + withTestContext { |
| 25 | + val insertedId = marketApi.insertSlot(createMarketSlot()) |
| 26 | + assertNotNull(insertedId) |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun GIVEN_insertedSlot_WHEN_getSlotById_THEN_returnsSlotWithMatchingData() { |
| 33 | + runTest { |
| 34 | + withTestContext { |
| 35 | + val slot = createMarketSlot(price = 250.0f, username = "Trader") |
| 36 | + val insertedId = marketApi.insertSlot(slot) |
| 37 | + assertNotNull(insertedId) |
| 38 | + |
| 39 | + val retrieved = marketApi.getSlot(insertedId) |
| 40 | + assertNotNull(retrieved) |
| 41 | + |
| 42 | + assertEquals(slot.price, retrieved.price) |
| 43 | + assertEquals(slot.minecraftUuid, retrieved.minecraftUuid) |
| 44 | + assertEquals(slot.minecraftUsername, retrieved.minecraftUsername) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun GIVEN_nonExistentId_WHEN_getSlot_THEN_returnsNull() { |
| 51 | + runTest { |
| 52 | + withTestContext { |
| 53 | + val retrieved = marketApi.getSlot(99999) |
| 54 | + assertNull(retrieved) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun GIVEN_insertedActiveSlot_WHEN_getActiveSlots_THEN_slotAppearsInList() { |
| 61 | + runTest { |
| 62 | + withTestContext { |
| 63 | + val slot = createMarketSlot(expired = false) |
| 64 | + marketApi.insertSlot(slot) |
| 65 | + |
| 66 | + val activeSlots = marketApi.getSlots(isExpired = false) |
| 67 | + assertNotNull(activeSlots) |
| 68 | + |
| 69 | + assertEquals(1, activeSlots.size) |
| 70 | + assertEquals(slot.minecraftUuid, activeSlots.first().minecraftUuid) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun GIVEN_insertedActiveSlot_WHEN_getExpiredSlots_THEN_returnsEmptyList() { |
| 77 | + runTest { |
| 78 | + withTestContext { |
| 79 | + marketApi.insertSlot(createMarketSlot(expired = false)) |
| 80 | + |
| 81 | + val expiredSlots = marketApi.getSlots(isExpired = true) |
| 82 | + assertNotNull(expiredSlots) |
| 83 | + |
| 84 | + assertTrue(expiredSlots.isEmpty()) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun GIVEN_insertedActiveSlot_WHEN_expireSlot_THEN_slotMovesToExpiredList() { |
| 91 | + runTest { |
| 92 | + withTestContext { |
| 93 | + val insertedId = marketApi.insertSlot(createMarketSlot(expired = false)) |
| 94 | + assertNotNull(insertedId) |
| 95 | + val activeSlot = marketApi.getSlot(insertedId) |
| 96 | + assertNotNull(activeSlot) |
| 97 | + |
| 98 | + marketApi.expireSlot(activeSlot) |
| 99 | + |
| 100 | + val expiredSlots = marketApi.getSlots(isExpired = true) |
| 101 | + assertNotNull(expiredSlots) |
| 102 | + val activeSlots = marketApi.getSlots(isExpired = false) |
| 103 | + assertNotNull(activeSlots) |
| 104 | + assertEquals(1, expiredSlots.size) |
| 105 | + assertTrue(activeSlots.isEmpty()) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun GIVEN_insertedSlot_WHEN_deleteSlot_THEN_slotCannotBeRetrieved() { |
| 112 | + runTest { |
| 113 | + withTestContext { |
| 114 | + val insertedId = marketApi.insertSlot(createMarketSlot()) |
| 115 | + assertNotNull(insertedId) |
| 116 | + val insertedSlot = marketApi.getSlot(insertedId) |
| 117 | + assertNotNull(insertedSlot) |
| 118 | + |
| 119 | + marketApi.deleteSlot(insertedSlot) |
| 120 | + |
| 121 | + val retrieved = marketApi.getSlot(insertedId) |
| 122 | + assertNull(retrieved) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun GIVEN_deletedSlot_WHEN_getSlots_THEN_deletedSlotIsAbsent() { |
| 129 | + runTest { |
| 130 | + withTestContext { |
| 131 | + val insertedId = marketApi.insertSlot(createMarketSlot()) |
| 132 | + assertNotNull(insertedId) |
| 133 | + val insertedSlot = marketApi.getSlot(insertedId) |
| 134 | + assertNotNull(insertedSlot) |
| 135 | + |
| 136 | + marketApi.deleteSlot(insertedSlot) |
| 137 | + |
| 138 | + val allSlots = marketApi.getSlots(isExpired = false) |
| 139 | + assertNotNull(allSlots) |
| 140 | + assertTrue(allSlots.none { it.id == insertedId }) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun GIVEN_multipleSlotsForPlayer_WHEN_countPlayerSlots_THEN_returnsExactCount() { |
| 147 | + runTest { |
| 148 | + withTestContext { |
| 149 | + val uuid = "550e8400-e29b-41d4-a716-446655440001" |
| 150 | + repeat(3) { marketApi.insertSlot(createMarketSlot(uuid = uuid)) } |
| 151 | + |
| 152 | + val count = marketApi.countPlayerSlots(uuid) |
| 153 | + |
| 154 | + assertEquals(3, count) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + fun GIVEN_noSlotsForPlayer_WHEN_countPlayerSlots_THEN_returnsZero() { |
| 161 | + runTest { |
| 162 | + withTestContext { |
| 163 | + val uuid = "550e8400-e29b-41d4-a716-446655440001" |
| 164 | + |
| 165 | + val count = marketApi.countPlayerSlots(uuid) |
| 166 | + |
| 167 | + assertEquals(0, count) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + fun GIVEN_slotsForTwoPlayers_WHEN_getUserSlots_THEN_returnsOnlyRequestedPlayerSlots() { |
| 174 | + runTest { |
| 175 | + withTestContext { |
| 176 | + val firstPlayerUuid = "550e8400-e29b-41d4-a716-446655440001" |
| 177 | + val secondPlayerUuid = "550e8400-e29b-41d4-a716-446655440002" |
| 178 | + repeat(2) { marketApi.insertSlot(createMarketSlot(uuid = firstPlayerUuid)) } |
| 179 | + marketApi.insertSlot(createMarketSlot(uuid = secondPlayerUuid)) |
| 180 | + |
| 181 | + val firstPlayerSlots = marketApi.getUserSlots(firstPlayerUuid, isExpired = false) |
| 182 | + assertNotNull(firstPlayerSlots) |
| 183 | + |
| 184 | + assertEquals(2, firstPlayerSlots.size) |
| 185 | + assertTrue(firstPlayerSlots.all { it.minecraftUuid == firstPlayerUuid }) |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + fun GIVEN_activeAndExpiredSlotsForPlayer_WHEN_getUserActiveSlots_THEN_returnsOnlyActiveSlots() { |
| 192 | + runTest { |
| 193 | + withTestContext { |
| 194 | + val uuid = "550e8400-e29b-41d4-a716-446655440001" |
| 195 | + marketApi.insertSlot(createMarketSlot(uuid = uuid, expired = false)) |
| 196 | + marketApi.insertSlot(createMarketSlot(uuid = uuid, expired = true)) |
| 197 | + |
| 198 | + val activeUserSlots = marketApi.getUserSlots(uuid, isExpired = false) |
| 199 | + assertNotNull(activeUserSlots) |
| 200 | + |
| 201 | + assertEquals(1, activeUserSlots.size) |
| 202 | + assertTrue(activeUserSlots.all { !it.expired }) |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + fun GIVEN_slotCreatedLongAgo_WHEN_getSlotsOlderThan_THEN_slotIsIncluded() { |
| 209 | + runTest { |
| 210 | + withTestContext { |
| 211 | + val oneHourInMillis = 60 * 60 * 1000L |
| 212 | + marketApi.insertSlot(createMarketSlot(timeOffset = -oneHourInMillis * 2)) |
| 213 | + |
| 214 | + val oldSlots = marketApi.getSlotsOlderThan(oneHourInMillis) |
| 215 | + assertNotNull(oldSlots) |
| 216 | + |
| 217 | + assertEquals(1, oldSlots.size) |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + fun GIVEN_recentlyCreatedSlot_WHEN_getSlotsOlderThan_THEN_slotIsNotIncluded() { |
| 224 | + runTest { |
| 225 | + withTestContext { |
| 226 | + val oneHourInMillis = 60 * 60 * 1000L |
| 227 | + marketApi.insertSlot(createMarketSlot(timeOffset = 0L)) |
| 228 | + |
| 229 | + val oldSlots = marketApi.getSlotsOlderThan(oneHourInMillis) |
| 230 | + assertNotNull(oldSlots) |
| 231 | + |
| 232 | + assertTrue(oldSlots.isEmpty()) |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + fun GIVEN_mixOfOldAndNewSlots_WHEN_getSlotsOlderThan_THEN_returnsOnlyOldSlots() { |
| 239 | + runTest { |
| 240 | + withTestContext { |
| 241 | + val oneHourInMillis = 60 * 60 * 1000L |
| 242 | + marketApi.insertSlot(createMarketSlot(timeOffset = -oneHourInMillis * 2)) |
| 243 | + marketApi.insertSlot(createMarketSlot(timeOffset = -oneHourInMillis * 3)) |
| 244 | + marketApi.insertSlot(createMarketSlot(timeOffset = 0L)) |
| 245 | + |
| 246 | + val oldSlots = marketApi.getSlotsOlderThan(oneHourInMillis) |
| 247 | + assertNotNull(oldSlots) |
| 248 | + |
| 249 | + assertEquals(2, oldSlots.size) |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | +} |
0 commit comments