test #433
@@ -18,11 +18,21 @@ class RecommendationSnapshotPersistenceAdapter(
|
|||||||
return repository.findLatestSnapshots(sectionType.name, offset, limit).map { it.toRecord() }
|
return repository.findLatestSnapshots(sectionType.name, offset, limit).map { it.toRecord() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun existsLatestSnapshot(sectionType: RecommendedSectionType): Boolean {
|
||||||
|
return repository.existsBySectionType(sectionType)
|
||||||
|
}
|
||||||
|
|
||||||
override fun replaceSnapshots(
|
override fun replaceSnapshots(
|
||||||
sectionType: RecommendedSectionType,
|
sectionType: RecommendedSectionType,
|
||||||
snapshotAt: LocalDateTime,
|
snapshotAt: LocalDateTime,
|
||||||
newSnapshots: List<RecommendationSnapshotRecord>
|
newSnapshots: List<RecommendationSnapshotRecord>
|
||||||
) {
|
) {
|
||||||
|
if (newSnapshots.isEmpty() && sectionType == RecommendedSectionType.AI_CHARACTER) {
|
||||||
|
repository.deleteBySectionType(sectionType)
|
||||||
|
repository.save(emptySnapshotMarker(sectionType, snapshotAt).toEntity())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
repository.deleteBySectionTypeAndSnapshotAt(sectionType, snapshotAt)
|
repository.deleteBySectionTypeAndSnapshotAt(sectionType, snapshotAt)
|
||||||
repository.saveAll(newSnapshots.map { it.toEntity() })
|
repository.saveAll(newSnapshots.map { it.toEntity() })
|
||||||
}
|
}
|
||||||
@@ -46,4 +56,21 @@ class RecommendationSnapshotPersistenceAdapter(
|
|||||||
randomTieBreaker = randomTieBreaker
|
randomTieBreaker = randomTieBreaker
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun emptySnapshotMarker(
|
||||||
|
sectionType: RecommendedSectionType,
|
||||||
|
snapshotAt: LocalDateTime
|
||||||
|
): RecommendationSnapshotRecord {
|
||||||
|
return RecommendationSnapshotRecord(
|
||||||
|
sectionType = sectionType,
|
||||||
|
targetId = EMPTY_SNAPSHOT_MARKER_TARGET_ID,
|
||||||
|
score = 0.0,
|
||||||
|
snapshotAt = snapshotAt,
|
||||||
|
randomTieBreaker = 0.0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val EMPTY_SNAPSHOT_MARKER_TARGET_ID = 0L
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ interface RecommendationSnapshotRepository : JpaRepository<RecommendationSnapsho
|
|||||||
from recommendation_snapshot latest
|
from recommendation_snapshot latest
|
||||||
where latest.section_type = :sectionType
|
where latest.section_type = :sectionType
|
||||||
)
|
)
|
||||||
|
and rs.target_id <> 0
|
||||||
order by rs.score desc, rs.random_tie_breaker asc
|
order by rs.score desc, rs.random_tie_breaker asc
|
||||||
limit :limit offset :offset
|
limit :limit offset :offset
|
||||||
""",
|
""",
|
||||||
@@ -29,4 +30,8 @@ interface RecommendationSnapshotRepository : JpaRepository<RecommendationSnapsho
|
|||||||
): List<RecommendationSnapshot>
|
): List<RecommendationSnapshot>
|
||||||
|
|
||||||
fun deleteBySectionTypeAndSnapshotAt(sectionType: RecommendedSectionType, snapshotAt: LocalDateTime)
|
fun deleteBySectionTypeAndSnapshotAt(sectionType: RecommendedSectionType, snapshotAt: LocalDateTime)
|
||||||
|
|
||||||
|
fun deleteBySectionType(sectionType: RecommendedSectionType)
|
||||||
|
|
||||||
|
fun existsBySectionType(sectionType: RecommendedSectionType): Boolean
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ interface RecommendationSnapshotPort {
|
|||||||
limit: Int = Int.MAX_VALUE
|
limit: Int = Int.MAX_VALUE
|
||||||
): List<RecommendationSnapshotRecord>
|
): List<RecommendationSnapshotRecord>
|
||||||
|
|
||||||
|
fun existsLatestSnapshot(sectionType: RecommendedSectionType): Boolean
|
||||||
|
|
||||||
fun replaceSnapshots(
|
fun replaceSnapshots(
|
||||||
sectionType: RecommendedSectionType,
|
sectionType: RecommendedSectionType,
|
||||||
snapshotAt: LocalDateTime,
|
snapshotAt: LocalDateTime,
|
||||||
|
|||||||
@@ -108,6 +108,40 @@ class RecommendationSnapshotPersistenceAdapterTest @Autowired constructor(
|
|||||||
assertEquals(3, repository.findAll().size)
|
assertEquals(3, repository.findAll().size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun shouldClearAiCharacterSectionSnapshotsWhenReplacingWithEmptySnapshots() {
|
||||||
|
val oldSnapshotAt = LocalDateTime.of(2026, 5, 28, 23, 59, 59)
|
||||||
|
val snapshotAt = LocalDateTime.of(2026, 5, 29, 23, 59, 59)
|
||||||
|
repository.saveAll(
|
||||||
|
listOf(
|
||||||
|
snapshot(RecommendedSectionType.AI_CHARACTER, targetId = 1L, score = 100.0, snapshotAt = oldSnapshotAt),
|
||||||
|
snapshot(RecommendedSectionType.CHEER_CREATOR, targetId = 2L, score = 200.0, snapshotAt = oldSnapshotAt)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
adapter.replaceSnapshots(RecommendedSectionType.AI_CHARACTER, snapshotAt, emptyList())
|
||||||
|
|
||||||
|
assertEquals(emptyList<RecommendationSnapshotRecord>(), adapter.findLatestSnapshots(RecommendedSectionType.AI_CHARACTER))
|
||||||
|
assertEquals(listOf(2L), adapter.findLatestSnapshots(RecommendedSectionType.CHEER_CREATOR).map { it.targetId })
|
||||||
|
assertEquals(true, adapter.existsLatestSnapshot(RecommendedSectionType.AI_CHARACTER))
|
||||||
|
assertEquals(
|
||||||
|
listOf(0L),
|
||||||
|
repository.findAll()
|
||||||
|
.filter { it.sectionType == RecommendedSectionType.AI_CHARACTER }
|
||||||
|
.map { it.targetId }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun shouldExcludeEmptySnapshotMarkerFromLatestSnapshotResults() {
|
||||||
|
val snapshotAt = LocalDateTime.of(2026, 5, 29, 23, 59, 59)
|
||||||
|
|
||||||
|
adapter.replaceSnapshots(RecommendedSectionType.AI_CHARACTER, snapshotAt, emptyList())
|
||||||
|
|
||||||
|
assertEquals(emptyList<RecommendationSnapshotRecord>(), adapter.findLatestSnapshots(RecommendedSectionType.AI_CHARACTER))
|
||||||
|
assertEquals(true, adapter.existsLatestSnapshot(RecommendedSectionType.AI_CHARACTER))
|
||||||
|
}
|
||||||
|
|
||||||
private fun snapshot(
|
private fun snapshot(
|
||||||
sectionType: RecommendedSectionType,
|
sectionType: RecommendedSectionType,
|
||||||
targetId: Long,
|
targetId: Long,
|
||||||
|
|||||||
Reference in New Issue
Block a user