From 6f2d50d5b69610456d583b3fd14491a99f394fac Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Thu, 4 Jun 2020 09:37:07 +0200 Subject: [PATCH 1/4] recalculateDiff added --- src/test-runs/diffResult.ts | 2 +- src/test-runs/test-runs.service.spec.ts | 295 ++++++++++++++++++------ src/test-runs/test-runs.service.ts | 117 ++++++---- 3 files changed, 302 insertions(+), 112 deletions(-) diff --git a/src/test-runs/diffResult.ts b/src/test-runs/diffResult.ts index 09041c34..66b7462c 100644 --- a/src/test-runs/diffResult.ts +++ b/src/test-runs/diffResult.ts @@ -2,7 +2,7 @@ import { TestStatus } from '@prisma/client'; export interface DiffResult { status: TestStatus; - imageName: string; + diffName: string; pixelMisMatchCount: number; diffPercent: number; isSameDimension: boolean; diff --git a/src/test-runs/test-runs.service.spec.ts b/src/test-runs/test-runs.service.spec.ts index 94d480b3..dfe9474f 100644 --- a/src/test-runs/test-runs.service.spec.ts +++ b/src/test-runs/test-runs.service.spec.ts @@ -17,6 +17,7 @@ const initService = async ({ testRunCreateMock = jest.fn(), getImageMock = jest.fn(), saveImageMock = jest.fn(), + deleteImageMock = jest.fn(), }) => { const module: TestingModule = await Test.createTestingModule({ providers: [ @@ -36,6 +37,7 @@ const initService = async ({ useValue: { getImage: getImageMock, saveImage: saveImageMock, + deleteImage: deleteImageMock, }, }, ], @@ -150,65 +152,24 @@ describe('TestRunsService', () => { browser: 'browser', viewport: 'viewport', device: 'device', + diffTollerancePercent: 1, }; it('no baseline', async () => { - const testVariation = { - id: '123', - projectId: 'project Id', - name: 'Test name', + const testRun = { + id: 'id', + imageName: 'imageName', baselineName: null, - os: 'OS', - browser: 'browser', - viewport: 'viewport', - device: 'device', + diffTollerancePercent: 1, ignoreAreas: '[]', - createdAt: new Date(), - updatedAt: new Date(), + diffName: null, }; - const createTestRequestDto = initCreateTestRequestDto; - const testRunCreateMock = jest.fn(); - const imageName = 'image name'; - const saveImageMock = jest.fn().mockReturnValueOnce(imageName); - service = await initService({ testRunCreateMock, saveImageMock }); - await service.create(testVariation, createTestRequestDto); - - expect(testRunCreateMock).toHaveBeenCalledWith({ - data: { - imageName, - testVariation: { - connect: { - id: testVariation.id, - }, - }, - build: { - connect: { - id: createTestRequestDto.buildId, - }, - }, - name: testVariation.name, - browser: testVariation.browser, - device: testVariation.device, - os: testVariation.os, - viewport: testVariation.viewport, - baselineName: testVariation.baselineName, - ignoreAreas: testVariation.ignoreAreas, - diffTollerancePercent: createTestRequestDto.diffTollerancePercent, - diffName: undefined, - pixelMisMatchCount: undefined, - diffPercent: undefined, - status: TestStatus.new, - }, - }); - }); - - it('with deleted baseline', async () => { const testVariation = { id: '123', projectId: 'project Id', name: 'Test name', - baselineName: 'baselineName', + baselineName: null, os: 'OS', browser: 'browser', viewport: 'viewport', @@ -217,16 +178,36 @@ describe('TestRunsService', () => { createdAt: new Date(), updatedAt: new Date(), }; + const diffResult: DiffResult = { + status: null, + diffName: null, + pixelMisMatchCount: null, + diffPercent: null, + isSameDimension: false, + }; const createTestRequestDto = initCreateTestRequestDto; - const testRunCreateMock = jest.fn(); + const testRunCreateMock = jest.fn().mockResolvedValueOnce(testRun); const imageName = 'image name'; const saveImageMock = jest.fn().mockReturnValueOnce(imageName); - const getImageMock = jest.fn().mockReturnValueOnce(null); + const image = 'image'; + const baseline = null; + const getImageMock = jest + .fn() + .mockReturnValueOnce(baseline) + .mockReturnValueOnce(image); service = await initService({ testRunCreateMock, saveImageMock, getImageMock }); + const getDiffMock = jest.fn().mockReturnValueOnce(diffResult); + service.getDiff = getDiffMock; + const saveDiffResultMock = jest.fn(); + service.saveDiffResult = saveDiffResultMock; await service.create(testVariation, createTestRequestDto); - expect(getImageMock).toHaveBeenCalledWith(testVariation.baselineName); + expect(saveImageMock).toHaveBeenCalledWith('screenshot', Buffer.from(createTestRequestDto.imageBase64, 'base64')); + expect(getImageMock).toHaveBeenNthCalledWith(1, testVariation.baselineName); + expect(getImageMock).toHaveBeenNthCalledWith(2, imageName); + expect(getDiffMock).toHaveBeenCalledWith(baseline, image, testRun.diffTollerancePercent, testRun.ignoreAreas); + expect(saveDiffResultMock).toHaveBeenCalledWith(testRun.id, diffResult); expect(testRunCreateMock).toHaveBeenCalledWith({ data: { imageName, @@ -256,7 +237,76 @@ describe('TestRunsService', () => { }); }); + // it('with deleted baseline', async () => { + // const testRun = { + // id: 'id', + // imageName: 'imageName', + // baselineName: 'baselineName', + // diffTollerancePercent: 12, + // ignoreAreas: '[]', + // diffName: null, + // }; + // const testVariation = { + // id: '123', + // projectId: 'project Id', + // name: 'Test name', + // baselineName: 'baselineName', + // os: 'OS', + // browser: 'browser', + // viewport: 'viewport', + // device: 'device', + // ignoreAreas: '[]', + // createdAt: new Date(), + // updatedAt: new Date(), + // }; + // const createTestRequestDto = initCreateTestRequestDto; + // const testRunCreateMock = jest.fn().mockResolvedValueOnce(testRun); + // const imageName = 'image name'; + // const saveImageMock = jest.fn().mockReturnValueOnce(imageName); + // const getImageMock = jest.fn().mockReturnValueOnce(null); + // service = await initService({ testRunCreateMock, saveImageMock, getImageMock }); + + // await service.create(testVariation, createTestRequestDto); + + // expect(getImageMock).toHaveBeenCalledWith(testVariation.baselineName); + // expect(testRunCreateMock).toHaveBeenCalledWith({ + // data: { + // imageName, + // testVariation: { + // connect: { + // id: testVariation.id, + // }, + // }, + // build: { + // connect: { + // id: createTestRequestDto.buildId, + // }, + // }, + // name: testVariation.name, + // browser: testVariation.browser, + // device: testVariation.device, + // os: testVariation.os, + // viewport: testVariation.viewport, + // baselineName: testVariation.baselineName, + // ignoreAreas: testVariation.ignoreAreas, + // diffTollerancePercent: createTestRequestDto.diffTollerancePercent, + // diffName: undefined, + // pixelMisMatchCount: undefined, + // diffPercent: undefined, + // status: TestStatus.new, + // }, + // }); + // }); + it('with baseline', async () => { + const testRun = { + id: 'id', + imageName: 'imageName', + baselineName: 'baselineName', + diffTollerancePercent: 1, + ignoreAreas: '[]', + diffName: null, + }; const testVariation = { id: '123', projectId: 'project Id', @@ -271,7 +321,7 @@ describe('TestRunsService', () => { updatedAt: new Date(), }; const createTestRequestDto = initCreateTestRequestDto; - const testRunCreateMock = jest.fn(); + const testRunCreateMock = jest.fn().mockResolvedValueOnce(testRun); const imageName = 'image name'; const saveImageMock = jest.fn().mockReturnValueOnce(imageName); const image = 'image'; @@ -281,17 +331,21 @@ describe('TestRunsService', () => { .mockReturnValueOnce(baseline) .mockReturnValueOnce(image); service = await initService({ testRunCreateMock, saveImageMock, getImageMock }); - const diff: DiffResult = { + const diffResult: DiffResult = { status: TestStatus.unresolved, - imageName: 'diff image name', + diffName: 'diff image name', pixelMisMatchCount: 11, diffPercent: 22, isSameDimension: true, }; - service.getDiff = jest.fn().mockReturnValueOnce(diff); + const getDiffMock = jest.fn().mockReturnValueOnce(diffResult); + service.getDiff = getDiffMock; + const saveDiffResultMock = jest.fn(); + service.saveDiffResult = saveDiffResultMock; await service.create(testVariation, createTestRequestDto); + expect(saveImageMock).toHaveBeenCalledWith('screenshot', Buffer.from(createTestRequestDto.imageBase64, 'base64')); expect(service.getDiff).toHaveBeenCalledWith( baseline, image, @@ -321,16 +375,34 @@ describe('TestRunsService', () => { baselineName: testVariation.baselineName, ignoreAreas: testVariation.ignoreAreas, diffTollerancePercent: createTestRequestDto.diffTollerancePercent, - diffName: diff.imageName, - pixelMisMatchCount: diff.pixelMisMatchCount, - diffPercent: diff.diffPercent, - status: diff.status, + status: TestStatus.new, }, }); + expect(getDiffMock).toHaveBeenCalledWith(baseline, image, testRun.diffTollerancePercent, testRun.ignoreAreas); + expect(saveDiffResultMock).toHaveBeenCalledWith(testRun.id, diffResult); }); }); describe('getDiff', () => { + it('no baseline', async () => { + const baseline = null; + const image = new PNG({ + width: 20, + height: 20, + }); + service = await initService({}); + + const result = service.getDiff(baseline, image, 0, '[]'); + + expect(result).toStrictEqual({ + status: null, + diffName: null, + pixelMisMatchCount: null, + diffPercent: null, + isSameDimension: false, + }); + }); + it('diff image dimensions mismatch', async () => { const baseline = new PNG({ width: 10, @@ -346,7 +418,7 @@ describe('TestRunsService', () => { expect(result).toStrictEqual({ status: TestStatus.unresolved, - imageName: null, + diffName: null, pixelMisMatchCount: null, diffPercent: null, isSameDimension: false, @@ -369,7 +441,7 @@ describe('TestRunsService', () => { expect(result).toStrictEqual({ status: TestStatus.ok, - imageName: null, + diffName: null, pixelMisMatchCount: 0, diffPercent: 0, isSameDimension: true, @@ -395,7 +467,7 @@ describe('TestRunsService', () => { expect(saveImageMock).toHaveBeenCalledTimes(0); expect(result).toStrictEqual({ status: TestStatus.ok, - imageName: null, + diffName: null, pixelMisMatchCount, diffPercent: 1.5, isSameDimension: true, @@ -413,8 +485,8 @@ describe('TestRunsService', () => { }); const pixelMisMatchCount = 200; mocked(Pixelmatch).mockReturnValueOnce(pixelMisMatchCount); - const imageName = 'diff name'; - const saveImageMock = jest.fn().mockReturnValueOnce(imageName); + const diffName = 'diff name'; + const saveImageMock = jest.fn().mockReturnValueOnce(diffName); service = await initService({ saveImageMock, }); @@ -424,11 +496,104 @@ describe('TestRunsService', () => { expect(saveImageMock).toHaveBeenCalledTimes(1); expect(result).toStrictEqual({ status: TestStatus.unresolved, - imageName, + diffName, pixelMisMatchCount, diffPercent: 2, isSameDimension: true, }); }); }); + + it('calculate no diff', async () => { + const testRun = { + id: 'id', + imageName: 'imageName', + baselineName: 'baselineName', + diffTollerancePercent: 12, + ignoreAreas: '[]', + diffName: null, + }; + const testRunFindOneMock = jest.fn().mockResolvedValueOnce(testRun); + const testRunUpdateMock = jest.fn(); + const baselineMock = 'baseline image'; + const imageeMock = 'image'; + const getImageMock = jest + .fn() + .mockReturnValueOnce(baselineMock) + .mockReturnValueOnce(imageeMock); + const deleteImageMock = jest.fn(); + const diffResult = { + id: 'test', + }; + const getDiffMock = jest.fn().mockReturnValue(diffResult); + service = await initService({ + testRunUpdateMock, + getImageMock, + deleteImageMock, + }); + service.findOne = testRunFindOneMock; + service.getDiff = getDiffMock; + + await service.recalculateDiff(testRun.id); + + expect(testRunFindOneMock).toHaveBeenCalledWith(testRun.id); + expect(getImageMock).toHaveBeenNthCalledWith(1, testRun.baselineName); + expect(getImageMock).toHaveBeenNthCalledWith(2, testRun.imageName); + expect(deleteImageMock).toHaveBeenCalledWith(testRun.diffName); + expect(getDiffMock).toHaveBeenCalledWith( + baselineMock, + imageeMock, + testRun.diffTollerancePercent, + testRun.ignoreAreas + ); + }); + + describe('saveDiffResult', () => { + it('no results', async () => { + const id = 'some id'; + const testRunUpdateMock = jest.fn(); + service = await initService({ + testRunUpdateMock, + }); + + await service.saveDiffResult(id, null); + + expect(testRunUpdateMock).toHaveBeenCalledWith({ + where: { id }, + data: { + status: TestStatus.new, + diffName: null, + pixelMisMatchCount: null, + diffPercent: null, + }, + }); + }); + }); + + it('with results', async () => { + const diff: DiffResult = { + status: TestStatus.unresolved, + diffName: 'diff image name', + pixelMisMatchCount: 11, + diffPercent: 22, + isSameDimension: true, + }; + const id = 'some id'; + const testRunUpdateMock = jest.fn(); + service = await initService({ + testRunUpdateMock, + }); + + await service.saveDiffResult(id, diff); + + expect(testRunUpdateMock).toHaveBeenCalledWith({ + where: { id }, + data: { + status: diff.status, + diffName: diff.diffName, + pixelMisMatchCount: diff.pixelMisMatchCount, + diffPercent: diff.diffPercent, + }, + }); + }); }); diff --git a/src/test-runs/test-runs.service.ts b/src/test-runs/test-runs.service.ts index c251e5e2..4593bf21 100644 --- a/src/test-runs/test-runs.service.ts +++ b/src/test-runs/test-runs.service.ts @@ -72,24 +72,35 @@ export class TestRunsService { }); } + async saveDiffResult(id: string, diffResult: DiffResult): Promise { + return this.prismaService.testRun.update({ + where: { id }, + data: { + diffName: diffResult && diffResult.diffName, + pixelMisMatchCount: diffResult && diffResult.pixelMisMatchCount, + diffPercent: diffResult && diffResult.diffPercent, + status: diffResult ? diffResult.status : TestStatus.new, + }, + }); + } + + async recalculateDiff(id: string): Promise { + const testRun = await this.findOne(id); + + const baseline = this.staticService.getImage(testRun.baselineName); + const image = this.staticService.getImage(testRun.imageName); + this.staticService.deleteImage(testRun.diffName); + + const diffResult = this.getDiff(baseline, image, testRun.diffTollerancePercent, testRun.ignoreAreas); + return this.saveDiffResult(id, diffResult); + } + async create(testVariation: TestVariation, createTestRequestDto: CreateTestRequestDto): Promise { // save image const imageBuffer = Buffer.from(createTestRequestDto.imageBase64, 'base64'); const imageName = this.staticService.saveImage('screenshot', imageBuffer); - let diff: DiffResult; - - if (testVariation.baselineName) { - let baseline: PNGWithMetadata; - baseline = this.staticService.getImage(testVariation.baselineName); - - if (baseline) { - const image = this.staticService.getImage(imageName); - diff = this.getDiff(baseline, image, createTestRequestDto.diffTollerancePercent, testVariation.ignoreAreas); - } - } - - return this.prismaService.testRun.create({ + const testRun = await this.prismaService.testRun.create({ data: { imageName, testVariation: { @@ -110,12 +121,21 @@ export class TestRunsService { baselineName: testVariation.baselineName, ignoreAreas: testVariation.ignoreAreas, diffTollerancePercent: createTestRequestDto.diffTollerancePercent, - diffName: diff && diff.imageName, - pixelMisMatchCount: diff && diff.pixelMisMatchCount, - diffPercent: diff && diff.diffPercent, - status: diff ? diff.status : TestStatus.new, + status: TestStatus.new, }, }); + + const baseline = this.staticService.getImage(testRun.baselineName); + const image = this.staticService.getImage(imageName); + + const diffResult = this.getDiff( + baseline, + image, + createTestRequestDto.diffTollerancePercent, + testVariation.ignoreAreas + ); + + return this.saveDiffResult(testRun.id, diffResult); } async delete(id: string): Promise { @@ -147,43 +167,48 @@ export class TestRunsService { getDiff(baseline: PNG, image: PNG, diffTollerancePercent: number, ignoreAreas: string): DiffResult { const result: DiffResult = { status: null, - imageName: null, + diffName: null, pixelMisMatchCount: null, diffPercent: null, - isSameDimension: baseline.width === image.width && baseline.height === image.height, + isSameDimension: false, }; - if (result.isSameDimension) { - const diff = new PNG({ - width: baseline.width, - height: baseline.height, - }); - - // compare - result.pixelMisMatchCount = Pixelmatch( - this.applyIgnoreAreas(baseline, JSON.parse(ignoreAreas)), - this.applyIgnoreAreas(image, JSON.parse(ignoreAreas)), - diff.data, - baseline.width, - baseline.height, - { - threshold: diffTollerancePercent / 100, - includeAA: true, + if (baseline) { + result.isSameDimension = baseline.width === image.width && baseline.height === image.height; + + if (result.isSameDimension) { + const diff = new PNG({ + width: baseline.width, + height: baseline.height, + }); + + // compare + result.pixelMisMatchCount = Pixelmatch( + this.applyIgnoreAreas(baseline, JSON.parse(ignoreAreas)), + this.applyIgnoreAreas(image, JSON.parse(ignoreAreas)), + diff.data, + baseline.width, + baseline.height, + { + threshold: diffTollerancePercent / 100, + includeAA: true, + } + ); + result.diffPercent = (result.pixelMisMatchCount * 100) / (image.width * image.height); + + if (result.diffPercent > diffTollerancePercent) { + // save diff + result.diffName = this.staticService.saveImage('diff', PNG.sync.write(diff)); + result.status = TestStatus.unresolved; + } else { + result.status = TestStatus.ok; } - ); - result.diffPercent = (result.pixelMisMatchCount * 100) / (image.width * image.height); - - if (result.diffPercent > diffTollerancePercent) { - // save diff - result.imageName = this.staticService.saveImage('diff', PNG.sync.write(diff)); - result.status = TestStatus.unresolved; } else { - result.status = TestStatus.ok; + // diff dimensions + result.status = TestStatus.unresolved; } - } else { - // diff dimensions - result.status = TestStatus.unresolved; } + return result; } From e6fe824a991e4a55fb3c8fc24afb300dfa25ada7 Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Thu, 4 Jun 2020 20:17:01 +0200 Subject: [PATCH 2/4] recalculateDiff added --- src/shared/static/static.service.ts | 22 +++++++++++----------- src/test-runs/test-runs.controller.ts | 8 ++++++++ src/test-runs/test-runs.service.ts | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/shared/static/static.service.ts b/src/shared/static/static.service.ts index 35ed4221..a73757b8 100644 --- a/src/shared/static/static.service.ts +++ b/src/shared/static/static.service.ts @@ -6,26 +6,26 @@ import { ConfigService } from '@nestjs/config'; @Injectable() export class StaticService { - constructor(private configService: ConfigService) { - } + constructor(private configService: ConfigService) {} saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): string { - const imageName = `${Date.now()}.${type}.png` + const imageName = `${Date.now()}.${type}.png`; writeFileSync(this.getImagePath(imageName), imageBuffer); - return imageName + return imageName; } getImage(imageName: string): PNGWithMetadata { - let image: PNGWithMetadata + let image: PNGWithMetadata; try { - image = PNG.sync.read(readFileSync(this.getImagePath(imageName))) + image = PNG.sync.read(readFileSync(this.getImagePath(imageName))); } catch (ex) { - console.log(`Cannot image: ${imageName}. ${ex}`) + console.log(`Cannot image: ${imageName}. ${ex}`); } return image; } async deleteImage(imageName: string): Promise { + if (!imageName) return; return new Promise((resolvePromise, reject) => { unlink(this.getImagePath(imageName), err => { if (err) { @@ -37,18 +37,18 @@ export class StaticService { } private getImagePath(imageName: string): string { - const dir = this.configService.get('IMG_UPLOAD_FOLDER') - this.ensureDirectoryExistence(dir) + const dir = this.configService.get('IMG_UPLOAD_FOLDER'); + this.ensureDirectoryExistence(dir); return path.resolve(dir, imageName); } private ensureDirectoryExistence(dir: string) { - const filePath = path.resolve(dir) + const filePath = path.resolve(dir); if (existsSync(filePath)) { return true; } else { mkdirSync(dir, { recursive: true }); - this.ensureDirectoryExistence(dir) + this.ensureDirectoryExistence(dir); } } } diff --git a/src/test-runs/test-runs.controller.ts b/src/test-runs/test-runs.controller.ts index 29e29903..2fb3f539 100644 --- a/src/test-runs/test-runs.controller.ts +++ b/src/test-runs/test-runs.controller.ts @@ -18,6 +18,14 @@ export class TestRunsController { return this.testRunsService.findMany(buildId); } + @Get('recalculateDiff/:id') + @ApiParam({ name: 'id', required: true }) + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + recalculateDiff(@Param('id', new ParseUUIDPipe()) id: string): Promise { + return this.testRunsService.recalculateDiff(id); + } + @Get('approve/:id') @ApiParam({ name: 'id', required: true }) @ApiBearerAuth() diff --git a/src/test-runs/test-runs.service.ts b/src/test-runs/test-runs.service.ts index 4593bf21..d5dd7e49 100644 --- a/src/test-runs/test-runs.service.ts +++ b/src/test-runs/test-runs.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@nestjs/common'; -import { PNG, PNGWithMetadata } from 'pngjs'; +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'; From c91bb6fce267125cbb023d971874f6e3dc23f90f Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Thu, 4 Jun 2020 20:47:36 +0200 Subject: [PATCH 3/4] tests are added --- src/shared/static/static.service.ts | 1 + src/test-runs/test-runs.service.spec.ts | 143 ++++++++++++------------ src/test-runs/test-runs.service.ts | 22 ++-- 3 files changed, 82 insertions(+), 84 deletions(-) diff --git a/src/shared/static/static.service.ts b/src/shared/static/static.service.ts index a73757b8..a4eafda0 100644 --- a/src/shared/static/static.service.ts +++ b/src/shared/static/static.service.ts @@ -15,6 +15,7 @@ export class StaticService { } getImage(imageName: string): PNGWithMetadata { + if (!imageName) return; let image: PNGWithMetadata; try { image = PNG.sync.read(readFileSync(this.getImagePath(imageName))); diff --git a/src/test-runs/test-runs.service.spec.ts b/src/test-runs/test-runs.service.spec.ts index dfe9474f..ac81e360 100644 --- a/src/test-runs/test-runs.service.spec.ts +++ b/src/test-runs/test-runs.service.spec.ts @@ -8,12 +8,15 @@ import { TestStatus, TestRun } from '@prisma/client'; import Pixelmatch from 'pixelmatch'; import { CreateTestRequestDto } from 'src/test/dto/create-test-request.dto'; import { DiffResult } from './diffResult'; +import { IgnoreAreaDto } from 'src/test/dto/ignore-area.dto'; jest.mock('pixelmatch'); const initService = async ({ + testRunDeleteMock = jest.fn(), testRunUpdateMock = jest.fn(), testRunFindOneMock = jest.fn(), + testRunFindManyMock = jest.fn(), testRunCreateMock = jest.fn(), getImageMock = jest.fn(), saveImageMock = jest.fn(), @@ -26,6 +29,8 @@ const initService = async ({ provide: PrismaService, useValue: { testRun: { + delete: testRunDeleteMock, + findMany: testRunFindManyMock, findOne: testRunFindOneMock, create: testRunCreateMock, update: testRunUpdateMock, @@ -237,67 +242,6 @@ describe('TestRunsService', () => { }); }); - // it('with deleted baseline', async () => { - // const testRun = { - // id: 'id', - // imageName: 'imageName', - // baselineName: 'baselineName', - // diffTollerancePercent: 12, - // ignoreAreas: '[]', - // diffName: null, - // }; - // const testVariation = { - // id: '123', - // projectId: 'project Id', - // name: 'Test name', - // baselineName: 'baselineName', - // os: 'OS', - // browser: 'browser', - // viewport: 'viewport', - // device: 'device', - // ignoreAreas: '[]', - // createdAt: new Date(), - // updatedAt: new Date(), - // }; - // const createTestRequestDto = initCreateTestRequestDto; - // const testRunCreateMock = jest.fn().mockResolvedValueOnce(testRun); - // const imageName = 'image name'; - // const saveImageMock = jest.fn().mockReturnValueOnce(imageName); - // const getImageMock = jest.fn().mockReturnValueOnce(null); - // service = await initService({ testRunCreateMock, saveImageMock, getImageMock }); - - // await service.create(testVariation, createTestRequestDto); - - // expect(getImageMock).toHaveBeenCalledWith(testVariation.baselineName); - // expect(testRunCreateMock).toHaveBeenCalledWith({ - // data: { - // imageName, - // testVariation: { - // connect: { - // id: testVariation.id, - // }, - // }, - // build: { - // connect: { - // id: createTestRequestDto.buildId, - // }, - // }, - // name: testVariation.name, - // browser: testVariation.browser, - // device: testVariation.device, - // os: testVariation.os, - // viewport: testVariation.viewport, - // baselineName: testVariation.baselineName, - // ignoreAreas: testVariation.ignoreAreas, - // diffTollerancePercent: createTestRequestDto.diffTollerancePercent, - // diffName: undefined, - // pixelMisMatchCount: undefined, - // diffPercent: undefined, - // status: TestStatus.new, - // }, - // }); - // }); - it('with baseline', async () => { const testRun = { id: 'id', @@ -395,11 +339,11 @@ describe('TestRunsService', () => { const result = service.getDiff(baseline, image, 0, '[]'); expect(result).toStrictEqual({ - status: null, - diffName: null, - pixelMisMatchCount: null, - diffPercent: null, - isSameDimension: false, + status: undefined, + diffName: undefined, + pixelMisMatchCount: undefined, + diffPercent: undefined, + isSameDimension: undefined, }); }); @@ -418,9 +362,9 @@ describe('TestRunsService', () => { expect(result).toStrictEqual({ status: TestStatus.unresolved, - diffName: null, - pixelMisMatchCount: null, - diffPercent: null, + diffName: undefined, + pixelMisMatchCount: undefined, + diffPercent: undefined, isSameDimension: false, }); }); @@ -441,7 +385,7 @@ describe('TestRunsService', () => { expect(result).toStrictEqual({ status: TestStatus.ok, - diffName: null, + diffName: undefined, pixelMisMatchCount: 0, diffPercent: 0, isSameDimension: true, @@ -467,7 +411,7 @@ describe('TestRunsService', () => { expect(saveImageMock).toHaveBeenCalledTimes(0); expect(result).toStrictEqual({ status: TestStatus.ok, - diffName: null, + diffName: undefined, pixelMisMatchCount, diffPercent: 1.5, isSameDimension: true, @@ -596,4 +540,61 @@ describe('TestRunsService', () => { }, }); }); + + it('findMany', async () => { + const buildId = 'some id'; + const testRunFindManyMock = jest.fn(); + service = await initService({ + testRunFindManyMock, + }); + + await service.findMany(buildId); + + expect(testRunFindManyMock).toHaveBeenCalledWith({ + where: { buildId }, + }); + }); + + it('delete', async () => { + const id = 'some id'; + const testRun = { + diffName: 'diffName', + imageName: 'imageName', + }; + const findOneMock = jest.fn().mockResolvedValueOnce(testRun); + const deleteImageMock = jest.fn(); + const testRunDeleteMock = jest.fn(); + service = await initService({ + deleteImageMock, + testRunDeleteMock, + }); + service.findOne = findOneMock; + + await service.delete(id); + + expect(findOneMock).toHaveBeenCalledWith(id); + expect(deleteImageMock).toHaveBeenNthCalledWith(1, testRun.diffName); + expect(deleteImageMock).toHaveBeenNthCalledWith(2, testRun.imageName); + expect(testRunDeleteMock).toHaveBeenCalledWith({ + where: { id }, + }); + }); + + it('updateIgnoreAreas', async () => { + const id = 'some id'; + const ignoreAreas: IgnoreAreaDto[] = [{ x: 1, y: 2, width: 10, height: 20 }]; + const testRunUpdateMock = jest.fn(); + service = await initService({ + testRunUpdateMock, + }); + + await service.updateIgnoreAreas(id, ignoreAreas); + + expect(testRunUpdateMock).toHaveBeenCalledWith({ + where: { id }, + data: { + ignoreAreas: JSON.stringify(ignoreAreas), + }, + }); + }); }); diff --git a/src/test-runs/test-runs.service.ts b/src/test-runs/test-runs.service.ts index d5dd7e49..a02021f3 100644 --- a/src/test-runs/test-runs.service.ts +++ b/src/test-runs/test-runs.service.ts @@ -141,14 +141,10 @@ export class TestRunsService { async delete(id: string): Promise { const testRun = await this.findOne(id); - try { - Promise.all([ - testRun.diffName && this.staticService.deleteImage(testRun.diffName), - this.staticService.deleteImage(testRun.imageName), - ]); - } catch (err) { - console.log(err); - } + Promise.all([ + this.staticService.deleteImage(testRun.diffName), + this.staticService.deleteImage(testRun.imageName), + ]); return this.prismaService.testRun.delete({ where: { id }, @@ -166,11 +162,11 @@ export class TestRunsService { getDiff(baseline: PNG, image: PNG, diffTollerancePercent: number, ignoreAreas: string): DiffResult { const result: DiffResult = { - status: null, - diffName: null, - pixelMisMatchCount: null, - diffPercent: null, - isSameDimension: false, + status: undefined, + diffName: undefined, + pixelMisMatchCount: undefined, + diffPercent: undefined, + isSameDimension: undefined, }; if (baseline) { From 25d571ae3ad7a3232320ffd912184c0a50c6f9ea Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Thu, 4 Jun 2020 20:59:22 +0200 Subject: [PATCH 4/4] diffName not overriden fixed --- src/test-runs/test-runs.service.spec.ts | 8 ++++---- src/test-runs/test-runs.service.ts | 9 +++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/test-runs/test-runs.service.spec.ts b/src/test-runs/test-runs.service.spec.ts index ac81e360..6f01c64a 100644 --- a/src/test-runs/test-runs.service.spec.ts +++ b/src/test-runs/test-runs.service.spec.ts @@ -340,7 +340,7 @@ describe('TestRunsService', () => { expect(result).toStrictEqual({ status: undefined, - diffName: undefined, + diffName: null, pixelMisMatchCount: undefined, diffPercent: undefined, isSameDimension: undefined, @@ -362,7 +362,7 @@ describe('TestRunsService', () => { expect(result).toStrictEqual({ status: TestStatus.unresolved, - diffName: undefined, + diffName: null, pixelMisMatchCount: undefined, diffPercent: undefined, isSameDimension: false, @@ -385,7 +385,7 @@ describe('TestRunsService', () => { expect(result).toStrictEqual({ status: TestStatus.ok, - diffName: undefined, + diffName: null, pixelMisMatchCount: 0, diffPercent: 0, isSameDimension: true, @@ -411,7 +411,7 @@ describe('TestRunsService', () => { expect(saveImageMock).toHaveBeenCalledTimes(0); expect(result).toStrictEqual({ status: TestStatus.ok, - diffName: undefined, + diffName: null, pixelMisMatchCount, diffPercent: 1.5, isSameDimension: true, diff --git a/src/test-runs/test-runs.service.ts b/src/test-runs/test-runs.service.ts index a02021f3..a8639371 100644 --- a/src/test-runs/test-runs.service.ts +++ b/src/test-runs/test-runs.service.ts @@ -89,7 +89,7 @@ export class TestRunsService { const baseline = this.staticService.getImage(testRun.baselineName); const image = this.staticService.getImage(testRun.imageName); - this.staticService.deleteImage(testRun.diffName); + await this.staticService.deleteImage(testRun.diffName); const diffResult = this.getDiff(baseline, image, testRun.diffTollerancePercent, testRun.ignoreAreas); return this.saveDiffResult(id, diffResult); @@ -141,10 +141,7 @@ export class TestRunsService { async delete(id: string): Promise { const testRun = await this.findOne(id); - Promise.all([ - this.staticService.deleteImage(testRun.diffName), - this.staticService.deleteImage(testRun.imageName), - ]); + Promise.all([this.staticService.deleteImage(testRun.diffName), this.staticService.deleteImage(testRun.imageName)]); return this.prismaService.testRun.delete({ where: { id }, @@ -163,7 +160,7 @@ export class TestRunsService { getDiff(baseline: PNG, image: PNG, diffTollerancePercent: number, ignoreAreas: string): DiffResult { const result: DiffResult = { status: undefined, - diffName: undefined, + diffName: null, pixelMisMatchCount: undefined, diffPercent: undefined, isSameDimension: undefined,