-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
GhostfolioSidekick.UnitTests/FileImporter/Generic/GenericParserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} }); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
GhostfolioSidekick.UnitTests/FileImporter/TestFiles/Generic/Example1/Example1.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ",", | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |