Skip to content

Commit

Permalink
test(core-snapshot): increase coverage to 100% (#3859)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner authored Jun 30, 2020
1 parent 3467289 commit 2bb51a2
Show file tree
Hide file tree
Showing 20 changed files with 341 additions and 183 deletions.
67 changes: 41 additions & 26 deletions __tests__/unit/core-snapshots/database-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import "jest-extended";
import { dirSync, setGracefulCleanup } from "tmp";

// @ts-ignore
import * as workerThreads from "worker_threads";
import { EventEmitter } from "events";
import { Connection } from "typeorm";
import { Container, Providers } from "@packages/core-kernel";
import { Sandbox } from "@packages/core-test-framework";
import { LocalFilesystem } from "@packages/core-kernel/src/services/filesystem/drivers/local";
import { SnapshotDatabaseService } from "@packages/core-snapshots/src/database-service";
import { Identifiers } from "@packages/core-snapshots/src/ioc";
import { Filesystem } from "@packages/core-snapshots/src/filesystem/filesystem";
import { LocalFilesystem } from "@packages/core-kernel/src/services/filesystem/drivers/local";
import { Identifiers } from "@packages/core-snapshots/src/ioc";
import { ProgressDispatcher } from "@packages/core-snapshots/src/progress-dispatcher";
import { BlockRepository, TransactionRepository, RoundRepository } from "@packages/core-snapshots/src/repositories";
import { BlockRepository, RoundRepository, TransactionRepository } from "@packages/core-snapshots/src/repositories";
import { Sandbox } from "@packages/core-test-framework";
import { EventEmitter } from "events";
import { dirSync, setGracefulCleanup } from "tmp";
import { Connection } from "typeorm";
// @ts-ignore
import * as workerThreads from "worker_threads";

import { Assets } from "./__fixtures__";

Expand Down Expand Up @@ -72,7 +72,7 @@ beforeEach(() => {
isConnected: true,
};

let lastBlock = Assets.blocksBigNumber[0];
const lastBlock = Assets.blocksBigNumber[0];
lastBlock.height = 100;

blockRepository = {
Expand Down Expand Up @@ -132,7 +132,7 @@ beforeEach(() => {

sandbox.app.bind(Identifiers.SnapshotDatabaseService).to(SnapshotDatabaseService).inSingletonScope();

let pluginConfiguration = sandbox.app.getTagged<Providers.PluginConfiguration>(
const pluginConfiguration = sandbox.app.getTagged<Providers.PluginConfiguration>(
Container.Identifiers.PluginConfiguration,
"plugin",
"@arkecosystem/core-snapshots",
Expand All @@ -153,7 +153,11 @@ afterEach(() => {
describe("DatabaseService", () => {
describe("init", () => {
it("should be ok", async () => {
database.init("default", false);
database.init("default", false, false);
});

it("should be ok with default parameters", async () => {
database.init();
});
});

Expand All @@ -175,7 +179,7 @@ describe("DatabaseService", () => {

describe("rollbackChain", () => {
it("should rollback chain to specific height", async () => {
let roundInfo = {
const roundInfo = {
round: 1,
nextRound: 2,
maxDelegates: 51,
Expand All @@ -193,13 +197,13 @@ describe("DatabaseService", () => {

filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);

let dumpOptions = {
const dumpOptions = {
network: "testnet",
skipCompression: false,
codec: "default",
};

let promise = database.dump(dumpOptions);
const promise = database.dump(dumpOptions);

await expect(promise).toResolve();
});
Expand All @@ -212,13 +216,13 @@ describe("DatabaseService", () => {

filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);

let dumpOptions = {
const dumpOptions = {
network: "testnet",
skipCompression: false,
codec: "default",
};

let promise = database.dump(dumpOptions);
const promise = database.dump(dumpOptions);

await expect(promise).rejects.toThrow();
});
Expand All @@ -231,13 +235,13 @@ describe("DatabaseService", () => {

filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);

let dumpOptions = {
const dumpOptions = {
network: "testnet",
skipCompression: false,
codec: "default",
};

let promise = database.dump(dumpOptions);
const promise = database.dump(dumpOptions);

await expect(promise).rejects.toThrow();
});
Expand All @@ -248,15 +252,15 @@ describe("DatabaseService", () => {

filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);

let dumpOptions = {
const dumpOptions = {
network: "testnet",
skipCompression: false,
codec: "default",
};

mockWorkerWrapper.start = jest.fn().mockRejectedValue(new Error());

let promise = database.dump(dumpOptions);
const promise = database.dump(dumpOptions);

await expect(promise).rejects.toThrow();
});
Expand All @@ -269,7 +273,18 @@ describe("DatabaseService", () => {

filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);

let promise = database.restore(Assets.metaData, { truncate: true });
const promise = database.restore(Assets.metaData, { truncate: true });

await expect(promise).toResolve();
});

it("should resolve without truncate", async () => {
const dir: string = dirSync().name;
const subdir: string = `${dir}/sub`;

filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);

const promise = database.restore(Assets.metaData, { truncate: false });

await expect(promise).toResolve();
});
Expand All @@ -282,7 +297,7 @@ describe("DatabaseService", () => {

mockWorkerWrapper.sync = jest.fn();

let promise = database.restore(Assets.metaData, { truncate: true });
const promise = database.restore(Assets.metaData, { truncate: true });

await expect(promise).toResolve();
});
Expand All @@ -295,7 +310,7 @@ describe("DatabaseService", () => {

mockWorkerWrapper.start = jest.fn().mockRejectedValue(new Error());

let promise = database.restore(Assets.metaData, { truncate: true });
const promise = database.restore(Assets.metaData, { truncate: true });

await expect(promise).rejects.toThrow();
});
Expand All @@ -308,7 +323,7 @@ describe("DatabaseService", () => {

filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);

let promise = database.verify(Assets.metaData);
const promise = database.verify(Assets.metaData);

await expect(promise).toResolve();
});
Expand All @@ -321,7 +336,7 @@ describe("DatabaseService", () => {

mockWorkerWrapper.start = jest.fn().mockRejectedValue(new Error());

let promise = database.verify(Assets.metaData);
const promise = database.verify(Assets.metaData);

await expect(promise).rejects.toThrow();
});
Expand Down
86 changes: 82 additions & 4 deletions __tests__/unit/core-snapshots/filesystem/filesystem.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import "jest-extended";
import { dirSync, setGracefulCleanup } from "tmp";

import { Container } from "@arkecosystem/core-kernel";
import { Sandbox } from "@packages/core-test-framework";
import { Identifiers } from "@packages/core-snapshots/src/ioc";
import { Filesystem } from "@packages/core-snapshots/src/filesystem/filesystem";
import { LocalFilesystem } from "@packages/core-kernel/src/services/filesystem/drivers/local";
import { Filesystem } from "@packages/core-snapshots/src/filesystem/filesystem";
import { Identifiers } from "@packages/core-snapshots/src/ioc";
import { Sandbox } from "@packages/core-test-framework";
import { cloneDeep } from "lodash";
import { dirSync, setGracefulCleanup } from "tmp";

import { metaData } from "../__fixtures__/assets";

Expand Down Expand Up @@ -68,4 +69,81 @@ describe("Filesystem", () => {
await expect(filesystem.readMetaData()).resolves.toEqual(metaData);
});
});

describe("validateMetaData", () => {
let tmpMeta;

beforeEach(() => {
const dir: string = dirSync().name;
const subdir: string = `${dir}/sub`;

filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);

tmpMeta = cloneDeep(metaData);
});

it("should throw if codec is missing", async () => {
delete tmpMeta.codec;

await expect(filesystem.writeMetaData(tmpMeta)).toResolve();

await expect(filesystem.readMetaData()).rejects.toThrowError();
});

it("should throw if skipCompression is missing", async () => {
delete tmpMeta.skipCompression;

await expect(filesystem.writeMetaData(tmpMeta)).toResolve();

await expect(filesystem.readMetaData()).rejects.toThrowError();
});

it("should throw if blocks is missing", async () => {
delete tmpMeta.blocks;

await expect(filesystem.writeMetaData(tmpMeta)).toResolve();

await expect(filesystem.readMetaData()).rejects.toThrowError();
});

it("should throw if blocks.count is missing", async () => {
delete tmpMeta.blocks.count;

await expect(filesystem.writeMetaData(tmpMeta)).toResolve();

await expect(filesystem.readMetaData()).rejects.toThrowError();
});

it("should throw if transactions is missing", async () => {
delete tmpMeta.transactions;

await expect(filesystem.writeMetaData(tmpMeta)).toResolve();

await expect(filesystem.readMetaData()).rejects.toThrowError();
});

it("should throw if transactions.count is missing", async () => {
delete tmpMeta.transactions.count;

await expect(filesystem.writeMetaData(tmpMeta)).toResolve();

await expect(filesystem.readMetaData()).rejects.toThrowError();
});

it("should throw if rounds is missing", async () => {
delete tmpMeta.rounds;

await expect(filesystem.writeMetaData(tmpMeta)).toResolve();

await expect(filesystem.readMetaData()).rejects.toThrowError();
});

it("should throw if rounds.count is missing", async () => {
delete tmpMeta.rounds.count;

await expect(filesystem.writeMetaData(tmpMeta)).toResolve();

await expect(filesystem.readMetaData()).rejects.toThrowError();
});
});
});
Loading

0 comments on commit 2bb51a2

Please sign in to comment.