Skip to content

Commit 7329ac6

Browse files
committed
feat(database): add GenerateUnittestAction
This commit adds the `GenerateUnittestAction` class to the database module. This action generates Java unit test code for Oracle PL/SQL code. It is available only for files written in the Oracle dialect. The action takes the selected PL/SQL code, renders it using a template, and prompts the user to provide additional information. The generated code is then sent to the chat window.
1 parent 120a59b commit 7329ac6

File tree

6 files changed

+78
-9
lines changed

6 files changed

+78
-9
lines changed

exts/database/src/main/kotlin/cc/unitmesh/database/actions/GenerateEntityAction.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.database.actions
22

3+
import cc.unitmesh.database.actions.base.SqlMigrationContext
34
import cc.unitmesh.devti.gui.sendToChatWindow
45
import cc.unitmesh.devti.intentions.action.base.AbstractChatIntention
56
import cc.unitmesh.devti.provider.ContextPrompter
@@ -15,7 +16,7 @@ class GenerateEntityAction : AbstractChatIntention() {
1516

1617
override fun priority() = 901
1718

18-
override fun getFamilyName(): String = "Generate Java Entity"
19+
override fun getFamilyName(): String = "PL/SQL Migration"
1920

2021
override fun getText(): String = "Generate Java Entity"
2122

@@ -31,7 +32,7 @@ class GenerateEntityAction : AbstractChatIntention() {
3132

3233
val templateRender = TemplateRender("genius/migration")
3334
val template = templateRender.getTemplate("gen-entity.vm")
34-
templateRender.context = GenEntityContext(
35+
templateRender.context = SqlMigrationContext(
3536
lang = file.language.displayName ?: "",
3637
sql = selectedText,
3738
)
@@ -48,7 +49,3 @@ class GenerateEntityAction : AbstractChatIntention() {
4849
}
4950
}
5051

51-
data class GenEntityContext(
52-
val lang: String = "",
53-
var sql: String = "",
54-
)

exts/database/src/main/kotlin/cc/unitmesh/database/actions/GenerateFunctionAction.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.database.actions
22

3+
import cc.unitmesh.database.actions.base.SqlMigrationContext
34
import cc.unitmesh.devti.gui.sendToChatWindow
45
import cc.unitmesh.devti.intentions.action.base.AbstractChatIntention
56
import cc.unitmesh.devti.provider.ContextPrompter
@@ -13,7 +14,7 @@ import com.intellij.sql.dialects.oracle.OraDialect
1314
class GenerateFunctionAction : AbstractChatIntention() {
1415
override fun priority() = 901
1516

16-
override fun getFamilyName(): String = "Generate Java Function"
17+
override fun getFamilyName(): String = "PL/SQL Migration"
1718

1819
override fun getText(): String = "Generate Java Function"
1920

@@ -38,7 +39,7 @@ class GenerateFunctionAction : AbstractChatIntention() {
3839

3940
val templateRender = TemplateRender("genius/migration")
4041
val template = templateRender.getTemplate("gen-function.vm")
41-
templateRender.context = GenFunctionContext(
42+
templateRender.context = SqlMigrationContext(
4243
lang = file.language.displayName ?: "",
4344
sql = selectedText,
4445
)
@@ -57,5 +58,4 @@ class GenerateFunctionAction : AbstractChatIntention() {
5758

5859
}
5960

60-
data class GenFunctionContext(val lang: String, val sql: String)
6161

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cc.unitmesh.database.actions
2+
3+
import cc.unitmesh.database.actions.base.SqlMigrationContext
4+
import cc.unitmesh.devti.gui.sendToChatWindow
5+
import cc.unitmesh.devti.intentions.action.base.AbstractChatIntention
6+
import cc.unitmesh.devti.provider.ContextPrompter
7+
import cc.unitmesh.devti.template.TemplateRender
8+
import com.intellij.openapi.diagnostic.logger
9+
import com.intellij.openapi.editor.Editor
10+
import com.intellij.openapi.project.Project
11+
import com.intellij.psi.PsiFile
12+
import com.intellij.sql.dialects.oracle.OraDialect
13+
14+
class GenerateUnittestAction : AbstractChatIntention() {
15+
override fun priority(): Int = 899
16+
17+
override fun getFamilyName(): String = "PL/SQL Migration"
18+
19+
override fun getText(): String = "Generate Java Unit Test"
20+
21+
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
22+
if (editor == null || file == null) return false
23+
return file.language is OraDialect
24+
}
25+
26+
val logger = logger<GenerateUnittestAction>()
27+
28+
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
29+
if (editor == null || file == null) return
30+
val selectedText = editor.selectionModel.selectedText ?: return
31+
32+
val templateRender = TemplateRender("genius/migration")
33+
val template = templateRender.getTemplate("gen-unittest.vm")
34+
templateRender.context = SqlMigrationContext(
35+
lang = file.language.displayName ?: "",
36+
sql = selectedText,
37+
)
38+
val prompter = templateRender.renderTemplate(template)
39+
40+
logger.info("Prompt: $prompter")
41+
42+
sendToChatWindow(project, getActionType()) { panel, service ->
43+
service.handlePromptAndResponse(panel, object : ContextPrompter() {
44+
override fun displayPrompt(): String = prompter
45+
override fun requestPrompt(): String = prompter
46+
}, null, false)
47+
}
48+
}
49+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cc.unitmesh.database.actions.base
2+
3+
data class SqlMigrationContext(
4+
val lang: String = "",
5+
var sql: String = "",
6+
)

exts/database/src/main/resources/cc.unitmesh.database.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
<bundleName>messages.AutoDevBundle</bundleName>
1616
<categoryKey>intention.category.llm</categoryKey>
1717
</autoDevIntention>
18+
<autoDevIntention>
19+
<className>cc.unitmesh.database.actions.GenerateUnittestAction</className>
20+
<bundleName>messages.AutoDevBundle</bundleName>
21+
<categoryKey>intention.category.llm</categoryKey>
22+
</autoDevIntention>
1823

1924
<livingDocumentation
2025
language="SQL"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
You are a professional application migration programmer.
2+
Write java unit test code to test the following Oracle PL/SQL code.
3+
4+
— When some function is missing in Java, just skip it.
5+
- If you find some function is not correct, please fix it.
6+
- Follow the Java coding style.
7+
8+
```${context.lang}
9+
${context.sql}
10+
```
11+
12+

0 commit comments

Comments
 (0)