Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
yoozo committed Nov 21, 2024
1 parent 24340e5 commit cd2375b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 21 deletions.
3 changes: 2 additions & 1 deletion packages/node-core/src/indexer/poi/poi.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Sequelize, Transaction} from '@subql/x-sequelize';
import {NodeConfig} from '../../configure';
import {ProofOfIndex} from '../entities/Poi.entity';
import {StoreCacheService} from '../storeModelProvider';
import {METADATA_ENTITY_NAME} from '../storeModelProvider/metadata/utils';
import {PoiService} from './poi.service';

jest.mock('@subql/x-sequelize', () => {
Expand Down Expand Up @@ -154,7 +155,7 @@ describe('PoiService', () => {
} as any;

await service.rewind(targetBlockHeight, transaction);
expect(storeCache.metadata.bulkRemove).toHaveBeenCalledWith(['lastCreatedPoiHeight']);
expect(storeCache.metadata.bulkRemove).toHaveBeenCalledWith(['lastCreatedPoiHeight'], transaction);
});
});

Expand Down
28 changes: 18 additions & 10 deletions packages/node-core/src/indexer/project.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {NodeConfig, ProjectUpgradeService} from '../configure';
import {BaseDsProcessorService} from './ds-processor.service';
import {DynamicDsService} from './dynamic-ds.service';
import {BaseProjectService} from './project.service';
import {StoreService} from './store.service';
import {Header, ISubqueryProject} from './types';
import {
BaseUnfinalizedBlocksService,
Expand Down Expand Up @@ -309,26 +310,33 @@ describe('BaseProjectService', () => {
init: jest.fn(),
initCoreTables: jest.fn(),
historical: true,
storeCache: {
modelProvider: {
metadata: {
findMany: jest.fn(() => ({})),
find: jest.fn((key: string) => {
findMany: jest.fn(async () => Promise.resolve({})),
find: jest.fn(async (key: string): Promise<any> => {
let result: any;
switch (key) {
case METADATA_LAST_FINALIZED_PROCESSED_KEY:
return lastFinalizedHeight;
result = lastFinalizedHeight;
break;
case METADATA_UNFINALIZED_BLOCKS_KEY:
return JSON.stringify(unfinalizedBlocks);
result = JSON.stringify(unfinalizedBlocks);
break;
case 'lastProcessedHeight':
return startBlock - 1;
result = startBlock - 1;
break;
case 'deployments':
return JSON.stringify({1: '1'});
result = JSON.stringify({1: '1'});
break;
default:
return undefined;
result = undefined;
break;
}
return Promise.resolve(result);
}),
set: jest.fn(),
flush: jest.fn(),
},
} as any,
resetCache: jest.fn(),
flushCache: jest.fn(),
_flushCache: jest.fn(),
Expand Down Expand Up @@ -359,7 +367,7 @@ describe('BaseProjectService', () => {
resetDynamicDatasource: jest.fn(),
} as unknown as DynamicDsService<any>, // dynamicDsService
new EventEmitter2(), // eventEmitter
new TestUnfinalizedBlocksService(nodeConfig, storeService.storeCache) // unfinalizedBlocksService
new TestUnfinalizedBlocksService(nodeConfig, storeService.modelProvider) // unfinalizedBlocksService
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ describe('cacheModel', () => {
testModel = new CachedModel(sequelize.model('entity1'), true, {} as NodeConfig, () => i++);
});

it('throws when trying to set undefined', () => {
expect(() => testModel.set('0x01', undefined as any, 1)).toThrow();
expect(() => testModel.set('0x01', null as any, 1)).toThrow();
it('throws when trying to set undefined', async () => {
await expect(() => testModel.set('0x01', undefined as any, 1)).rejects.toThrow();
await expect(() => testModel.set('0x01', null as any, 1)).rejects.toThrow();
});

// it should keep same behavior as hook we used
Expand Down Expand Up @@ -287,7 +287,7 @@ describe('cacheModel', () => {
},
1
);
const result = testModel.get('entity1_id_0x01');
const result = await testModel.get('entity1_id_0x01');
// data should be erased from removeCache
expect((testModel as any).removeCache.entity1_id_0x01).toBeUndefined();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ export class CachedModel<T extends BaseEntity = BaseEntity>
// Experimental, this means getCache keeps duplicate data from setCache,
// we can remove this once memory is too full.
this.getCache.set(id, copiedData);
// Handle remove cache, when removed data been created again
if (this.removeCache[id] && this.removeCache[id].removedAtBlock === blockHeight) {
delete this.removeCache[id];
}

this.flushableRecordCounter += 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {CacheMetadataModel} from './metadata';
import {METADATA_ENTITY_NAME} from './metadata/utils';
import {CachedModel} from './model';
import {CachePoiModel, POI_ENTITY_NAME} from './poi';
import {Exporter, ICachedModelControl, IStoreModelProvider} from './types';
import {ICachedModelControl, IStoreModelProvider} from './types';

const logger = getLogger('StoreCacheService');

Expand Down
2 changes: 1 addition & 1 deletion packages/node-core/src/indexer/test.runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('TestRunner', () => {
(testRunner as any).storeService = {
getStore: () => storeMock,
setBlockHeight: jest.fn(),
storeCache: mockStoreCache,
modelProvider: mockStoreCache,
} as any;

await testRunner.runTest(testMock, sandboxMock, indexBlock);
Expand Down
6 changes: 3 additions & 3 deletions packages/node-core/src/indexer/test.runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export class TestRunner<A, SA, B, DS> {
logger.debug('Fetching block');
const [block] = await this.apiService.fetchBlocks([test.blockHeight]);

this.storeService.setBlockHeight(test.blockHeight);
await this.storeService.setBlockHeight(test.blockHeight);
// Ensure a block height is set so that data is flushed correctly
this.storeService.modelProvider.metadata.set('lastProcessedHeight', test.blockHeight - 1);
await this.storeService.modelProvider.metadata.set('lastProcessedHeight', test.blockHeight - 1);
const store = this.storeService.getStore();
sandbox.freeze(store, 'store');

Expand Down Expand Up @@ -99,7 +99,7 @@ export class TestRunner<A, SA, B, DS> {
} else {
Object.keys(actualEntity).forEach((attr) => {
// EntityClass has private store on it, don't need to check it.
if (attr === 'store') return;
if (attr === '#store') return;

const expectedAttr = (expectedEntity as Record<string, any>)[attr] ?? null;
const actualAttr = (actualEntity as Record<string, any>)[attr] ?? null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('SchemaMigration integration tests', () => {

projectService = app.get('IProjectService');
const projectUpgradeService = app.get('IProjectUpgradeService');
const storeCache = app.get(StoreCacheService);
const storeCache = app.get('IStoreModelProvider');
const cacheSpy = jest.spyOn(storeCache, 'updateModels');
const apiService = app.get(ApiService);

Expand Down

0 comments on commit cd2375b

Please sign in to comment.