Skip to content

Commit

Permalink
fix: improve tests and edge-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
infloop committed Nov 21, 2023
1 parent f89211a commit 89dd289
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 26 deletions.
83 changes: 69 additions & 14 deletions src/common/registry/fetch/utils/batches.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { makeBatches } from './batches';

describe('makeBatches util', () => {
test('should create batches with correct offset and batchSize', () => {
test('should create batches with correct zero offset and batchSize', () => {
const batchSize = 3;
const offset = 0;
const totalAmount = 10;
const limit = 10;

const result = makeBatches(batchSize, offset, totalAmount);
const result = makeBatches(batchSize, offset, limit);

expect(result).toEqual([
{ offset: 0, batchSize: 3 },
Expand All @@ -16,40 +16,95 @@ describe('makeBatches util', () => {
]);
});

test('should create a single batch when totalAmount is less than batchSize', () => {
test('should create batches with correct not zero offset and batchSize', () => {
const batchSize = 3;
const offset = 1;
const limit = 10;

const result = makeBatches(batchSize, offset, limit);

expect(result).toEqual([
{ offset: 1, batchSize: 3 },
{ offset: 4, batchSize: 3 },
{ offset: 7, batchSize: 3 },
{ offset: 10, batchSize: 1 },
]);
});

test('should create a single batch when limit is less than batchSize', () => {
const batchSize = 10;
const offset = 2;
const totalAmount = 8;
const limit = 8;

const result = makeBatches(batchSize, offset, totalAmount);
const result = makeBatches(batchSize, offset, limit);

expect(result).toEqual([{ offset: 2, batchSize: 8 }]);
});

test('should handle cases when totalAmount is 0', () => {
test('should handle cases when limit is 0', () => {
const batchSize = 5;
const offset = 0;
const totalAmount = 0;
const limit = 0;

const result = makeBatches(batchSize, offset, totalAmount);
const result = makeBatches(batchSize, offset, limit);

expect(result).toEqual([]);
});

test('should throw error when batchSize is 0', () => {
const batchSize = 0;
const offset = 2;
const totalAmount = 8;
const limit = 8;

expect(() => makeBatches(batchSize, offset, limit)).toThrowError('batchSize must be greater than 0 and integer');
});

test('should throw error when batchSize is not a number', () => {
const batchSize: any = 'test';
const offset = 2;
const limit = 8;

expect(() => makeBatches(batchSize, offset, limit)).toThrowError('batchSize must be greater than 0 and integer');
});

test('should throw error when limit is not a number', () => {
const batchSize = 10;
const offset = 2;
const limit: any = 'test';

expect(() => makeBatches(batchSize, offset, limit)).toThrowError('limit should be positive integer');
});

test('should throw error when limit is a negative number', () => {
const batchSize = 10;
const offset = 2;
const limit = -1;

expect(() => makeBatches(batchSize, offset, limit)).toThrowError('limit should be positive integer');
});

test('should throw error when offset is not a number', () => {
const batchSize = 10;
const offset: any = 'test';
const limit = 2;

expect(() => makeBatches(batchSize, offset, limit)).toThrowError('offset should be positive integer');
});

test('should throw error when offset is a negative number', () => {
const batchSize = 10;
const offset = -2;
const limit = 10;

expect(() => makeBatches(batchSize, offset, totalAmount)).toThrowError('batchSize must be greater than 0');
expect(() => makeBatches(batchSize, offset, limit)).toThrowError('offset should be positive integer');
});

test('should create batches correct offset and batchSize when totalAmount is a multiple of batchSize', () => {
test('should create batches correct offset and batchSize when limit is a multiple of batchSize', () => {
const batchSize = 4;
const offset = 2;
const totalAmount = 16;
const limit = 16;

const result = makeBatches(batchSize, offset, totalAmount);
const result = makeBatches(batchSize, offset, limit);

expect(result).toEqual([
{ offset: 2, batchSize: 4 },
Expand Down
16 changes: 12 additions & 4 deletions src/common/registry/fetch/utils/batches.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
export const makeBatches = (batchSize: number, offset: number, totalAmount: number) => {
if (batchSize < 1) throw new Error('batchSize must be greater than 0');
const numberOfBatches = Math.ceil(totalAmount / batchSize);
export const makeBatches = (batchSize: number, offset: number, limit: number) => {
if (!Number.isInteger(offset) || offset < 0) {
throw new RangeError('offset should be positive integer');
}
if (!Number.isInteger(limit) || limit < 0) {
throw new RangeError('limit should be positive integer');
}
if (!Number.isInteger(batchSize) || batchSize < 1) {
throw new RangeError('batchSize must be greater than 0 and integer');
}
const numberOfBatches = Math.ceil(limit / batchSize);
const batches: { offset: number; batchSize: number }[] = [];

for (let i = 0; i < numberOfBatches; i++) {
const currentOffset = offset + i * batchSize;
const currentBatchSize = Math.min(batchSize, totalAmount - i * batchSize);
const currentBatchSize = Math.min(batchSize, limit - i * batchSize);
batches.push({ offset: currentOffset, batchSize: currentBatchSize });
}

Expand Down
20 changes: 16 additions & 4 deletions src/common/registry/fetch/utils/split-hex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,34 @@ import { splitHex } from './split-hex';

describe('splitHex util', () => {
test('splits a hex string into chunks of specified length', () => {
const hexString = '0x123456789abcdefg'; // 15 characters
const hexString = '0x123456789abcdefABCDEF012'; // 24 characters
const chunkLength = 4;
const result = splitHex(hexString, chunkLength);
expect(result).toEqual(['0x1234', '0x5678', '0x9abc', '0xdefg']);
expect(result).toEqual(['0x1234', '0x5678', '0x9abc', '0xdefA', '0xBCDE', '0xF012']);
});

test('throws RangeError if chunkLength is less than 1', () => {
const hexString = '0xabcdef';
const chunkLength = 0;
expect(() => splitHex(hexString, chunkLength)).toThrowError('chunkLength should be positive');
expect(() => splitHex(hexString, chunkLength)).toThrowError('chunkLength should be positive integer');
});

test('throws Error if input is not a hex-like string', () => {
const hexString = 'abcdef'; // Missing '0x' prefix
const chunkLength = 2;
expect(() => splitHex(hexString, chunkLength)).toThrowError('not a hex-like string');
expect(() => splitHex(hexString, chunkLength)).toThrowError('hexString is not a hex-like string');
});

test('throws Error if input is bad hex string', () => {
const hexString = '0x01234567890AbCdEfG'; // G symbol
const chunkLength = 2;
expect(() => splitHex(hexString, chunkLength)).toThrowError('hexString is not a hex-like string');
});

test('throws Error if input is not a string', () => {
const hexString: string = <any>1245;
const chunkLength = 2;
expect(() => splitHex(hexString, chunkLength)).toThrowError('hexString is not a hex-like string');
});

test('handles empty input string', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/common/registry/fetch/utils/split-hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
* @returns array of hex-like strings
*/
export const splitHex = (hexString: string, chunkLength: number) => {
if (chunkLength < 1) {
throw new RangeError('chunkLength should be positive');
if (!Number.isInteger(chunkLength) || chunkLength < 1) {
throw new RangeError('chunkLength should be positive integer');
}

if (typeof hexString !== 'string' || hexString.substring(0, 2) !== '0x') {
throw new Error('not a hex-like string');
if (typeof hexString !== 'string' || !hexString.match(/^0x[0-9A-Fa-f]*$/)) {
throw new Error('hexString is not a hex-like string');
}

const parts: string[] = [];
Expand Down

0 comments on commit 89dd289

Please sign in to comment.