@@ -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,145 @@ 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+ initialApiCount , err = dataflowTester .Dal .Count (
178+ dal .From (& qa.QaApi {}),
179+ dal .Where ("qa_project_id = ?" , qaProjectId ),
180+ )
181+ if err != nil {
182+ t .Fatal (err )
183+ }
184+ if initialApiCount == 0 {
185+ t .Error ("Expected API data to be imported initially" )
186+ }
187+
188+ var initialTestCaseCount int64
189+ initialTestCaseCount , err = dataflowTester .Dal .Count (
190+ dal .From (& qa.QaTestCase {}),
191+ dal .Where ("qa_project_id = ?" , qaProjectId ),
192+ )
193+ if err != nil {
194+ t .Fatal (err )
195+ }
196+ if initialTestCaseCount == 0 {
197+ t .Error ("Expected test cases to be imported initially" )
198+ }
199+
200+ var initialTestCaseExecutionCount int64
201+ initialTestCaseExecutionCount , err = dataflowTester .Dal .Count (
202+ dal .From (& qa.QaTestCaseExecution {}),
203+ dal .Where ("qa_project_id = ?" , qaProjectId ),
204+ )
205+ if err != nil {
206+ t .Fatal (err )
207+ }
208+
209+ if initialTestCaseExecutionCount == 0 {
210+ t .Error ("Expected test case executions to be imported initially" )
211+ }
212+
213+ // 2. Second import non-incremental - test cases
214+ nonApiDataFile , err := os .Open ("raw_tables/qa_non_api_test_cases.csv" )
215+ if err != nil {
216+ t .Fatal (err )
217+ }
218+ defer nonApiDataFile .Close ()
219+
220+ err = svc .ImportQaTestCases (qaProjectId , qaProjectName , nonApiDataFile , false )
221+ if err != nil {
222+ t .Fatal (err )
223+ }
224+
225+ // Verify API data was cleaned up
226+ var finalApiCount int64
227+ finalApiCount , err = dataflowTester .Dal .Count (
228+ dal .From (& qa.QaApi {}),
229+ dal .Where ("qa_project_id = ?" , qaProjectId ),
230+ )
231+ if err != nil {
232+ t .Fatal (err )
233+ }
234+ if finalApiCount != 0 {
235+ t .Errorf ("Expected API data to be cleaned up, but found %d records" , finalApiCount )
236+ }
237+
238+ // Verify test case execution data was cleaned up
239+ var finalTestCaseExecutionCount int64
240+ finalTestCaseExecutionCount , err = dataflowTester .Dal .Count (
241+ dal .From (& qa.QaTestCaseExecution {}),
242+ dal .Where ("qa_project_id = ?" , qaProjectId ),
243+ )
244+ if err != nil {
245+ t .Fatal (err )
246+ }
247+ if finalTestCaseExecutionCount != 0 {
248+ t .Errorf ("Expected test case executions to be cleaned up, but found %d records" , finalTestCaseExecutionCount )
249+ }
250+
251+ // Verify test case count is correct (should be 2)
252+ var finalTestCaseCount int64
253+ finalTestCaseCount , err = dataflowTester .Dal .Count (
254+ dal .From (& qa.QaTestCase {}),
255+ dal .Where ("qa_project_id = ?" , qaProjectId ),
256+ )
257+ if err != nil {
258+ t .Fatal (err )
259+ }
260+ if finalTestCaseCount != 2 {
261+ t .Errorf ("Expected 2 test cases after non-incremental import, got %d" , finalTestCaseCount )
262+ }
263+ }
0 commit comments