Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for LedgerBalance. Better data type conversion. Unit Tests #4

Merged
merged 1 commit into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions QFXparser.Testing/ParsingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Linq;
using Xunit;

namespace QFXparser.Testing
{
public class ParsingTest
{
[Fact]
public void TestSimple()
{
var parser = new FileParser("test.qfx");
var statement = parser.BuildStatement();
var transactions = statement.Transactions.ToList();
Assert.Equal(3, transactions.Count);
Assert.Equal(-768.33m, statement.LedgerBalance.Amount);
Assert.Equal(new DateTime(2018, 5, 25, 0, 0, 0, DateTimeKind.Utc), statement.LedgerBalance.AsOf);
Assert.Equal(-27.18m, transactions[0].Amount);
Assert.Equal("AMAZON.COM AMZN.COM/BILL AMZN.CO", transactions[0].Memo);
Assert.Equal(new DateTime(2018, 4, 27, 16, 0, 0, DateTimeKind.Utc), transactions[0].PostedOn);
Assert.Equal(0m, transactions[2].Amount); // test for default value if invalid in .qfx
}

[Fact]
public void TestDateTimeNoTimeZoneAssumesUtc()
{
Assert.Equal(new DateTime(2018, 5, 25, 0, 0, 0, DateTimeKind.Utc), ParsingHelper.ParseDate("20180525000000"));
}

[Fact]
public void TestDateTimeWithUtcTimeZone()
{
Assert.Equal(new DateTime(2018, 5, 25, 0, 0, 0, DateTimeKind.Utc), ParsingHelper.ParseDate("20180525000000[0:UTC]"));
}

[Fact]
public void TestDateTimeWithMstTimeZone()
{
Assert.Equal(new DateTime(2018, 1, 19, 0, 0, 0, DateTimeKind.Utc), ParsingHelper.ParseDate("20180119000000.000[-7:MST]"));
}
}
}
25 changes: 25 additions & 0 deletions QFXparser.Testing/QFXparser.Testing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\QFXparser\QFXparser.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="test.qfx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
79 changes: 79 additions & 0 deletions QFXparser.Testing/test.qfx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE

<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<DTSERVER>20180708011545[0:UTC]
<LANGUAGE>ENG
<FI>
<ORG>Bank of America
<FID>9876
</FI>
<INTU.BID>3456
</SONRS>
</SIGNONMSGSRSV1>
<CREDITCARDMSGSRSV1>
<CCSTMTTRNRS>
<TRNUID>20180708011545[0:UTC]
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<CCSTMTRS>
<CURDEF>USD
<CCACCTFROM>
<ACCTID>1234123409870987
</CCACCTFROM>
<BANKTRANLIST>
<DTSTART>20180427160000[0:UTC]
<DTEND>20180525160000[0:UTC]
<STMTTRN>
<TRNTYPE>PAYMENT
<DTPOSTED>20180427160000[0:UTC]
<TRNAMT>-27.18
<FITID>555444
<CORRECTFITID>555444
<CORRECTACTION>REPLACE
<NAME>AMAZON.COM AMZN.COM/BILL AMZN.CO
<MEMO>AMAZON.COM AMZN.COM/BILL AMZN.CO
</STMTTRN>
<STMTTRN>
<TRNTYPE>PAYMENT
<DTPOSTED>20180430160000[0:UTC]
<TRNAMT>-25.00
<FITID>666876
<CORRECTFITID>666876
<CORRECTACTION>REPLACE
<NAME>NEW JERSEY E-ZPASS 888-288-6865
<MEMO>NEW JERSEY E-ZPASS 888-288-6865
</STMTTRN>
<STMTTRN>
<TRNTYPE>CREDIT
<DTPOSTED>20180501160000[0:UTC]
<TRNAMT>56.77invalid
<FITID>46576
<CORRECTFITID>46576
<CORRECTACTION>REPLACE
<NAME>PMT FROM BILL PAYER SERVICE
</STMTTRN>
</BANKTRANLIST>
<LEDGERBAL>
<BALAMT>-768.33
<DTASOF>20180525000000[0:UTC]
</LEDGERBAL>
</CCSTMTRS>
</CCSTMTTRNRS>
</CREDITCARDMSGSRSV1>
</OFX>
14 changes: 14 additions & 0 deletions QFXparser.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QFXparser.TestApp", "QFXpar
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QFXparser", "QFXparser\QFXparser.csproj", "{F99BA880-DE1C-4D30-BEB3-EB0406E41955}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QFXparser.Testing", "QFXparser.Testing\QFXparser.Testing.csproj", "{AE23D849-AF0F-4231-81DD-C345C7321702}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -41,6 +43,18 @@ Global
{F99BA880-DE1C-4D30-BEB3-EB0406E41955}.Release|x64.Build.0 = Release|Any CPU
{F99BA880-DE1C-4D30-BEB3-EB0406E41955}.Release|x86.ActiveCfg = Release|Any CPU
{F99BA880-DE1C-4D30-BEB3-EB0406E41955}.Release|x86.Build.0 = Release|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Debug|x64.ActiveCfg = Debug|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Debug|x64.Build.0 = Debug|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Debug|x86.ActiveCfg = Debug|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Debug|x86.Build.0 = Debug|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Release|Any CPU.Build.0 = Release|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Release|x64.ActiveCfg = Release|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Release|x64.Build.0 = Release|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Release|x86.ActiveCfg = Release|Any CPU
{AE23D849-AF0F-4231-81DD-C345C7321702}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions QFXparser/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("QFXparser.Testing")]
11 changes: 11 additions & 0 deletions QFXparser/LedgerBalance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace QFXparser
{
public class LedgerBalance
{
public decimal Amount { get; set; }

public DateTime AsOf { get; set; }
}
}
5 changes: 4 additions & 1 deletion QFXparser/NodeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ internal enum NodeType
TransactionOpen,
TransactionClose,
StatementProp,
TransactionProp
TransactionProp,
LedgerBalanceOpen,
LedgerBalanceClose,
LedgerBalanceProp
}
}
38 changes: 38 additions & 0 deletions QFXparser/ParsingHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace QFXparser
{
internal static class ParsingHelper
{
private static readonly Regex _dateTimeRegex = new Regex(
"^(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2})(?<hour>\\d{2})(?<min>\\d{2})(?<sec>\\d{2})(\\[(?<tzId>\\d+):(?<timezone>\\w{3})\\])?");

public static DateTime? ParseDate(string content)
{
DateTime? result;
var match = _dateTimeRegex.Match(content);
if (!match.Success)
{
result = null;
}
else
{
var groups = match.Groups;
var year = int.Parse(groups["year"].Value);
var month = int.Parse(groups["month"].Value);
var day = int.Parse(groups["day"].Value);
var hour = int.Parse(groups["hour"].Value);
var minute = int.Parse(groups["min"].Value);
var second = int.Parse(groups["sec"].Value);
var timeZone = groups["timezone"].Success ? groups["timezone"].Value : "UTC";

var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
result = new DateTimeOffset(year, month, day, hour, minute, second, timeZoneInfo.BaseUtcOffset).ToUniversalTime().DateTime;
}
return result;
}
}
}
Loading