Inheritance: Entity
        public decimal GetRDI(User user, DateTime date, NutrientEntity nutrientEntity)
        {
            var dailyCalorieNeed = userProfileBusinessLogic.GetDailyCalorieNeed(user);

            //9 kcal/gram ger kolhydrater i energi. 15 - 30 % av vårt energiintag bör bestå av fett
            return (dailyCalorieNeed * 0.3M) / 9;
        }
        public void shouldReturnWeightPrognosisXml()
        {
            AutoMappingsBootStrapper.MapHistoryGraphToAmChartData();

            var user = new User("Min user");
            var userContextMock = new Mock<IUserContext>(MockBehavior.Strict);
            userContextMock.Setup(x => x.User).Returns(user);

            var graphBuilderMock = new Mock<IGraphBuilder>(MockBehavior.Strict);

            ILine line = new Line(1, new Dictionary<DateTime, decimal> { { DateTime.Now.AddDays(1), 35M } }, "MyTitle");
            var graph = new Graph { Labels = new[] { new Label { Value = "val1", Index = "1" } }, LinesContainer = new LinesContainer { Lines = new[] { line } } };
            var userWeightBusinessLogicMock = new Mock<IUserWeightBusinessLogic>();
            userWeightBusinessLogicMock.Setup(x => x.GetProjectionList(It.Is<User>(y => y == user))).Returns(line);
            graphBuilderMock.Setup(x => x.GetGraph(It.Is<ILine[]>(y => y[0] == line))).Returns(graph);

            var resultController = new CarbonFitness.App.Web.Controllers.ResultController(null, null, userContextMock.Object, graphBuilderMock.Object, userWeightBusinessLogicMock.Object, null);

            var result = resultController.ShowWeightPrognosisXml();
            Assert.That(result.ObjectToSerialize, Is.AssignableTo(typeof(AmChartData)));

            var amChartData = (AmChartData) result.ObjectToSerialize;
            var dataPoints = amChartData.DataPoints;
            Assert.That(dataPoints, Is.Not.Null);

            var firstPoint = dataPoints.First();
            Assert.That(firstPoint.Value, Is.EqualTo("val1"));

            Assert.That(amChartData.GraphRoot.Graphs[0].values[0].Value, Is.EqualTo("35"));
            userWeightBusinessLogicMock.Verify();
        }
 public ActivityLevelType GetActivityLevel(User user)
 {
     var activityLevel = GetUserProfile(user).ActivityLevel;
     if(activityLevel == null) {
         return GetActivityLevelFromString("Låg");
     }
     return activityLevel;
 }
 public decimal GetBMI(User user)
 {
     var profile = GetUserProfile(user);
     if(profile.Length == 0 ) {
         return 0;
     }
     var lenghtInMeter = profile.Length / 100;
     return profile.Weight / (lenghtInMeter * lenghtInMeter);
 }
        public ILine GetNutrientHistory(NutrientEntity nutrientEntity, User user)
        {
            var userIngredients = Get100DaysUserIngredients(user);

            var valueSumPerDateFromUserIngredients = GetValueSumPerDateFromUserIngredients(userIngredients, x => x.GetNutrientIngredientDisplayValue(x.GetNutrient(nutrientEntity)));

            int nutrientId = nutrientRepository.GetByName(nutrientEntity.ToString()).Id;
            return new Line(nutrientId, valueSumPerDateFromUserIngredients);
        }
 public UserIngredient AddUserIngredient(User user, string ingredientName, int measure, DateTime dateTime)
 {
     var userIngredient = new UserIngredient();
     userIngredient.User = user;
     userIngredient.Ingredient = GetExistingIngredient(ingredientName);
     userIngredient.Measure = measure;
     userIngredient.Date = dateTime.AddSeconds(1); // Otherwise it will show up on both today and yesterday
     return userIngredientRepository.SaveOrUpdate(userIngredient);
 }
        protected override void LoadTestData()
        {
            var userRepository = new UserRepository();
            userA = userRepository.SaveOrUpdate(new User());

            userProfileRepository = new UserProfileRepository();
            userProfileRepository.SaveOrUpdate(new UserProfile() { User = userA });

            NHibernateSession.Current.Flush();
        }
Beispiel #8
0
        public void SetUp()
        {
            expectedDate = DateTime.Now.AddDays(42).Date;
            expectedWeight = 80M;
            expectedUser = new User {Username = "******"};

            userWeightBusinessLogicMock = new Mock<IUserWeightBusinessLogic>();
            userContextMock = new Mock<IUserContext>();
            userContextMock.Setup(x => x.User).Returns(expectedUser);
        }
 public IEnumerable<SelectListItem> GetViewTypes(User user)
 {
     var nutrientOptions = new List<SelectListItem>();
     addGraphlineOption("Vikt (kg)", "Weight", nutrientOptions);
     IEnumerable<Nutrient> nutrients = nutrientBusinessLogic.GetNutrients();
     foreach (Nutrient nutrient in nutrients) {
         addGraphlineOption(nutrientTranslator.GetString(nutrient.Name), nutrient.Name, nutrientOptions);
     }
     return nutrientOptions;
 }
        public User Create(User user)
        {
            var existingUser = userRepository.Get(user.Username);
            if (existingUser != null) {
                throw new UserAlreadyExistException(user.Username);
            }

            var savedUser = userRepository.Save(user);
            userProfileRepository.Save(new UserProfile {User = savedUser});
            return savedUser;
        }
Beispiel #11
0
        public void shouldGetUserFromThreadIdentity()
        {
            var userBusinessLogicMock = new Mock<IUserBusinessLogic>();
            userBusinessLogicMock.Setup(x => x.Get(It.IsAny<string>())).Returns(new User {Username = "******"});
            var user = new User("myUser");
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("myUser"), null);
            Assert.That(new UserContext(userBusinessLogicMock.Object, null).User.Username, Is.EqualTo(user.Username));

            userBusinessLogicMock.Setup(x => x.Get(It.IsAny<string>())).Returns(new User {Username = "******"});

            var newUserName = "******";
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(newUserName), null);
            Assert.That(new UserContext(userBusinessLogicMock.Object, null).User.Username, Is.EqualTo(newUserName));
        }
        public void shouldCreateUserProfileWhenCreatingAUser()
        {
            var user = new User();
            var userProfileRepositoryMock = new Mock<IUserProfileRepository>(MockBehavior.Strict);
            userProfileRepositoryMock.Setup(x => x.Save(It.Is<UserProfile>(y=> y.User == user))).Returns(new UserProfile());

            var userRepositoryMock = new Mock<IUserRepository>();
            userRepositoryMock.Setup(x => x.Save(user)).Returns(user);

            var userBusinessLogic = new UserBusinessLogic(userRepositoryMock.Object, userProfileRepositoryMock.Object);
            userBusinessLogic.Create(user);

            userProfileRepositoryMock.VerifyAll();
        }
        public UserWeight SaveWeight(User user, decimal weight, DateTime date)
        {
            if (weight == 0) {
                throw new InvalidWeightException("Cannot save zero weight");
            }

            var userWeight = userWeightRepository.FindByDate(user, date);

            if (userWeight == null) {
                userWeight = new UserWeight {Date = date, User = user, Weight = weight};
            }

            userWeight.Weight = weight;

            var savedUserWeight = userWeightRepository.SaveOrUpdate(userWeight);
            return savedUserWeight;
        }
Beispiel #14
0
        public void shouldPassCreatedUserIdToDetails()
        {
            var userName = "******";
            var password = "******";

            var factory = new MockFactory(MockBehavior.Strict);
            var userContextMock = new Mock<IUserContext>();
            var mock = factory.Create<IUserBusinessLogic>();
            var createdUser = new User {Username = userName};
            mock.Setup(x => x.Create(It.Is<User>(y => y.Username == userName))).Returns(createdUser);

            var controller = new CarbonFitness.App.Web.Controllers.UserController(mock.Object, userContextMock.Object);
            var viewResult = (RedirectToRouteResult) controller.Create(userName, password);

            var idRouteValue = viewResult.RouteValues["id"];
            Assert.That(idRouteValue.ToString() == createdUser.Id.ToString(), "Expected id" + createdUser.Id + ", but was:" + idRouteValue);
        }
        public void shouldFindByDate()
        {
            var expectedDate = DateTime.Now.Date;
            var expectedWeight = 80.5M;

            var user = new User("UserWeightRepositoryFindByDateTester");
            var userWeight = new UserWeight {Date = expectedDate, User = user, Weight = expectedWeight};

            NHibernateSession.Current.Save(user);
            NHibernateSession.Current.Save(userWeight);

            var repository = new UserWeightRepository();
            var foundUserWeight = repository.FindByDate(user, expectedDate);

            Assert.That(foundUserWeight, Is.Not.Null);
            Assert.That(foundUserWeight.Date, Is.EqualTo(expectedDate));
            Assert.That(foundUserWeight.Weight, Is.EqualTo(expectedWeight));
        }
        public INutrientAverage GetNutrientAverage(IEnumerable<NutrientEntity> nutrientEntities, User user)
        {
            var result = new NutrientAverage();
            result.NutrientValues = new Dictionary<NutrientEntity, decimal>();

            var nutrientSums = GetNutrientSumList(nutrientEntities, user);
            if(nutrientSums.Count() == 0) {
                return result;
            }

            foreach (var nutrientEntity in nutrientEntities) {
                var nutrientSum = 0M;
                foreach (var sum in nutrientSums) {
                    nutrientSum += sum.NutrientValues[nutrientEntity];
                }
                var average = nutrientSum / nutrientSums.Count();
                result.NutrientValues.Add(nutrientEntity, average);
            }
            return result;
        }
        public void shouldGetHistoryListInDateOrder()
        {
            const string username = "******";
            var user = new User(username);
            NHibernateSession.Current.Save(user);

            for (var i = 0; i < 10; i++) {
                NHibernateSession.Current.Save(new UserWeight { Date = ValueGenerator.getRandomDate(), Weight = i, User = user });
            }

            var result = new UserWeightRepository().GetHistoryList(user);

            Assert.That(result.Count(), Is.EqualTo(10));

            var tempDate = DateTime.MinValue;
            foreach (var userWeight in result) {
                Assert.That(userWeight.Date, Is.GreaterThanOrEqualTo(tempDate));
                tempDate = userWeight.Date;
            }
        }
        public void shouldGetHistoryList()
        {
            const string username = "******";
            var user = new User(username);

            var session = NHibernateSession.Current;

            session.Save(user);

            for (var i = 0; i < 5; i++) {
                session.Save(new UserWeight { Date = DateTime.Now.AddDays(i), Weight = i, User = user });
                session.Flush();
            }

            var repository = new UserWeightRepository();

            var result = repository.GetHistoryList(user);

            Assert.That(result.ElementAt(0).Weight, Is.EqualTo(0));
            Assert.That(result.Count(), Is.EqualTo(5));
            Assert.That(result.Where(x => x.User.Username == username).Count(), Is.EqualTo(5));
        }
        protected override void LoadTestData()
        {
            var ingredientRepository = new IngredientRepository();
            ingredient = ingredientRepository.SaveOrUpdate(new Ingredient {Name = "Pannbiff"});

            user = new UserRepository().Save(new User { Username = "******" });
            user2 = new UserRepository().Save(new User { Username = "******" });
            var user3 = new UserRepository().Save(new User { Username = "******" });
            NHibernateSession.Current.Flush();

            var userIngredientRepository = new UserIngredientRepository();

            userIngredientRepository.SaveOrUpdate(new UserIngredient {Ingredient = ingredient, Measure = 10, User = user, Date = now});
            userIngredientRepository.SaveOrUpdate(new UserIngredient {Ingredient = ingredient, Measure = 100, User = user, Date = now});
            userIngredientRepository.SaveOrUpdate(new UserIngredient {Ingredient = ingredient, Measure = 200, User = user, Date = now.AddDays(-1)});

            userIngredientRepository.SaveOrUpdate(new UserIngredient { Ingredient = ingredient, Measure = 10, User = user2, Date = now });
            userIngredientRepository.SaveOrUpdate(new UserIngredient { Ingredient = ingredient, Measure = 100, User = user3, Date = now });
            userIngredientRepository.SaveOrUpdate(new UserIngredient { Ingredient = ingredient, Measure = 200, User = user2, Date = now.AddDays(-1) });

            NHibernateSession.Current.Flush();
        }
        private void AssertProperty(object expectedResult, User user, UserProfile resultProfile, Func<UserProfileBusinessLogic, object> propertyToFetch)
        {
            var userProfileRepositoryMock = new Mock<IUserProfileRepository>();
            userProfileRepositoryMock.Setup(x => x.GetByUserId(user.Id)).Returns(resultProfile);

            var result = propertyToFetch(new UserProfileBusinessLogic(userProfileRepositoryMock.Object, null, null, null));

            Assert.That(result, Is.EqualTo(expectedResult));
            userProfileRepositoryMock.VerifyAll();
        }
 private UserProfile GetUserProfile(User user)
 {
     if (userProfile == null) {
         userProfile = UserProfileRepository.GetByUserId(user.Id);
         if (userProfile == null) {
             userProfile = new UserProfile {User = user, IdealWeight = 0};
         }
     }
     return userProfile;
 }
 public decimal GetIdealWeight(User user)
 {
     return GetUserProfile(user).IdealWeight;
 }
 public GenderType GetGender(User user)
 {
     var gender = GetUserProfile(user).Gender;
     if(gender == null) {
         return GetGenderTypeFromString("Man"); //Default
     }
     return gender;
 }
 public decimal GetDailyCalorieNeed(User user)
 {
     return CalorieCalculator.GetDailyCalorieNeed(GetWeight(user), GetLength(user), GetAge(user), GetGender(user), GetActivityLevel(user));
 }
 public decimal GetBMR(User user)
 {
     return CalorieCalculator.GetBMR(GetWeight(user), GetLength(user), GetAge(user), GetGender(user));
 }
 public decimal GetLength(User user)
 {
     return GetUserProfile(user).Length;
 }
 public decimal GetWeight(User user)
 {
     return GetUserProfile(user).Weight;
 }
 public IEnumerable<SelectListItem> GetViewTypes(User user)
 {
     return PopulateViewTypes(GenderTypeBusinessLogic.GetGenderTypes().ToArray(), UserProfileBusinessLogic.GetGender(user));
 }
 public int GetAge(User user)
 {
     return GetUserProfile(user).Age;
 }
 public void SaveProfile(User user, decimal idealWeight, decimal length, decimal weight, int age, string genderName, string activityLevelName)
 {
     var userProfile = GetUserProfile(user);
     userProfile.IdealWeight = idealWeight;
     userProfile.Length = length;
     userProfile.Weight = weight;
     userProfile.Age = age;
     userProfile.Gender = GetGenderTypeFromString(genderName);
     userProfile.ActivityLevel = GetActivityLevelFromString(activityLevelName);
     UserProfileRepository.SaveOrUpdate(userProfile);
 }