@@ -12,15 +12,16 @@ import { CommentDto } from '../shared/dto/comment.dto';
1212import { BuildDto } from '../builds/dto/build.dto' ;
1313import { TestRunResultDto } from '../test-runs/dto/testRunResult.dto' ;
1414import { TestVariationsService } from '../test-variations/test-variations.service' ;
15+ import { convertBaselineDataToQuery } from '../shared/dto/baseline-data.dto' ;
1516
1617@Injectable ( )
1718export class TestRunsService {
1819 constructor (
1920 private testVariationService : TestVariationsService ,
2021 private prismaService : PrismaService ,
2122 private staticService : StaticService ,
22- private eventsGateway : EventsGateway ,
23- ) { }
23+ private eventsGateway : EventsGateway
24+ ) { }
2425
2526 async findMany ( buildId : string ) : Promise < TestRun [ ] > {
2627 return this . prismaService . testRun . findMany ( {
@@ -44,8 +45,24 @@ export class TestRunsService {
4445 }
4546
4647 async postTestRun ( createTestRequestDto : CreateTestRequestDto ) : Promise < TestRunResultDto > {
47- const testVariation = await this . testVariationService . findOrCreate ( createTestRequestDto ) ;
48+ const baselineData = convertBaselineDataToQuery ( createTestRequestDto ) ;
4849
50+ // creates variatioin if does not exist
51+ const testVariation = await this . testVariationService . findOrCreate ( createTestRequestDto . projectId , baselineData ) ;
52+
53+ // delete previous test run if exists
54+ let [ previousTestRun ] = await this . prismaService . testRun . findMany ( {
55+ where : {
56+ buildId : createTestRequestDto . buildId ,
57+ ...baselineData ,
58+ } ,
59+ } ) ;
60+
61+ if ( ! ! previousTestRun ) {
62+ await this . delete ( previousTestRun . id ) ;
63+ }
64+
65+ // create test run result
4966 const testRun = await this . create ( testVariation , createTestRequestDto ) ;
5067
5168 return new TestRunResultDto ( testRun , testVariation ) ;
@@ -54,14 +71,14 @@ export class TestRunsService {
5471 async emitUpdateBuildEvent ( buildId : string ) {
5572 const build = await this . prismaService . build . findOne ( {
5673 where : {
57- id : buildId
74+ id : buildId ,
5875 } ,
5976 include : {
60- testRuns : true
61- }
62- } )
63- const buildDto = new BuildDto ( build )
64- this . eventsGateway . buildUpdated ( buildDto )
77+ testRuns : true ,
78+ } ,
79+ } ) ;
80+ const buildDto = new BuildDto ( build ) ;
81+ this . eventsGateway . buildUpdated ( buildDto ) ;
6582 }
6683
6784 async approve ( id : string ) : Promise < TestRun > {
@@ -93,7 +110,7 @@ export class TestRunsService {
93110 } ,
94111 } ) ;
95112
96- this . emitUpdateBuildEvent ( testRun . buildId )
113+ this . emitUpdateBuildEvent ( testRun . buildId ) ;
97114 return testRunUpdated ;
98115 }
99116
@@ -105,7 +122,7 @@ export class TestRunsService {
105122 } ,
106123 } ) ;
107124
108- this . emitUpdateBuildEvent ( testRun . buildId )
125+ this . emitUpdateBuildEvent ( testRun . buildId ) ;
109126 return testRun ;
110127 }
111128
0 commit comments