diff --git a/src/app.module.ts b/src/app.module.ts index da84c918..1c5a00a6 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -5,7 +5,6 @@ import { BuildsModule } from './builds/builds.module'; import { ProjectsModule } from './projects/projects.module'; import { TestRunsModule } from './test-runs/test-runs.module'; import { TestVariationsModule } from './test-variations/test-variations.module'; -import { TestModule } from './test/test.module'; import { PrismaService } from './prisma/prisma.service'; import { ConfigModule } from '@nestjs/config'; @@ -18,7 +17,6 @@ import { ConfigModule } from '@nestjs/config'; ProjectsModule, TestRunsModule, TestVariationsModule, - TestModule, ], providers: [PrismaService], }) diff --git a/src/test/dto/create-test-request.dto.ts b/src/test-runs/dto/create-test-request.dto.ts similarity index 100% rename from src/test/dto/create-test-request.dto.ts rename to src/test-runs/dto/create-test-request.dto.ts diff --git a/src/test/dto/ignore-area.dto.ts b/src/test-runs/dto/ignore-area.dto.ts similarity index 100% rename from src/test/dto/ignore-area.dto.ts rename to src/test-runs/dto/ignore-area.dto.ts diff --git a/src/test/dto/testRunResult.dto.ts b/src/test-runs/dto/testRunResult.dto.ts similarity index 100% rename from src/test/dto/testRunResult.dto.ts rename to src/test-runs/dto/testRunResult.dto.ts diff --git a/src/test-runs/test-runs.controller.spec.ts b/src/test-runs/test-runs.controller.spec.ts index 99ef17a3..d8a2ef09 100644 --- a/src/test-runs/test-runs.controller.spec.ts +++ b/src/test-runs/test-runs.controller.spec.ts @@ -1,6 +1,8 @@ import { Test, TestingModule } from '@nestjs/testing'; import { TestRunsController } from './test-runs.controller'; import { TestRunsService } from './test-runs.service'; +import { TestVariationsService } from '../test-variations/test-variations.service'; +import { PrismaService } from '../prisma/prisma.service'; describe('TestRuns Controller', () => { let controller: TestRunsController; @@ -8,7 +10,11 @@ describe('TestRuns Controller', () => { beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [TestRunsController], - providers: [{ provide: TestRunsService, useValue: {} }] + providers: [ + { provide: TestRunsService, useValue: {} }, + { provide: TestVariationsService, useValue: {} }, + { provide: PrismaService, useValue: {} }, + ], }).compile(); controller = module.get(TestRunsController); diff --git a/src/test-runs/test-runs.controller.ts b/src/test-runs/test-runs.controller.ts index 02447bfa..e2e226ef 100644 --- a/src/test-runs/test-runs.controller.ts +++ b/src/test-runs/test-runs.controller.ts @@ -1,10 +1,13 @@ -import { Controller, Delete, UseGuards, Param, ParseUUIDPipe, Put, Body, Get, Query } from '@nestjs/common'; -import { ApiTags, ApiParam, ApiBearerAuth, ApiQuery } from '@nestjs/swagger'; +import { Controller, Delete, UseGuards, Param, ParseUUIDPipe, Put, Body, Get, Query, Post } from '@nestjs/common'; +import { ApiTags, ApiParam, ApiBearerAuth, ApiQuery, ApiSecurity, ApiOkResponse } from '@nestjs/swagger'; import { JwtAuthGuard } from '../auth/guards/auth.guard'; import { TestRun } from '@prisma/client'; import { TestRunsService } from './test-runs.service'; -import { IgnoreAreaDto } from '../test/dto/ignore-area.dto'; +import { IgnoreAreaDto } from './dto/ignore-area.dto'; import { CommentDto } from '../shared/dto/comment.dto'; +import { TestRunResultDto } from './dto/testRunResult.dto'; +import { ApiGuard } from '../auth/guards/api.guard'; +import { CreateTestRequestDto } from './dto/create-test-request.dto'; @ApiTags('test-runs') @Controller('test-runs') @@ -69,4 +72,12 @@ export class TestRunsController { updateComment(@Param('testRunId', new ParseUUIDPipe()) id: string, @Body() body: CommentDto): Promise { return this.testRunsService.updateComment(id, body); } + + @Post() + @ApiSecurity('api_key') + @ApiOkResponse({ type: TestRunResultDto }) + @UseGuards(ApiGuard) + postTestRun(@Body() createTestRequestDto: CreateTestRequestDto): Promise { + return this.testRunsService.postTestRun(createTestRequestDto); + } } diff --git a/src/test-runs/test-runs.module.ts b/src/test-runs/test-runs.module.ts index 1d3c5204..87349b1b 100644 --- a/src/test-runs/test-runs.module.ts +++ b/src/test-runs/test-runs.module.ts @@ -4,9 +4,10 @@ import { SharedModule } from '../shared/shared.module'; import { PrismaService } from '../prisma/prisma.service'; import { TestRunsController } from './test-runs.controller'; import { EventsGateway } from '../events/events.gateway'; +import { TestVariationsModule } from '../test-variations/test-variations.module'; @Module({ - imports: [SharedModule], + imports: [SharedModule, TestVariationsModule], providers: [TestRunsService, PrismaService, EventsGateway], exports: [TestRunsService], controllers: [TestRunsController] diff --git a/src/test-runs/test-runs.service.spec.ts b/src/test-runs/test-runs.service.spec.ts index 055f120e..76675774 100644 --- a/src/test-runs/test-runs.service.spec.ts +++ b/src/test-runs/test-runs.service.spec.ts @@ -6,12 +6,13 @@ import { StaticService } from '../shared/static/static.service'; import { PNG } from 'pngjs'; import { TestStatus, Build, TestRun } from '@prisma/client'; import Pixelmatch from 'pixelmatch'; -import { CreateTestRequestDto } from '../test/dto/create-test-request.dto'; +import { CreateTestRequestDto } from './dto/create-test-request.dto'; import { DiffResult } from './diffResult'; -import { IgnoreAreaDto } from '../test/dto/ignore-area.dto'; +import { IgnoreAreaDto } from './dto/ignore-area.dto'; import { EventsGateway } from '../events/events.gateway'; import { CommentDto } from '../shared/dto/comment.dto'; import { BuildDto } from '../builds/dto/build.dto'; +import { TestVariationsService } from '../test-variations/test-variations.service'; jest.mock('pixelmatch'); @@ -43,7 +44,7 @@ const initService = async ({ }, build: { findOne: buildFindOneMock, - } + }, }, }, { @@ -58,9 +59,13 @@ const initService = async ({ provide: EventsGateway, useValue: { newTestRun: eventNewTestRunMock, - buildUpdated: eventBuildUpdatedMock + buildUpdated: eventBuildUpdatedMock, }, }, + { + provide: TestVariationsService, + useValue: {}, + }, ], }).compile(); @@ -113,7 +118,7 @@ describe('TestRunsService', () => { status: TestStatus.failed, }, }); - expect(service.emitUpdateBuildEvent).toBeCalledWith(testRun.buildId) + expect(service.emitUpdateBuildEvent).toBeCalledWith(testRun.buildId); }); describe('approve', () => { @@ -174,7 +179,7 @@ describe('TestRunsService', () => { }, }, }); - expect(service.emitUpdateBuildEvent).toBeCalledWith(testRun.buildId) + expect(service.emitUpdateBuildEvent).toBeCalledWith(testRun.buildId); }); }); @@ -615,23 +620,23 @@ describe('TestRunsService', () => { }, ], }; - const buildFindOneMock = jest.fn().mockResolvedValueOnce(build) - const eventBuildUpdatedMock = jest.fn() + const buildFindOneMock = jest.fn().mockResolvedValueOnce(build); + const eventBuildUpdatedMock = jest.fn(); service = await initService({ buildFindOneMock, eventBuildUpdatedMock, }); - await service.emitUpdateBuildEvent(build.id) + await service.emitUpdateBuildEvent(build.id); expect(buildFindOneMock).toHaveBeenCalledWith({ where: { - id: build.id + id: build.id, }, include: { - testRuns: true - } - }) - expect(eventBuildUpdatedMock).toHaveBeenCalledWith(new BuildDto(build)) - }) + testRuns: true, + }, + }); + expect(eventBuildUpdatedMock).toHaveBeenCalledWith(new BuildDto(build)); + }); }); diff --git a/src/test-runs/test-runs.service.ts b/src/test-runs/test-runs.service.ts index 695320c3..d7991fca 100644 --- a/src/test-runs/test-runs.service.ts +++ b/src/test-runs/test-runs.service.ts @@ -1,8 +1,8 @@ import { Injectable } from '@nestjs/common'; import { PNG } from 'pngjs'; import Pixelmatch from 'pixelmatch'; -import { CreateTestRequestDto } from '../test/dto/create-test-request.dto'; -import { IgnoreAreaDto } from '../test/dto/ignore-area.dto'; +import { CreateTestRequestDto } from './dto/create-test-request.dto'; +import { IgnoreAreaDto } from './dto/ignore-area.dto'; import { StaticService } from '../shared/static/static.service'; import { PrismaService } from '../prisma/prisma.service'; import { TestRun, TestStatus, TestVariation } from '@prisma/client'; @@ -10,10 +10,13 @@ import { DiffResult } from './diffResult'; import { EventsGateway } from '../events/events.gateway'; import { CommentDto } from '../shared/dto/comment.dto'; import { BuildDto } from '../builds/dto/build.dto'; +import { TestRunResultDto } from '../test-runs/dto/testRunResult.dto'; +import { TestVariationsService } from '../test-variations/test-variations.service'; @Injectable() export class TestRunsService { constructor( + private testVariationService: TestVariationsService, private prismaService: PrismaService, private staticService: StaticService, private eventsGateway: EventsGateway, @@ -40,6 +43,14 @@ export class TestRunsService { }); } + async postTestRun(createTestRequestDto: CreateTestRequestDto): Promise { + const testVariation = await this.testVariationService.findOrCreate(createTestRequestDto); + + const testRun = await this.create(testVariation, createTestRequestDto); + + return new TestRunResultDto(testRun, testVariation); + } + async emitUpdateBuildEvent(buildId: string) { const build = await this.prismaService.build.findOne({ where: { diff --git a/src/test-variations/test-variations.controller.ts b/src/test-variations/test-variations.controller.ts index 9b2c9678..450b6e85 100644 --- a/src/test-variations/test-variations.controller.ts +++ b/src/test-variations/test-variations.controller.ts @@ -4,7 +4,7 @@ import { TestVariationsService } from './test-variations.service'; import { TestVariation, Baseline } from '@prisma/client'; import { JwtAuthGuard } from '../auth/guards/auth.guard'; import { PrismaService } from '../prisma/prisma.service'; -import { IgnoreAreaDto } from '../test/dto/ignore-area.dto'; +import { IgnoreAreaDto } from '../test-runs/dto/ignore-area.dto'; import { CommentDto } from '../shared/dto/comment.dto'; @ApiTags('test-variations') diff --git a/src/test-variations/test-variations.service.spec.ts b/src/test-variations/test-variations.service.spec.ts index fb059f6b..e16fd16b 100644 --- a/src/test-variations/test-variations.service.spec.ts +++ b/src/test-variations/test-variations.service.spec.ts @@ -1,9 +1,9 @@ import { Test, TestingModule } from '@nestjs/testing'; import { TestVariationsService } from './test-variations.service'; import { PrismaService } from '../prisma/prisma.service'; -import { CreateTestRequestDto } from '../test/dto/create-test-request.dto'; +import { CreateTestRequestDto } from '../test-runs/dto/create-test-request.dto'; import { StaticService } from '../shared/static/static.service'; -import { IgnoreAreaDto } from 'src/test/dto/ignore-area.dto'; +import { IgnoreAreaDto } from '../test-runs/dto/ignore-area.dto'; import { TestVariation, Baseline } from '@prisma/client'; import { CommentDto } from '../shared/dto/comment.dto'; diff --git a/src/test-variations/test-variations.service.ts b/src/test-variations/test-variations.service.ts index 24a71842..3900c78f 100644 --- a/src/test-variations/test-variations.service.ts +++ b/src/test-variations/test-variations.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; -import { CreateTestRequestDto } from '../test/dto/create-test-request.dto'; -import { IgnoreAreaDto } from '../test/dto/ignore-area.dto'; +import { CreateTestRequestDto } from '../test-runs/dto/create-test-request.dto'; +import { IgnoreAreaDto } from '../test-runs/dto/ignore-area.dto'; import { PrismaService } from '../prisma/prisma.service'; import { TestVariation, Baseline } from '@prisma/client'; import { StaticService } from '../shared/static/static.service'; diff --git a/src/test/test.controller.spec.ts b/src/test/test.controller.spec.ts deleted file mode 100644 index ef9dafa7..00000000 --- a/src/test/test.controller.spec.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { TestController } from './test.controller'; -import { TestService } from './test.service'; -import { TestVariationsService } from '../test-variations/test-variations.service'; -import { TestRunsService } from '../test-runs/test-runs.service'; -import { PrismaService } from '../prisma/prisma.service'; - -describe('Test Controller', () => { - let controller: TestController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [TestController], - providers: [ - { provide: PrismaService, useValue: {} }, - { provide: TestService, useValue: {} }, - { provide: TestVariationsService, useValue: {} }, - { provide: TestRunsService, useValue: {} }, - ], - }).compile(); - - controller = module.get(TestController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/test/test.controller.ts b/src/test/test.controller.ts deleted file mode 100644 index 1ffcf3bc..00000000 --- a/src/test/test.controller.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { - Controller, - UseGuards, - Post, - Body, -} from '@nestjs/common'; -import { ApiTags, ApiOkResponse, ApiSecurity } from '@nestjs/swagger'; -import { TestService } from './test.service'; -import { CreateTestRequestDto } from './dto/create-test-request.dto'; -import { ApiGuard } from '../auth/guards/api.guard'; -import { TestRunResultDto } from './dto/testRunResult.dto'; - -@ApiTags('test') -@Controller('test') -export class TestController { - constructor(private testService: TestService) { } - - @Post() - @ApiSecurity('api_key') - @ApiOkResponse({ type: TestRunResultDto }) - @UseGuards(ApiGuard) - postTestRun(@Body() createTestRequestDto: CreateTestRequestDto): Promise { - return this.testService.postTestRun(createTestRequestDto); - } -} diff --git a/src/test/test.module.ts b/src/test/test.module.ts deleted file mode 100644 index b80767ba..00000000 --- a/src/test/test.module.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Module } from '@nestjs/common'; -import { TestService } from './test.service'; -import { TestController } from './test.controller'; -import { TestRunsModule } from '../test-runs/test-runs.module'; -import { TestVariationsModule } from '../test-variations/test-variations.module'; -import { UsersModule } from '../users/users.module'; -import { PrismaService } from '../prisma/prisma.service'; - -@Module({ - imports: [TestRunsModule, TestVariationsModule, UsersModule], - providers: [TestService, PrismaService], - controllers: [TestController], - exports: [TestService] -}) -export class TestModule {} diff --git a/src/test/test.service.spec.ts b/src/test/test.service.spec.ts deleted file mode 100644 index 05b7f0f9..00000000 --- a/src/test/test.service.spec.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { TestService } from './test.service'; -import { TestRunsService } from '../test-runs/test-runs.service'; -import { TestVariationsService } from '../test-variations/test-variations.service'; - -describe('TestService', () => { - let service: TestService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [TestService, { provide: TestRunsService, useValue: {} }, { provide: TestVariationsService, useValue: {} },], - }).compile(); - - service = module.get(TestService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/src/test/test.service.ts b/src/test/test.service.ts deleted file mode 100644 index 1568172c..00000000 --- a/src/test/test.service.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { TestVariationsService } from '../test-variations/test-variations.service'; -import { TestRunsService } from '../test-runs/test-runs.service'; -import { CreateTestRequestDto } from './dto/create-test-request.dto'; -import { TestRunResultDto } from './dto/testRunResult.dto'; - -@Injectable() -export class TestService { - constructor( - private testVariationService: TestVariationsService, - private testRunsService: TestRunsService, - ) { } - - async postTestRun(createTestRequestDto: CreateTestRequestDto): Promise { - const testVariation = await this.testVariationService.findOrCreate(createTestRequestDto); - - const testRun = await this.testRunsService.create(testVariation, createTestRequestDto); - - return new TestRunResultDto(testRun, testVariation); - } -}