Skip to content

Commit

Permalink
Changes to testing
Browse files Browse the repository at this point in the history
  • Loading branch information
IR96334 committed Dec 3, 2024
1 parent 5118315 commit ae80725
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 92 deletions.
30 changes: 11 additions & 19 deletions backend/src/services/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export async function getModelReleases(
modelId: string,
querySemver?: string,
): Promise<Array<ReleaseDoc & { model: ModelInterface; files: FileInterface[] }>> {
const query = querySemver === undefined ? { modelId } : getQuerySyntax(querySemver, modelId)
const query = querySemver === undefined ? { modelId } : convertSemverQueryToMongoQuery(querySemver, modelId)
const results = await Release.aggregate()
.match(query)
.sort({ updatedAt: -1 })
Expand Down Expand Up @@ -335,39 +335,31 @@ export async function getReleaseBySemver(user: UserInterface, modelId: string, s
return release
}

function getSemverQueryBounds(querySemver: string) {
function parseSemverQuery(querySemver: string) {
const semverRangeStandardised = semver.validRange(querySemver, { includePrerelease: false })

if (!semverRangeStandardised) {
throw BadReq(`Semver range, '${querySemver}' is invalid.`)
throw BadReq('Semver range is invalid.', { semverQuery: querySemver })
}

const [expressionA, expressionB] = semverRangeStandardised.split(' ')

let lowerInclusivity, lowerSemver, upperInclusivity, upperSemver
let lowerInclusivity, upperInclusivity
let lowerSemverObj, upperSemverObj

//If lower semver
//LOWER SEMVER
if (expressionA.includes('>')) {
lowerInclusivity = expressionA.includes('>=')
lowerSemver = expressionA.replace(/[<>=]/g, '')
lowerSemverObj = semverStringToObject(expressionA.replace(/[<>=]/g, ''))
} else {
//upper semver
upperInclusivity = expressionA.includes('<=')
upperSemver = expressionA.replace(/[<=]/g, '')
upperSemverObj = semverStringToObject(expressionA.replace(/[<=]/g, ''))
}

if (expressionB) {
upperInclusivity = expressionB.includes('<=')
upperSemver = expressionB.replace(/[<=]/g, '')
}

let lowerSemverObj, upperSemverObj
if (lowerSemver) {
lowerSemverObj = semverStringToObject(lowerSemver)
}

if (upperSemver) {
upperSemverObj = semverStringToObject(upperSemver)
upperSemverObj = semverStringToObject(expressionB.replace(/[<=]/g, ''))
}

return { lowerSemverObj, upperSemverObj, lowerInclusivity, upperInclusivity }
Expand Down Expand Up @@ -418,8 +410,8 @@ interface QueryBoundInterface {
)[]
}

export function getQuerySyntax(querySemver: string, modelID: string) {
const { lowerSemverObj, upperSemverObj, lowerInclusivity, upperInclusivity } = getSemverQueryBounds(querySemver)
function convertSemverQueryToMongoQuery(querySemver: string, modelID: string) {
const { lowerSemverObj, upperSemverObj, lowerInclusivity, upperInclusivity } = parseSemverQuery(querySemver)

const queryQueue: QueryBoundInterface[] = []

Expand Down
2 changes: 2 additions & 0 deletions backend/test/services/__snapshots__/release.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`services > release > convertSemverQueryToMongoQuery > good 1`] = `undefined`;

exports[`services > release > getModelReleases > good 1`] = `undefined`;

exports[`services > release > getModelReleases > good 2`] = `undefined`;
Expand Down
96 changes: 23 additions & 73 deletions backend/test/services/release.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
getAllFileIds,
getFileByReleaseFileName,
getModelReleases,
getQuerySyntax,
getReleaseBySemver,
getReleasesForExport,
newReleaseComment,
Expand Down Expand Up @@ -365,78 +364,29 @@ describe('services > release', () => {

//test good - give good semver range with lower and upper bounds, returns valid query
//test fail - invalid range
test('getQuerySyntax > good'),
async () => {
const testQuery = getQuerySyntax('1.0.x', 'testModelID')
expect(testQuery).toBe({
modelId: 'testModelID',
$and: [
{
$or: [
{
'semver.major': {
$gte: 1,
},
'semver.minor': {
$gte: 0,
},
'semver.patch': {
$gte: 0,
},
},
{
'semver.major': {
$gt: 1,
},
},
{
'semver.major': {
$gte: 1,
},
'semver.minor': {
$gt: 0,
},
},
],
},
{
$or: [
{
'semver.major': {
$lte: 1,
},
'semver.minor': {
$lte: 1,
},
'semver.patch': {
$lt: 0,
},
},
{
'semver.major': {
$lt: 1,
},
},
{
'semver.major': {
$lte: 1,
},
'semver.minor': {
$lt: 1,
},
},
],
},
],
})
}

test('getQuerySyntax > bad'),
async () => {
const querySemver = '>2.^2.x'
const modelId = 'testModelID'
expect(() => getQuerySyntax(querySemver, modelId)).rejects.toThrowError(/^Semver range, '>2.^2.x' is invalid./)
}
test('convertSemverQueryToMongoQuery > good', async () => {
releaseModelMocks.aggregate.mockImplementation(() => ({
match: vi.fn().mockImplementation(() => ({
sort: vi.fn().mockImplementation(() => ({
lookup: vi.fn().mockImplementation(() => ({
append: vi.fn().mockImplementation(() => ({
lookup: vi.fn().mockImplementation(() => [{ _id: 'release', modelId: 'test', semver: '1.0.0' }]),
})),
})),
})),
})),
}))

await getModelReleases({ dn: 'user' } as UserInterface, 'modelId', '2.2.X')

expect(releaseModelMocks.match.mock.calls.at(0)).toMatchSnapshot()
})

test('convertSemverQueryToMongoQuery > bad', async () => {
expect(async () => await getModelReleases({ dn: 'user' } as UserInterface, 'test', '^2.2v.x')).rejects.toThrowError(
/^Semver range is invalid./,
)
})

test('getReleaseBySemver > good', async () => {
const mockRelease = { _id: 'release' }
Expand Down

0 comments on commit ae80725

Please sign in to comment.