Skip to content

Commit

Permalink
Return multiple callbacks in processFile, add unit tests for Swissquote
Browse files Browse the repository at this point in the history
  • Loading branch information
dickwolff committed Jan 11, 2024
1 parent 5ad64c4 commit 4833c98
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 140 deletions.
3 changes: 2 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import type { Config } from '@jest/types';
// Sync object
const config: Config.InitialOptions = {
verbose: true,
testTimeout: 30000,
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
coverageDirectory: 'coverage',
collectCoverageFrom: ['src/**/*.ts'],
coverageReporters: ['text', 'cobertura']
coverageReporters: ['text', 'cobertura', 'html']
};

export default config;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "trading212-to-ghostfolio",
"name": "export-to-ghostfolio",
"version": "1.0.0",
"description": "Convert Trading 212 export to Ghostfolio import",
"description": "Convert multiple broker exports to Ghostfolio import",
"main": "index.js",
"scripts": {
"start": "nodemon",
Expand Down
5 changes: 3 additions & 2 deletions src/converters/abstractconverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ export abstract class AbstractConverter {
* Process an export file.
*
* @param inputFile The file to convert.
* @param callback A callback to execute after processing has succeeded.
* @param successCallback A callback to execute after processing has succeeded.
* @param errorCallback A callback to execute after processing has failed.
*/
abstract processFile(inputFile: string, callback: any): void;
abstract processFile(inputFile: string, successCallback: any, errorCallback: any): void;

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

// Read file contents of the CSV export.
const csvFile = fs.readFileSync(inputFile, "utf-8");
Expand Down Expand Up @@ -233,7 +233,7 @@ export class DeGiroConverter extends AbstractConverter {

this.progress.stop()

callback(result);
successCallback(result);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/converters/finpensionConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class FinpensionConverter extends AbstractConverter {
/**
* @inheritdoc
*/
public processFile(inputFile: string, callback: any): void {
public processFile(inputFile: string, successCallback: any, errorCallback: any): void {

// Read file contents of the CSV export.
const csvFile = fs.readFileSync(inputFile, "utf-8");
Expand Down Expand Up @@ -163,7 +163,7 @@ export class FinpensionConverter extends AbstractConverter {

this.progress.stop()

callback(result);
successCallback(result);
});

// Catch any error.
Expand Down
102 changes: 99 additions & 3 deletions src/converters/swissquoteComverter.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,109 @@
import * as fs from "fs";
import { SwissquoteConverter } from "./swissquoteConverter";
import { GhostfolioExport } from "../models/ghostfolioExport";

describe("swissquoteConverter", () => {

it("should construct", () => {

it("should construct", () => {
// Act
const sut = new SwissquoteConverter();

// Asssert
expect(sut).toBeTruthy();
});

it("should process sample CSV file", (done) => {

// Act
const sut = new SwissquoteConverter();
const inputFile = "sample-swissquote-export.csv";

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

// Assert
expect(actualExport).toBeTruthy();

// Finish the test
done();
}, () => { fail("Should not have an error!"); });
});

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
const sut = new SwissquoteConverter();

// Asssert
expect(sut).toBeTruthy();
let tempFileName = "tmp/testinput/swissquote-filedoesnotexist.csv";

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

// Assert
expect(err).toBeTruthy();
expect(err.message.includes("no such file or directory")).toBeTruthy();
done();
});
});

it("the input file is empty", (done) => {

// Act
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) => {

// 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) => {

// Assert
expect(err).toBeTruthy();
done();
});
});
});
});
Loading

0 comments on commit 4833c98

Please sign in to comment.