public void Test_TryDownload_Sequence() { var formatObject1 = new FormatObject("test", new DateTime(2003, 1, 1)); var newDateTime1 = new DateTimeOffset(new DateTime(2003, 1, 2), TimeSpan.FromHours(0)); var formatObject2 = new FormatObject("test", new DateTime(2003, 1, 2)); var newDateTime2 = new DateTimeOffset(new DateTime(2003, 1, 3), TimeSpan.FromHours(0)); var source = new Mock <ISourceSpecification>(); source.SetupGet(x => x.UriFormat) .Returns(@"http://www.forexite.com/free_forex_quotes/{Year}/{MonthNumber}/{DayNumber}{MonthNumber}{ShortYear}.zip"); source.SetupGet(x => x.FilenameFormat).Returns("{DayNumber}{MonthNumber}{ShortYear}.zip"); source.Setup(x => x.IsValidFile(It.IsAny <IWebResponse>())) .Returns <IWebResponse>(y => { return(y.ContentType.Equals(@"application/zip")); }); source.Setup(x => x.GetUpperBoundExclusive(formatObject1)).Returns(newDateTime1); source.Setup(x => x.GetUpperBoundExclusive(formatObject2)).Returns(newDateTime2); var formatIterator = new Mock <IFormatIterator>(); formatIterator.Setup(x => x.GetNextCandidates(It.Is <FormatObject>(y => y.Equals(formatObject1)))) .Returns(new[] { formatObject2, }); DirectoryInfo downloadLocation = new DirectoryInfo(@"d:\test"); var webResponse = new Mock <IWebResponse>(); webResponse.SetupGet(x => x.ContentType).Returns(@"application/zip"); var webClient = new Mock <IWebClient>(); webClient.Setup(x => x.DownloadFile(It.IsAny <Uri>(), It.IsAny <string>())).Returns(webResponse.Object); var target = new Downloader() { Source = source.Object, DownloadLocation = downloadLocation, FormatIterator = formatIterator.Object, WebClient = webClient.Object, }; var expected = new TryDownloadResult() { DestinationFile = new FileInfo(string.Format(@"{0}\_020103.zip", downloadLocation)), FormatObject = formatObject2, IsSuccess = true, UpperBoundExclusive = newDateTime2 }; var actual = target.TryDownload(formatObject1); webClient.Verify(x => x.DownloadFile(It.IsAny <Uri>(), It.IsAny <string>()), Times.Once()); source.Verify(x => x.IsValidFile(webResponse.Object), Times.Once()); source.Verify(x => x.GetUpperBoundExclusive(formatObject2), Times.Once()); Assert.AreEqual(actual.DestinationFile.FullName, expected.DestinationFile.FullName); Assert.AreEqual(actual.FormatObject, expected.FormatObject); Assert.AreEqual(actual.IsSuccess, expected.IsSuccess); Assert.AreEqual(actual.UpperBoundExclusive, expected.UpperBoundExclusive); }
public void Downloader_TryDownload_IncorrectResponse() { var formatObject = new FormatObject("test", new DateTime(2003, 3, 2), TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time")); var source = new Mock <ISourceSpecification>(); source.SetupGet(x => x.UriFormat) .Returns(@"http://www.forexite.com/free_forex_quotes/{Year}/{MonthNumber}/{DayNumber}{MonthNumber}{ShortYear}.zip"); source.SetupGet(x => x.FilenameFormat).Returns("{DayNumber}{MonthNumber}{ShortYear}.zip"); source.Setup(x => x.IsValidFile(It.IsAny <IWebResponse>())) .Returns <IWebResponse>(y => { return(y.ContentType.Equals(@"application/zip")); }); DirectoryInfo downloadLocation = new DirectoryInfo(@"d:\test"); var webResponse = new Mock <IWebResponse>(); webResponse.Setup(x => x.ContentType).Returns(@"text\html"); var webClient = new Mock <IWebClient>(); webClient.Setup(x => x.DownloadFile(It.IsAny <Uri>(), It.IsAny <string>())).Returns(webResponse.Object); var target = new Downloader() { Source = source.Object, DownloadLocation = downloadLocation, WebClient = webClient.Object }; var expected = new TryDownloadResult() { DestinationFile = default(FileInfo), IsSuccess = false, }; var actual = target.TryDownload(formatObject); webClient.Verify(x => x.DownloadFile(It.IsAny <Uri>(), It.IsAny <string>()), Times.Once()); source.Verify(x => x.IsValidFile(webResponse.Object), Times.Once()); Assert.AreEqual(actual.DestinationFile, expected.DestinationFile); //Assert.AreEqual(actual.FormatObject, expected.FormatObject); Assert.AreEqual(actual.IsSuccess, expected.IsSuccess); }
public IEnumerable <IExtractionResult <FormatObject> > Extract(FormatObject lastExtracted) { var lastAttemptedFormat = lastExtracted; foreach (Pair pair in Source.GetDiscriminatingFilePairs()) { if (!(TempDownloadLocation.Exists)) { TempDownloadLocation.Create(); } DateTimeOffset?dateUpperBoundExclusive = null; bool isDownloadSuccess = true; while ((dateUpperBoundExclusive ?? DateTimeOffset.MinValue) < DateTimeOffset.Now) { TryDownloadResult result = Downloader.TryDownload(lastAttemptedFormat); var previous = dateUpperBoundExclusive; if (previous == result.UpperBoundExclusive) { // downloader caught in a loop break; } dateUpperBoundExclusive = result.UpperBoundExclusive; isDownloadSuccess = result.IsSuccess; lastAttemptedFormat = result.FormatObject; if (result.IsSuccess) { yield return(new FileNameExtractionResult() { FileFullName = result.DestinationFile.FullName, Pair = pair, CurrentPosition = result.FormatObject, IsSuccess = result.IsSuccess }); } } } }