Skip to content

Commit 5db87a5

Browse files
committed
Implemented test cases for the action that converts from SQL files to annotations.
1 parent 6215ac8 commit 5db87a5

File tree

73 files changed

+556
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+556
-27
lines changed

src/test/kotlin/org/domaframework/doma/intellij/action/dao/ConvertSqlAnnotationToFileActionTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import com.intellij.psi.PsiDocumentManager
2121
import org.domaframework.doma.intellij.DomaSqlTest
2222

2323
class ConvertSqlAnnotationToFileActionTest : DomaSqlTest() {
24-
private val sqlConversionPackage = "sqlconversion"
24+
private val sqlConversionPackage = "sqltofile"
2525
private val convertActionName = "Convert to SQL file (set sqlFile=true)"
2626
private val convertFamilyName = "Convert @Sql annotation to SQL file"
2727

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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.action.dao
17+
18+
import com.intellij.openapi.fileEditor.FileDocumentManager
19+
import com.intellij.psi.PsiDocumentManager
20+
import org.domaframework.doma.intellij.DomaSqlTest
21+
22+
class ConvertSqlFileToAnnotationActionTest : DomaSqlTest() {
23+
private val sqlToAnnotationPackage = "sqltoannotation"
24+
private val convertActionName = "Convert to @Sql annotation (set sqlFile=false)"
25+
private val convertFamilyName = "Convert SQL file to @Sql annotation"
26+
27+
fun testIntentionAvailableForSelectWithSqlFile() {
28+
val daoName = "SelectWithSqlFileDao"
29+
val sqlFileName = "selectEmployee"
30+
doTest(daoName, sqlFileName)
31+
}
32+
33+
fun testIntentionAvailableForInsertWithSqlFile() {
34+
val daoName = "InsertWithSqlFileDao"
35+
val sqlFileName = "insertEmployee"
36+
doTest(daoName, sqlFileName)
37+
}
38+
39+
fun testIntentionAvailableForUpdateWithSqlFile() {
40+
val daoName = "UpdateWithSqlFileDao"
41+
val sqlFileName = "updateEmployee"
42+
doTest(daoName, sqlFileName)
43+
}
44+
45+
fun testIntentionAvailableForDeleteWithSqlFile() {
46+
val daoName = "DeleteWithSqlFileDao"
47+
val sqlFileName = "deleteEmployee"
48+
doTest(daoName, sqlFileName)
49+
}
50+
51+
fun testIntentionAvailableForScriptWithSqlFile() {
52+
val daoName = "ScriptWithSqlFileDao"
53+
val sqlFileName = "createTables"
54+
doTest(daoName, sqlFileName, isScript = true)
55+
}
56+
57+
fun testIntentionAvailableForBatchInsertWithSqlFile() {
58+
val daoName = "BatchInsertWithSqlFileDao"
59+
val sqlFileName = "batchInsertEmployees"
60+
doTest(daoName, sqlFileName)
61+
}
62+
63+
fun testIntentionAvailableForBatchUpdateWithSqlFile() {
64+
val daoName = "BatchUpdateWithSqlFileDao"
65+
val sqlFileName = "batchUpdateEmployees"
66+
doTest(daoName, sqlFileName)
67+
}
68+
69+
fun testIntentionAvailableForBatchDeleteWithSqlFile() {
70+
val daoName = "BatchDeleteWithSqlFileDao"
71+
val sqlFileName = "batchDeleteEmployees"
72+
doTest(daoName, sqlFileName)
73+
}
74+
75+
fun testIntentionAvailableForSqlProcessorWithSqlFile() {
76+
val daoName = "SqlProcessorWithSqlFileDao"
77+
val sqlFileName = "processData"
78+
doTest(daoName, sqlFileName)
79+
}
80+
81+
fun testIntentionNotAvailableForMethodWithSqlAnnotation() {
82+
val daoName = "MethodWithSqlAnnotationDao"
83+
addDaoJavaFile("$sqlToAnnotationPackage/$daoName.java")
84+
val daoClass = findDaoClass("$sqlToAnnotationPackage.$daoName")
85+
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)
86+
87+
val intentions = myFixture.availableIntentions
88+
val convertIntention = intentions.find { it is ConvertSqlFileToAnnotationAction }
89+
90+
assertNull("$convertFamilyName intention should NOT be available when @Sql annotation already exists", convertIntention)
91+
}
92+
93+
fun testIntentionNotAvailableForMethodWithoutSqlFile() {
94+
val daoName = "MethodWithoutSqlFileDao"
95+
addDaoJavaFile("$sqlToAnnotationPackage/$daoName.java")
96+
val daoClass = findDaoClass("$sqlToAnnotationPackage.$daoName")
97+
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)
98+
99+
val intentions = myFixture.availableIntentions
100+
val convertIntention = intentions.find { it is ConvertSqlFileToAnnotationAction }
101+
102+
assertNull("$convertFamilyName intention should NOT be available when SQL file doesn't exist", convertIntention)
103+
}
104+
105+
fun testSqlFormattingInAnnotation() {
106+
val daoName = "SelectWithComplexSqlFileDao"
107+
val sqlFileName = "selectComplexQuery"
108+
doTest(daoName, sqlFileName)
109+
}
110+
111+
private fun doTest(
112+
daoName: String,
113+
sqlFileName: String,
114+
isScript: Boolean = false,
115+
) {
116+
addDaoJavaFile("$sqlToAnnotationPackage/$daoName.java")
117+
val extension = if (isScript) "script" else "sql"
118+
addSqlFile("$sqlToAnnotationPackage/$daoName/$sqlFileName.$extension")
119+
120+
val sqlFile = findSqlFile("$sqlToAnnotationPackage/$daoName/$sqlFileName.$extension")
121+
if (sqlFile == null) {
122+
fail("SQL file $sqlFileName.$extension should exist in $sqlToAnnotationPackage/$daoName")
123+
return
124+
}
125+
126+
myFixture.configureFromExistingVirtualFile(sqlFile)
127+
val intention = myFixture.findSingleIntention(convertActionName)
128+
129+
assertNotNull(
130+
"$convertActionName intention should be available",
131+
intention,
132+
)
133+
assertEquals(convertActionName, intention.text)
134+
assertEquals(convertFamilyName, intention.familyName)
135+
136+
myFixture.launchAction(intention)
137+
138+
val docMgr = PsiDocumentManager.getInstance(project)
139+
val fdm = FileDocumentManager.getInstance()
140+
fdm.saveAllDocuments()
141+
docMgr.commitAllDocuments()
142+
143+
val daoClass = findDaoClass("$sqlToAnnotationPackage.$daoName")
144+
myFixture.configureFromExistingVirtualFile(daoClass.containingFile.virtualFile)
145+
myFixture.checkResultByFile("java/doma/example/dao/$sqlToAnnotationPackage/$daoName.after.java")
146+
147+
val sqlFileAfter = findSqlFile("$sqlToAnnotationPackage/$daoName/$sqlFileName.$extension")
148+
assertNull("SQL file should be deleted after conversion", sqlFileAfter)
149+
}
150+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package doma.example.dao.sqltoannotation;
2+
3+
import doma.example.entity.Employee;
4+
import org.seasar.doma.BatchDelete;
5+
import org.seasar.doma.Dao;
6+
import org.seasar.doma.Sql;
7+
8+
import java.util.List;
9+
10+
@Dao
11+
public interface BatchDeleteWithSqlFileDao {
12+
@BatchDelete
13+
@Sql("""
14+
DELETE FROM employee
15+
WHERE id = /* employees.id */1
16+
""")
17+
int[] batchDeleteEmployees(List<Employee> employees);
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package doma.example.dao.sqltoannotation;
2+
3+
import doma.example.entity.Employee;
4+
import org.seasar.doma.BatchDelete;
5+
import org.seasar.doma.Dao;
6+
7+
import java.util.List;
8+
9+
@Dao
10+
public interface BatchDeleteWithSqlFileDao {
11+
@BatchDelete(sqlFile = true)
12+
int[] batchDeleteEmployees(List<Employee> employees);
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package doma.example.dao.sqltoannotation;
2+
3+
import doma.example.entity.Employee;
4+
import org.seasar.doma.BatchInsert;
5+
import org.seasar.doma.Dao;
6+
import org.seasar.doma.Sql;
7+
8+
import java.util.List;
9+
10+
@Dao
11+
public interface BatchInsertWithSqlFileDao {
12+
@BatchInsert
13+
@Sql("""
14+
INSERT INTO employee
15+
(id
16+
, name
17+
, age)
18+
VALUES ( /* employees.id */1
19+
, /* employees.name */'John'
20+
, /* employees.age */30 )
21+
""")
22+
int[] batchInsertEmployees(List<Employee> employees);
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package doma.example.dao.sqltoannotation;
2+
3+
import doma.example.entity.Employee;
4+
import org.seasar.doma.BatchInsert;
5+
import org.seasar.doma.Dao;
6+
7+
import java.util.List;
8+
9+
@Dao
10+
public interface BatchInsertWithSqlFileDao {
11+
@BatchInsert(sqlFile = true)
12+
int[] batchInsertEmployees(List<Employee> employees);
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package doma.example.dao.sqltoannotation;
2+
3+
import doma.example.entity.Employee;
4+
import org.seasar.doma.BatchUpdate;
5+
import org.seasar.doma.Dao;
6+
import org.seasar.doma.Sql;
7+
8+
import java.util.List;
9+
10+
@Dao
11+
public interface BatchUpdateWithSqlFileDao {
12+
@BatchUpdate
13+
@Sql("""
14+
UPDATE employee
15+
SET name = /* employees.name */'John'
16+
, age = /* employees.age */30
17+
WHERE id = /* employees.id */1
18+
""")
19+
int[] batchUpdateEmployees(List<Employee> employees);
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package doma.example.dao.sqltoannotation;
2+
3+
import doma.example.entity.Employee;
4+
import org.seasar.doma.BatchUpdate;
5+
import org.seasar.doma.Dao;
6+
7+
import java.util.List;
8+
9+
@Dao
10+
public interface BatchUpdateWithSqlFileDao {
11+
@BatchUpdate(sqlFile = true)
12+
int[] batchUpdateEmployees(List<Employee> employees);
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package doma.example.dao.sqltoannotation;
2+
3+
import doma.example.entity.Employee;
4+
import org.seasar.doma.Dao;
5+
import org.seasar.doma.Delete;
6+
import org.seasar.doma.Sql;
7+
8+
@Dao
9+
public interface DeleteWithSqlFileDao {
10+
@Delete
11+
@Sql("""
12+
DELETE FROM employee
13+
WHERE id = /* employee.id */1
14+
""")
15+
int deleteEmployee(Employee employee);
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package doma.example.dao.sqltoannotation;
2+
3+
import doma.example.entity.Employee;
4+
import org.seasar.doma.Dao;
5+
import org.seasar.doma.Delete;
6+
7+
@Dao
8+
public interface DeleteWithSqlFileDao {
9+
@Delete(sqlFile = true)
10+
int deleteEmployee(Employee employee);
11+
}

0 commit comments

Comments
 (0)