Example #1
0
        public async Task GetChangesWithOneScheduleWhichAlreadyExistsAndDidNotChangeReturnsEmptyListTest(string email)
        {
            // arrange
            var ct    = new CancellationToken(false);
            var weeks = new Dictionary <int, WeekItem>
            {
                { 0, new WeekItem(6, 2015) },
            };

            IFlexKidsClient     flexKidsClient     = A.Fake <IFlexKidsClient>();
            IKseParser          parser             = A.Fake <IKseParser>();
            IScheduleRepository scheduleRepository = A.Dummy <IScheduleRepository>();
            var sut = new Scheduler(flexKidsClient, parser, scheduleRepository);

            _ = A.CallTo(() => parser.GetIndexContent(A <string> ._))
                .Returns(new IndexContent
            {
                Email = email,
                Weeks = weeks,
            });
            _ = A.CallTo(() => flexKidsClient.GetSchedulePage(0, ct)).Returns("GetSchedulePage0");
            _ = A.CallTo(() => scheduleRepository.Get(2015, 6))
                .Returns(new WeekSchedule {
            });

            // act
            IEnumerable <ScheduleDiff> result = await sut.ProcessAsync(ct);

            // assert
            Assert.Empty(result);
            _ = A.CallTo(() => flexKidsClient.GetAvailableSchedulesPage(ct)).MustHaveHappenedOnceExactly();
            _ = A.CallTo(() => parser.GetIndexContent(A <string> ._)).MustHaveHappenedOnceExactly();
            _ = A.CallTo(() => flexKidsClient.GetSchedulePage(0, ct)).MustHaveHappenedOnceExactly();
            _ = A.CallTo(() => scheduleRepository.Get(2015, 6)).MustHaveHappenedOnceExactly();
        }
Example #2
0
        public async Task GetChangesWithNoChangesReturnsEmptyListTest(string email)
        {
            // arrange
            var             ct             = new CancellationToken(false);
            IFlexKidsClient flexKidsClient = A.Fake <IFlexKidsClient>();
            IKseParser      parser         = A.Fake <IKseParser>();

            _ = A.CallTo(() => parser.GetIndexContent(A <string> ._))
                .Returns(new IndexContent
            {
                Email = email,
                Weeks = new Dictionary <int, WeekItem>(),
            });

            IScheduleRepository scheduleRepository = A.Dummy <IScheduleRepository>();
            var sut = new Scheduler(flexKidsClient, parser, scheduleRepository);

            // act
            IEnumerable <ScheduleDiff> result = await sut.ProcessAsync(ct);

            // assert
            Assert.Empty(result);
            _ = A.CallTo(() => flexKidsClient.GetAvailableSchedulesPage(ct)).MustHaveHappenedOnceExactly();
            _ = A.CallTo(() => parser.GetIndexContent(A <string> ._)).MustHaveHappenedOnceExactly();

            A.CallTo(() => flexKidsClient.GetSchedulePage(A <int> ._, ct)).MustNotHaveHappened();
            A.CallTo(scheduleRepository).MustNotHaveHappened();
        }
        public async Task <IEnumerable <ScheduleDiff> > ProcessAsync(CancellationToken ct = default)
        {
            var indexPage = await _flexKidsClient.GetAvailableSchedulesPage(ct);

            IndexContent indexContent          = _parser.GetIndexContent(indexPage);
            var          weekSchedulesToImport = new List <WeekAndImportedSchedules>(indexContent.Weeks.Count);

            foreach (KeyValuePair <int, WeekItem> item in indexContent.Weeks)
            {
                var htmlComboboxIndex = item.Key;
                var year       = item.Value.Year;
                var weekNumber = item.Value.WeekNr;

                var htmlSchedule = await _flexKidsClient.GetSchedulePage(htmlComboboxIndex, ct);

                var parsedSchedules = _parser.GetScheduleFromContent(htmlSchedule, year).ToList();

                WeekSchedule weekSchedule = await _repo.Get(year, weekNumber);

                weekSchedulesToImport.Add(new WeekAndImportedSchedules
                {
                    WeekSchedule  = weekSchedule,
                    ScheduleItems = parsedSchedules,
                });
            }

            return(await ProcessRawData(weekSchedulesToImport));
        }
Example #4
0
        public IEnumerable <ScheduleDiff> GetChanges()
        {
            var rooterFirstPage  = flexKidsConnection.GetAvailableSchedulesPage();
            var indexContent     = parser.GetIndexContent(rooterFirstPage);
            var somethingChanged = false;
            var weekAndHtml      = new Dictionary <int, WeekAndHtml>(indexContent.Weeks.Count);

            foreach (var i in indexContent.Weeks)
            {
                var htmlSchedule = flexKidsConnection.GetSchedulePage(i.Key);
                var htmlHash     = hash.Hash(htmlSchedule);
                var week         = repo.GetWeek(i.Value.Year, i.Value.WeekNr);

                if (week == null || htmlHash != week.Hash)
                {
                    somethingChanged = true;
                }

                weekAndHtml.Add(i.Key, new WeekAndHtml
                {
                    Week            = GetCreateOrUpdateWeek(week, i.Value.Year, i.Value.WeekNr, htmlHash),
                    Hash            = htmlHash,
                    Html            = htmlSchedule,
                    ScheduleChanged = (week == null || htmlHash != week.Hash)
                });
            }

            if (somethingChanged == false)
            {
                return(Enumerable.Empty <ScheduleDiff>());
            }

            var diffsResult = new List <ScheduleDiff>();

            foreach (var item in weekAndHtml.Select(a => a.Value))
            {
                var dbSchedules = repo.GetSchedules(item.Week.Year, item.Week.WeekNr);
                IList <ScheduleDiff> diffResult;
                if (item.ScheduleChanged)
                {
                    var parsedSchedules = parser.GetScheduleFromContent(item.Html, item.Week.Year);
                    diffResult = GetDiffs(dbSchedules, parsedSchedules, item.Week);

                    var schedulesToDelete = diffResult
                                            .Where(x => x.Status == ScheduleStatus.Removed)
                                            .Select(x => x.Schedule);
                    repo.Delete(schedulesToDelete);

                    var schedulesToInsert = diffResult
                                            .Where(x => x.Status == ScheduleStatus.Added)
                                            .Select(x => x.Schedule);
                    foreach (var schedule in schedulesToInsert)
                    {
                        repo.Insert(schedule);
                    }

                    OnScheduleChanged(diffResult.OrderBy(x => x.Start).ThenBy(x => x.Status));
                }
                else
                {
                    diffResult = new List <ScheduleDiff>(dbSchedules.Count);
                    foreach (var dbSchedule in dbSchedules)
                    {
                        diffResult.Add(new ScheduleDiff
                        {
                            Schedule = dbSchedule,
                            Status   = ScheduleStatus.Unchanged
                        });
                    }
                }
                diffsResult.AddRange(diffResult);
            }

            return(diffsResult);
        }