public void TestInitialize()
 {
     var fixture = new Fixture();
     factories = Substitute.For<Factories>();
     navigator = Substitute.For<Navigator>();
     stateProvider = Substitute.For<StateProvider>();
     facade = Substitute.For<ReplacementBuilderAndSugarEstimatorFacade>();
     clipboard = Substitute.For<Clipboard>();
     messageDialog = Substitute.For<MessageDialog>();
     navigation = new InsulinEditingViewModel.Navigation();
     CreateSut();
     insulin = fixture.Create<Insulin>();
     insulin.InitializeCircumstances(new List<Guid>());
     insulin.SetOwner(factories);
     sugar = new Sugar();
     sugar.SetOwner(factories);
     factories.InsulinCircumstances.Returns(fixture.CreateMany<InsulinCircumstance>().ToList());
     factories.CreateSugar().Returns(sugar);
     settings = new Settings { MaxBolus = 5 };
     factories.Settings.Returns(settings);
     meal = fixture.Create<Meal>();
     factories.Finder.FindMealByInsulin(insulin).Returns(meal);
     factories.Finder.FindInsulinById(insulin.Id).Returns(insulin);
     var replacementAndEstimatedSugars = new ReplacementAndEstimatedSugars();
     replacementAndEstimatedSugars.EstimatedSugars = new List<Sugar>();
     replacementAndEstimatedSugars.Replacement
         = new Replacement { InsulinTotal = new Insulin(), Items = new List<ReplacementItem>() };
     facade.GetReplacementAndEstimatedSugars(Arg.Any<Meal>(), Arg.Any<Insulin>(), Arg.Any<Sugar>())
             .Returns(replacementAndEstimatedSugars);
     factories.MealNames.Returns(new List<MealName>());
     stateProvider.State.Returns(new Dictionary<string, object>());
 }
 public IList<Pattern> GetPatternsFor(Insulin insulin, Meal meal, Sugar currentBefore = null)
 {
     var patterns = new List<Pattern>();
     finder = factories.Finder;
     settings = factories.Settings;
     searchedInsulin = insulin;
     searchedMeal = meal;
     searchedItems = meal.NormalizedItems();
     searchedSugarBefore = currentBefore;
     foreach (var testMeal in factories.Meals.Where(m => m != searchedMeal))
     {
         var mealHasMatch = testMeal.Items.Any(item =>
             searchedItems.Any(searchedItem =>
                 item.ProductId == searchedItem.ProductId && item.Unit == searchedItem.Unit));
         if (mealHasMatch)
             foreach (var item in testMeal.NormalizedItems())
                 foreach (var searchedItem in searchedItems)
                     if (item.ProductId == searchedItem.ProductId && item.Unit == searchedItem.Unit)
                     {
                         this.searchedItem = searchedItem;
                         this.item = item;
                         this.meal = testMeal;
                         if (ConsiderPattern())
                             patterns.Add(BuildPattern());
                     }
     }
     return patterns;
 }
Esempio n. 3
0
 public void BloodSugarInMgdL(float from, SugarUnit unit, float expectedTo)
 {
     var factories = new FactoriesImpl();
     factories.StorageCreator = new StorageCreatorStub();
     factories.Settings.SugarUnit = unit;
     var sut = new Sugar { BloodSugar = from };
     sut.SetOwner(factories);
     Assert.AreEqual(expectedTo, sut.BloodSugarInMgdL);
 }
 public void AddSugar()
 {
     var sugar = new Sugar { BloodSugar = 110 };
     factories.CreateSugar().Returns(sugar).AndDoes(_ => factories.Sugars.Add(sugar));
     sut.Load();
     var command = new JournalViewModel.AddSugarCommand();
     sut.Add(command);
     sugarEditing.Received().Show(Arg.Is<SugarViewModel>(vm => "110" == vm.BloodSugar));
     Assert.AreEqual(1, sut.Items.Count);
     navigator.DidNotReceive().GoToNewInsulin();
 }
 public void AddSugarAndSugarEditingCancel()
 {
     var sugar = new Sugar();
     factories.CreateSugar().Returns(sugar).AndDoes(_ => factories.Sugars.Add(sugar));
     sut.Load();
     var command = new JournalViewModel.AddSugarCommand();
     sut.Add(command);
     sugarEditing.Cancel.Call();
     Assert.IsEmpty(factories.Sugars);
     Assert.IsEmpty(sut.Items);
 }
 public void TestInitialize()
 {
     sut = new Sut();
     var sugar = new Sugar();
     var factories = Substitute.For<Factories>();
     sugarViewModel = new SugarViewModel(sugar, factories);
     var stateProvider = Substitute.For<StateProvider>();
     stateProvider.State.Returns(new Dictionary<string, object>());
     factories.Settings.Returns(new Settings());
     sut.StateProvider = stateProvider;
 }
 public void ChooseAndSugarEditingCancel()
 {
     var sugar = new Sugar();
     factories.Sugars.Add(sugar);
     sut.Load();
     var viewModel = new SugarViewModel(sugar, factories);
     sut.Choose(viewModel);
     sugarEditing.Cancel.Call();
     Assert.IsNotEmpty(factories.Sugars);
     Assert.IsNotEmpty(sut.Items);
 }
 public void Choose(JournalItemViewModel vm)
 {
     if (vm is SugarViewModel)
     {
         editedSugar = (vm as SugarViewModel).Sugar;
         var sugarCopy = editedSugar.GetCopy();
         editedSugarViewModel = new SugarViewModel(sugarCopy, this.factories);
         sugarEditing.Show(editedSugarViewModel);
     }
     else
         vm.Choose(Navigator);
     choosedAnAdded = false;
 }
 public ReplacementAndEstimatedSugars GetReplacementAndEstimatedSugars(Meal meal, Insulin insulin,
     Sugar currentBefore)
 {
     var patterns = patternBuilder.GetPatternsFor(insulin, meal,
         currentBefore: currentBefore);
     var replacement = replacementBuilder.GetReplacementFor(meal, patterns);
     var estimatedSugars = sugarEstimator.GetEstimatedSugarsAfter(meal, currentBefore,
         usingReplacementItems: replacement.Items);
     return new ReplacementAndEstimatedSugars
     {
         Replacement = replacement,
         EstimatedSugars = estimatedSugars
     };
 }
 public void GetReplacementAndEstimatedSugars()
 {
     var patternBuilder = Substitute.For<PatternBuilder>();
     var replacementBuilder = Substitute.For<ReplacementBuilder>();
     var sugarEstimator = Substitute.For<SugarEstimator>();
     var sut = new ReplacementBuilderAndSugarEstimatorFacadeImpl(patternBuilder, replacementBuilder,
         sugarEstimator);
     var meal = new Meal();
     var insulin = new Insulin();
     var sugar = new Sugar();
     var patterns = new List<Pattern>();
     var replacement = new Replacement { Items = new List<ReplacementItem>() };
     var estimatedSugars = new List<Sugar>();
     patternBuilder.GetPatternsFor(insulin, meal, currentBefore: sugar).Returns(patterns);
     replacementBuilder.GetReplacementFor(meal, patterns).Returns(replacement);
     sugarEstimator.GetEstimatedSugarsAfter(meal, sugar, replacement.Items).Returns(estimatedSugars);
     var result = sut.GetReplacementAndEstimatedSugars(meal, insulin, sugar);
     Assert.AreSame(replacement, result.Replacement);
     Assert.AreSame(estimatedSugars, result.EstimatedSugars);
 }
Esempio n. 11
0
 public void Relate(Sugar currentBefore, List<CollectedSugar> collectedSugars)
 {
     foreach (var collected in collectedSugars)
         collected.Related = new Sugar
         {
             DateTime = collected.Collected.DateTime,
             BloodSugar = currentBefore.BloodSugar
                 + (collected.Collected.BloodSugar - collected.Source.Pattern.Before.BloodSugar)
                 * collected.Source.Pattern.Factor
         };
 }
 public void WhenSugarAlreadyExistsCalculatesImmediately(bool openedWithNoBolus)
 {
     var sugar = new Sugar { BloodSugar = 100 };
     factories.Finder.FindSugarBeforeInsulin(insulin).Returns(sugar);
     if (openedWithNoBolus)
         insulin.NormalBolus = insulin.SquareWaveBolus = 0;
     InitializeViewModel();
     Assert.IsTrue(sut.IsCalculated);
     facade.ReceivedWithAnyArgs().GetReplacementAndEstimatedSugars(null, null, null);
 }
Esempio n. 13
0
 public SugarViewModel(Sugar sugar, Factories factories)
 {
     Sugar = sugar;
     this.factories = factories;
 }
 public void FindItem()
 {
     var sugar = new Sugar { Id = Guid.NewGuid() };
     var insulin = new Insulin { Id = Guid.NewGuid() };
     var meal = new Meal { Id = Guid.NewGuid() };
     factories.Sugars.Add(sugar);
     factories.Insulins.Add(insulin);
     factories.Meals.Add(meal);
     sut.Load();
     Assert.IsNull(sut.FindItem(Guid.Empty));
     Assert.IsInstanceOf<SugarViewModel>(sut.FindItem(sugar.Id));
     Assert.IsInstanceOf<InsulinViewModel>(sut.FindItem(insulin.Id));
     Assert.IsInstanceOf<MealViewModel>(sut.FindItem(meal.Id));
 }
 public void TombstoneAndUntombstoneWhenSugarEditing()
 {
     var sugar = new Sugar { DateTime = DateTime.Now };
     factories.Sugars.Add(sugar);
     var viewModel = new SugarViewModel(sugar, factories);
     sut.Load();
     sut.Choose(viewModel);
     viewModel.BloodSugar = "100";
     sut.Tombstone();
     sugarEditing.Cancel.Call();
     sut.Untombstone();
     Assert.IsTrue(sugarEditing.IsVisible);
     sugarEditing.Confirm.Call();
     Assert.AreEqual(100, sugar.BloodSugar);
 }
 public void MakeViewModelCopiesFoundSugar()
 {
     var sugar = new Sugar { BloodSugar = 150 };
     factories.Finder.FindSugarBeforeInsulin(insulin).Returns(sugar);
     InitializeViewModel();
     sut.CurrentSugar.BloodSugar = "155";
     Assert.AreEqual(150, sugar.BloodSugar);
 }
 public void FindDate()
 {
     var sugar = new Sugar { DateTime = DateTime.Now };
     factories.Sugars.Add(sugar);
     sut.Load();
     Assert.AreEqual(sugar.DateTime.Date, sut.FindDate(sugar.DateTime.Date).Date);
 }
 public void ChooseAndSugarEditingDelete()
 {
     var sugar = new Sugar();
     factories.Sugars.Add(sugar);
     sut.Load();
     var viewModel = new SugarViewModel(sugar, factories);
     sut.Choose(viewModel);
     Assert.IsTrue(sugarEditing.CanDelete);
     sugarEditing.Delete.Call();
     Assert.IsEmpty(factories.Sugars);
     Assert.IsEmpty(sut.Items);
 }
 public void ChooseCreatesACopyOfSugar()
 {
     var sugar = new Sugar { BloodSugar = 100 };
     var viewModel = new SugarViewModel(sugar, factories);
     sut.Choose(viewModel);
     sugarEditing.Subject.BloodSugar = "110";
     Assert.AreEqual(100, sugar.BloodSugar);
 }
 public void CancelAndReturnDoesntDeleteTheFoundSugar()
 {
     var sugar = new Sugar();
     factories.Finder.FindSugarBeforeInsulin(insulin).Returns(sugar);
     factories.Sugars.Returns(new List<Sugar> { sugar });
     InitializeViewModel();
     sut.CancelAndReturn();
     Assert.IsNotEmpty(factories.Sugars);
 }
 public void ConvertsTimeToLocal()
 {
     var sugar = new Sugar { DateTime = DateTime.UtcNow };
     var sut = new InsulinEditingViewModel.SugarChartItemViewModel(sugar);
     Assert.AreEqual(sugar.DateTime.ToLocalTime(), sut.DateTime);
 }
Esempio n. 22
0
 public IList<Sugar> GetEstimatedSugarsAfter(Meal meal, Sugar currentBefore,
     IList<ReplacementItem> usingReplacementItems)
 {
     var collectedByHour = sugarCollector.CollectByHour(meal, usingReplacementItems);
     var collectedSugars = collectedByHour.Values.SelectMany(values => values).ToList();
     sugarRelator.Relate(currentBefore, collectedSugars);
     sugarWeighter.Weigth(meal, collectedSugars);
     var result = sugarAggregator.Aggregate(collectedByHour);
     return result.Keys.ToList();
 }
 public void ShouldNotFocusSugarWhenExistingSugar()
 {
     var sugar = new Sugar { BloodSugar = 150 };
     factories.Finder.FindSugarBeforeInsulin(insulin).Returns(sugar);
     InitializeViewModel();
     Assert.IsFalse(sut.ShouldFocusSugar);
 }
 public void MakeViewModelFindsSugar()
 {
     var sugar = new Sugar { BloodSugar = 150 };
     factories.Finder.FindSugarBeforeInsulin(insulin).Returns(sugar);
     InitializeViewModel();
     Assert.AreEqual(sugar.BloodSugar.ToString(), sut.CurrentSugar.BloodSugar);
 }
 public void ChooseAndSugarEditingConfirm()
 {
     var sugar = new Sugar { BloodSugar = 100 };
     factories.Sugars.Add(sugar);
     sut.Load();
     var items = sut.Items.ToList();
     var viewModel = new SugarViewModel(sugar, factories);
     sut.Choose(viewModel);
     sugarEditing.Subject.BloodSugar = "110";
     sugarEditing.Confirm.Call();
     Assert.AreEqual(110, sugar.BloodSugar);
     Assert.AreNotEqual(items, sut.Items, "Should refresh");
 }
 public void ChooseWhenSugar()
 {
     var sugar = new Sugar { BloodSugar = 100 };
     var viewModel = new SugarViewModel(sugar, factories);
     sut.Choose(viewModel);
     sugarEditing.Received().Show(Arg.Is<SugarViewModel>(vm => "100" == vm.BloodSugar));
 }
 private void SetupSugars()
 {
     var sugar = new Sugar();
     sugar.SetOwner(factories);
     sugar.Id = Guid.NewGuid();
     factories.Sugars.Returns(new List<Sugar> { sugar });
     factories.CreateSugar().Returns(_ => { factories.Sugars.Add(sugar); return sugar; });
 }
 public void DeleteAndSaveAndReturnDoesntDeleteTheFoundSugar()
 {
     var sugar = new Sugar();
     factories.Finder.FindSugarBeforeInsulin(insulin).Returns(sugar);
     factories.Sugars.Returns(new List<Sugar> { sugar });
     factories.Insulins.Returns(new List<Insulin>());
     InitializeViewModel();
     messageDialog.Confirm(null, null).ReturnsForAnyArgs(true);
     sut.DeleteAndSaveAndReturn();
     Assert.IsNotEmpty(factories.Sugars);
 }