public void TestGetStateAll() { var noUse = UsState.GetStateByPostalCode("AZ");//the internal member is a singleton, this gets it pop'ed var theStates = UsState._theStates; foreach (var state in theStates) { System.Diagnostics.Debug.WriteLine(state.StateAbbrev); var randOut = state.GetRandomDriversLicense(); System.Diagnostics.Debug.WriteLine("{0} '{1}'", state.StateAbbrev, randOut); var randIn = state.ValidDriversLicense(randOut); Assert.IsTrue(randIn); } }
public void TestSolvePercentHsGradByStateAndRace() { //test resolves to simple natl average given no input var testResult = AmericanSchoolBase.SolvePercentGradByStateAndRace(null, null); //test resolves to natl avg's when no state Assert.AreEqual(89.6, testResult); testResult = AmericanSchoolBase.SolvePercentGradByStateAndRace(null, NorthAmericanRace.Mixed); Assert.AreEqual(89.6, testResult); testResult = AmericanSchoolBase.SolvePercentGradByStateAndRace(null, NorthAmericanRace.AmericanIndian); Assert.AreEqual(77.6, testResult); var testState = UsState.GetStateByPostalCode("CA"); var testStateData = UsStateData.GetStateData(testState.ToString()); Assert.IsNotNull(testStateData); //percent-highschool-grad="80.6" //asian="89.0" natl-percent="82.0" // 89 - 82 = 7 testResult = AmericanSchoolBase.SolvePercentGradByStateAndRace(testState.ToString(), NorthAmericanRace.Asian); Assert.AreEqual(87.6, testResult); //american-indian="70.0" // 70 - 82 = -12 testResult = AmericanSchoolBase.SolvePercentGradByStateAndRace(testState.ToString(), NorthAmericanRace.AmericanIndian); Assert.AreEqual(68.6, testResult); testState = UsState.GetStateByPostalCode("ID"); Assert.IsNotNull(testState); Assert.IsNotNull(UsStateData.GetStateData(testState.ToString())); testResult = AmericanSchoolBase.SolvePercentGradByStateAndRace(testState.ToString(), NorthAmericanRace.Asian, OccidentalEdu.Bachelor | OccidentalEdu.Grad); //percent-college-grad="23.9" // //asian="54.0" natl-percent="30.0" // 54.0 - 30.0 = 24, 23.9+24 = 47.9 Assert.AreEqual(47.9, testResult); }
public static UsCityStateZip RandomAmericanCity(string zipCodePrefix = null, bool pickSuburbAtRandom = true) { const string HAS_HIGH_SCHOOL = "has-high-school"; const string VALUE = "value"; //set defaults var ctz = new AddressData { PostalCode = $"{UsCityStateZip.DF_ZIPCODE_PREFIX}{Etx.RandomInteger(1, 99):00}", RegionAbbrev = UsCityStateZip.DF_STATE_ABBREV }; //pick a zip code prefix at random if (String.IsNullOrWhiteSpace(zipCodePrefix)) { zipCodePrefix = UsCityStateZip.RandomAmericanPartialZipCode() ?? UsCityStateZip.DF_ZIPCODE_PREFIX; } //x-ref it to the zip code data var xpathString = $"//{UsCityStateZip.ZIP_CODE_PLURAL}//{UsCityStateZip.ZIP_CODE_SINGULAR}[@{PREFIX}='{zipCodePrefix}']"; UsCityStateZip.UsZipCodeXml = UsCityStateZip.UsZipCodeXml ?? XmlDocXrefIdentifier.GetEmbeddedXmlDoc(UsCityStateZip.US_ZIP_CODE_DATA, Assembly.GetExecutingAssembly()); if (UsCityStateZip.UsZipCodeXml == null) { return(null); } var randZipCode = UsCityStateZip.UsZipCodeXml.SelectSingleNode(xpathString); if (randZipCode?.ParentNode?.Attributes?[NAME] == null) { ctz.Locality = UsCityStateZip.DF_CITY_NAME; return(new UsCityStateZip(ctz)); } //get the containing us state ctz.RegionName = randZipCode.ParentNode.Attributes[NAME].Value; var nfState = UsState.GetStateByName(ctz.RegionName) ?? UsState.GetStateByPostalCode(UsCityStateZip.DF_STATE_ABBREV); ctz.RegionAbbrev = nfState.StateAbbrev ?? UsCityStateZip.DF_STATE_ABBREV; ctz.SortingCode = $"{Etx.MyRand.Next(1, 9999):0000}"; if (!randZipCode.HasChildNodes) { ctz.PostalCode = $"{zipCodePrefix}{Etx.RandomInteger(1, 99):00}"; } else { //pick a particular zip-code (ZIP5) var zipCodes = randZipCode.ChildNodes.Cast <XmlElement>() .Where( x => x.Attributes[HAS_HIGH_SCHOOL] != null && x.Attributes[HAS_HIGH_SCHOOL].Value == Boolean.TrueString) .Select(x => x.Attributes[VALUE].Value).ToArray(); if (zipCodes.Length <= 0) { return(new UsCityStateZip(ctz)); } var pickNum = Etx.RandomInteger(0, zipCodes.Length - 1); ctz.PostalCode = zipCodes[pickNum]; } var rr = new UsCityStateZip(ctz); rr.SetAddrDataToXmlValues(pickSuburbAtRandom); return(rr); }
/// <summary> /// Based on the <see cref="ZipCode"/> and <see cref="StateName"/> that are assigned. /// </summary> /// <returns></returns> protected internal XmlNode GetCityXmlNode() { UsCityXml = UsCityXml ?? XmlDocXrefIdentifier.GetEmbeddedXmlDoc(US_CITY_DATA, Assembly.GetExecutingAssembly()); if (UsCityXml == null) { return(null); } string searchCrit; XmlNodeList matchedNodes = null; var data = GetData(); //search by city-state if (!String.IsNullOrWhiteSpace(data.RegionAbbrev) && !String.IsNullOrWhiteSpace(data.Locality) && UsState.GetStateByPostalCode(data.RegionAbbrev) != null) { var cityName = FinesseCityName(data.Locality); searchCrit = $"//state[@abbreviation='{data.RegionAbbrev}']/city[@name='{cityName.EscapeString(EscapeStringType.XML)}']"; matchedNodes = UsCityXml.SelectNodes(searchCrit); //try again on place names if (matchedNodes == null || matchedNodes.Count <= 0) { searchCrit = $"//state[@abbreviation='{data.RegionAbbrev}']//place[@name='{cityName.EscapeString(EscapeStringType.XML)}']/.."; matchedNodes = UsCityXml.SelectNodes(searchCrit); } } //search by first 3 of Zip Code else if (!String.IsNullOrWhiteSpace(data.PostalCode) && data.PostalCode.Length >= 3) { var zipCodePrefix = data.PostalCode.Substring(0, 3); searchCrit = $"//{ZIP_CODE_SINGULAR}[@{PREFIX}='{zipCodePrefix}']/.."; matchedNodes = UsCityXml.SelectNodes(searchCrit); } if (matchedNodes == null || matchedNodes.Count <= 0) { return(null); } if (matchedNodes.Count == 1) { return(matchedNodes[0]); } //choose one with a msa-code value if possible foreach (var matchedNode in matchedNodes) { var matchedElem = matchedNode as XmlElement; if (matchedElem == null) { continue; } if (!matchedElem.HasAttributes) { continue; } var hasMsaCode = !String.IsNullOrWhiteSpace(matchedElem.GetAttribute(MSA_CODE)); var hasCbsaCode = !String.IsNullOrWhiteSpace(matchedElem.GetAttribute(CBSA_CODE)); if (hasCbsaCode || hasMsaCode) { return(matchedElem); } } var pick = Etx.RandomInteger(0, matchedNodes.Count - 1); return(matchedNodes[pick]); }
public void GetStateTest() { var testState = "AZ"; var testResult = UsState.GetStateByPostalCode(testState); Assert.IsNotNull(testResult); Assert.IsInstanceOf <Arizona>(testResult); Assert.AreEqual(testState, testResult.StateAbbrev); var testDlResult = testResult.GetRandomDriversLicense(); Assert.IsNotNull(testDlResult); System.Diagnostics.Debug.WriteLine(testDlResult); Assert.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(testDlResult, "^[ABDY][0-9]{8}$")); Assert.IsTrue(testResult.ValidDriversLicense("B70350018")); Assert.IsTrue(testResult.ValidDriversLicense("686553072")); Assert.IsFalse(testResult.ValidDriversLicense("68655307")); testState = "AR"; testResult = UsState.GetStateByPostalCode(testState); Assert.IsNotNull(testResult); Assert.IsInstanceOf <Arkansas>(testResult); Assert.AreEqual(testState, testResult.StateAbbrev); testDlResult = testResult.GetRandomDriversLicense(); Assert.IsNotNull(testDlResult); System.Diagnostics.Debug.WriteLine(testDlResult); Assert.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(testDlResult, "^[0-9]{9}$")); Assert.IsFalse(testResult.ValidDriversLicense("U7376050")); Assert.IsTrue(testResult.ValidDriversLicense("686553072")); Assert.IsTrue(testResult.ValidDriversLicense("68655307")); testState = "CA"; testResult = UsState.GetStateByPostalCode(testState); Assert.IsNotNull(testResult); Assert.IsInstanceOf <California>(testResult); Assert.AreEqual(testState, testResult.StateAbbrev); testDlResult = testResult.GetRandomDriversLicense(); Assert.IsNotNull(testDlResult); System.Diagnostics.Debug.WriteLine(testDlResult); Assert.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(testDlResult, "^[A-Z][0-9]{7}$")); Assert.IsTrue(testResult.ValidDriversLicense("U7376050")); Assert.IsFalse(testResult.ValidDriversLicense("686553072")); testState = "CT"; testResult = UsState.GetStateByPostalCode(testState); Assert.IsNotNull(testResult); Assert.IsInstanceOf <Connecticut>(testResult); Assert.AreEqual(testState, testResult.StateAbbrev); testDlResult = testResult.GetRandomDriversLicense(); Assert.IsNotNull(testDlResult); System.Diagnostics.Debug.WriteLine(testDlResult); }