Skip to content

Commit b6870ca

Browse files
committed
feat(customize): clean up qa_apis and qa_test_case_executions when non-incremental import qa_test_cases
- 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 b6870ca

4 files changed

Lines changed: 179 additions & 3 deletions

File tree

backend/plugins/customize/e2e/import_qa_test_cases_test.go

Lines changed: 158 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,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+
}
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)