public void CreaeteFromMealAndUserDataTest_ShouldCreateUserMealProfileObjectWithAllRelevantDataOfInputParameters()
        {
            Meal userMeal = new Meal()
            {
                Id = Guid.NewGuid(),
                PreMealSugarLevel = 290,
                PostMealSugarLevel = 170
            };
            string userName = "******";
            UserBaseInsulinCalcProfile insulineProfile = new RapidInsulinProfile()
            {
                UnitReductionValue = 40,
                InsulinCarbohydrateRatio = 12.5,
                MaxSugarRange = 150
            };
            SpontaneousUserModel userData = new SpontaneousUserModel()
            {
                BaseInsulinCalcProfile = insulineProfile,
            };

            //act
            UserMealProfileFactory mealFactory = new UserMealProfileFactory();
            UserMealProfile mealProfile = mealFactory.CreaeteFromMealAndUserData(userMeal, userName, userData);

            //assert
            Assert.IsNotNull(mealProfile);
            Assert.AreEqual(mealProfile.MealId, userMeal.Id);
            Assert.AreEqual(mealProfile.UserName, userName);
            Assert.AreEqual(mealProfile.UserInsulinProfile, insulineProfile);
        }
        public void CreaeteFromMealAndUserDataTest_ShouldCreateUserMealProfileObjectWithCorrectCalculatedCarbsDeviation_and_MealCarbsAmount_Properies()
        {
            Meal userMeal = new Meal()
            {
                Id = Guid.NewGuid(),
                PreMealSugarLevel = 290,
                PostMealSugarLevel = 170,
                CarbAmount = 85
            };
            string userName = "******";
            UserBaseInsulinCalcProfile insulineProfile = new RapidInsulinProfile()
            {
                UnitReductionValue = 40,
                InsulinCarbohydrateRatio = 12.5,
                MaxSugarRange = 150
            };
            SpontaneousUserModel userData = new SpontaneousUserModel()
            {
                BaseInsulinCalcProfile = insulineProfile,
            };

            //act
            UserMealProfileFactory mealFactory = new UserMealProfileFactory();
            UserMealProfile mealProfile = mealFactory.CreaeteFromMealAndUserData(userMeal, userName, userData);

            AlgorithmCalculations.AlgorithmResult algResult = AlgorithmCalculations.CalcMealCarbsDeviation(userMeal, mealProfile.UserInsulinProfile);

            //assert
            Assert.IsNotNull(mealProfile);
            Assert.AreEqual(mealProfile.CalculatedCarbsDeviation, algResult.Result);
            Assert.AreEqual(mealProfile.MealCarbsAmount, mealProfile.ExpectedCarbsAmount + algResult.Result);
        }
        internal void UpdatePostMealSugar(int sugarLevel)
        {
            var userProfile = GetUserProfile();
            var userData = userProfile.GetUserData();
            if (userData.UserMeals == null)
            {
                log.WarnFormat("[UpdatePostMealSugar] User={0} tried to update postmeal sugar, but meal doesn't exists sugarLevel={1}.", userProfile.UserName, sugarLevel);
                return;
            }

            var userMeal = userData.UserMeals.OrderByDescending(c => c.CreatedAt).ToList().FirstOrDefault();
            if (userMeal != null)
            {
                userMeal.PostMealSugarLevel = sugarLevel;
                userMeal.PostMealSugarUpdatedAt = DateTime.UtcNow;
                userProfile.SetUserData(userData);
                userProfile.Save();

                //Create userMeailProfile object and save in DB
                UserMealProfileFactory mealProfileFactory = new UserMealProfileFactory();
                mealProfileFactory.SaveDataInUserMealProfile(userMeal, userProfile.UserName, userData);

                log.InfoFormat("[UpdatePostMealSugar] user={0}, meal={1}, sugarLevel={2}.", userProfile.UserName, userMeal, sugarLevel);
            }
            else log.ErrorFormat("[UpdatePostMealSugar] Error: Can't find last meal.");
        }
        public void CreaeteFromMealAndUserDataTest_ShouldReturnNullIfOneOfInputParametersIsNullOrEmpty()
        {
            Meal userMeal = new Meal()
            {
                Id = Guid.NewGuid(),
                PreMealSugarLevel = 290,
                PostMealSugarLevel = 170
            };
            string userName = "******";
            UserBaseInsulinCalcProfile insulineProfile = new RapidInsulinProfile()
            {
                UnitReductionValue = 40,
                InsulinCarbohydrateRatio = 12.5,
                MaxSugarRange = 150
            };
            SpontaneousUserModel userData = new SpontaneousUserModel()
            {
                BaseInsulinCalcProfile = insulineProfile,
            };

            //act
            UserMealProfileFactory mealFactory = new UserMealProfileFactory();
            UserMealProfile mealProfile1 = mealFactory.CreaeteFromMealAndUserData(null, userName, userData);
            UserMealProfile mealProfile2 = mealFactory.CreaeteFromMealAndUserData(userMeal, null, userData);
            UserMealProfile mealProfile3 = mealFactory.CreaeteFromMealAndUserData(userMeal, userName, null);

            //assert
            Assert.IsNull(mealProfile1);
            Assert.IsNull(mealProfile2);
            Assert.IsNull(mealProfile3);
        }