Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
♻️ Update data access test
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed May 28, 2020
1 parent 42e7ccd commit d8b8925
Show file tree
Hide file tree
Showing 6 changed files with 499 additions and 515 deletions.
115 changes: 0 additions & 115 deletions elements/lisk-chain/test/fixtures/block.ts

This file was deleted.

24 changes: 0 additions & 24 deletions elements/lisk-chain/test/fixtures/default_account.ts

This file was deleted.

51 changes: 20 additions & 31 deletions elements/lisk-chain/test/unit/data_access/cache/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

import { BlockCache } from '../../../../src/data_access/cache';
import { BlockHeader as BlockHeaderInstance } from '../../../fixtures/block';
import { createFakeBlockHeader } from '../../../utils/block';

describe('data_access.cache.block', () => {
const MIN_CACHE_SIZE = 303;
Expand All @@ -34,17 +34,15 @@ describe('data_access.cache.block', () => {

describe('add', () => {
it('should add block header to cache', () => {
// eslint-disable-next-line new-cap
const block = BlockHeaderInstance({ height: 1 });
const block = createFakeBlockHeader({ height: 1 });
blocksCache.add(block);

expect(blocksCache.items).toStrictEqual([block]);
});

it('should only contain maximum of 500 block header at given point in time', () => {
const [blocks] = Array.from({ length: 510 }, (_, i) =>
// eslint-disable-next-line new-cap
blocksCache.add(BlockHeaderInstance({ height: i })),
blocksCache.add(createFakeBlockHeader({ height: i })),
);
const blockIds = blocks.map(b => b.id);

Expand All @@ -55,14 +53,12 @@ describe('data_access.cache.block', () => {

it('should remove the least height block header and add new highest height block header', () => {
const [blocks] = Array.from({ length: 510 }, (_, i) =>
// eslint-disable-next-line new-cap
blocksCache.add(BlockHeaderInstance({ height: i })),
blocksCache.add(createFakeBlockHeader({ height: i })),
);
const maxHeight = Math.max(...blocksCache.items.map(b => b.height));
const minHeight = Math.min(...blocksCache.items.map(b => b.height));
const [lowestHeightBlock] = blocks.filter(b => b.height === minHeight);
// eslint-disable-next-line new-cap
const newBlock = BlockHeaderInstance({ height: maxHeight + 1 });
const newBlock = createFakeBlockHeader({ height: maxHeight + 1 });

expect(blocksCache.getByHeight(minHeight)).toEqual(lowestHeightBlock);

Expand All @@ -78,13 +74,11 @@ describe('data_access.cache.block', () => {

it('should only allow to insert block header with highest height', () => {
const [blocks] = Array.from({ length: 510 }, (_, i) =>
// eslint-disable-next-line new-cap
blocksCache.add(BlockHeaderInstance({ height: i })),
blocksCache.add(createFakeBlockHeader({ height: i })),
);
const minHeight = Math.min(...blocksCache.items.map(b => b.height));
const [lowestHeightBlock] = blocks.filter(b => b.height === minHeight);
// eslint-disable-next-line new-cap
const newBlock = BlockHeaderInstance({ height: minHeight + 1 });
const newBlock = createFakeBlockHeader({ height: minHeight + 1 });

expect(blocksCache.getByHeight(minHeight)).toEqual(lowestHeightBlock);

Expand All @@ -99,8 +93,7 @@ describe('data_access.cache.block', () => {
describe('remove', () => {
it('if the cache is emptied below the min cache size it should set needsRefill to true', () => {
const [blocks] = Array.from({ length: 303 }, (_, i) =>
// eslint-disable-next-line new-cap
blocksCache.add(BlockHeaderInstance({ height: i })),
blocksCache.add(createFakeBlockHeader({ height: i })),
);

blocksCache.remove(blocks[302].id);
Expand All @@ -110,22 +103,20 @@ describe('data_access.cache.block', () => {

describe('getByID', () => {
it('should return undefined if block does not exists', () => {
// eslint-disable-next-line new-cap
const block = BlockHeaderInstance({ height: 1 });
const block = createFakeBlockHeader({ height: 1 });
blocksCache.add(block);

expect(blocksCache.items).toStrictEqual([block]);
expect(blocksCache.getByID('123')).toBeUndefined();
expect(blocksCache.getByID(Buffer.from('123'))).toBeUndefined();
});

it('should return undefined if block does not exists when empty', () => {
expect(blocksCache.items).toStrictEqual([]);
expect(blocksCache.getByID('123')).toBeUndefined();
expect(blocksCache.getByID(Buffer.from('123'))).toBeUndefined();
});

it('should return the block for a given id', () => {
// eslint-disable-next-line new-cap
const block = BlockHeaderInstance({ height: 1 });
const block = createFakeBlockHeader({ height: 1 });
blocksCache.add(block);

expect(blocksCache.items).toStrictEqual([block]);
Expand All @@ -135,24 +126,24 @@ describe('data_access.cache.block', () => {

describe('getByIDs', () => {
it('should return empty array if the cache is empty', () => {
expect(blocksCache.getByIDs(['123'])).toBeEmpty();
expect(blocksCache.getByIDs([Buffer.from('123')])).toBeEmpty();
});

it('should return empty array if matching block ids does not exists', () => {
const [blocks] = Array.from({ length: 10 }, (_, i) =>
// eslint-disable-next-line new-cap
blocksCache.add(BlockHeaderInstance({ height: i })),
blocksCache.add(createFakeBlockHeader({ height: i })),
);
const blockIds = blocks.map(b => b.id);

expect(blocksCache.items).toStrictEqual(blocks);
expect(blocksCache.getByIDs([...blockIds, '111111'])).toBeEmpty();
expect(
blocksCache.getByIDs([...blockIds, Buffer.from('111111')]),
).toBeEmpty();
});

it('should return all the blocks for given block ids', () => {
const [blocks] = Array.from({ length: 10 }, (_, i) =>
// eslint-disable-next-line new-cap
blocksCache.add(BlockHeaderInstance({ height: i })),
blocksCache.add(createFakeBlockHeader({ height: i })),
);
const blockIds = blocks.map(b => b.id);

Expand All @@ -168,8 +159,7 @@ describe('data_access.cache.block', () => {

it('should return empty array if blocks does not exists between height range', () => {
const [blocks] = Array.from({ length: 10 }, (_, i) =>
// eslint-disable-next-line new-cap
blocksCache.add(BlockHeaderInstance({ height: i + 1 })),
blocksCache.add(createFakeBlockHeader({ height: i + 1 })),
);

expect(blocksCache.items).toStrictEqual(blocks);
Expand All @@ -180,8 +170,7 @@ describe('data_access.cache.block', () => {

it('should return all the blocks for given block height range', () => {
const [blocks] = Array.from({ length: 10 }, (_, i) =>
// eslint-disable-next-line new-cap
blocksCache.add(BlockHeaderInstance({ height: i + 1 })),
blocksCache.add(createFakeBlockHeader({ height: i + 1 })),
);
const heights = blocks.map(b => b.height);
const fromHeight = heights[0];
Expand Down
Loading

0 comments on commit d8b8925

Please sign in to comment.