watcher-ts/packages/uni-info-watcher/test/test-db.ts
Ashwin Phatak 9e1ed70d18
Tests for getPrevEntityVersion (#217)
* Insert and remove a dummy BlockProgress from db.

* Test to fetch a Token entity from pruned region.

* Check if db is empty initially.

* Test to fetch a Token entity from frothy region.

* Test to fetch a Token from frothy region (same block num).

* Test to fetch a Token entity from another branch.

* Tests to fetch a Token entity (multiple tokens).

* Test to fetch updated Token entity in pruned region.

Co-authored-by: prathamesh0 <prathamesh.musale0@gmail.com>
2021-08-16 15:42:34 +05:30

37 lines
1.2 KiB
TypeScript

//
// Copyright 2021 Vulcanize, Inc.
//
import { QueryRunner, FindConditions } from 'typeorm';
import { Database } from '../src/database';
import { BlockProgress } from '../src/entity/BlockProgress';
import { Token } from '../src/entity/Token';
export class TestDatabase extends Database {
async removeEntities<Entity> (queryRunner: QueryRunner, entity: new () => Entity, findConditions?: FindConditions<Entity>): Promise<void> {
const repo = queryRunner.manager.getRepository(entity);
const entities = await repo.find(findConditions);
await repo.remove(entities);
}
async isEmpty (): Promise<boolean> {
const dbTx = await this.createTransactionRunner();
try {
const dataBP = await this.getEntities(dbTx, BlockProgress, {}, {}, { limit: 1 });
const dataToken = await this.getEntities(dbTx, Token, {}, {}, { limit: 1 });
const dataSyncStatus = await this.getSyncStatus(dbTx);
if (dataBP.length > 0 || dataToken.length > 0 || dataSyncStatus) {
return false;
}
return true;
} catch (error) {
await dbTx.rollbackTransaction();
throw error;
} finally {
await dbTx.release();
}
}
}