-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27443a7
commit b10f052
Showing
5 changed files
with
218 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// (c) Copyright 2024, SAP SE and ClearlyDefined contributors. Licensed under the MIT license. | ||
// SPDX-License-Identifier: MIT | ||
|
||
const chaiAsPromised = require('chai-as-promised') | ||
const chai = require('chai') | ||
chai.use(chaiAsPromised) | ||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const DefinitionQueueUpgrader = require('../../../providers/upgrade/defUpgradeQueue') | ||
|
||
describe('DefinitionQueueUpgrader', () => { | ||
const definition = { coordinates: 'test', _meta: { schemaVersion: '1.0.0' } } | ||
let queue, upgrader | ||
|
||
beforeEach(async () => { | ||
const logger = { debug: sinon.stub() } | ||
queue = { | ||
queue: sinon.stub().resolves(), | ||
initialize: sinon.stub().resolves() | ||
} | ||
const queueFactory = sinon.stub().returns(queue) | ||
upgrader = new DefinitionQueueUpgrader({ logger, queue: queueFactory }) | ||
}) | ||
|
||
it('returns an instance of DefinitionQueueUpgrader', () => { | ||
expect(upgrader).to.be.an.instanceOf(DefinitionQueueUpgrader) | ||
}) | ||
|
||
it('sets and gets current schema version', () => { | ||
upgrader.currentSchema = '1.0.0' | ||
expect(upgrader.currentSchema).to.equal('1.0.0') | ||
}) | ||
|
||
it('initializes', async () => { | ||
await upgrader.initialize() | ||
expect(queue.initialize.calledOnce).to.be.true | ||
}) | ||
|
||
it('connects to queue after setupProcessing', async () => { | ||
await upgrader.initialize() | ||
const definitionService = { currentSchema: '1.0.0' } | ||
const logger = { debug: sinon.stub() } | ||
queue.dequeueMultiple = sinon.stub().resolves([]) | ||
upgrader.setupProcessing(definitionService, logger, true) | ||
expect(queue.dequeueMultiple.calledOnce).to.be.true | ||
}) | ||
|
||
context('validate', () => { | ||
it('should fail if current schema version is not set', async () => { | ||
await expect(upgrader.validate(definition)).to.be.rejectedWith(Error) | ||
}) | ||
|
||
it('fails if it is not initialized', async () => { | ||
upgrader.currentSchema = '1.0.0' | ||
const stale = { coordinates: 'test', _meta: { schemaVersion: '0.0.1' } } | ||
await expect(upgrader.validate(stale)).to.be.rejectedWith(Error) | ||
}) | ||
}) | ||
|
||
context('validate after set up', () => { | ||
beforeEach(async () => { | ||
await upgrader.initialize() | ||
upgrader.currentSchema = '1.0.0' | ||
}) | ||
|
||
it('does not queue null definition', async () => { | ||
const result = await upgrader.validate(null) | ||
expect(result).to.be.not.ok | ||
expect(queue.queue.called).to.be.false | ||
}) | ||
|
||
it('does not queue an up-to-date definition', async () => { | ||
const definition = { coordinates: 'test', _meta: { schemaVersion: '1.0.0' } } | ||
const result = await upgrader.validate(definition) | ||
expect(result).to.deep.equal(definition) | ||
expect(queue.queue.called).to.be.false | ||
}) | ||
|
||
it('queues and returns a stale definition', async () => { | ||
const definition = { coordinates: 'test', _meta: { schemaVersion: '0.0.1' } } | ||
const result = await upgrader.validate(definition) | ||
expect(result).to.deep.equal(definition) | ||
expect(queue.queue.calledOnce).to.be.true | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// (c) Copyright 2024, SAP SE and ClearlyDefined contributors. Licensed under the MIT license. | ||
// SPDX-License-Identifier: MIT | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const { DefinitionVersionChecker, factory } = require('../../../providers/upgrade/defVersionCheck') | ||
|
||
describe('DefinitionVersionChecker', () => { | ||
let logger, checker | ||
beforeEach(() => { | ||
logger = { debug: sinon.stub() } | ||
checker = new DefinitionVersionChecker({ logger }) | ||
}) | ||
|
||
it('returns an instance of DefinitionVersionChecker', () => { | ||
expect(checker).to.be.an.instanceOf(DefinitionVersionChecker) | ||
}) | ||
|
||
it('creates a new instance of DefinitionVersionChecker using factory', () => { | ||
const checker = factory({ logger: logger }) | ||
expect(checker).to.be.an.instanceOf(DefinitionVersionChecker) | ||
}) | ||
|
||
it('sets and gets current schema version', () => { | ||
checker.currentSchema = '1.0.0' | ||
expect(checker.currentSchema).to.equal('1.0.0') | ||
}) | ||
|
||
it('initializes and returns undefined', async () => { | ||
const result = await checker.initialize() | ||
expect(result).to.be.not.ok | ||
}) | ||
|
||
it('returns after setupProcessing', async () => { | ||
const result = checker.setupProcessing() | ||
expect(result).to.be.not.ok | ||
}) | ||
|
||
it('throws an error in validate if current schema version is not set', async () => { | ||
const definition = { _meta: { schemaVersion: '1.0.0' } } | ||
let errorMessage = '' | ||
try { | ||
await checker.validate(definition) | ||
fail('should have failed') | ||
} catch (error) { | ||
errorMessage = error.message | ||
} | ||
expect(errorMessage).to.be.not.empty | ||
}) | ||
|
||
context('validate after current schema version is set', () => { | ||
beforeEach(() => { | ||
checker.currentSchema = '1.0.0' | ||
}) | ||
|
||
it('returns the definition if it is up-to-date', async () => { | ||
const definition = { _meta: { schemaVersion: '1.0.0' } } | ||
const result = await checker.validate(definition) | ||
expect(result).to.deep.equal(definition) | ||
}) | ||
|
||
it('returns undefined for a stale definition', async () => { | ||
const definition = { _meta: { schemaVersion: '0.1.0' } } | ||
const result = await checker.validate(definition) | ||
expect(result).to.be.undefined | ||
}) | ||
|
||
it('returns undefined for a definition without schema version', async () => { | ||
const definition = {} | ||
const result = await checker.validate(definition) | ||
expect(result).to.be.undefined | ||
}) | ||
|
||
it('handles null', async () => { | ||
checker.currentSchema = '1.0.0' | ||
const result = await checker.validate(null) | ||
expect(result).to.be.not.ok | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters