Skip to content

Commit 07107d3

Browse files
authored
feat(customize): clean up qa_apis and qa_test_case_executions when non-incremental import qa_test_cases (#8445)
- Implement new e2e test case for QA data cleanup functionality - Add test data files for QA test cases, APIs, and test case executions - Update ImportQaTestCases method to delete old data for non-incremental imports - Verify that API data, test cases, and test case executions are properly cleaned up
1 parent 15eba06 commit 07107d3

File tree

4 files changed

+167
-3
lines changed

4 files changed

+167
-3
lines changed

backend/plugins/customize/e2e/import_qa_test_cases_test.go

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
id,name,create_time,creator_name,type,qa_api_id
2+
tc-1,Full Test Case 1,2023-02-01T10:00:00.000+00:00,user-a,functional,
3+
tc-2,Full Test Case 2 API,2023-02-02T11:00:00.000+00:00,user-b,api,api-1
4+
api-1,Test API 1,2023-02-01T10:00:00.000+00:00,user-a,GET,/api/v1/test
5+
api-2,Test API 2,2023-02-02T11:00:00.000+00:00,user-b,POST,/api/v1/test
6+
exec-1,tc-1,2023-02-01T10:30:00.000+00:00,2023-02-01T10:35:00.000+00:00,passed,user-a
7+
exec-2,tc-2,2023-02-02T11:30:00.000+00:00,2023-02-02T11:40:00.000+00:00,failed,user-b
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
id,name,create_time,creator_name,type,qa_api_id
2+
tc-1,Functional Test Case 1,2023-02-01T10:00:00.000+00:00,user-a,functional,
3+
tc-2,Functional Test Case 2,2023-02-02T11:00:00.000+00:00,user-b,functional,

backend/plugins/customize/service/service.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,21 @@ func (s *Service) qaApiHandler(qaProjectId string) func(record map[string]interf
453453
func (s *Service) ImportQaTestCases(qaProjectId, qaProjectName string, file io.ReadCloser, incremental bool) errors.Error {
454454
if !incremental {
455455
// delete old data associated with this qaProjectId
456+
// delete qa_test_cases
456457
err := s.dal.Delete(&qa.QaTestCase{}, dal.Where("qa_project_id = ?", qaProjectId))
457458
if err != nil {
458459
return errors.Default.Wrap(err, fmt.Sprintf("failed to delete old qa_test_cases for qaProjectId %s", qaProjectId))
459460
}
460-
// using ImportQaApis to delete data in qa_apis
461+
// delete qa_apis
462+
err = s.dal.Delete(&qa.QaApi{}, dal.Where("qa_project_id = ?", qaProjectId))
463+
if err != nil {
464+
return errors.Default.Wrap(err, fmt.Sprintf("failed to delete old qa_apis for qaProjectId %s", qaProjectId))
465+
}
466+
// delete qa_test_case_executions
467+
err = s.dal.Delete(&qa.QaTestCaseExecution{}, dal.Where("qa_project_id = ?", qaProjectId))
468+
if err != nil {
469+
return errors.Default.Wrap(err, fmt.Sprintf("failed to delete old qa_test_case_executions for qaProjectId %s", qaProjectId))
470+
}
461471
// never delete data in qa_projects
462472
}
463473
// create or update qa_projects

0 commit comments

Comments
 (0)