@@ -21,6 +21,7 @@ import (
2121 "os"
2222 "testing"
2323
24+ "github.com/apache/incubator-devlake/core/dal"
2425 "github.com/apache/incubator-devlake/core/models/domainlayer/crossdomain"
2526 "github.com/apache/incubator-devlake/core/models/domainlayer/qa"
2627 "github.com/apache/incubator-devlake/helpers/e2ehelper"
@@ -34,8 +35,9 @@ func TestImportQaTestCasesDataFlow(t *testing.T) {
3435
3536 // Flush the relevant tables
3637 dataflowTester .FlushTabler (& qa.QaTestCase {})
37- dataflowTester .FlushTabler (& qa.QaProject {}) // qaTestCaseHandler also creates/updates QaProject
38- dataflowTester .FlushTabler (& qa.QaApi {}) // qaTestCaseHandler also creates/updates QaApi for API test cases
38+ dataflowTester .FlushTabler (& qa.QaProject {}) // qaTestCaseHandler also creates/updates QaProject
39+ dataflowTester .FlushTabler (& qa.QaApi {}) // qaTestCaseHandler also creates/updates QaApi for API test cases
40+ dataflowTester .FlushTabler (& qa.QaTestCaseExecution {})
3941 dataflowTester .FlushTabler (& crossdomain.Account {}) // qaTestCaseHandler also creates/updates Account for API test cases
4042
4143 // Create a new service instance
@@ -117,3 +119,157 @@ func TestImportQaTestCasesDataFlow(t *testing.T) {
117119 },
118120 )
119121}
122+
123+ func TestImportQaTestCasesDataCleanup (t * testing.T ) {
124+ var plugin impl.Customize
125+ dataflowTester := e2ehelper .NewDataFlowTester (t , "customize" , plugin )
126+
127+ // Flush all relevant tables
128+ dataflowTester .FlushTabler (& qa.QaTestCase {})
129+ dataflowTester .FlushTabler (& qa.QaProject {})
130+ dataflowTester .FlushTabler (& qa.QaApi {})
131+ dataflowTester .FlushTabler (& qa.QaTestCaseExecution {})
132+ dataflowTester .FlushTabler (& crossdomain.Account {})
133+
134+ svc := service .NewService (dataflowTester .Dal )
135+
136+ qaProjectId := "test-cleanup-project"
137+ qaProjectName := "Test Cleanup Project"
138+
139+ // 1. First import import test cases with API references
140+ testCasesFile , err := os .Open ("raw_tables/qa_full_test_cases_input.csv" )
141+ if err != nil {
142+ t .Fatal (err )
143+ }
144+ defer testCasesFile .Close ()
145+
146+ err = svc .ImportQaTestCases (qaProjectId , qaProjectName , testCasesFile , false )
147+ if err != nil {
148+ t .Fatal (err )
149+ }
150+
151+ // import test case executions
152+ testCaseExecutionsFile , err := os .Open ("raw_tables/qa_test_case_executions_input.csv" )
153+ if err != nil {
154+ t .Fatal (err )
155+ }
156+ defer testCaseExecutionsFile .Close ()
157+
158+ err = svc .ImportQaTestCaseExecutions (qaProjectId , testCaseExecutionsFile , false )
159+ if err != nil {
160+ t .Fatal (err )
161+ }
162+
163+ // Then import APIs
164+ apisFile , err := os .Open ("raw_tables/qa_apis_input.csv" )
165+ if err != nil {
166+ t .Fatal (err )
167+ }
168+ defer apisFile .Close ()
169+
170+ err = svc .ImportQaApis (qaProjectId , apisFile , false )
171+ if err != nil {
172+ t .Fatal (err )
173+ }
174+
175+ // Verify APIs, test cases and test case executions were imported
176+ var initialApiCount int64
177+ err = dataflowTester .Dal .First (
178+ & initialApiCount ,
179+ dal .Select ("COUNT(*)" ),
180+ dal .From (& qa.QaApi {}),
181+ dal .Where ("qa_project_id = ?" , qaProjectId ),
182+ )
183+ if err != nil {
184+ t .Fatal (err )
185+ }
186+ if initialApiCount == 0 {
187+ t .Error ("Expected API data to be imported initially" )
188+ }
189+
190+ var initialTestCaseCount int64
191+ err = dataflowTester .Dal .First (
192+ & initialTestCaseCount ,
193+ dal .Select ("COUNT(*)" ),
194+ dal .From (& qa.QaTestCase {}),
195+ dal .Where ("qa_project_id = ?" , qaProjectId ),
196+ )
197+ if err != nil {
198+ t .Fatal (err )
199+ }
200+ if initialTestCaseCount == 0 {
201+ t .Error ("Expected test cases to be imported initially" )
202+ }
203+
204+ var initialTestCaseExecutionCount int64
205+ err = dataflowTester .Dal .First (
206+ & initialTestCaseExecutionCount ,
207+ dal .Select ("COUNT(*)" ),
208+ dal .From (& qa.QaTestCaseExecution {}),
209+ dal .Where ("qa_project_id = ?" , qaProjectId ),
210+ )
211+ if err != nil {
212+ t .Fatal (err )
213+ }
214+
215+ if initialTestCaseExecutionCount == 0 {
216+ t .Error ("Expected test case executions to be imported initially" )
217+ }
218+
219+ // 2. Second import non-incremental - test cases
220+ nonApiDataFile , err := os .Open ("raw_tables/qa_non_api_test_cases.csv" )
221+ if err != nil {
222+ t .Fatal (err )
223+ }
224+ defer nonApiDataFile .Close ()
225+
226+ err = svc .ImportQaTestCases (qaProjectId , qaProjectName , nonApiDataFile , false )
227+ if err != nil {
228+ t .Fatal (err )
229+ }
230+
231+ // Verify API data was cleaned up
232+ var finalApiCount int64
233+ err = dataflowTester .Dal .First (
234+ & finalApiCount ,
235+ dal .Select ("COUNT(*)" ),
236+ dal .From (& qa.QaApi {}),
237+ dal .Where ("qa_project_id = ?" , qaProjectId ),
238+ )
239+ if err != nil {
240+ t .Fatal (err )
241+ }
242+ if finalApiCount != 0 {
243+ t .Errorf ("Expected API data to be cleaned up, but found %d records" , finalApiCount )
244+ }
245+
246+ // Verify test case execution data was cleaned up
247+ var finalTestCaseExecutionCount int64
248+ err = dataflowTester .Dal .First (
249+ & finalTestCaseExecutionCount ,
250+ dal .Select ("COUNT(*)" ),
251+ dal .From (& qa.QaTestCaseExecution {}),
252+ dal .Where ("qa_project_id = ?" , qaProjectId ),
253+ )
254+ if err != nil {
255+ t .Fatal (err )
256+ }
257+ if finalTestCaseExecutionCount != 0 {
258+ t .Errorf ("Expected test case executions to be cleaned up, but found %d records" , finalTestCaseExecutionCount )
259+ }
260+
261+ // Verify test case count is correct (should be 2)
262+ var finalTestCaseCount int64
263+ err = dataflowTester .Dal .First (
264+ & finalTestCaseCount ,
265+ dal .Select ("COUNT(*)" ),
266+ dal .From (& qa.QaTestCase {}),
267+ dal .Where ("qa_project_id = ?" , qaProjectId ),
268+ )
269+ if err != nil {
270+ t .Fatal (err )
271+ }
272+ if finalTestCaseCount != 2 {
273+ t .Errorf ("Expected 2 test cases after non-incremental import, got %d" , finalTestCaseCount )
274+ }
275+ }
0 commit comments