public async void CallTheAccidentStatisticsUrlFor2016()
            {
                var accidentStatistics = AutoFixture.CreateMany <AccidentStatistic>();

                HttpTest.RespondWithJson(accidentStatistics, 200);

                await TransportForLondonClient.GetAllAccidentStatistics(2016);

                HttpTest.ShouldHaveCalled("https://fake-api.tfl.gov.uk/AccidentStats/2016");
            }
        public void ThrowNotSupportedExceptionWhenFromRangeLessThan2005()
        {
            Func <Task <Paging <AccidentStatistic> > > action = async() =>
                                                                await TransportForLondonClient.GetAccidentStatistics(
                from : DateTime.Parse("01 December 2004 16:13:00"),
                to : DateTime.Parse("01 January 2016 09:11:00"),
                severity : Severity.Fatal,
                sortOptions : ByDateAscending);

            action.Should().Throw <NotSupportedException>().WithMessage(ErrorMessages.DatesBelow2005NotSupported);
        }
        public async void GetFirstPageAccidentStatistics_WhenThePageIsLessThanOne()
        {
            var accidentStatistics = AutoFixture.CreateMany <AccidentStatistic>();

            HttpTest.RespondWithJson(accidentStatistics, 200);

            var actual = await TransportForLondonClient.GetAccidentStatistics(year : 2016, page : -1, pageSize : 5);

            actual.Should().NotBeNull();
            actual.Data.Should().NotBeNull();
            actual.Total.Should().Be(3);
            actual.Page.Should().Be(1);
            actual.PageSize.Should().Be(3);
        }
        public void ThrowNotSupportedExceptionWhenFromOrTooRangeInCurrentYear()
        {
            Func <Task <Paging <AccidentStatistic> > > action = async() =>
            {
                var yesterday = DateTime.UtcNow.AddDays(-1);
                return(await TransportForLondonClient.GetAccidentStatistics(
                           from : yesterday,
                           to : DateTime.UtcNow,
                           severity : Severity.Fatal,
                           sortOptions : ByDateAscending));
            };

            action.Should().Throw <NotSupportedException>().WithMessage(string.Format(ErrorMessages.DatesFromMaxYearNotSupported, 2017));
        }
        public async void GetFirstPageAccidentStatisticsCorrect()
        {
            var accidentStatistics = AutoFixture.CreateMany <AccidentStatistic>();

            HttpTest.RespondWithJson(accidentStatistics, 200);

            var actual = await TransportForLondonClient.GetAccidentStatistics(2016, 1, 5);

            actual.Should().NotBeNull();
            actual.Data.Should().NotBeNull();
            actual.Total.Should().Be(3);
            actual.Page.Should().Be(1);
            actual.PageSize.Should().Be(3);
        }
        public async void FilterWithFatalSeverityAndSortAllDataByDateAscending()
        {
            var accidentStatistics = LoadAll2017AccidentTestData();

            HttpTest.RespondWithJson(accidentStatistics, 200);

            var actual = await TransportForLondonClient.GetAccidentStatistics(
                year : 2017,
                pageSize : 300,
                filter : filter => filter.Severity == Severity.Fatal,
                sortOptions : ByDateAscending);

            actual.Data.Count().Should().Be(262);
            actual.Data.First().DateAsString.Should().Be("2017-01-05T09:11:00Z");
            actual.Data.Last().DateAsString.Should().Be("2017-12-29T10:58:00Z");
        }
        public async void SwapValuesIfFromDateGreaterThanToDate()
        {
            var accidentStatistics = Get2017AccidentData();

            HttpTest.ResponseQueue.Enqueue(CreateHttpResponseMessage(accidentStatistics));
            HttpTest.ResponseQueue.Enqueue(CreateHttpResponseMessage(accidentStatistics));

            await TransportForLondonClient.GetAccidentStatistics(
                from : DateTime.Parse("01 December 2017 16:13:00"),
                to : DateTime.Parse("01 January 2016 09:11:00"),
                pageSize : 300,
                severity : Severity.Fatal,
                sortOptions : ByDateAscending);

            HttpTest.ShouldHaveCalled("https://fake-api.tfl.gov.uk/AccidentStats/2016");
            HttpTest.ShouldHaveCalled("https://fake-api.tfl.gov.uk/AccidentStats/2017");
        }
        public async void FilterAllDataWithFatalSeverity()
        {
            var accidentStatistics = LoadAll2017AccidentTestData();

            HttpTest.RespondWithJson(accidentStatistics, 200);

            var actual = await TransportForLondonClient.GetAccidentStatistics(
                year : 2017,
                pageSize : 300,
                filter : filter => filter.Severity == Severity.Fatal);

            actual.Should().NotBeNull();
            actual.Data.Should().NotBeNull();
            actual.Total.Should().Be(262); // And not 54178
            actual.Page.Should().Be(1);
            actual.PageSize.Should().Be(262);
            actual.Data.Count().Should().Be(262);
        }
        public async void FilterWithFatalSeverityAndDateRangeAndSortDataAscending()
        {
            var accidentStatistics = LoadAll2017AccidentTestData();

            HttpTest.RespondWithJson(accidentStatistics, 200);

            var actual = await TransportForLondonClient.GetAccidentStatistics(
                year : 2017,
                pageSize : 300,
                filter : filter =>
                filter.Severity == Severity.Fatal &&
                filter.Date >= DateTime.Parse("05 January 2017 09:11:00") &&
                filter.Date <= DateTime.Parse("10 January 2017 16:13:00"),
                sortOptions : ByDateAscending);

            actual.Total.Should().Be(10);
            actual.Data.Count().Should().Be(10);
            actual.Data.First().DateAsString.Should().Be("2017-01-05T09:11:00Z");
            actual.Data.Last().DateAsString.Should().Be("2017-01-10T16:13:00Z");
        }
Exemplo n.º 10
0
        private async Task GenerateDataFromLiveFeed(AccidentStatisticDbContext context)
        {
            _logger.Debug("About to generate data from the TFL live feed");
            var configuration = Configuration.Create();
            var logger        = Logger.Create();
            ITransportForLondonClient transportForLondonClient = new TransportForLondonClient(configuration, logger);
            var lastYear  = configuration.MaximumYear;
            var firstYear = 2005;

            if (IsTestDatabase(context))
            {
                firstYear = 2017;
            }
            for (int year = lastYear; year >= firstYear; year--)
            {
                try
                {
                    _logger.Information($"Getting data for year '{year}'");
                    Stopwatch stopwatch          = Stopwatch.StartNew();
                    var       accidentStatistics =
                        await transportForLondonClient.GetAllAccidentStatistics(year).ConfigureAwait(false);

                    stopwatch.Stop();
                    _logger.Debug($"Took '{stopwatch.Elapsed.ToString()}' to retrieve data for year '{year}'");
                    stopwatch = Stopwatch.StartNew();
                    await GeneraDataFor(context, accidentStatistics);

                    stopwatch.Stop();
                    _logger.Debug($"Took '{stopwatch.Elapsed.ToString()}' to insert data for year '{year}' into the local database");
                }
                catch (Exception e)
                {
                    Trace.TraceError($"Failed to get data for year '{year}'", e.Message, e.InnerException?.Message);
                }
            }
        }
Exemplo n.º 11
0
 public TransportForLondonClientShould()
 {
     this.HttpTest = new HttpTest();
     this.TransportForLondonClient = new TransportForLondonClient(Configuration.Create(), Logger.Create());
     this.AutoFixture = new Fixture();
 }
Exemplo n.º 12
0
 public TransportForLondonClientShould()
 {
     transportForLondonClient = new TransportForLondonClient(
         Configuration.Create(),
         Logger.Create());
 }