Skip to content

Commit

Permalink
test: DataLayerService
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley committed May 29, 2024
1 parent e560c49 commit 7afde0b
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
141 changes: 141 additions & 0 deletions runner/src/server/services/data-layer/data-layer-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { type ServerUnaryCall, status } from '@grpc/grpc-js';

import { createDataLayerService, type ProvisioningTask } from './data-layer-service'; // Adjust this path accordingly
import { ProvisioningStatus } from '../../../generated/ProvisioningStatus';
import type Provisioner from '../../../provisioner';

describe('DataLayerService', () => {
describe('CheckProvisioningStatus', () => {
it('should return NOT_FOUND if the task does not exist', (done) => {
const call = {
request: { accountId: 'testAccount', functionName: 'testFunction' }
} as unknown as ServerUnaryCall<any, any>;

const callback = (error: any): void => {
expect(error.code).toBe(status.NOT_FOUND);
expect(error.details).toBe('Provisioning task does not exist');
done();
};

createDataLayerService().CheckProvisioningStatus(call, callback);
});

it('should return PENDING if the task is pending', (done) => {
const tasks = {
'testAccount:testFunction': { pending: true, completed: false, failed: false } as unknown as ProvisioningTask
};
const call = {
request: { accountId: 'testAccount', functionName: 'testFunction' }
} as unknown as ServerUnaryCall<any, any>;
const callback = (_error: any, response: any): void => {
expect(response.status).toBe(ProvisioningStatus.PENDING);
done();
};

createDataLayerService(undefined, tasks).CheckProvisioningStatus(call, callback);
});

it('should return COMPLETE if the task is completed', (done) => {
const tasks = {
'testAccount:testFunction': { pending: false, completed: true, failed: false } as unknown as ProvisioningTask
};
const call = {
request: { accountId: 'testAccount', functionName: 'testFunction' }
} as unknown as ServerUnaryCall<any, any>;
const callback = (_error: any, response: any): void => {
expect(response.status).toBe(ProvisioningStatus.COMPLETE);
done();
};

createDataLayerService(undefined, tasks).CheckProvisioningStatus(call, callback);
});

it('should return FAILED if the task has failed', (done) => {
const tasks = {
'testAccount:testFunction': { pending: false, completed: false, failed: true } as unknown as ProvisioningTask
};
const call = {
request: { accountId: 'testAccount', functionName: 'testFunction' }
} as unknown as ServerUnaryCall<any, any>;
const callback = (_error: any, response: any): void => {
expect(response.status).toBe(ProvisioningStatus.FAILED);
done();
};

createDataLayerService(undefined, tasks).CheckProvisioningStatus(call, callback);
});
});

describe('Provision', () => {
it('should return ALREADY_EXISTS if the task is already pending', (done) => {
const tasks = {
'testAccount:testFunction': { pending: true, completed: false, failed: false } as unknown as ProvisioningTask
};
const call = {
request: { accountId: 'testAccount', functionName: 'testFunction', schema: 'schema' }
} as unknown as ServerUnaryCall<any, any>;
const callback = (error: any): void => {
expect(error.code).toBe(status.ALREADY_EXISTS);
expect(error.details).toBe('Provisioning task already exists');
done();
};

createDataLayerService(undefined, tasks).Provision(call, callback);
});

it('should return ALREADY_EXISTS if the task has already completed', (done) => {
const provisioner = {
fetchUserApiProvisioningStatus: jest.fn().mockResolvedValue(true)
} as unknown as Provisioner;
const call = {
request: { accountId: 'testAccount', functionName: 'testFunction', schema: 'testSchema' }
} as unknown as ServerUnaryCall<any, any>;
console.log('hi');
const callback = (error: any): void => {
console.log(error);
console.log(status.ALREADY_EXISTS, error.code);
expect(error.code).toBe(status.ALREADY_EXISTS);
expect(error.details).toBe('Provisioning task has already completed');
done();
};

createDataLayerService(provisioner).Provision(call, callback);
});

it('should start a new provisioning task and return PENDING', (done) => {
const tasks: Record<any, any> = {};
const provisioner = {
fetchUserApiProvisioningStatus: jest.fn().mockResolvedValue(false),
provisionUserApi: jest.fn().mockResolvedValue(null)
} as unknown as Provisioner;
const call = {
request: { accountId: 'testAccount', functionName: 'testFunction', schema: 'testSchema' }
} as unknown as ServerUnaryCall<any, any>;
const callback = (_error: any, response: any): void => {
expect(response.status).toBe(ProvisioningStatus.PENDING);
expect(tasks['testAccount:testFunction']).toBeDefined();
expect(tasks['testAccount:testFunction'].pending).toBe(true);
done();
};

createDataLayerService(provisioner, tasks).Provision(call, callback);
});

it('should return INTERNAL error if checking provisioning status fails', (done) => {
const tasks: Record<any, any> = {};
const provisioner = {
fetchUserApiProvisioningStatus: jest.fn().mockRejectedValue(new Error('boom'))
} as unknown as Provisioner;
const call = {
request: { accountId: 'testAccount', functionName: 'testFunction', schema: 'testSchema' }
} as unknown as ServerUnaryCall<any, any>;
const callback = (error: any): void => {
expect(error.code).toBe(status.INTERNAL);
expect(error.details).toBe('boom');
done();
};

createDataLayerService(provisioner, tasks).Provision(call, callback);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { type ProvisionRequest__Output } from '../../../generated/ProvisionReque
import { type ProvisionResponse } from '../../../generated/ProvisionResponse';
import { ProvisioningStatus } from '../../../generated/ProvisioningStatus';

class ProvisioningTask {
export class ProvisioningTask {
public failed: boolean;
public pending: boolean;
public completed: boolean;
Expand Down

0 comments on commit 7afde0b

Please sign in to comment.