Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
stalgiag committed Jul 2, 2024
1 parent adf47dc commit de92a00
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 69 deletions.
73 changes: 14 additions & 59 deletions server/models/services/CollectionJobService.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {
const runnableTestsResolver = require('../../resolvers/TestPlanReport/runnableTestsResolver');
const getGraphQLContext = require('../../graphql-context');
const { getBotUserByAtId } = require('./UserService');
const { getAtVersions } = require('./AtService');
const getAtVersionWithRequirements = require('../../util/getAtVersionWithRequirements');

const axiosConfig = {
headers: {
Expand All @@ -36,63 +36,6 @@ const axiosConfig = {
timeout: 1000
};

const getAtVersionWithRequirements = async (
atId,
exactAtVersion,
minimumAtVersion,
transaction
) => {
try {
if (exactAtVersion) {
return exactAtVersion;
}

if (!minimumAtVersion) {
throw new Error(
'Either exactAtVersion or minimumAtVersion must be provided'
);
}

const isMinimumVersionSupported =
await minimumAtVersion.supportedByAutomation;
if (isMinimumVersionSupported) {
return minimumAtVersion;
}

const matchingAts = await getAtVersions({
where: {
atId,
releasedAt: { [Op.gte]: minimumAtVersion.releasedAt }
},
pagination: {
order: [['releasedAt', 'ASC']]
},
transaction
});

const supportedAts = await Promise.all(
matchingAts.map(async version => {
const supportedByAutomation = await version.supportedByAutomation;
return supportedByAutomation ? version.toJSON() : null;
})
);

const latestSupportedAt = supportedAts.filter(Boolean)[0];

if (!latestSupportedAt) {
throw new Error(
`No suitable AT version found for automation for AT ${atId} ` +
`with minimumAtVersion ${minimumAtVersion?.name}`
);
}

return latestSupportedAt;
} catch (error) {
console.error('Error while determining AT version:', error);
throw error;
}
};

// association helpers to be included with Models' results

/**
Expand Down Expand Up @@ -714,7 +657,19 @@ const restartCollectionJob = async ({ id }, { transaction }) => {
return null;
}

return triggerWorkflow(job, [], { transaction });
const testPlanReport = await getTestPlanReportById({
id: job.testPlanRun.testPlanReportId,
transaction
});

const atVersion = await getAtVersionWithRequirements(
testPlanReport.at.id,
testPlanReport.exactAtVersion,
testPlanReport.minimumAtVersion,
transaction
);

return triggerWorkflow(job, [], atVersion, { transaction });
};

/**
Expand Down
2 changes: 2 additions & 0 deletions server/scripts/populate-test-data/pg_dump_test_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ $$;
-- Data for Name: AtVersion; Type: TABLE DATA; Schema: public; Owner: atr
--

INSERT INTO "AtVersion" ("atId", "name", "releasedAt") VALUES (2, '2023.3.3', '2023-12-02');

-- INSERT INTO "AtVersion" ("atId", "name", "releasedAt") VALUES (2, '2019.3', '2022-05-02');
-- INSERT INTO "AtVersion" ("atId", "name", "releasedAt") VALUES (2, '2020.1', '2022-05-02');
-- INSERT INTO "AtVersion" ("atId", "name", "releasedAt") VALUES (2, '2020.2', '2022-05-02');
Expand Down
47 changes: 37 additions & 10 deletions server/tests/integration/automation-scheduler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const AtLoader = require('../../models/loaders/AtLoader');
const BrowserLoader = require('../../models/loaders/BrowserLoader');
const getGraphQLContext = require('../../graphql-context');
const { COLLECTION_JOB_STATUS } = require('../../util/enums');
const getAtVersionWithRequirements = require('../../util/getAtVersionWithRequirements');

let mockAutomationSchedulerServer;
let apiServer;
Expand Down Expand Up @@ -241,21 +242,28 @@ describe('Automation controller', () => {
data: { id: '999', status: 'QUEUED' }
});

const {
testPlanReport: {
runnableTests,
testPlanVersion: {
gitSha: testPlanVersionGitSha,
testPlan: { directory: testPlanName }
}
}
} = await query(
const { testPlanReport } = await query(
`
query {
testPlanReport(id: "${testPlanReportId}") {
runnableTests {
id
}
minimumAtVersion {
name
id
releasedAt
supportedByAutomation
}
exactAtVersion {
name
id
releasedAt
supportedByAutomation
}
at {
id
}
testPlanVersion {
testPlan {
directory
Expand All @@ -268,18 +276,37 @@ describe('Automation controller', () => {
{ transaction }
);

const {
runnableTests,
testPlanVersion: {
gitSha: testPlanVersionGitSha,
testPlan: { directory: testPlanName }
},
minimumAtVersion,
exactAtVersion,
at
} = testPlanReport;

const testIds = runnableTests.map(({ id }) => id);

const collectionJob = await scheduleCollectionJobByMutation({
transaction
});

const atVersion = await getAtVersionWithRequirements(
at.id,
exactAtVersion,
minimumAtVersion,
transaction
);

const expectedRequestBody = {
testPlanVersionGitSha,
testIds,
testPlanName,
jobId: parseInt(collectionJob.scheduleCollectionJob.id),
transactionId: transaction.id
transactionId: transaction.id,
atVersion
};

expect(axiosPostMock).toHaveBeenCalledWith(
Expand Down
61 changes: 61 additions & 0 deletions server/util/getAtVersionWithRequirements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { Op } = require('sequelize');
const { getAtVersions } = require('../models/services/AtService');

const getAtVersionWithRequirements = async (
atId,
exactAtVersion,
minimumAtVersion,
transaction
) => {
try {
if (exactAtVersion) {
return exactAtVersion;
}

if (!minimumAtVersion) {
throw new Error(
'Either exactAtVersion or minimumAtVersion must be provided'
);
}

const isMinimumVersionSupported =
await minimumAtVersion.supportedByAutomation;
if (isMinimumVersionSupported) {
return minimumAtVersion;
}

const matchingAts = await getAtVersions({
where: {
atId,
releasedAt: { [Op.gte]: minimumAtVersion.releasedAt }
},
pagination: {
order: [['releasedAt', 'ASC']]
},
transaction
});

const supportedAts = await Promise.all(
matchingAts.map(async version => {
const supportedByAutomation = await version.supportedByAutomation;
return supportedByAutomation ? version.toJSON() : null;
})
);

const latestSupportedAt = supportedAts.filter(Boolean)[0];

if (!latestSupportedAt) {
throw new Error(
`No suitable AT version found for automation for AT ${atId} ` +
`with minimumAtVersion ${minimumAtVersion?.name}`
);
}

return latestSupportedAt;
} catch (error) {
console.error('Error while determining AT version:', error);
throw error;
}
};

module.exports = getAtVersionWithRequirements;

0 comments on commit de92a00

Please sign in to comment.