feat: 增加一些插入书籍的测试代码
This commit is contained in:
63
src/test/kotlin/com/lanyuanxiaoyao/bookstore/BookImport.kt
Normal file
63
src/test/kotlin/com/lanyuanxiaoyao/bookstore/BookImport.kt
Normal file
@@ -0,0 +1,63 @@
|
||||
package com.lanyuanxiaoyao.bookstore
|
||||
|
||||
import cn.hutool.core.util.IdUtil
|
||||
import jakarta.annotation.Resource
|
||||
import java.io.File
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import org.springframework.test.context.ActiveProfiles
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("mysql-local")
|
||||
class BookImport {
|
||||
private val log = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@Resource
|
||||
private lateinit var bookRepository: BookRepository
|
||||
|
||||
@Resource
|
||||
private lateinit var chapterRepository: ChapterRepository
|
||||
|
||||
@Resource
|
||||
private lateinit var lineRepository: LineRepository
|
||||
|
||||
@Test
|
||||
fun run() {
|
||||
val book = bookRepository.findById("149e22b4411f4112a209fd7d17725d44").orElseThrow()
|
||||
|
||||
val bookFile = File("\\\\192.168.31.127\\home\\Drive\\books\\online\\H\\best\\超级淫乱系统.txt")
|
||||
val lines = bookFile.readText().split(Regex("\\n\\s*\\n"))
|
||||
var chapterIndex = 0
|
||||
var chapterName = ""
|
||||
lines.forEachIndexed { index, line ->
|
||||
if (index % 2 == 0) {
|
||||
chapterIndex++
|
||||
chapterName = line.trim().split(Regex("\\s+"))[1]
|
||||
} else {
|
||||
val chapter = chapterRepository.save(
|
||||
Chapter(
|
||||
IdUtil.fastSimpleUUID(),
|
||||
chapterIndex,
|
||||
chapterName,
|
||||
null,
|
||||
book
|
||||
)
|
||||
)
|
||||
val chapterLines = line.split("\n")
|
||||
.map { it.trim() }
|
||||
.mapIndexed { lineIndex, text ->
|
||||
Line(
|
||||
IdUtil.fastSimpleUUID(),
|
||||
lineIndex.toLong(),
|
||||
text,
|
||||
null,
|
||||
chapter
|
||||
)
|
||||
}
|
||||
lineRepository.saveAll(chapterLines)
|
||||
println("$chapterIndex $chapterName ${chapterLines.size}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.lanyuanxiaoyao.bookstore
|
||||
|
||||
import jakarta.annotation.Resource
|
||||
import jakarta.transaction.Transactional
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import org.springframework.data.domain.Sort
|
||||
import org.springframework.test.context.ActiveProfiles
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("mysql-local")
|
||||
class ChapterDescription {
|
||||
private val log = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
@Resource
|
||||
private lateinit var bookRepository: BookRepository
|
||||
|
||||
@Resource
|
||||
private lateinit var chapterRepository: ChapterRepository
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
fun run() {
|
||||
val chapters = chapterRepository.findAll(Sort.by(Sort.Direction.ASC, "sequence"))
|
||||
val chapter = chapters[0]
|
||||
val lines = chapter.content
|
||||
lines.forEach { line -> log.info(line.text) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user