Best JavaScript code snippet using differencify
database-service.test.ts
Source:database-service.test.ts
1import "jest-extended";2import { Container, Providers } from "@packages/core-kernel";3import { Queue } from "@packages/core-kernel/dist/contracts/kernel";4import { interfaces } from "@packages/core-kernel/dist/ioc";5import { MemoryQueue } from "@packages/core-kernel/dist/services/queue/drivers/memory";6import { LocalFilesystem } from "@packages/core-kernel/src/services/filesystem/drivers/local";7import { SnapshotDatabaseService } from "@packages/core-snapshots/src/database-service";8import { Filesystem } from "@packages/core-snapshots/src/filesystem/filesystem";9import { Identifiers } from "@packages/core-snapshots/src/ioc";10import { ProgressDispatcher } from "@packages/core-snapshots/src/progress-dispatcher";11import { BlockRepository, RoundRepository, TransactionRepository } from "@packages/core-snapshots/src/repositories";12import { Sandbox } from "@packages/core-test-framework";13import { EventEmitter } from "events";14import { dirSync, setGracefulCleanup } from "tmp";15import { Connection } from "typeorm";16import { Assets } from "./__fixtures__";17EventEmitter.prototype.constructor = Object.prototype.constructor;18let sandbox: Sandbox;19let database: SnapshotDatabaseService;20let filesystem: Filesystem;21class MockWorkerWrapper extends EventEmitter {22 public constructor() {23 super();24 }25 public async start() {26 this.emit("count", 1);27 this.emit("exit");28 }29 public async sync() {}30 public async terminate() {}31}32let mockWorkerWrapper;33jest.mock("@packages/core-snapshots/src/workers/worker-wrapper", () => {34 return {35 WorkerWrapper: jest.fn().mockImplementation(() => {36 return mockWorkerWrapper;37 }),38 };39});40const configuration = {41 chunkSize: 50000,42 dispatchUpdateStep: 1000,43 connection: {},44 cryptoPackages: [],45};46let logger;47let connection: Partial<Connection>;48let blockRepository: Partial<BlockRepository>;49let transactionRepository: Partial<TransactionRepository>;50let roundRepository: Partial<RoundRepository>;51let progressDispatcher: Partial<ProgressDispatcher>;52let eventDispatcher;53beforeEach(() => {54 mockWorkerWrapper = new MockWorkerWrapper();55 mockWorkerWrapper.sync = jest.fn().mockResolvedValue({ numberOfTransactions: 1, height: 1 });56 mockWorkerWrapper.terminate = jest.fn();57 mockWorkerWrapper.removeListener = jest.fn();58 logger = {59 info: jest.fn(),60 error: jest.fn(),61 warning: jest.fn(),62 };63 connection = {64 isConnected: true,65 };66 eventDispatcher = {67 dispatch: jest.fn(),68 };69 const lastBlock = Assets.blocksBigNumber[0];70 lastBlock.height = 100;71 blockRepository = {72 fastCount: jest.fn().mockResolvedValue(1),73 truncate: jest.fn(),74 delete: jest.fn(),75 findFirst: jest.fn().mockResolvedValue(Assets.blocksBigNumber[0] as any),76 findLast: jest.fn().mockResolvedValue(lastBlock as any),77 findByHeight: jest.fn().mockResolvedValue(lastBlock as any),78 rollback: jest.fn(),79 countInRange: jest.fn().mockResolvedValue(5),80 };81 transactionRepository = {82 fastCount: jest.fn(),83 delete: jest.fn(),84 countInRange: jest.fn().mockResolvedValue(5),85 };86 roundRepository = {87 fastCount: jest.fn(),88 delete: jest.fn(),89 countInRange: jest.fn().mockResolvedValue(5),90 };91 progressDispatcher = {92 start: jest.fn(),93 end: jest.fn(),94 update: jest.fn(),95 };96 sandbox = new Sandbox();97 sandbox.app.bind(Container.Identifiers.LogService).toConstantValue(logger);98 sandbox.app.bind(Container.Identifiers.DatabaseConnection).toConstantValue(connection);99 sandbox.app.bind(Container.Identifiers.ApplicationNetwork).toConstantValue("testnet");100 sandbox.app.bind(Container.Identifiers.FilesystemService).to(LocalFilesystem).inSingletonScope();101 sandbox.app.bind(Identifiers.SnapshotFilesystem).to(Filesystem).inSingletonScope();102 sandbox.app.bind(Identifiers.SnapshotBlockRepository).toConstantValue(blockRepository);103 sandbox.app.bind(Identifiers.SnapshotTransactionRepository).toConstantValue(transactionRepository);104 sandbox.app.bind(Identifiers.SnapshotRoundRepository).toConstantValue(roundRepository);105 sandbox.app.bind(Identifiers.ProgressDispatcher).toConstantValue(progressDispatcher);106 sandbox.app.bind(Identifiers.SnapshotVersion).toConstantValue("3.0.0-next.0");107 sandbox.app.bind(Container.Identifiers.EventDispatcherService).toConstantValue(eventDispatcher);108 sandbox.app109 .bind(Container.Identifiers.QueueFactory)110 .toFactory((context: interfaces.Context) => async <K, T>(name?: string): Promise<Queue> =>111 sandbox.app.resolve<Queue>(MemoryQueue).make(),112 );113 sandbox.app.bind(Identifiers.SnapshotDatabaseService).to(SnapshotDatabaseService).inSingletonScope();114 sandbox.app.bind(Container.Identifiers.PluginConfiguration).to(Providers.PluginConfiguration).inSingletonScope();115 sandbox.app116 .get<Providers.PluginConfiguration>(Container.Identifiers.PluginConfiguration)117 .from("@arkecosystem/core-snapshots", configuration);118 database = sandbox.app.get<SnapshotDatabaseService>(Identifiers.SnapshotDatabaseService);119 filesystem = sandbox.app.get<Filesystem>(Identifiers.SnapshotFilesystem);120});121afterEach(() => {122 setGracefulCleanup();123});124describe("DatabaseService", () => {125 describe("init", () => {126 it("should be ok", async () => {127 database.init("default", false, false);128 });129 it("should be ok with default parameters", async () => {130 database.init();131 });132 });133 describe("truncate", () => {134 it("should call delete and clear method on transaction, block and round", async () => {135 await expect(database.truncate()).toResolve();136 expect(blockRepository.truncate).toHaveBeenCalled();137 });138 });139 describe("getLastBlock", () => {140 it("should return block", async () => {141 await expect(database.getLastBlock()).toResolve();142 });143 });144 describe("rollbackChain", () => {145 it("should rollback chain to specific height", async () => {146 const roundInfo = {147 round: 1,148 nextRound: 2,149 maxDelegates: 51,150 roundHeight: 1,151 };152 await expect(database.rollback(roundInfo)).toResolve();153 });154 });155 describe("dump", () => {156 it("should resolve", async () => {157 const dir: string = dirSync().name;158 const subdir: string = `${dir}/sub`;159 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);160 const dumpOptions = {161 network: "testnet",162 skipCompression: false,163 codec: "default",164 };165 // await expect(database.dump(dumpOptions)).toResolve();166 await database.dump(dumpOptions);167 });168 it("should throw error if last block is not found", async () => {169 blockRepository.findLast = jest.fn().mockResolvedValue(undefined);170 const dir: string = dirSync().name;171 const subdir: string = `${dir}/sub`;172 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);173 const dumpOptions = {174 network: "testnet",175 skipCompression: false,176 codec: "default",177 };178 const promise = database.dump(dumpOptions);179 await expect(promise).rejects.toThrow();180 });181 it("should throw error if last and first block are in same range", async () => {182 blockRepository.findLast = jest.fn().mockResolvedValue(Assets.blocks[0]);183 const dir: string = dirSync().name;184 const subdir: string = `${dir}/sub`;185 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);186 const spyOnDeleteSnapshot = jest.spyOn(filesystem, "deleteSnapshot");187 const dumpOptions = {188 network: "testnet",189 skipCompression: false,190 codec: "default",191 };192 const promise = database.dump(dumpOptions);193 await expect(promise).rejects.toThrow();194 expect(spyOnDeleteSnapshot).toHaveBeenCalled();195 });196 it("should throw error if error in worker", async () => {197 const dir: string = dirSync().name;198 const subdir: string = `${dir}/sub`;199 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);200 const spyOnDeleteSnapshot = jest.spyOn(filesystem, "deleteSnapshot");201 const dumpOptions = {202 network: "testnet",203 skipCompression: false,204 codec: "default",205 };206 mockWorkerWrapper.start = jest.fn().mockRejectedValue(new Error());207 const promise = database.dump(dumpOptions);208 await expect(promise).rejects.toThrow();209 expect(spyOnDeleteSnapshot).toHaveBeenCalled();210 });211 });212 describe("restore", () => {213 it("should resolve", async () => {214 const dir: string = dirSync().name;215 const subdir: string = `${dir}/sub`;216 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);217 const promise = database.restore(Assets.metaData, { truncate: true });218 await expect(promise).toResolve();219 });220 it("should resolve without truncate", async () => {221 const dir: string = dirSync().name;222 const subdir: string = `${dir}/sub`;223 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);224 const promise = database.restore(Assets.metaData, { truncate: false });225 await expect(promise).toResolve();226 });227 it("should resolve with empty result", async () => {228 const dir: string = dirSync().name;229 const subdir: string = `${dir}/sub`;230 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);231 mockWorkerWrapper.sync = jest.fn();232 const promise = database.restore(Assets.metaData, { truncate: true });233 await expect(promise).toResolve();234 });235 it("should throw error if error in worker", async () => {236 const dir: string = dirSync().name;237 const subdir: string = `${dir}/sub`;238 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);239 mockWorkerWrapper.start = jest.fn().mockRejectedValue(new Error());240 const promise = database.restore(Assets.metaData, { truncate: true });241 await expect(promise).rejects.toThrow();242 });243 it("should throw error if error on sync in blocks worker", async () => {244 const dir: string = dirSync().name;245 const subdir: string = `${dir}/sub`;246 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);247 mockWorkerWrapper.sync = jest.fn().mockRejectedValueOnce(new Error("Blocks error"));248 const promise = database.restore(Assets.metaData, { truncate: true });249 await expect(promise).rejects.toThrow("Blocks error");250 });251 it("should throw error if error on sync in transactions worker", async () => {252 const dir: string = dirSync().name;253 const subdir: string = `${dir}/sub`;254 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);255 mockWorkerWrapper.sync = jest256 .fn()257 .mockResolvedValueOnce({ numberOfTransactions: 1, height: 1 })258 .mockRejectedValueOnce(new Error("Transactions error"));259 const promise = database.restore(Assets.metaData, { truncate: true });260 await expect(promise).rejects.toThrow("Transactions error");261 });262 it("should throw error if error on sync in rounds worker", async () => {263 const dir: string = dirSync().name;264 const subdir: string = `${dir}/sub`;265 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);266 mockWorkerWrapper.sync = jest267 .fn()268 .mockResolvedValueOnce({ numberOfTransactions: 1, height: 1 })269 .mockResolvedValueOnce({})270 .mockRejectedValueOnce(new Error("Rounds error"));271 const promise = database.restore(Assets.metaData, { truncate: true });272 await expect(promise).rejects.toThrow("Rounds error");273 });274 });275 describe("verify", () => {276 it("should resolve", async () => {277 const dir: string = dirSync().name;278 const subdir: string = `${dir}/sub`;279 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);280 const promise = database.verify(Assets.metaData);281 await expect(promise).toResolve();282 });283 it("should throw error if error in worker", async () => {284 const dir: string = dirSync().name;285 const subdir: string = `${dir}/sub`;286 filesystem.getSnapshotPath = jest.fn().mockReturnValue(subdir);287 mockWorkerWrapper.start = jest.fn().mockRejectedValue(new Error());288 const promise = database.verify(Assets.metaData);289 await expect(promise).rejects.toThrow();290 });291 });...
filesystem.ts
Source:filesystem.ts
...7 private snapshot?: string;8 public setSnapshot(snapshot: string): void {9 this.snapshot = snapshot;10 }11 public getSnapshotPath(): string {12 Utils.assert.defined<string>(this.snapshot);13 return `${process.env.CORE_PATH_DATA}/snapshots/${this.snapshot}/`;14 }15 public async deleteSnapshot(): Promise<void> {16 await this.filesystem.delete(this.getSnapshotPath());17 }18 public async snapshotExists(): Promise<boolean> {19 return this.filesystem.exists(this.getSnapshotPath());20 }21 public async prepareDir(): Promise<void> {22 await this.filesystem.makeDirectory(this.getSnapshotPath());23 }24 public async writeMetaData(meta: Meta.MetaData): Promise<void> {25 await this.filesystem.put(`${this.getSnapshotPath()}meta.json`, JSON.stringify(meta));26 }27 public async readMetaData(): Promise<Meta.MetaData> {28 const buffer = await this.filesystem.get(`${this.getSnapshotPath()}meta.json`);29 const meta = JSON.parse(buffer.toString());30 this.validateMetaData(meta);31 return meta;32 }33 private validateMetaData(meta: Meta.MetaData): void {34 Utils.assert.defined(meta.codec);35 Utils.assert.defined(meta.skipCompression);36 Utils.assert.defined(meta.blocks?.count);37 Utils.assert.defined(meta.transactions?.count);38 Utils.assert.defined(meta.rounds?.count);39 }...
Using AI Code Generation
1import { getSnapshotPath } from 'differencify'2import { getSnapshotPath } from 'jest-image-snapshot/src/utils'3import { getSnapshotPath } from 'jest-screenshot/src/utils'4import { getSnapshotPath } from 'differencify'5import { getSnapshotPath } from 'jest-image-snapshot/src/utils'6import { getSnapshotPath } from 'jest-screenshot/src/utils'7import { getSnapshotPath } from 'differencify'8import { getSnapshotPath } from 'jest-image-snapshot/src/utils'9import { getSnapshotPath } from 'jest-screenshot/src/utils'10import { getSnapshotPath } from 'differencify'11import { getSnapshotPath } from 'jest-image-snapshot/src/utils'12import { getSnapshotPath } from 'jest-screenshot/src/utils'13import { getSnapshotPath } from 'differencify'14import { getSnapshotPath } from 'jest-image-snapshot/src/utils'15import { getSnapshotPath } from 'jest-screenshot/src/utils'16import { getSnapshotPath } from 'differencify'17import { getSnapshotPath } from 'jest-image-snapshot/src/utils'18import { get
Using AI Code Generation
1const differencify = require('differencify');2const { getSnapshotPath } = differencify;3const path = require('path');4const snapshotPath = getSnapshotPath(path.resolve(__dirname, 'test.js'));5console.log(snapshotPath);6const differencify = require('differencify');7const { getSnapshotPath } = differencify;8const path = require('path');9const snapshotPath = getSnapshotPath(path.resolve(__dirname, 'test.spec.js'));10console.log(snapshotPath);11const differencify = require('differencify');12const { getSnapshotPath } = differencify;13const path = require('path');14const snapshotPath = getSnapshotPath(path.resolve(__dirname, 'test.test.js'));15console.log(snapshotPath);16const differencify = require('differencify');17const { getSnapshotPath } = differencify;18const path = require('path');19const snapshotPath = getSnapshotPath(path.resolve(__dirname, 'test.test.ts'));20console.log(snapshotPath);21const differencify = require('differencify');22const { getSnapshotPath } = differencify;23const path = require('path');24const snapshotPath = getSnapshotPath(path.resolve(__dirname, 'test.spec.ts'));25console.log(snapshotPath);26const differencify = require('differencify');27const { getSnapshotPath } = differenc
Using AI Code Generation
1const {getSnapshotPath} = require('differencify');2const path = require('path');3describe('test', () => {4 it('test', () => {5 const snapshotPath = getSnapshotPath(path.join(__dirname, 'test.png'));6 console.log(snapshotPath);7 });8});9const {getSnapshotPath} = require('differencify');10const path = require('path');11const fs = require('fs');12describe('test', () => {13 it('test', () => {14 const snapshotPath = getSnapshotPath(path.join(__dirname, 'test.png'));15 fs.writeFileSync(snapshotPath, 'image data');16 });17});
Using AI Code Generation
1const {getSnapshotPath} = require('differencify');2const path = require('path');3describe('test', () => {4 it('test', async () => {5 const snapshotPath = getSnapshotPath(path.join(__dirname, 'test.js'), 'test', 'test');6 console.log(snapshotPath);7 });8});9const {getSnapshotPath} = require('differencify');10const path = require('path');11describe('test', () => {12 it('test', async () => {13 const snapshotPath = getSnapshotPath(path.join(__dirname, 'test.js'), 'test', 'test');14 await page.screenshot({path: snapshotPath});15 });16});17const snapshotPath = getSnapshotPath(path.join(__dirname, 'test.js'), 'test', 'test');18await page.screenshot({path: snapshotPath});19const snapshotPath = getSnapshotPath(path.join(__dirname, 'test.js'), 'test', 'test');20await page.screenshot({path: snapshotPath});21expect(snapshotPath).toMatchSnapshot();22const {getSnapshotPath} = require('differencify');23const path = require('path');24describe('test', () => {25 it('test', async () => {26 const snapshotPath = getSnapshotPath(path.join(__dirname, 'test.js'), 'test', 'test');27 await page.screenshot({path: snapshotPath});
Using AI Code Generation
1import { getSnapshotPath } from 'differencify'2import { getSnapshotPath } from 'differencify'3const snapshotPath = getSnapshotPath('mySnapshotName');4import { getSnapshotPath } from 'differencify'5const snapshotPath = getSnapshotPath('mySnapshotName');6import { getSnapshotPath } from 'differencify'7const snapshotPath = getSnapshotPath('mySnapshotName');8import { getSnapshotPath } from 'differencify'9const snapshotPath = getSnapshotPath('mySnapshotName');10import { getSnapshotPath } from 'differencify'11const snapshotPath = getSnapshotPath('mySnapshotName');12import { getSnapshotPath } from 'differencify'13const snapshotPath = getSnapshotPath('mySnapshotName');14import { getSnapshotPath } from 'differencify'15const snapshotPath = getSnapshotPath('mySnapshotName');16import { getSnapshotPath } from 'differencify'17const snapshotPath = getSnapshotPath('mySnapshotName');18import { getSnapshotPath } from 'differencify'19const snapshotPath = getSnapshotPath('mySnapshotName');20import { getSnapshotPath } from 'differencify'21const snapshotPath = getSnapshotPath('mySnapshotName');22import { getSnapshotPath } from 'differencify'23const snapshotPath = getSnapshotPath('mySnapshotName');
Using AI Code Generation
1const { getSnapshotPath } = require("differencify");2const snapshotPath = getSnapshotPath("test.js");3console.log(snapshotPath);4const { getSnapshotPath } = require("differencify");5const snapshotPath = getSnapshotPath("test.js", "test1");6console.log(snapshotPath);7const { getSnapshotPath } = require("differencify");8const snapshotPath = getSnapshotPath("test.js", "test1", "test2");9console.log(snapshotPath);10const { getSnapshotPath } = require("differencify");11const snapshotPath = getSnapshotPath("test.js", "test1", "test2", "test3");12console.log(snapshotPath);13const { getSnapshotPath } = require("differencify");14const snapshotPath = getSnapshotPath("test.js", "test1", "test2", "test3", "test4");15console.log(snapshotPath);16const { getSnapshotPath } = require("differencify");17const snapshotPath = getSnapshotPath("test.js", "test1", "test2", "test3", "test4", "test5");18console.log(snapshotPath);
Using AI Code Generation
1const { getSnapshotPath } = require('differencify');2const snapshotPath = getSnapshotPath('testImage');3await page.screenshot({ path: snapshotPath });4const { pass } = await page.compareScreenshot(snapshotPath);5await browser.close();6process.exit(pass ? 0 : 1);
Using AI Code Generation
1const { getSnapshotPath } = require('differencify');2const snapshotPath = getSnapshotPath('test.js', 'test name');3console.log(snapshotPath);4const { getSnapshotPath } = require('differencify');5const snapshotPath = getSnapshotPath('test.spec.js', 'test name');6console.log(snapshotPath);7const { getSnapshotPath } = require('differencify');8const snapshotPath = getSnapshotPath('test.test.js', 'test name');9console.log(snapshotPath);10const { getSnapshotPath } = require('differencify');11const snapshotPath = getSnapshotPath('test.spec.ts', 'test name');12console.log(snapshotPath);13const { getSnapshotPath } = require('differencify');14const snapshotPath = getSnapshotPath('test.test.ts', 'test name');15console.log(snapshotPath);16const { getSnapshotPath } = require('differencify');17const snapshotPath = getSnapshotPath('test.ts', 'test name');18console.log(snapshotPath);19const { getSnapshotPath } = require('differencify');20const snapshotPath = getSnapshotPath('test.test.tsx', 'test name');21console.log(snapshotPath);
Using AI Code Generation
1const { getSnapshotPath } = require('differencify');2const snapshotPath = getSnapshotPath('test.js');3console.log(snapshotPath);4const { getSnapshotPath } = require('differencify');5const snapshotPath = getSnapshotPath('test.js');6console.log(snapshotPath);7const { getSnapshotPath } = require('differencify');8const snapshotPath = getSnapshotPath('test.js');9console.log(snapshotPath);10const { getSnapshotPath } = require('differencify');11const snapshotPath = getSnapshotPath('test.js');12console.log(snapshotPath);13const { getSnapshotPath } = require('differencify');14const snapshotPath = getSnapshotPath('test.js');15console.log(snapshotPath);16const { getSnapshotPath } = require('differencify');17const snapshotPath = getSnapshotPath('test.js');18console.log(snapshotPath);19const { getSnapshotPath } = require('differencify');20const snapshotPath = getSnapshotPath('test.js');21console.log(snapshotPath);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!