Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for certain sync features #228

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ apiRouter.get('/', (_req, res) => {
authMode: state.authMode,
gitRev: state.gitRev,
name: appName,
nodeVersion: appVersion,
nodeVersion: process.version,
privacyMask: config.has('server.privacyMask'),
version: process.env.npm_package_version
version: appVersion
},
endpoints: ['/api/v1'],
versions: [1]
Expand Down
63 changes: 60 additions & 3 deletions app/tests/unit/services/version.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ jest.mock('../../../src/db/models/tables/version', () => ({
query: jest.fn(),
returning: jest.fn(),
throwIfNotFound: jest.fn(),
update: jest.fn(),
updateAndFetchById: jest.fn(),
where: jest.fn()
where: jest.fn(),
whereNot: jest.fn()
}));

const validUuidv4 = '3f4da093-6399-4711-8765-36ec5f8017c2';
Expand All @@ -41,6 +43,7 @@ beforeEach(() => {
});

afterAll(() => {
jest.clearAllMocks();
listAllObjectVersionsSpy.mockRestore();
objectSpy.mockRestore();
});
Expand Down Expand Up @@ -233,7 +236,7 @@ describe('update', () => {
describe('updateIsLatest', () => {
it('Updates a version of an object if it is the latest', async () => {
const versionSpy = jest.spyOn(service, 'removeDuplicateLatest');
versionSpy.mockResolvedValue(true);
versionSpy.mockResolvedValueOnce(true);
listAllObjectVersionsSpy.mockResolvedValue({
DeleteMarkers: [{}],
Versions: [{ IsLatest: true, VersionId: validUuidv4 }]
Expand Down Expand Up @@ -262,7 +265,7 @@ describe('updateIsLatest', () => {

it('Does not update if file is not the latest', async () => {
const versionSpy = jest.spyOn(service, 'removeDuplicateLatest');
versionSpy.mockResolvedValue(true);
versionSpy.mockResolvedValueOnce(true);
listAllObjectVersionsSpy.mockResolvedValue({
DeleteMarkers: [{}],
Versions: [
Expand Down Expand Up @@ -293,3 +296,57 @@ describe('updateIsLatest', () => {
expect(versionTrx.commit).toHaveBeenCalledTimes(1);
});
});

describe('RemoveDuplicateLatest', () => {
wilwong89 marked this conversation as resolved.
Show resolved Hide resolved
it('sets all other versions to isLatest=false', async () => {
listAllObjectVersionsSpy.mockResolvedValue({
DeleteMarkers: [{}],
Versions: [{ IsLatest: true, VersionId: validUuidv4 }]
});
objectSpy.mockResolvedValue({
path: '/test',
bucketId: '0000-0000'
});
Version.where.mockResolvedValueOnce([{ isLatest: true },{ isLatest: true }]);
wilwong89 marked this conversation as resolved.
Show resolved Hide resolved

await service.removeDuplicateLatest(validUuidv4, OBJECT_ID);

expect(Version.startTransaction).toHaveBeenCalledTimes(1);
expect(Version.query).toHaveBeenCalledTimes(2);
expect(Version.update).toHaveBeenCalledTimes(1);

expect(Version.where).toHaveBeenCalledTimes(1);
expect(Version.where).toHaveBeenCalledWith('objectId', OBJECT_ID);

expect(Version.whereNot).toHaveBeenCalledTimes(1);
expect(Version.whereNot).toHaveBeenCalledWith({ 'id': validUuidv4 });

expect(Version.andWhere).toHaveBeenCalledTimes(2);
expect(Version.andWhere).toHaveBeenCalledWith('objectId', OBJECT_ID);
expect(Version.andWhere).toHaveBeenCalledWith({ 'isLatest': true });
});

it('does not set other versions to false', async () => {
listAllObjectVersionsSpy.mockResolvedValue({
DeleteMarkers: [{}],
Versions: [{ IsLatest: false, VersionId: validUuidv4 }]
});
objectSpy.mockResolvedValue({
path: '/test',
bucketId: '0000-0000'
});
Version.where.mockResolvedValueOnce([{ isLatest: false },{ isLatest: false }]);

await service.removeDuplicateLatest(validUuidv4, OBJECT_ID);

expect(Version.startTransaction).toHaveBeenCalledTimes(1);
expect(Version.query).toHaveBeenCalledTimes(1);

expect(Version.where).toHaveBeenCalledTimes(1);
expect(Version.where).toHaveBeenCalledWith('objectId', OBJECT_ID);

expect(Version.update).toHaveBeenCalledTimes(0);
expect(Version.whereNot).toHaveBeenCalledTimes(0);
expect(Version.andWhere).toHaveBeenCalledTimes(0);
});
});
Loading