Skip to content

Commit

Permalink
Remove fs dependency from unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dickwolff committed Feb 1, 2024
1 parent 3a8fcf9 commit 6f64d22
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/converters/abstractconverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export abstract class AbstractConverter {

const contents = fs.readFileSync(inputFile, "utf-8");

this.processFile(contents,successCallback, errorCallback);
this.processFileContents(contents,successCallback, errorCallback);
}

/**
Expand All @@ -38,13 +38,13 @@ export abstract class AbstractConverter {
abstract isIgnoredRecord(record: any): boolean;

/**
* Process an export file.
* Process export file contents.
*
* @param input The file contents to convert.
* @param successCallback A callback to execute after processing has succeeded.
* @param errorCallback A callback to execute after processing has failed.
*/
abstract processFile(input: string, successCallback: CallableFunction, errorCallback: CallableFunction): void;
abstract processFileContents(input: string, successCallback: CallableFunction, errorCallback: CallableFunction): void;

/**
* Retrieve headers from the input file.
Expand Down
2 changes: 1 addition & 1 deletion src/converters/degiroConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class DeGiroConverter extends AbstractConverter {
/**
* @inheritdoc
*/
public processFile(input: string, successCallback: any, errorCallback: any): void {
public processFileContents(input: string, successCallback: any, errorCallback: any): void {

// Parse the CSV and convert to Ghostfolio import format.
parse(input, {
Expand Down
2 changes: 1 addition & 1 deletion src/converters/finpensionConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class FinpensionConverter extends AbstractConverter {
/**
* @inheritdoc
*/
public processFile(input: string, successCallback: any, errorCallback: any): void {
public processFileContents(input: string, successCallback: any, errorCallback: any): void {

// Parse the CSV and convert to Ghostfolio import format.
const parser = parse(input, {
Expand Down
33 changes: 6 additions & 27 deletions src/converters/swissquoteComverter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from "fs";
import { SwissquoteConverter } from "./swissquoteConverter";
import { GhostfolioExport } from "../models/ghostfolioExport";

Expand All @@ -20,7 +19,7 @@ describe("swissquoteConverter", () => {
const inputFile = "sample-swissquote-export.csv";

// Act
sut.processFile(inputFile, (actualExport: GhostfolioExport) => {
sut.readAndProcessFile(inputFile, (actualExport: GhostfolioExport) => {

// Assert
expect(actualExport).toBeTruthy();
Expand All @@ -31,22 +30,6 @@ describe("swissquoteConverter", () => {
});

describe("should throw an error if", () => {
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(jest.fn());
jest.spyOn(console, 'error').mockImplementation(jest.fn());

// Create test input folder before run.
if(!fs.existsSync("tmp/testinput")) {
fs.mkdirSync("tmp/testinput");
}
});

afterAll(() => {

// Clean test input folder after run.
fs.rmSync("tmp/testinput", { recursive: true });
})

it("the input file does not exist", (done) => {

// Act
Expand All @@ -55,7 +38,7 @@ describe("swissquoteConverter", () => {
let tempFileName = "tmp/testinput/swissquote-filedoesnotexist.csv";

// Act
sut.processFile(tempFileName, () => { fail("Should not succeed!"); }, (err: Error) => {
sut.readAndProcessFile(tempFileName, () => { fail("Should not succeed!"); }, (err: Error) => {

// Assert
expect(err).toBeTruthy();
Expand All @@ -70,35 +53,31 @@ describe("swissquoteConverter", () => {
const sut = new SwissquoteConverter();

// Create temp file.
let tempFileName = "tmp/testinput/swissquote-filedoesisempty.csv";
let tempFileContent = "";
tempFileContent += "Date;Order #;Transaction;Symbol;Name;ISIN;Quantity;Unit price;Costs;Accrued Interest;Net Amount;Balance;Currency\n";
fs.writeFileSync(tempFileName, tempFileContent);

// Act
sut.processFile(tempFileName, () => { fail("Should not succeed!"); }, (err: Error) => {
sut.processFileContents(tempFileContent, () => { fail("Should not succeed!"); }, (err: Error) => {

// Assert
expect(err).toBeTruthy();
expect(err.message).toContain("An error ocurred while parsing")
done();
});
});

it("Yahoo Finance got empty input for query", (done) => {

// Act
const sut = new SwissquoteConverter();

// Create temp file.
let tempFileName = "tmp/testinput/swissquote-yahoofinanceerrortest.csv";
let tempFileContent = "";
tempFileContent += "Date;Order #;Transaction;Symbol;Name;ISIN;Quantity;Unit price;Costs;Accrued Interest;Net Amount;Balance;Currency\n";
tempFileContent += "10-08-2022 15:30:02;113947121;Buy;;;;200.0;19.85;5.96;0.00;-3975.96;168660.08;USD";
fs.writeFileSync(tempFileName, tempFileContent);


// Act
sut.processFile(tempFileName, () => { fail("Should not succeed!"); }, (err) => {
sut.processFileContents(tempFileContent, () => { fail("Should not succeed!"); }, (err) => {

// Assert
expect(err).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion src/converters/swissquoteConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class SwissquoteConverter extends AbstractConverter {
/**
* @inheritdoc
*/
public processFile(input: string, successCallback: any, errorCallback: any): void {
public processFileContents(input: string, successCallback: any, errorCallback: any): void {

// Parse the CSV and convert to Ghostfolio import format.
const parser = parse(input, {
Expand Down
2 changes: 1 addition & 1 deletion src/converters/trading212Converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Trading212Converter extends AbstractConverter {
/**
* @inheritdoc
*/
public processFile(input: string, successCallback: any, errorCallback: any): void {
public processFileContents(input: string, successCallback: any, errorCallback: any): void {

// Parse the CSV and convert to Ghostfolio import format.
parse(input, {
Expand Down

0 comments on commit 6f64d22

Please sign in to comment.