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; }
public InsulinViewModel(Insulin insulin, Factories factories, IList<InsulinCircumstanceViewModel> allCircumstances) { Insulin = insulin; this.factories = factories; this.allCircumstances = allCircumstances; }
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 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); }
public void ExportsAndImportsSugarsAndInsulinsAndInsulinCircumstances() { var sugar = factories.Sugars[0]; var insulin = factories.Insulins[0]; var insulinCircumstance = factories.InsulinCircumstances[0]; var sut = new ExportAndImportImpl(factories); var data = sut.Export(); factories.Sugars.Clear(); factories.Insulins.Clear(); factories.InsulinCircumstances.Clear(); var empty = new Insulin(); empty.InitializeCircumstances(new List<Guid>()); insulin.CopyCircumstancesFrom(empty); Assert.IsEmpty(insulin.Circumstances); sut.Import(data); Assert.AreEqual(sugar.Id, factories.Sugars[0].Id); Assert.AreEqual(insulin.Id, factories.Insulins[0].Id); Assert.AreSame(factories.InsulinCircumstances[0], factories.Insulins[0].Circumstances.Single()); Assert.AreEqual(insulinCircumstance.Id, factories.InsulinCircumstances[0].Id); sut.Import(data); Assert.AreEqual(1, factories.Sugars.Count); Assert.AreEqual(1, factories.Insulins.Count); Assert.AreEqual(1, factories.InsulinCircumstances.Count); }
private void SetupInsulins() { var insulin = new Insulin(); insulin.SetOwner(factories); insulin.Id = Guid.NewGuid(); insulin.InitializeCircumstances(new List<Guid>()); factories.Insulins.Returns(new List<Insulin> { insulin }); factories.CreateInsulin().Returns(_ => { factories.Insulins.Add(insulin); return insulin; }); var insulinCircumstance = new InsulinCircumstance(); insulinCircumstance.SetOwner(factories); insulinCircumstance.Id = Guid.NewGuid(); insulin.AddCircumstance(insulinCircumstance); factories.InsulinCircumstances.Returns(new List<InsulinCircumstance> { insulinCircumstance }); factories.CreateInsulinCircumstance() .Returns(_ => { factories.InsulinCircumstances.Add(insulinCircumstance); return insulinCircumstance; }); }
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 ChooseWhenInsulin() { var navigator = new Mock<Navigator>(); // Why this test isn't stable with NSubstitute instead of Moq? sut.Navigator = navigator.Object; var insulin = new Insulin { Id = Guid.NewGuid() }; var viewModel = new InsulinViewModel(insulin, factories, new List<InsulinCircumstanceViewModel>()); sut.Choose(viewModel); navigator.Verify(Navigator => Navigator.GoToInsulinEditing(insulin.Id)); }
public void Do(ReplacementBuilderImpl replacementBuilder) { var insulinTotal = new Insulin(); var patterns = replacementBuilder.replacement.Items.Select(item => item.Pattern); foreach (var pattern in patterns) { var meal = pattern.From; var totalCuInMeal = meal.Items.Sum(item => item.Cu); var percentOfCu = totalCuInMeal == 0 ? 0 : pattern.Match.Cu / totalCuInMeal; insulinTotal.NormalBolus += (float)Math.Round(percentOfCu * pattern.Insulin.NormalBolus * pattern.Factor, 1); var totalFpuInMeal = meal.Items.Sum(item => item.Fpu); var percentOfFpu = totalFpuInMeal == 0 ? 0 : pattern.Match.Fpu / totalFpuInMeal; insulinTotal.SquareWaveBolus += (float)Math.Round(percentOfFpu * pattern.Insulin.SquareWaveBolus * pattern.Factor, 1); insulinTotal.SquareWaveBolusHours += (float)Math.Round(percentOfFpu * pattern.Insulin.SquareWaveBolusHours * pattern.Factor, 1); } replacementBuilder.replacement.InsulinTotal = insulinTotal; }
public void CopyCircumstancesFrom(Insulin source) { if (source.circumstances != null) this.circumstances = source.circumstances.ToList(); }
public void GoToInsulinEditingWhenInsulinExists() { sut.Load(); sut.IsDirty = true; var insulin = new Insulin { Id = Guid.NewGuid() }; factories.Finder.FindInsulinByMeal(meal).Returns(insulin); sut.GoToInsulinEditing(); sut.Navigator.Received().GoToInsulinEditingRelatedToMeal(insulin.Id, meal.Id); }
public void TextAndText2() { insulin = new Insulin { Note = string.Empty }; var sut = new InsulinViewModel(insulin, factories, allCircumstances: allCircumstances); sut.NormalBolus = "1"; Assert.AreEqual("1 U", sut.Text); Assert.IsEmpty(sut.Text2); sut.SquareWaveBolus = "2"; Assert.AreEqual("1 U 2 U for ? h", sut.Text); Assert.IsEmpty(sut.Text2); sut.SquareWaveBolusHours = "3"; Assert.AreEqual("1 U 2 U for 3 h", sut.Text); Assert.IsEmpty(sut.Text2); sut.NormalBolus = ""; Assert.AreEqual("2 U for 3 h", sut.Text); Assert.IsEmpty(sut.Text2); sut.Note = "note"; Assert.AreEqual("2 U for 3 h", sut.Text); Assert.AreEqual("note", sut.Text2); sut.NormalBolus = "1"; sut.SquareWaveBolus = ""; Assert.AreEqual("1 U", sut.Text); Assert.AreEqual("note", sut.Text2); }
public void TestInitialize() { insulin = new Fixture().Create<Insulin>(); factories = Substitute.For<Factories>(); factories.Finder.Returns(new FinderImpl(factories)); insulin.SetOwner(factories); var settings = new Settings { MaxBolus = 3 }; factories.Settings.Returns(settings); factories.InsulinCircumstances.Returns(new Fixture().CreateMany<InsulinCircumstance>(5).ToList()); var circumstanceIds = factories.InsulinCircumstances.Take(3) .Select(circumstance => circumstance.Id).ToList(); insulin.InitializeCircumstances(circumstanceIds); allCircumstances = factories.InsulinCircumstances .Select(circumstance => new InsulinCircumstanceViewModel(circumstance, factories)).ToList(); }
public void ChangeModel(Insulin insulin) { Insulin = insulin; }
private byte Points(Insulin left, Insulin right) { var leftCircumstances = left.ReadCircumstances(); var rightCircumstances = right.ReadCircumstances(); var sameCircumstances = leftCircumstances.Intersect(rightCircumstances).Count(); return (byte)(sameCircumstances * POINTS_FOR_SAME_CIRCUMSTANCE); }