import S3Service from "../../../src/services/s3.service"; import createS3Stub from "../../mocks/s3.mock"; import { Options } from "csv-parser"; import sinon from "sinon"; const chai = require("chai"); const expect = chai.expect; describe("S3 Service", () => { let s3Stub: sinon.SinonStub; beforeEach(() => { s3Stub = createS3Stub(); }); afterEach(() => { s3Stub.restore(); }); const dirBase = "test/unit/data/s3service"; describe("listKeys", () => { const dirName = "listkeys"; it("should recursively list all keys in directory", async () => { const s3Service = new S3Service(); const keys = await s3Service.listKeys(dirBase, dirName); expect(keys.sort()).to.deep.equal([ dirName + "/01.txt", dirName + "/02.txt", dirName + "/subdir/01.txt", ]); }); it("should succeed if assertKeysExist contains only existing keys", async () => { const s3Service = new S3Service(); const keys = await s3Service.listKeys(dirBase, dirName, ["01.txt", "subdir/01.txt"]); expect(keys.sort()).to.deep.equal([ dirName + "/01.txt", dirName + "/02.txt", dirName + "/subdir/01.txt", ]); }); it("should fail if assertKeysExist contains any non-existing key", async () => { const s3Service = new S3Service(); await s3Service .listKeys(dirBase, dirName, ["01.txt", "03.txt"]) .then(() => Promise.reject("Expected method to throw")) .catch((err) => { if (typeof err === "string") expect.fail(undefined, undefined, err); expect(err.message).to.contain("Missing file"); }); }); }); describe("readLinesCsv", () => { async function testReadLinesCsv(filename: string) { const s3Service = new S3Service(); const csvOptions: Options = { mapValues: ({ header, value }) => { return header === "header2" ? parseInt(value) : value; }, }; const lines: string[] = []; const lineReader = s3Service.readLinesCsv(dirBase, filename, csvOptions); for await (const l of lineReader) lines.push(l); expect(lines).to.deep.equal([ { header1: "A", header2: 1 }, { header1: "0", header2: -1 }, { header1: "2", header2: NaN }, { header1: "", header2: 4 }, ]); } it("should read lines of a CSV file with headers and mapValues", async () => { await testReadLinesCsv("readlines.csv"); }); it("should read lines of a gzipped CSV file with headers and mapValues", async () => { await testReadLinesCsv("readlines.csv.gz"); }); }); });