Skip to content

Commit c9133fc

Browse files
committed
Add test cases for custom function support
1 parent a9c1048 commit c9133fc

File tree

16 files changed

+242
-4
lines changed

16 files changed

+242
-4
lines changed

src/test/kotlin/org/domaframework/doma/intellij/DomaSqlTest.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() {
5757
addEntityJavaFile("Project.java")
5858
addEntityJavaFile("ProjectDetail.java")
5959
addEntityJavaFile("Principal.java")
60+
61+
addExpressionJavaFile("TestExpressionFunctions.java")
62+
addExpressionJavaFile("TestNotExpressionFunctions.java")
6063
}
6164

6265
@Throws(Exception::class)
@@ -188,7 +191,7 @@ open class DomaSqlTest : LightJavaCodeInsightFixtureTestCase() {
188191
protected fun addExpressionJavaFile(fileName: String) {
189192
val file = File("$testDataPath/java/$packagePath/expression/$fileName")
190193
myFixture.addFileToProject(
191-
"main/java/$packagePath/entity/$fileName",
194+
"main/java/$packagePath/expression/$fileName",
192195
file.readText(),
193196
)
194197
}

src/test/kotlin/org/domaframework/doma/intellij/complate/sql/SqlCompleteTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class SqlCompleteTest : DomaSqlTest() {
5959
"$testDapName/completeOptionalByForItem.sql",
6060
"$testDapName/completeOptionalBatchAnnotation.sql",
6161
"$testDapName/completeForDirectiveItem.sql",
62+
"$testDapName/completeImplementCustomFunction.sql",
6263
)
6364
myFixture.enableInspections(SqlBindVariableValidInspector())
6465
}
@@ -400,6 +401,36 @@ class SqlCompleteTest : DomaSqlTest() {
400401
)
401402
}
402403

404+
fun testCompleteImplementCustomFunction() {
405+
settingCustomFunctions(
406+
mutableListOf("doma.example.expression.TestExpressionFunctions", "doma.example.expression.TestNotExpressionFunctions"),
407+
)
408+
innerDirectiveCompleteTest(
409+
"$testDapName/completeImplementCustomFunction.sql",
410+
listOf("userId()", "userName()", "userAge()", "langCode()", "isGest()", "isBlank()", "isNotBlank()"),
411+
listOf("getId()", "getName()", "getAge()", "getLangCode()", "isManager()"),
412+
)
413+
}
414+
415+
fun testCompleteNotImplementCustomFunction() {
416+
innerDirectiveCompleteTest(
417+
"$testDapName/completeImplementCustomFunction.sql",
418+
listOf("isEmpty()", "roundDownTimePart()", "isBlank()", "isNotBlank()"),
419+
listOf(
420+
"userId()",
421+
"userName()",
422+
"userAge()",
423+
"langCode()",
424+
"isGest()",
425+
"getId()",
426+
"getName()",
427+
"getAge()",
428+
"getLangCode()",
429+
"isManager()",
430+
),
431+
)
432+
}
433+
403434
private fun innerDirectiveCompleteTest(
404435
sqlFileName: String,
405436
expectedSuggestions: List<String>,

src/test/kotlin/org/domaframework/doma/intellij/inspection/sql/ParameterDefinedTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.domaframework.doma.intellij.inspection.sql
1717

1818
import org.domaframework.doma.intellij.DomaSqlTest
19+
import org.domaframework.doma.intellij.common.helper.ActiveProjectHelper
1920
import org.domaframework.doma.intellij.inspection.sql.inspector.SqlBindVariableValidInspector
2021

2122
/**
@@ -39,6 +40,7 @@ class ParameterDefinedTest : DomaSqlTest() {
3940
"$testDaoName/callStaticPropertyPackageName.sql",
4041
"$testDaoName/bindVariableForItemHasNextAndIndex.sql",
4142
"$testDaoName/optionalDaoParameterFieldAccess.sql",
43+
"$testDaoName/implementCustomFunctions.sql",
4244
)
4345
myFixture.enableInspections(SqlBindVariableValidInspector())
4446
}
@@ -122,4 +124,20 @@ class ParameterDefinedTest : DomaSqlTest() {
122124

123125
myFixture.testHighlighting(false, false, false, sqlFile)
124126
}
127+
128+
fun testImplementCustomFunctions() {
129+
if (project != null) {
130+
ActiveProjectHelper.setCurrentActiveProject(project)
131+
}
132+
settingCustomFunctions(
133+
mutableListOf("doma.example.expression.TestExpressionFunctions", "doma.example.expression.TestNotExpressionFunctions"),
134+
)
135+
136+
val sqlFile =
137+
findSqlFile("$testDaoName/implementCustomFunctions.sql")
138+
assertNotNull("Not Found SQL File", sqlFile)
139+
if (sqlFile == null) return
140+
141+
myFixture.testHighlighting(false, false, false, sqlFile)
142+
}
125143
}

src/test/kotlin/org/domaframework/doma/intellij/reference/SqlReferenceTestCase.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class SqlReferenceTestCase : DomaSqlTest() {
4545
addSqlFile("$testPackage/$testDaoName/referenceStaticField.sql")
4646
addSqlFile("$testPackage/$testDaoName/referenceListFieldMethod.sql")
4747
addSqlFile("$testPackage/$testDaoName/referenceForItem.sql")
48+
addSqlFile("$testPackage/$testDaoName/referenceCustomFunction.sql")
4849
}
4950

5051
fun testReferenceDaoMethodParameter() {
@@ -126,6 +127,20 @@ class SqlReferenceTestCase : DomaSqlTest() {
126127
)
127128
}
128129

130+
fun testReferenceCustomFunction() {
131+
settingCustomFunctions(
132+
mutableListOf("doma.example.expression.TestExpressionFunctions", "doma.example.expression.TestNotExpressionFunctions"),
133+
)
134+
referenceTest(
135+
"referenceCustomFunction",
136+
mapOf(
137+
"detail" to listOf("$daoParameterResolve:detail"),
138+
"projectDetailId" to listOf("$fieldResolve:projectDetailId"),
139+
"userId" to listOf("$methodResolve:userId"),
140+
),
141+
)
142+
}
143+
129144
private fun referenceTest(
130145
sqlFileName: String,
131146
resolveExpects: Map<String, List<String?>>,
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright Doma Tools Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.domaframework.doma.intellij.setting.state
17+
18+
import org.domaframework.doma.intellij.DomaSqlTest
19+
import org.domaframework.doma.intellij.setting.SettingComponent
20+
21+
class DomaToolsCustomFunctionSettingsTest : DomaSqlTest() {
22+
private val packageName = "doma.example.expression"
23+
24+
override fun setUp() {
25+
super.setUp()
26+
}
27+
28+
fun testCustomFunctionClassNamesPersistence() {
29+
val settings = DomaToolsCustomFunctionSettings.getInstance(project)
30+
val testClassNames =
31+
mutableListOf(
32+
"$packageName.TestExpressionFunctions",
33+
"$packageName.TestNotExpressionFunctions",
34+
)
35+
settings.getState().customFunctionClassNames = testClassNames.toMutableList()
36+
37+
val loaded = DomaToolsCustomFunctionSettings.State()
38+
loaded.customFunctionClassNames = testClassNames.toMutableList()
39+
settings.loadState(loaded)
40+
41+
assertEquals(testClassNames, settings.getState().customFunctionClassNames)
42+
}
43+
44+
fun testApplyAndReset() {
45+
val settings = DomaToolsCustomFunctionSettings.getInstance(project)
46+
val component = SettingComponent()
47+
val initialClassNames = mutableListOf("$packageName.TestExpressionFunctions")
48+
val newClassNames = mutableListOf("$packageName.TestExpressionFunctions", "$packageName.TestNotExpressionFunctions")
49+
50+
// init
51+
settings.getState().customFunctionClassNames = initialClassNames.toMutableList()
52+
settings.reset(component)
53+
assertEquals(initialClassNames, component.customFunctionClassNames)
54+
55+
// apply
56+
component.customFunctionClassNames = newClassNames.toMutableList()
57+
settings.apply(component)
58+
assertEquals(newClassNames, settings.getState().customFunctionClassNames)
59+
60+
// reset
61+
settings.getState().customFunctionClassNames = initialClassNames.toMutableList()
62+
settings.reset(component)
63+
assertEquals(initialClassNames, component.customFunctionClassNames)
64+
}
65+
66+
fun testIsModified() {
67+
val settings = DomaToolsCustomFunctionSettings.getInstance(project)
68+
val component = SettingComponent()
69+
val classNames = mutableListOf("$packageName.TestExpressionFunctions")
70+
settings.getState().customFunctionClassNames = classNames.toMutableList()
71+
component.customFunctionClassNames = classNames.toMutableList()
72+
assertFalse(settings.isModified(component))
73+
74+
val addClassNames = mutableListOf("$packageName.TestExpressionFunctions", "$packageName.TestNotExpressionFunctions")
75+
component.customFunctionClassNames = addClassNames
76+
assertTrue(settings.isModified(component))
77+
}
78+
}

src/test/testData/sql/formatter/FormattedInsertWithBindVariable.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ INSERT INTO /*# tableName */
77
, x3
88
, x4)
99
VALUES ( /* reportId */1
10-
, /* @tenantId() */1
10+
, /* reportId */1
1111
/*%for entity : entities */
1212
, /* entity.value */'abc'
1313
/*%end*/

src/test/testData/sql/formatter/InsertWithBindVariable.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ INSERT INTO /*# tableName */
66
/*%end*/
77
, x3
88
, x4)
9-
VALUES ( /* reportId */1, /* @tenantId() */1 /*%for entity : entities */, /* entity.value */'abc'
9+
VALUES ( /* reportId */1, /* reportId */1 /*%for entity : entities */, /* entity.value */'abc'
1010
/*%end*/
1111
, /* @userId() */1
1212
, x5

src/test/testData/src/main/java/doma/example/dao/EmployeeSummaryDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ interface EmployeeSummaryDao {
3939

4040
@Select
4141
Project optionalDaoParameterFieldAccess(Optional<Project> project, OptionalInt id, OptionalLong longId, OptionalDouble doubleId);
42+
43+
@Select
44+
EmployeeSummary implementCustomFunctions(EmployeeSummary employee);
4245
}

src/test/testData/src/main/java/doma/example/dao/SqlCompleteTestDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,7 @@ interface SqlCompleteTestDao {
100100
@Select
101101
Employee completeForDirectiveItem(List<Project> projects);
102102

103+
@Select
104+
Employee completeImplementCustomFunction(Project project);
105+
103106
}

src/test/testData/src/main/java/doma/example/dao/reference/ReferenceTestDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ public interface ReferenceTestDao {
2222
@Select
2323
List<Employee> referenceForItem(List<List<Employee>> employeesList);
2424

25+
@Select
26+
Project referenceCustomFunction(ProjectDetail detail);
27+
2528
}

0 commit comments

Comments
 (0)