1
0

feat: 增加一些插入书籍的测试代码

This commit is contained in:
2024-12-22 22:34:09 +08:00
parent 2cc0840d3e
commit f0e4778034
4 changed files with 103 additions and 0 deletions

1
.idea/modules.xml generated
View File

@@ -3,6 +3,7 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/.idea/modules/bookstore.main.iml" filepath="$PROJECT_DIR$/.idea/modules/bookstore.main.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/modules/bookstore.main.iml" filepath="$PROJECT_DIR$/.idea/modules/bookstore.main.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/bookstore.test.iml" filepath="$PROJECT_DIR$/.idea/modules/bookstore.test.iml" />
</modules> </modules>
</component> </component>
</project> </project>

9
.idea/modules/bookstore.test.iml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$/../../src/test" dumb="true">
<sourceFolder url="file://$MODULE_DIR$/../../src/test/kotlin" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/../../src/test/resources" type="java-test-resource" />
</content>
</component>
</module>

View 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}")
}
}
}
}

View File

@@ -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) }
}
}