From cd39bbe7d3ae2bbf0b80ae1238f6ae2690152aae Mon Sep 17 00:00:00 2001 From: Connor O'Shea Date: Sun, 17 Feb 2019 21:57:29 -0500 Subject: [PATCH] Add test for Alberta license --- IdParser.Test/DriversLicenseTests.cs | 53 ++++++++++++++++++++ IdParser.Test/IdParser.Test.csproj | 3 ++ IdParser.Test/Licenses/AB.txt | 22 ++++++++ IdParser/Barcode.cs | 37 +++++++++----- IdParser/Enums/IssuerIdentificationNumber.cs | 6 ++- IdParser/Parsers/Id/WeightInPoundsParser.cs | 32 ++++++++++++ 6 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 IdParser.Test/Licenses/AB.txt diff --git a/IdParser.Test/DriversLicenseTests.cs b/IdParser.Test/DriversLicenseTests.cs index 517ed0f..16906ec 100644 --- a/IdParser.Test/DriversLicenseTests.cs +++ b/IdParser.Test/DriversLicenseTests.cs @@ -2842,6 +2842,59 @@ public void TestCTLicenseUndefinedCharacters() Assert.AreEqual("Connecticut", idCard.IssuerIdentificationNumber.GetDescription()); } + [TestMethod] + public void TestABLicense() + { + var expected = new DriversLicense + { + Name = new Name + { + First = "MARY", + Middle = "SMITH", + Last = "MOTORIST" + }, + + Address = new Address + { + StreetLine1 = "123 MAPLE LEAF TERR SW", + City = "CALGARY", + JurisdictionCode = "AB", + PostalCode = "T4G7A7", + Country = Country.Canada + }, + + DateOfBirth = new DateTime(1993, 02, 04), + Sex = Sex.Female, + EyeColor = EyeColor.Brown, + HairColor = HairColor.Brown, + Height = Height.FromMetric(155), + Weight = Weight.FromMetric(50), + + IdNumber = "123400-056", + AamvaVersionNumber = Version.Aamva2005, + + ExpirationDate = new DateTime(2019, 01, 08), + + Jurisdiction = new DriversLicenseJurisdiction + { + VehicleClass = "5", + EndorsementCodes = "A" + } + }; + + // Alberta specifies the weight range following the weight in kilograms + expected.Weight.WeightRange = WeightRange.Lbs101To130; + + var file = License("AB"); + var idCard = Barcode.Parse(file, Validation.None); + + AssertIdCard(expected, idCard); + AssertLicense(expected, idCard); + + Assert.AreEqual("T4G 7A7", idCard.Address.PostalCodeDisplay); + Assert.AreEqual("Alberta", idCard.IssuerIdentificationNumber.GetDescription()); + } + [TestMethod] public void TestLeadingWhitespaceLicense() { diff --git a/IdParser.Test/IdParser.Test.csproj b/IdParser.Test/IdParser.Test.csproj index 62c17a0..a6fad89 100644 --- a/IdParser.Test/IdParser.Test.csproj +++ b/IdParser.Test/IdParser.Test.csproj @@ -54,6 +54,9 @@ + + PreserveNewest + PreserveNewest diff --git a/IdParser.Test/Licenses/AB.txt b/IdParser.Test/Licenses/AB.txt new file mode 100644 index 0000000..6316758 --- /dev/null +++ b/IdParser.Test/Licenses/AB.txt @@ -0,0 +1,22 @@ +@ + +ANSI 990876030001DLabacDLDCSMOTORIST +DCTMARY SMITH +DABMOTORIST +DACMARY +DADSMITH +DAG123 MAPLE LEAF TERR SW +DAQ123400-056 +DAKT4G 7A7 +DAICALGARY +DAJAB +DBCF +DBB19930204 +DAR5 +DATA +DBA20190108 +DAU155 CM +DAW50 KG +DCE2 +DAZBROWN +DAYBROWN diff --git a/IdParser/Barcode.cs b/IdParser/Barcode.cs index b7c8ad3..3ef65e2 100644 --- a/IdParser/Barcode.cs +++ b/IdParser/Barcode.cs @@ -185,7 +185,21 @@ private static Country ParseCountry(IssuerIdentificationNumber iin, Version vers private static List GetSubfileRecords(IdentificationCard idCard, Version version, string input) { - int offset = 0; + var offset = ParseSubfileOffset(idCard, version, input); + var records = input.Substring(offset).Split(new[] { ParseDataElementSeparator(input), ParseSegmentTerminator(input) }, StringSplitOptions.RemoveEmptyEntries).ToList(); + var firstRecord = records[0].Substring(0, 2); + + if (firstRecord == "DL" || firstRecord == "ID") + { + records[0] = records[0].Substring(2); + } + + return records; + } + + private static int ParseSubfileOffset(IdentificationCard idCard, Version version, string input) + { + var offset = 0; if (version == Version.Aamva2000) { @@ -199,14 +213,21 @@ private static List GetSubfileRecords(IdentificationCard idCard, Version } else if (version >= Version.Aamva2003) { - offset = Convert.ToInt32(input.Substring(23, 4)); + var offsetAsString = input.Substring(23, 4); + + // Alberta specifies characters, like "abac", in the place of the expected offset + // which causes the parsing of the subfile records to fail + if (offsetAsString.All(char.IsDigit)) + { + offset = Convert.ToInt32(offsetAsString); + } } if (offset == 0) { // Some jurisdictions, like Ontario, have a zero offset, which is incorrect. // Set the offset to the start of the subfile type indicator. - var subfileRegex = new Regex("[0-9]{8}[A-Z]{2}[D][A-Z]{2}"); + var subfileRegex = new Regex("(DL|ID)([\\d\\w]{3,8})(DL|ID|Z\\w)([DZ][A-Z]{2})"); var match = subfileRegex.Match(input); if (match.Success) @@ -218,15 +239,7 @@ private static List GetSubfileRecords(IdentificationCard idCard, Version } } - var records = input.Substring(offset).Split(new[] { ParseDataElementSeparator(input), ParseSegmentTerminator(input) }, StringSplitOptions.RemoveEmptyEntries).ToList(); - var firstRecord = records[0].Substring(0, 2); - - if (firstRecord == "DL" || firstRecord == "ID") - { - records[0] = records[0].Substring(2); - } - - return records; + return offset; } private static void PopulateIdCard(IdentificationCard idCard, Version version, Country? country, List subfileRecords, Validation validationLevel) diff --git a/IdParser/Enums/IssuerIdentificationNumber.cs b/IdParser/Enums/IssuerIdentificationNumber.cs index f1584c1..04f9ac2 100644 --- a/IdParser/Enums/IssuerIdentificationNumber.cs +++ b/IdParser/Enums/IssuerIdentificationNumber.cs @@ -16,10 +16,14 @@ public enum IssuerIdentificationNumber [Abbreviation("AK")] Alaska = 636059, + [Country(Country.Canada)] + [Abbreviation("AB")] + Alberta = 604432, + [Country(Country.Canada)] [Abbreviation("AB")] [Description("Alberta")] - Alberta, + AlbertaNonIso = 990876, [Country(Country.Usa)] [Abbreviation("AS")] diff --git a/IdParser/Parsers/Id/WeightInPoundsParser.cs b/IdParser/Parsers/Id/WeightInPoundsParser.cs index c77202b..3eb6910 100644 --- a/IdParser/Parsers/Id/WeightInPoundsParser.cs +++ b/IdParser/Parsers/Id/WeightInPoundsParser.cs @@ -1,4 +1,5 @@ using System; +using System.Text.RegularExpressions; using IdParser.Attributes; namespace IdParser.Parsers.Id @@ -12,6 +13,11 @@ public WeightInPoundsParser(IdentificationCard idCard, Version version, Country public override void ParseAndSet(string input) { + if (TryParseMetric(input)) + { + return; + } + var weight = Convert.ToInt16(input); if (IdCard.Weight == null) @@ -22,5 +28,31 @@ public override void ParseAndSet(string input) IdCard.Weight.SetImperial(weight); } + + /// + /// Alberta put the weight in kilograms in the weight in pounds parser ¯\_(ツ)_/¯ + /// + private bool TryParseMetric(string input) + { + var metricRegex = new Regex("(?\\d+)+\\s*KG"); + var match = metricRegex.Match(input); + + if (match.Success) + { + var weight = Convert.ToInt16(match.Groups["Weight"].Value); + + if (IdCard.Weight == null) + { + IdCard.Weight = Weight.FromMetric(weight); + return true; + } + + IdCard.Weight.SetMetric(weight); + + return true; + } + + return false; + } } }