private void OnParticipantAdded(object sender, ParticipantEventArgs e) { var partBase = ParticipantBaseMap(e.Participant); partBase.StudyCentre = _repository.FindStudyCentre(partBase.CentreId); var viewModel = new ParticipantListItemViewModel(partBase, _repository); _ageUpdater.AddParticipant(viewModel); AllParticipants.AddNewItem(viewModel); AllParticipants.CommitNew(); SetDisplayName(); }
public void TestAgeUpdatingStartOnAdd() { var moqArgs = new MoqTimerEventArgs(true); var mockTimer = new Mock <IDispatcherTimer>(MockBehavior.Strict); mockTimer.SetupProperty(m => m.Interval); mockTimer.Setup(m => m.Start()).Verifiable(); mockTimer.Setup(m => m.Stop()).Verifiable(); IDispatcherTimer timer = mockTimer.Object; var ageService = new AgeUpdatingService(new ParticipantListItemViewModel[0], timer); mockTimer.Verify(m => m.Start(), Times.Never); TimeSpan toDob = TimeSpan.FromMinutes(-10); var p = new ParticipantListItemViewModel( new ParticipantBaseModel { Id = 1, DateTimeBirth = moqArgs.StartAt + toDob }); TimeSpan expectedInterval = TimeSpan.FromDays(1) + toDob; TimeSpan tolerance = TimeSpan.FromSeconds(defaultSecsTolerance); mockTimer.SetupSet(m => m.Interval = It.IsAny <TimeSpan>()).Callback <TimeSpan>(i => { string usrMsg = string.Format(intervalLogTemplate, i, expectedInterval, tolerance); if (i < expectedInterval - tolerance || i > expectedInterval + tolerance) { throw new ArgumentOutOfRangeException("Interval", usrMsg); } else { Console.WriteLine(usrMsg); } }); ageService.AddParticipant(p); mockTimer.Verify(m => m.Start(), Times.Once); TimeSpan laterDayversary = TimeSpan.FromMinutes(-5); p = new ParticipantListItemViewModel( new ParticipantBaseModel { Id = 2, DateTimeBirth = moqArgs.StartAt + laterDayversary }); ageService.AddParticipant(p); toDob = TimeSpan.FromMinutes(-30); p = new ParticipantListItemViewModel( new ParticipantBaseModel { Id = 2, DateTimeBirth = moqArgs.StartAt + toDob }); expectedInterval = TimeSpan.FromDays(1) + toDob; ageService.AddParticipant(p); toDob = TimeSpan.FromDays(-1).Add(TimeSpan.FromMinutes(5)); p = new ParticipantListItemViewModel( new ParticipantBaseModel { Id = 2, DateTimeBirth = moqArgs.StartAt + toDob }); expectedInterval = TimeSpan.FromDays(1) + toDob; ageService.AddParticipant(p); }
MyMoq TestAgeUpdatingSetInterval(params DateTime[] birthDateTimes) { if (birthDateTimes == null || birthDateTimes.Length < 2) { throw new ArgumentException("must include at least 2 birthDateTimes"); } var moqArgs = new MoqTimerEventArgs(true); var participants = GetParticipants(moqArgs, birthDateTimes); var expectedTimesFromNow = TimesFromNow(birthDateTimes, moqArgs.StartAt); var mockTimer = new Mock <IDispatcherTimer>(MockBehavior.Strict); mockTimer.SetupProperty(m => m.Interval); mockTimer.Setup(m => m.Start()); mockTimer.Setup(m => m.Stop()); TimeSpan expectedInterval = expectedTimesFromNow[0]; TimeSpan tolerance = TimeSpan.FromSeconds(defaultSecsTolerance); bool requiresInterval = true; mockTimer.SetupSet(m => m.Interval = It.IsAny <TimeSpan>()).Callback <TimeSpan>(i => { string usrMsg = string.Format(intervalLogTemplate, i, expectedInterval, tolerance); if (requiresInterval && (i < expectedInterval - tolerance || i > expectedInterval + tolerance)) { throw new ArgumentOutOfRangeException("Interval", usrMsg); } else { Console.WriteLine(usrMsg); } }); IDispatcherTimer timer = mockTimer.Object; Console.WriteLine("instantiating AgeUpdatingService"); var ageService = new AgeUpdatingService(participants, timer); Console.WriteLine("finished instantiation"); mockTimer.Verify(m => m.Start(), Times.AtLeastOnce); Action <TimeSpan, TimeSpan> validateIntervals = new Action <TimeSpan, TimeSpan>((last, curr) => { if (curr.Ticks > 0) { moqArgs.StartAt += last; expectedInterval = curr; mockTimer.Raise(m => m.Tick += null, moqArgs); } }); moqArgs.StartAt += expectedTimesFromNow.First(); //will have been set during instantiation expectedTimesFromNow.AggregatePairSelect((prev, cur) => cur - prev) .Where(t => t.Ticks != 0) .AggregatePairForEach(new TimeSpan(), validateIntervals); requiresInterval = false; mockTimer.Raise(m => m.Tick += null, moqArgs); Console.WriteLine("Adding new participants"); moqArgs.StartAt = DateTime.Now; foreach (var addedInterval in (new int[] { 10, -10 }).Select(i => TimeSpan.FromSeconds(i))) { participants.Add(new ParticipantListItemViewModel( new ParticipantBaseModel { Id = participants.Count + 1, DateTimeBirth = moqArgs.StartAt - TimeSpan.FromDays(1) + addedInterval })); ageService.AddParticipant(participants[participants.Count - 1]); } requiresInterval = true; moqArgs.StartAt += TimeSpan.FromSeconds(10); expectedTimesFromNow = TimesFromNow(from p in participants where p.AgeDays <= 28 select p.DateTimeBirth, moqArgs.StartAt); expectedTimesFromNow.AggregatePairSelect(new TimeSpan(), (prev, cur) => cur - prev) .Where(t => t.Ticks != 0) .AggregatePairForEach(new TimeSpan(), validateIntervals); requiresInterval = false; mockTimer.Raise(m => m.Tick += null, moqArgs); return(new MyMoq { AgeService = ageService, Moq = mockTimer }); }