Skip to content

Commit

Permalink
Implemented generic parser
Browse files Browse the repository at this point in the history
  • Loading branch information
VibeNL committed Aug 29, 2023
1 parent 0b00774 commit 9763697
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using AutoFixture;
using FluentAssertions;
using GhostfolioSidekick.FileImporter.Generic;
using GhostfolioSidekick.Ghostfolio.API;
using Moq;

namespace GhostfolioSidekick.UnitTests.FileImporter.Generic
{
public class GenericParserTests
{
readonly Mock<IGhostfolioAPI> api;

public GenericParserTests()
{
api = new Mock<IGhostfolioAPI>();
}

[Fact]
public async Task CanConvertOrders_TestFileSingleOrder_True()
{
// Arrange
var parser = new GenericParser(api.Object);

// Act
var canParse = await parser.CanConvertOrders(new[] { "./FileImporter/TestFiles/Generic/Example1/Example1.csv" });

// Assert
canParse.Should().BeTrue();
}

[Fact]
public async Task ConvertToOrders_TestFileSingleOrder_Converted()
{
// Arrange
var parser = new GenericParser(api.Object);
var fixture = new Fixture();

var asset = fixture.Build<Asset>().With(x => x.Currency, "USD").Create();
var account = fixture.Create<Account>();

api.Setup(x => x.GetAccountByName(account.Name)).ReturnsAsync(account);
api.Setup(x => x.FindSymbolByISIN("US67066G1040", null)).ReturnsAsync(asset);

// Act
var orders = await parser.ConvertToOrders(account.Name, new[] { "./FileImporter/TestFiles/Generic/Example1/Example1.csv" });

// Assert
orders.Should().BeEquivalentTo(new[] { new Order {
AccountId = account.Id,
Asset = asset,
Comment = "Transaction Reference: [BUY_US67066G1040_2023-08-07]",
Currency = asset.Currency,
FeeCurrency = "USD",
Date = new DateTime(2023,08,7, 0,0,0, DateTimeKind.Utc),
Fee = 0.02M,
Quantity = 0.0267001M,
Type = OrderType.BUY,
UnitPrice = 453.33M,
ReferenceCode = "BUY_US67066G1040_2023-08-07"
} });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OrderType,Symbol,Date,Currency,Quantity,UnitPrice,Fee
BUY,US67066G1040,2023-08-07,USD,0.0267001000,453.33,0.02
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<None Update="FileImporter\TestFiles\DeGiro\Example1\TestFileSingleOrder.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FileImporter\TestFiles\Generic\Example1\Example1.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FileImporter\TestFiles\Nexo\Example1\Example1.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
50 changes: 50 additions & 0 deletions GhostfolioSidekick/FileImporter/Generic/GenericParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using CsvHelper.Configuration;
using GhostfolioSidekick.Ghostfolio.API;
using System.Globalization;

namespace GhostfolioSidekick.FileImporter.Generic
{
public class GenericParser : RecordBaseImporter<GenericRecord>
{
public GenericParser(IGhostfolioAPI api) : base(api)
{
}

protected override async Task<IEnumerable<Order>> ConvertOrders(GenericRecord record, Account account, IEnumerable<GenericRecord> allRecords)
{
var asset = await api.FindSymbolByISIN(record.Symbol);

if (string.IsNullOrWhiteSpace(record.Id))
{
record.Id = $"{record.OrderType}_{record.Symbol}_{record.Date.ToString("yyyy-MM-dd")}";
}

var order = new Order
{
AccountId = account.Id,
Asset = asset,
Currency = record.Currency,
Date = record.Date,
Comment = $"Transaction Reference: [{record.Id}]",
Fee = record.Fee ?? 0,
FeeCurrency = record.Currency,
Quantity = record.Quantity,
Type = record.OrderType,
UnitPrice = record.UnitPrice,
ReferenceCode = record.Id,
};

return new[] { order };
}

protected override CsvConfiguration GetConfig()
{
return new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = true,
CacheFields = true,
Delimiter = ",",
};
}
}
}
24 changes: 24 additions & 0 deletions GhostfolioSidekick/FileImporter/Generic/GenericRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using GhostfolioSidekick.Ghostfolio.API;

namespace GhostfolioSidekick.FileImporter.Generic
{
public class GenericRecord
{
public OrderType OrderType { get; set; }

public string? Symbol { get; set; }

public DateTime Date { get; set; }

public string Currency { get; set; }

public decimal Quantity { get; set; }

public decimal UnitPrice { get; set; }

public decimal? Fee { get; set; }

[CsvHelper.Configuration.Attributes.Optional]
public string? Id { get; set; }
}
}

0 comments on commit 9763697

Please sign in to comment.