Skip to content

Commit

Permalink
test: minor test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianNoise committed Jan 14, 2022
1 parent 192681d commit 17c5b5d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
18 changes: 11 additions & 7 deletions test/integration/Quota.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ describe('A quota server with', (): void => {
describe('pod quota enabled', (): void => {
const port = getPort('PodQuota');
const baseUrl = `http://localhost:${port}/`;
const pod1 = `${baseUrl}arthur`;
const pod2 = `${baseUrl}abel`;
const rootFilePath = getTestFolder('quota-pod');

let app: App;
Expand Down Expand Up @@ -78,8 +80,8 @@ describe('A quota server with', (): void => {

// Test quota in the first pod
it('should return a 413 when the quota is exceeded during write.', async(): Promise<void> => {
const testFile1 = `${baseUrl}abel/profile/test1.txt`;
const testFile2 = `${baseUrl}abel/test2.txt`;
const testFile1 = `${pod1}/profile/test1.txt`;
const testFile2 = `${pod1}/test2.txt`;

const response1 = performSimplePutWithLength(testFile1, 500);
await expect(response1).resolves.toBeDefined();
Expand All @@ -92,7 +94,7 @@ describe('A quota server with', (): void => {

// Test if writing in another pod is still possible
it('should allow writing in a pod that isnt full yet.', async(): Promise<void> => {
const testFile1 = `${baseUrl}arthur/profile/test1.txt`;
const testFile1 = `${pod2}/profile/test1.txt`;

const response1 = performSimplePutWithLength(testFile1, 500);
await expect(response1).resolves.toBeDefined();
Expand All @@ -101,8 +103,8 @@ describe('A quota server with', (): void => {

// Both pods should not accept this request anymore
it('should block PUT requests to different pods if their quota is exceeded.', async(): Promise<void> => {
const testFile1 = `${baseUrl}abel/test2.txt`;
const testFile2 = `${baseUrl}arthur/test2.txt`;
const testFile1 = `${pod1}/test2.txt`;
const testFile2 = `${pod2}/test2.txt`;

const response1 = performSimplePutWithLength(testFile1, 3000);
await expect(response1).resolves.toBeDefined();
Expand All @@ -118,6 +120,8 @@ describe('A quota server with', (): void => {
describe('global quota enabled', (): void => {
const port = getPort('GlobalQuota');
const baseUrl = `http://localhost:${port}/`;
const pod1 = `${baseUrl}arthur`;
const pod2 = `${baseUrl}abel`;
const rootFilePath = getTestFolder('quota-global');

let app: App;
Expand Down Expand Up @@ -164,8 +168,8 @@ describe('A quota server with', (): void => {
});

it('should return 413 when trying to write to any pod when global quota is exceeded.', async(): Promise<void> => {
const testFile1 = `${baseUrl}abel/test3.txt`;
const testFile2 = `${baseUrl}arthur/profile/test4.txt`;
const testFile1 = `${pod1}/test3.txt`;
const testFile2 = `${pod2}/profile/test4.txt`;

const response1 = performSimplePutWithLength(testFile1, 8000);
await expect(response1).resolves.toBeDefined();
Expand Down
8 changes: 1 addition & 7 deletions test/unit/quota/PodQuotaStrategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,7 @@ describe('PodQuotaStrategy', (): void => {
return res;
},
),
canHandle: jest.fn(),
writeDocument: jest.fn(),
getData: jest.fn(),
getChildren: jest.fn(),
writeContainer: jest.fn(),
deleteResource: jest.fn(),
};
} as any;
strategy = new PodQuotaStrategy(mockSize, mockReporter, identifierStrategy, accessor);
});

Expand Down
4 changes: 2 additions & 2 deletions test/unit/storage/accessors/AtomicFileDataAccessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ describe('AtomicFileDataAccessor', (): void => {

it('should delete temp file when done writing.', async(): Promise<void> => {
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined();
// Being: 'resource' the file and '.internal' the folder in the root dir
expect(Object.keys(cache.data).length + Object.keys(cache.data['.internal'].tempFiles).length).toEqual(2);
expect(Object.keys(cache.data['.internal'].tempFiles)).toHaveLength(0);
expect(cache.data.resource).toBe('data');
});

it('should throw an error when writing the data goes wrong.', async(): Promise<void> => {
Expand Down
11 changes: 2 additions & 9 deletions test/unit/storage/accessors/ValidatingDataAccessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,13 @@ describe('ValidatingDataAccessor', (): void => {
beforeEach(async(): Promise<void> => {
jest.clearAllMocks();
childAccessor = {
canHandle: jest.fn(),
writeDocument: jest.fn(),
getData: jest.fn(),
getChildren: jest.fn(),
writeContainer: jest.fn(),
deleteResource: jest.fn(),
getMetadata: jest.fn(),
};
} as any;
childAccessor.getChildren = jest.fn();
validator = {
canHandle: jest.fn(),
handle: jest.fn(async(input: ValidatorInput): Promise<Representation> => input.representation),
handleSafe: jest.fn(),
};
} as any;
validatingAccessor = new ValidatingDataAccessor(childAccessor, validator);
});

Expand Down
15 changes: 7 additions & 8 deletions test/unit/storage/validators/QuotaValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ describe('QuotaValidator', (): void => {

// Step 3
it('should destroy the stream when quota is exceeded during write.', async(): Promise<void> => {
const createQuotaGuardSpy = jest.spyOn(mockedStrategy, 'createQuotaGuard')
.mockResolvedValueOnce(guardStream(new PassThrough({
async transform(this): Promise<void> {
this.destroy();
},
})));
mockedStrategy.createQuotaGuard.mockResolvedValueOnce(guardStream(new PassThrough({
async transform(this): Promise<void> {
this.destroy(new Error('error'));
},
})));

const result = validator.handle(mockInput);
await expect(result).resolves.toBeDefined();
Expand All @@ -84,8 +83,8 @@ describe('QuotaValidator', (): void => {
});

// Consume the stream
await expect(readableToString(awaitedResult.data)).rejects.toThrow();
expect(createQuotaGuardSpy).toHaveBeenCalledTimes(1);
await expect(readableToString(awaitedResult.data)).rejects.toThrow('error');
expect(mockedStrategy.createQuotaGuard).toHaveBeenCalledTimes(1);
await expect(prom).resolves.toBeUndefined();
});

Expand Down

0 comments on commit 17c5b5d

Please sign in to comment.