From 0ca3e240cc0d3c57726df733e60a479258d6c835 Mon Sep 17 00:00:00 2001 From: Roman Meszaros Date: Tue, 18 Oct 2022 13:41:49 +0200 Subject: [PATCH] FIX TEST Multiple files cross-contamination extraction --- src/extractor/fileExtractor.ts | 18 ++++++++++++++++++ test/services/zipToSQLite.test.ts | 24 ++++++++++++++++-------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/extractor/fileExtractor.ts b/src/extractor/fileExtractor.ts index 9556a82..a389a29 100644 --- a/src/extractor/fileExtractor.ts +++ b/src/extractor/fileExtractor.ts @@ -90,4 +90,22 @@ export class FileExtractor { database.createTable(this.serviceName, this.table); } } + + /** + * Used mainly for testing. When multiple files for the same service are + * processed needs to dump the extractors data between different files. + * @param serviceName + */ + static dumpExtractorData(serviceName: string) { + if (!FileExtractor.registeredExtractors[serviceName]) { + throw new Error(`Extractors for service ${serviceName} do not exist`); + } + + // eslint-disable-next-line no-restricted-syntax + for (const extractorEntry of FileExtractor.registeredExtractors[ + serviceName + ]) { + extractorEntry.extractor.table.rows = []; + } + } } diff --git a/test/services/zipToSQLite.test.ts b/test/services/zipToSQLite.test.ts index 09a021d..7fdbcda 100644 --- a/test/services/zipToSQLite.test.ts +++ b/test/services/zipToSQLite.test.ts @@ -1,5 +1,6 @@ import * as zip from "@zip.js/zip.js"; +import { FileExtractor } from "../../src/extractor/fileExtractor"; import { zipToSQLiteInstance } from "../../src/services/zipToSQLite"; import { deleteFile, @@ -56,14 +57,21 @@ describe("services/zipExporter", () => { expect(fileExists(`zip/${serviceName}/${testFile}.sqlite`)).toBeTruthy(); }; - const testPromises: Promise[] = []; - Object.entries(testFiles).forEach((service: [string, string[]]) => { - const serviceName = service[0]; - const testFilesForService = service[1]; - testFilesForService.forEach((testFile: string) => { - testPromises.push(runTestAgainstFile(serviceName, testFile)); - }); - }); + const testPromises = Object.entries(testFiles).map( + async (service: [string, string[]]) => { + const serviceName = service[0]; + const testFilesForService = service[1]; + + // Extracting data from different zip files of the same service type + // needs to be done in sequence. Extractor table data needs to be + // cleared before another zip file's data get extracted. + // eslint-disable-next-line no-restricted-syntax + for (const testFile of testFilesForService) { + FileExtractor.dumpExtractorData(serviceName); + await runTestAgainstFile(serviceName, testFile); + } + } + ); await Promise.all(testPromises); });