Skip to content

Commit

Permalink
Improve testability with dependency injection, bring etoro test cover…
Browse files Browse the repository at this point in the history
…age to 100%
  • Loading branch information
dickwolff committed Feb 9, 2024
1 parent d5cad1c commit 42a29f8
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 255 deletions.
17 changes: 10 additions & 7 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AbstractConverter } from "./converters/abstractconverter";
import { Trading212Converter } from "./converters/trading212Converter";
import { SwissquoteConverter } from "./converters/swissquoteConverter";
import { FinpensionConverter } from "./converters/finpensionConverter";
import { YahooFinanceService } from "./yahooFinanceService";


export function createAndRunConverter(converterType: string, inputFilePath: string, outputFilePath: string, completionCallback: CallableFunction, errorCallback: CallableFunction) {
Expand Down Expand Up @@ -42,45 +43,47 @@ export function createAndRunConverter(converterType: string, inputFilePath: stri

function createConverter(converterType: string): AbstractConverter {

const yahooFinanceService = new YahooFinanceService();

let converter: AbstractConverter;

switch (converterType) {
case "t212":
case "trading212":
console.log("[i] Processing file using Trading212 converter");
converter = new Trading212Converter();
converter = new Trading212Converter(yahooFinanceService);
break;
case "degiro":
console.log("[i] Processing file using DeGiro converter");
console.log("[i] NOTE: There is a new version available of the DeGiro converter");
console.log("[i] The new converter has multiple record parsing improvements and also supports platform fees.");
console.log("[i] The new converter is currently in beta and we're looking for your feedback!");
console.log("[i] You can run the beta converter with the command 'npm run start degiro-v2'.");
converter = new DeGiroConverter();
converter = new DeGiroConverter(yahooFinanceService);
break;
case "degiro-v2":
console.log("[i] Processing file using DeGiro converter (V2 Beta)");
console.log("[i] NOTE: You are running a converter that is currently in beta.");
console.log("[i] If you have any issues, please report them on GitHub. Many thanks!");
converter = new DeGiroConverterV2();
converter = new DeGiroConverterV2(yahooFinanceService);
break;
case "fp":
case "finpension":
console.log("[i] Processing file using Finpension converter");
converter = new FinpensionConverter();
converter = new FinpensionConverter(yahooFinanceService);
break;
case "sq":
case "swissquote":
console.log("[i] Processing file using Swissquote converter");
converter = new SwissquoteConverter();
converter = new SwissquoteConverter(yahooFinanceService);
break;
case "schwab":
console.log("[i] Processing file using Schwab converter");
converter = new SchwabConverter();
converter = new SchwabConverter(yahooFinanceService);
break;
case "etoro":
console.log("[i] Processing file using Etoro converter");
converter = new EtoroConverter();
converter = new EtoroConverter(yahooFinanceService);
break;
default:
throw new Error(`Unknown converter '${converterType}' provided`);
Expand Down
8 changes: 7 additions & 1 deletion src/converters/abstractconverter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import * as fs from "fs";
import * as cliProgress from "cli-progress";
import { YahooFinanceService } from "../yahooFinanceService";

export abstract class AbstractConverter {

protected yahooFinanceService: YahooFinanceService;

protected progress: cliProgress.MultiBar;

constructor() {
constructor(yahooFinanceService: YahooFinanceService) {

this.yahooFinanceService = yahooFinanceService;

this.progress = new cliProgress.MultiBar(
{
stopOnComplete: true,
Expand Down
3 changes: 2 additions & 1 deletion src/converters/degiroConverter.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { YahooFinanceService } from "../yahooFinanceService";
import { DeGiroConverter } from "./degiroConverter";

describe("degiroConverter", () => {

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

// Act
const sut = new DeGiroConverter();
const sut = new DeGiroConverter(new YahooFinanceService());

// Asssert
expect(sut).toBeTruthy();
Expand Down
15 changes: 6 additions & 9 deletions src/converters/degiroConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ import { GhostfolioOrderType } from "../models/ghostfolioOrderType";

export class DeGiroConverter extends AbstractConverter {

private yahooFinanceService: YahooFinanceService;

constructor() {
super();

this.yahooFinanceService = new YahooFinanceService();
constructor(yahooFinanceService: YahooFinanceService) {
super(yahooFinanceService);

dayjs.extend(customParseFormat);
}
Expand All @@ -37,12 +33,13 @@ export class DeGiroConverter extends AbstractConverter {
return columnValue;
}
}, async (_, records: DeGiroRecord[]) => {

// If records is empty, parsing failed..
if (records === undefined) {
if (records === undefined || records.length === 0) {
return errorCallback(new Error("An error ocurred while parsing!"));
}


console.log("[i] Read CSV file. Start processing..");
const result: GhostfolioExport = {
meta: {
Expand Down Expand Up @@ -89,7 +86,7 @@ export class DeGiroConverter extends AbstractConverter {
this.progress);
}
catch (err) {
this.logQueryError(record.isin || record.product, idx);
this.logQueryError(record.isin || record.product, idx);
return errorCallback(err);
}

Expand Down
3 changes: 2 additions & 1 deletion src/converters/degiroConverterV2.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { YahooFinanceService } from "../yahooFinanceService";
import { DeGiroConverterV2 } from "./degiroConverterV2";

describe("degiroConverterV2", () => {

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

// Act
const sut = new DeGiroConverterV2();
const sut = new DeGiroConverterV2(new YahooFinanceService());

// Asssert
expect(sut).toBeTruthy();
Expand Down
Loading

0 comments on commit 42a29f8

Please sign in to comment.