Use scenario JSON files in tests

This will check that the data returned matches the data in the JSON
files, rather than checking against constants/magic values.
This commit is contained in:
Koen Vlaswinkel
2022-10-28 16:34:51 +02:00
parent c4d9ef26a8
commit 562986546d

View File

@@ -3,6 +3,8 @@ import { expect } from 'chai';
import * as Octokit from '@octokit/rest';
import { retry } from '@octokit/plugin-retry';
import { faker } from '@faker-js/faker';
import {
getRepositoryFromNwo,
getVariantAnalysis,
@@ -15,6 +17,10 @@ import {
} from '../../../../src/vscode-tests/factories/remote-queries/shared/variant-analysis-submission';
import { MockGitHubApiServer } from '../../../../src/mocks/mock-gh-api-server';
import * as getRepoJson from '../../../../src/mocks/scenarios/problem-query-success/0-getRepo.json';
import * as variantAnalysisJson from '../../../../src/mocks/scenarios/problem-query-success/1-submitVariantAnalysis.json';
import * as variantAnalysisRepoJson from '../../../../src/mocks/scenarios/problem-query-success/9-getVariantAnalysisRepo.json';
const mockCredentials = {
getOctokit: () => Promise.resolve(new Octokit.Octokit({ retry }))
} as unknown as Credentials;
@@ -24,6 +30,10 @@ before(() => mockServer.startServer());
afterEach(() => mockServer.unloadScenario());
after(() => mockServer.stopServer());
const controllerRepoId = variantAnalysisJson.response.body.controller_repo.id;
const variantAnalysisId = variantAnalysisJson.response.body.id;
const repoTaskId = variantAnalysisRepoJson.response.body.repository.id;
describe('submitVariantAnalysis', () => {
it('returns the submitted variant analysis', async () => {
await mockServer.loadScenario('problem-query-success');
@@ -31,7 +41,7 @@ describe('submitVariantAnalysis', () => {
const result = await submitVariantAnalysis(mockCredentials, createMockSubmission());
expect(result).not.to.be.undefined;
expect(result.id).to.eq(146);
expect(result.id).to.eq(variantAnalysisId);
});
});
@@ -39,7 +49,7 @@ describe('getVariantAnalysis', () => {
it('returns the variant analysis', async () => {
await mockServer.loadScenario('problem-query-success');
const result = await getVariantAnalysis(mockCredentials, 557804416, 146);
const result = await getVariantAnalysis(mockCredentials, controllerRepoId, variantAnalysisId);
expect(result).not.to.be.undefined;
expect(result.status).not.to.be.undefined;
@@ -50,10 +60,10 @@ describe('getVariantAnalysisRepo', () => {
it('returns the variant analysis repo task', async () => {
await mockServer.loadScenario('problem-query-success');
const result = await getVariantAnalysisRepo(mockCredentials, 557804416, 146, 206444);
const result = await getVariantAnalysisRepo(mockCredentials, controllerRepoId, variantAnalysisId, repoTaskId);
expect(result).not.to.be.undefined;
expect(result.repository.id).to.eq(206444);
expect(result.repository.id).to.eq(repoTaskId);
});
});
@@ -61,11 +71,11 @@ describe('getVariantAnalysisRepoResult', () => {
it('returns the variant analysis repo result', async () => {
await mockServer.loadScenario('problem-query-success');
const result = await getVariantAnalysisRepoResult(mockCredentials, 'https://objects-origin.githubusercontent.com/codeql-query-console/codeql-variant-analysis-repo-tasks/146/206444/f6752c5c-ad60-46ba-b8dc-977546108458');
const result = await getVariantAnalysisRepoResult(mockCredentials, `https://objects-origin.githubusercontent.com/codeql-query-console/codeql-variant-analysis-repo-tasks/${variantAnalysisId}/${repoTaskId}/${faker.datatype.uuid()}`);
expect(result).not.to.be.undefined;
expect(result).to.be.an('ArrayBuffer');
expect(result.byteLength).to.eq(81841);
expect(result.byteLength).to.eq(variantAnalysisRepoJson.response.body.artifact_size_in_bytes);
});
});
@@ -76,6 +86,6 @@ describe('getRepositoryFromNwo', () => {
const result = await getRepositoryFromNwo(mockCredentials, 'github', 'mrva-demo-controller-repo');
expect(result).not.to.be.undefined;
expect(result.id).to.eq(557804416);
expect(result.id).to.eq(getRepoJson.response.body.id);
});
});