public void FoodItemInvalidFoodGroup() { FoodItem foodItem = new FoodItem( Guid.NewGuid(), "Apple", "It fell on Newton's head.", 60 ); Assert.IsNotNull( foodItem.ID ); Assert.AreEqual( "Apple", foodItem.Name ); Assert.AreEqual( "It fell on Newton's head.", foodItem.Description ); Assert.AreEqual( 60, foodItem.CaloriesPerServing ); Assert.AreEqual( 0, foodItem.FoodGroupsPerServing.Count ); Assert.IsFalse( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.AreEqual( Messages.Error_No_FoodGroups, foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); // Assign a food group that is valid and one that is not valid FoodGroup foodGroup = new FoodGroup( Guid.NewGuid(), "Fruit", "" ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( foodGroup, 1 ) ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( null, 0 ) ); Assert.IsFalse( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); // Should contain two messages seperated by double spaces. Assert.IsTrue( foodItem["FoodGroupsPerServing"].Contains( Messages.Error_No_Quantity ) ); Assert.IsTrue( foodItem["FoodGroupsPerServing"].Contains( Messages.Error_No_ServingItem ) ); Assert.AreEqual( Messages.Error_No_ServingItem.Length + Messages.Error_No_Quantity.Length + 2, foodItem["FoodGroupsPerServing"].Length ); Assert.IsNull( foodItem["CaloriesPerServing"] ); foodItem.FoodGroupsPerServing.RemoveAt( 1 ); Assert.IsTrue( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.IsNull( foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); }
public void FoodItemAddedToChildrenWhenAddedToRepository() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); DataRepository dataRepository = new DataRepository( configurationMock.Object ); FoodItemNodeViewModel foodItemNodeViewModel = new FoodItemNodeViewModel( dataRepository, null ); Int32 originalChildCount = foodItemNodeViewModel.Children.Count; FoodItem newFoodItem = new FoodItem( Guid.NewGuid(), "New Food Item", "Some Description", 92 ); newFoodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( dataRepository.GetAllFoodGroups()[0], 1 ) ); dataRepository.SaveItem( newFoodItem ); Assert.AreEqual( originalChildCount + 1, foodItemNodeViewModel.Children.Count ); foreach (ClickableTreeNodeViewModel node in foodItemNodeViewModel.Children) { FoodItem foodItem = dataRepository.GetFoodItem( (Guid)node.Parameter ); Assert.IsNotNull( foodItem ); Assert.AreEqual( foodItem.Name, node.Name ); } }
public void DeleteFoodItem() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); HealthTracker.DataRepository.Services.DataRepository dataRepository = new HealthTracker.DataRepository.Services.DataRepository( configurationMock.Object ); // Make a deep copy of a food item. FoodItem fruitSalad = new FoodItem(); fruitSalad.InitializeData( dataRepository.FindFoodItem( f => f.Name == "Fruit Salad" ) ); // Delete the food item. Show that the food item has been deleted, but none of the // other data repository items have not been effected. Int32 origFoodGroupCount = dataRepository.GetAllFoodGroups().Count; Int32 origFoodItemCount = dataRepository.GetAllFoodItems().Count; Int32 origMealTypeCount = dataRepository.GetAllMealTypes().Count; Int32 origMealTemplateCount = dataRepository.GetAllMealTemplates().Count; Int32 origMealCount = dataRepository.GetAllMeals().Count; dataRepository.Remove( fruitSalad ); Assert.AreEqual( origFoodGroupCount, dataRepository.GetAllFoodGroups().Count ); Assert.AreEqual( origFoodItemCount - 1, dataRepository.GetAllFoodItems().Count ); Assert.AreEqual( origMealTypeCount, dataRepository.GetAllMealTypes().Count ); Assert.AreEqual( origMealTemplateCount, dataRepository.GetAllMealTemplates().Count ); Assert.AreEqual( origMealCount, dataRepository.GetAllMeals().Count ); Assert.IsNotNull( fruitSalad.ID ); Assert.IsNull( dataRepository.FindFoodItem( f => f.ID == fruitSalad.ID ) ); }
public void FoodItemDefault() { FoodItem foodItem = new FoodItem(); Assert.IsNotNull( foodItem.ID ); Assert.IsNull( foodItem.Name ); Assert.IsNull( foodItem.Description ); Assert.AreEqual( 0, foodItem.CaloriesPerServing ); Assert.AreEqual( 0, foodItem.FoodGroupsPerServing.Count ); Assert.IsFalse( foodItem.IsValid ); Assert.AreEqual( Messages.Error_No_Name, foodItem["Name"] ); Assert.AreEqual( Messages.Error_No_FoodGroups, foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); // Assign a name foodItem.Name = "Fred"; Assert.IsFalse( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.AreEqual( Messages.Error_No_FoodGroups, foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); // Assign a food group FoodGroup foodGroup = new FoodGroup( Guid.NewGuid(), "Vegetable", "" ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( foodGroup, 1 ) ); Assert.IsTrue( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.IsNull( foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); }
public void FoodItemAddedToViewModelWhenAddedToRepository() { var data = new MockData(); var dataRespositoryMock = CreateMockDataRepository( data.Foods() ); var viewModel = new AllFoodItemsViewModel( dataRespositoryMock.Object ); var food = new FoodItem( Guid.NewGuid(), "New Type", "For a unit test", 42.0M ); data.Foods().Add( food ); dataRespositoryMock.Raise( e => e.ItemAdded += null, new RepositoryObjectEventArgs( food ) ); AssertViewModelContents( viewModel, data.Foods() ); }
public void InitializeData() { FoodGroup fruit = new FoodGroup( Guid.NewGuid(), "Fruit", "" ); FoodGroup meat = new FoodGroup( Guid.NewGuid(), "Meat", "" ); FoodGroup vegitable = new FoodGroup( Guid.NewGuid(), "Vegetable", "" ); FoodGroup dairy = new FoodGroup( Guid.NewGuid(), "Dairy", "" ); FoodGroup grain = new FoodGroup( Guid.NewGuid(), "Grain", "" ); MealType lunch = new MealType( Guid.NewGuid(), "Lunch", "", DateTime.Now, false ); MealType snack = new MealType( Guid.NewGuid(), "Snack", "", DateTime.Now, false ); DateTime currentDateTime = DateTime.Now; FoodItem hamburger = new FoodItem( Guid.NewGuid(), "Hamburger", "", 300 ); hamburger.FoodGroupsPerServing.Add( new Serving<FoodGroup>( grain, 2 ) ); hamburger.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1 ) ); FoodItem glassOfMilk = new FoodItem( Guid.NewGuid(), "Glass of Milk", "", 90 ); glassOfMilk.FoodGroupsPerServing.Add( new Serving<FoodGroup>( dairy, 1 ) ); FoodItem babyCarrots = new FoodItem( Guid.NewGuid(), "Baby Carrots", "", 40 ); babyCarrots.FoodGroupsPerServing.Add( new Serving<FoodGroup>( vegitable, 1 ) ); MealTemplate sourceMealTemplate = new MealTemplate( Guid.NewGuid(), lunch, currentDateTime, "Test Meal", "This is a test" ); sourceMealTemplate.FoodItemServings.Add( new Serving<FoodItem>( hamburger, 1 ) ); sourceMealTemplate.FoodItemServings.Add( new Serving<FoodItem>( glassOfMilk, 2 ) ); sourceMealTemplate.FoodItemServings.Add( new Serving<FoodItem>( babyCarrots, 1.5M ) ); MealTemplate mealTemplate = new MealTemplate(); Assert.AreNotEqual( sourceMealTemplate.ID, mealTemplate.ID ); mealTemplate.InitializeData( sourceMealTemplate ); Assert.AreEqual( sourceMealTemplate.ID, mealTemplate.ID ); Assert.AreEqual( sourceMealTemplate.Name, mealTemplate.Name ); Assert.AreEqual( sourceMealTemplate.Description, mealTemplate.Description ); Assert.AreEqual( sourceMealTemplate.Calories, mealTemplate.Calories ); Assert.AreEqual( sourceMealTemplate.FoodGroupServings.Count, mealTemplate.FoodGroupServings.Count ); Assert.AreEqual( sourceMealTemplate.FoodItemServings.Count, mealTemplate.FoodItemServings.Count ); mealTemplate.FoodItemServings[0].Quantity += 1; Assert.AreNotEqual( sourceMealTemplate.Calories, mealTemplate.Calories ); // Create a new mealTemplate template that is just baby carrots (kind of a snack) sourceMealTemplate = new MealTemplate( Guid.NewGuid(), snack, DateTime.Now, "Snack", "This is a snack" ); sourceMealTemplate.FoodItemServings.Add( new Serving<FoodItem>( babyCarrots, 2.5M ) ); mealTemplate.InitializeData( sourceMealTemplate ); Assert.AreEqual( sourceMealTemplate.ID, mealTemplate.ID ); Assert.AreEqual( sourceMealTemplate.Name, mealTemplate.Name ); Assert.AreEqual( sourceMealTemplate.Description, mealTemplate.Description ); Assert.AreEqual( sourceMealTemplate.Calories, mealTemplate.Calories ); Assert.AreEqual( sourceMealTemplate.FoodGroupServings.Count, mealTemplate.FoodGroupServings.Count ); Assert.AreEqual( sourceMealTemplate.FoodItemServings.Count, mealTemplate.FoodItemServings.Count ); }
public void FoodItemIDNameDescriptionCalories() { FoodItem foodItem = new FoodItem( new Guid( "5326e353-fb06-41c6-a30e-e75a9f1efdc6" ), "Orange", "You're round and juicy and sweet!", 40 ); Assert.AreEqual( new Guid( "5326e353-fb06-41c6-a30e-e75a9f1efdc6" ), foodItem.ID ); Assert.AreEqual( "Orange", foodItem.Name ); Assert.AreEqual( "You're round and juicy and sweet!", foodItem.Description ); Assert.AreEqual( 40, foodItem.CaloriesPerServing ); Assert.AreEqual( 0, foodItem.FoodGroupsPerServing.Count ); Assert.IsFalse( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.AreEqual( Messages.Error_No_FoodGroups, foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); // Assign a food group FoodGroup foodGroup = new FoodGroup( Guid.NewGuid(), "Fruit", "" ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( foodGroup, 1 ) ); Assert.IsTrue( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.IsNull( foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); }
public void CannotSaveAfterFoodItemModificationSaved() { var dataRepositoryMock = new Mock<IDataRepository>(); dataRepositoryMock.Setup( x => x.GetAllFoodItems() ) .Returns( new ReadOnlyCollection<FoodItem>( new List<FoodItem>() ) ); dataRepositoryMock.Setup( x => x.GetAllMealTypes() ) .Returns( new ReadOnlyCollection<MealType>( new List<MealType>() ) ); var foodGroupOne = new FoodGroup( Guid.NewGuid(), "Food Group One", "" ); var foodGroupTwo = new FoodGroup( Guid.NewGuid(), "Food Group Two", "" ); var foodItemOne = new FoodItem( Guid.NewGuid(), "Food Item #1", "", 100 ); foodItemOne.FoodGroupsPerServing.Add( new Serving<FoodGroup>( foodGroupOne, 1.5M ) ); var foodItemTwo = new FoodItem( Guid.NewGuid(), "Food Item #2", "", 150 ); foodItemTwo.FoodGroupsPerServing.Add( new Serving<FoodGroup>( foodGroupTwo, 2.25M ) ); var meal = new Meal( Guid.NewGuid(), new MealType( Guid.NewGuid(), "Test", "", DateTime.Now, false ), DateTime.Now, "Test Meal", "This is a test" ); meal.FoodItemServings.Add( new Serving<FoodItem>( foodItemOne, 1 ) ); meal.FoodItemServings.Add( new Serving<FoodItem>( foodItemTwo, 2 ) ); dataRepositoryMock.Setup( x => x.GetMeal( meal.ID ) ).Returns( meal ); dataRepositoryMock.Setup( x => x.Contains( meal ) ).Returns( true ); var viewModel = CreateViewModelForMeal( meal, dataRepositoryMock ); Assert.IsFalse( viewModel.IsDirty ); Assert.IsFalse( viewModel.SaveCommand.CanExecute( null ) ); viewModel.FoodItemServings[0].Quantity += 1.5M; Assert.IsTrue( viewModel.IsDirty ); Assert.IsTrue( viewModel.SaveCommand.CanExecute( null ) ); viewModel.SaveCommand.Execute( null ); Assert.IsFalse( viewModel.IsDirty ); Assert.IsFalse( viewModel.SaveCommand.CanExecute( null ) ); }
public void CannotDeleteFoodItemIfUsed() { var loggerMock = new Mock<ILoggerFacade>(); var dataRepositoryMock = new Mock<IDataRepository>(); var interactionServiceMock = new Mock<IInteractionService>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var foodItem = new FoodItem( Guid.NewGuid(), "Test", "This is a test", 90.5M ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "test", "test" ), 1.5M ) ); dataRepositoryMock.Setup( x => x.Contains( foodItem ) ).Returns( true ); dataRepositoryMock.Setup( x => x.ItemIsUsed( foodItem ) ).Returns( true ); dataRepositoryMock.Setup( x => x.GetFoodItem( foodItem.ID ) ).Returns( foodItem ); dataRepositoryMock .Setup( x => x.GetAllFoodGroups() ) .Returns( new ReadOnlyCollection<FoodGroup>( new List<FoodGroup>() ) ); var viewModel = new FoodItemViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItem?ID=" + foodItem.ID.ToString(), UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); // Setup the regions so we can determine if the view has been removed or not var view = new UserControl(); view.DataContext = viewModel; var views = new List<UserControl>(); views.Add( new UserControl() ); views.Add( view ); views.Add( new UserControl() ); views.Add( new UserControl() ); var regionMock = new Mock<IRegion>(); var regions = new List<IRegion>(); regions.Add( regionMock.Object ); regionManagerMock.Setup( x => x.Regions.GetEnumerator() ).Returns( regions.GetEnumerator() ); regionMock.Setup( x => x.Views.GetEnumerator() ).Returns( views.GetEnumerator() ); Assert.IsFalse( viewModel.DeleteCommand.CanExecute( null ) ); dataRepositoryMock.VerifyAll(); interactionServiceMock.Verify( x => x.ShowMessageBox( It.IsAny<String>(), It.IsAny<String>(), It.IsAny<MessageBoxButton>(), It.IsAny<MessageBoxImage>() ), Times.Never() ); regionMock.Verify( x => x.Remove( It.IsAny<Object>() ), Times.Never() ); }
public void ExistingFoodItemViewModelIsNotNew() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var dataRepositoryMock = new Mock<IDataRepository>(); var interactionServiceMock = new Mock<IInteractionService>(); var foodItem = new FoodItem( Guid.NewGuid(), "Test Food Item", "", 420 ); dataRepositoryMock.Setup( x => x.GetAllFoodGroups() ) .Returns( new ReadOnlyCollection<FoodGroup>( new List<FoodGroup>() ) ); dataRepositoryMock.Setup( x => x.Contains( foodItem ) ).Returns( true ); dataRepositoryMock.Setup( x => x.GetFoodItem( foodItem.ID ) ).Returns( foodItem ); var testFoodItemViewModel = new FoodItemViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItem.ID.ToString(), UriKind.Relative ) ); testFoodItemViewModel.OnNavigatedTo( navigationContext ); Assert.IsFalse( testFoodItemViewModel.IsNew ); }
/// <summary> /// Close test template. All of the close tests follow this based pattern, so they all call this rather than repeating everything /// </summary> private void RunCloseTest( Boolean makeDirty, Boolean makeInvalid, MessageBoxResult messageResponse, Boolean expectRemove, Boolean expectSave ) { var loggerMock = new Mock<ILoggerFacade>(); Mock<IDataRepository> dataRepositoryMock = new Mock<IDataRepository>(); Mock<IRegionNavigationService> regionNavigationServiceMock = new Mock<IRegionNavigationService>(); Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>(); Mock<IRegion> regionWithoutViewMock = new Mock<IRegion>(); Mock<IRegion> regionMock = new Mock<IRegion>(); Mock<IInteractionService> interactionServiceMock = new Mock<IInteractionService>(); // Set up the food group list in the data repository mock. FoodGroup testFoodGroup = new FoodGroup( Guid.NewGuid(), "test", "The only food group in the mock data repository" ); List<FoodGroup> foodGroups = new List<FoodGroup>(); foodGroups.Add( testFoodGroup ); dataRepositoryMock.Setup( x => x.GetAllFoodGroups() ).Returns( new ReadOnlyCollection<FoodGroup>( foodGroups ) ); FoodItem foodItem = new FoodItem( Guid.NewGuid(), "Test Food Item", "Test Food Item Description", 42.0M ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( testFoodGroup, 5.0M ) ); Assert.IsTrue( foodItem.IsValid ); dataRepositoryMock.Setup( x => x.GetFoodItem( foodItem.ID ) ).Returns( foodItem ); dataRepositoryMock.Setup( x => x.Contains( foodItem ) ).Returns( true ); // Create the view model under test and associate it with a view FoodItemViewModel viewModel = new FoodItemViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); UserControl view = new UserControl(); view.DataContext = viewModel; // Set up two regions each with their own set of views. List<UserControl> views = new List<UserControl>(); views.Add( new UserControl() ); views.Add( view ); views.Add( new UserControl() ); views.Add( new UserControl() ); List<UserControl> viewsWithoutView = new List<UserControl>(); viewsWithoutView.Add( new UserControl() ); viewsWithoutView.Add( new UserControl() ); List<IRegion> regions = new List<IRegion>(); regions.Add( regionMock.Object ); regionManagerMock.Setup( x => x.Regions.GetEnumerator() ).Returns( regions.GetEnumerator() ); regionWithoutViewMock.Setup( x => x.Views.GetEnumerator() ).Returns( viewsWithoutView.GetEnumerator() ); regionMock.Setup( x => x.Views.GetEnumerator() ).Returns( views.GetEnumerator() ); // Navigate to the view that "displays" our food mealType. This loads the view model NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItem.ID.ToString(), UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); if (makeDirty) { if (makeInvalid) { interactionServiceMock .Setup( x => x.ShowMessageBox( Messages.Question_FoodItem_Close, DisplayStrings.CloseCaption, MessageBoxButton.YesNo, MessageBoxImage.Question ) ) .Returns( messageResponse ); viewModel.Name = ""; Assert.IsTrue( viewModel.IsDirty ); Assert.IsFalse( viewModel.IsValid ); } else { interactionServiceMock .Setup( x => x.ShowMessageBox( Messages.Question_FoodItem_Save, DisplayStrings.SaveChangesCaption, MessageBoxButton.YesNoCancel, MessageBoxImage.Question ) ) .Returns( messageResponse ); viewModel.Name = "Something Else"; Assert.IsTrue( viewModel.IsDirty ); Assert.IsTrue( viewModel.IsValid ); } } else { // This will fail if we have passed in the non-sensical makeDirty == false, makeInvalid == true Assert.AreEqual( makeDirty, viewModel.IsDirty ); } // Attempt a close. viewModel.CloseCommand.Execute( null ); // If we were dirty, then we need to verify that the correct interaction was done, otherwise, that no interaction was done if (makeDirty) { interactionServiceMock.VerifyAll(); } else { interactionServiceMock.Verify( x => x.ShowMessageBox( It.IsAny<String>(), It.IsAny<String>(), It.IsAny<MessageBoxButton>(), It.IsAny<MessageBoxImage>() ), Times.Never() ); } if (expectRemove) { regionMock.Verify( x => x.Remove( view ), Times.Exactly( 1 ) ); regionMock.Verify( x => x.Remove( It.IsAny<UserControl>() ), Times.Exactly( 1 ) ); } else { regionMock.Verify( x => x.Remove( It.IsAny<UserControl>() ), Times.Never() ); } if (expectSave) { dataRepositoryMock.Verify( x => x.SaveItem( foodItem ), Times.Exactly( 1 ) ); dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<FoodItem>() ), Times.Exactly( 1 ) ); } else { dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<FoodItem>() ), Times.Never() ); } }
public void SaveNotCalledForNonChangedFoodItem() { var loggerMock = new Mock<ILoggerFacade>(); var regionManagerMock = new Mock<IRegionManager>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var dataRepositoryMock = new Mock<IDataRepository>(); var interactionServiceMock = new Mock<IInteractionService>(); var foodItem = new FoodItem( Guid.NewGuid(), "FoodItem", "", 420 ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test Group", "" ), 1 ) ); dataRepositoryMock.Setup( x => x.GetAllFoodGroups() ) .Returns( new ReadOnlyCollection<FoodGroup>( new List<FoodGroup>() ) ); dataRepositoryMock.Setup( x => x.Contains( foodItem ) ).Returns( true ); dataRepositoryMock.Setup( x => x.GetFoodItem( foodItem.ID ) ).Returns( foodItem ); var foodItemViewModel = new FoodItemViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItem.ID.ToString(), UriKind.Relative ) ); foodItemViewModel.OnNavigatedTo( navigationContext ); Assert.IsTrue( foodItemViewModel.IsValid ); Assert.IsFalse( foodItemViewModel.IsDirty ); Assert.IsFalse( foodItemViewModel.IsNew ); foodItemViewModel.SaveCommand.Execute( null ); dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<FoodItem>() ), Times.Never() ); }
public void AddingMealForDateToRepositoryUpdatesCaloriesAndFoodGroups() { PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler(); var loggerMock = new Mock<ILoggerFacade>(); var dataRepositoryMock = new Mock<IDataRepository>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var navigationServiceMock = new Mock<IRegionNavigationService>(); var data = new MockData(); var fullMealList = data.Meals(); var todaysMeals = (from meal in fullMealList where meal.DateAndTimeOfMeal.Date == DateTime.Today select meal).ToList(); dataRepositoryMock.Setup( x => x.GetAllMealsForDate( DateTime.Today ) ).Returns( new ReadOnlyCollection<Meal>( todaysMeals ) ); var viewModel = new DailyLogViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); viewModel.CurrentDate = DateTime.Today; viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged; Int32 originalFoodGroupCount = viewModel.FoodGroupServings.Count; Decimal originalFoodGroupServings = (from foodGroupServing in viewModel.FoodGroupServings select foodGroupServing.Quantity).Sum(); Decimal originalCalories = viewModel.Calories; var foodGroupServings = viewModel.FoodGroupServings; Assert.AreEqual( 5, foodGroupServings.Count ); var serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Fruit" ); Assert.IsNotNull( serving ); Assert.AreEqual( 2.5M, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Starch" ); Assert.IsNotNull( serving ); Assert.AreEqual( 5.0M, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Dairy" ); Assert.IsNotNull( serving ); Assert.AreEqual( 4.75M, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Meat" ); Assert.IsNotNull( serving ); Assert.AreEqual( 2.5M, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Vegetable" ); Assert.IsNotNull( serving ); Assert.AreEqual( 3.5M, serving.Quantity ); var foodItem = new FoodItem( Guid.NewGuid(), "test", "", 42 ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test1", "" ), 2 ) ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test2", "" ), 1 ) ); var newMeal = new Meal( Guid.NewGuid(), new MealType( Guid.NewGuid(), "Doesn't Matter", "", DateTime.Now, false ), DateTime.Now, "Test Meal", "Just a test" ); newMeal.FoodItemServings.Add( new Serving<FoodItem>( foodItem, 1 ) ); todaysMeals.Add( newMeal ); dataRepositoryMock.Raise( e => e.ItemAdded += null, new RepositoryObjectEventArgs( newMeal ) ); Assert.AreEqual( originalCalories + 42, viewModel.Calories ); Assert.AreEqual( originalFoodGroupCount + 2, viewModel.FoodGroupServings.Count ); Assert.AreEqual( originalFoodGroupServings + 3, (from foodGroupServing in viewModel.FoodGroupServings select foodGroupServing.Quantity).Sum() ); foodGroupServings = viewModel.FoodGroupServings; Assert.AreEqual( 7, foodGroupServings.Count ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Fruit" ); Assert.IsNotNull( serving ); Assert.AreEqual( 2.5M, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Starch" ); Assert.IsNotNull( serving ); Assert.AreEqual( 5.0M, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Dairy" ); Assert.IsNotNull( serving ); Assert.AreEqual( 4.75M, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Meat" ); Assert.IsNotNull( serving ); Assert.AreEqual( 2.5M, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Vegetable" ); Assert.IsNotNull( serving ); Assert.AreEqual( 3.5M, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Test1" ); Assert.IsNotNull( serving ); Assert.AreEqual( 2, serving.Quantity ); serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Test2" ); Assert.IsNotNull( serving ); Assert.AreEqual( 1, serving.Quantity ); Assert.AreEqual( 2, propertyChangedHandler.PropertiesChanged.Count ); Assert.AreEqual( viewModel, propertyChangedHandler.Sender ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Calories" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupServings" ) ); }
/// <summary> /// Determine if a food item is currently referenced by some other entitiy in the repository. /// </summary> /// <param name="foodItem">The food item</param> /// <returns>True if used, false otherwise.</returns> public Boolean ItemIsUsed( FoodItem foodItem ) { // I foresee this getting slow once we get a lot of meals in there. // May need to implement a usage count on the food item list. foreach (MealTemplate mealTemplate in _mealTemplates) { foreach (Serving<FoodItem> serving in mealTemplate.FoodItemServings) { if (serving.Entity.ID == foodItem.ID) { return true; } } } return false; }
public void FoodItemNameCalories() { FoodItem foodItem = new FoodItem( Guid.NewGuid(), "Water", "", -1 ); Assert.IsNotNull( foodItem.ID ); Assert.AreEqual( "Water", foodItem.Name ); Assert.IsNull( foodItem.Description ); Assert.AreEqual( -1, foodItem.CaloriesPerServing ); Assert.AreEqual( 0, foodItem.FoodGroupsPerServing.Count ); Assert.IsFalse( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.AreEqual( Messages.Error_No_FoodGroups, foodItem["FoodGroupsPerServing"] ); Assert.AreEqual( Messages.Error_Negative_Calories, foodItem["CaloriesPerServing"] ); // Assign a food group FoodGroup foodGroup = new FoodGroup( Guid.NewGuid(), "Water", "" ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( foodGroup, 1 ) ); Assert.IsFalse( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.IsNull( foodItem["FoodGroupsPerServing"] ); Assert.AreEqual( Messages.Error_Negative_Calories, foodItem["CaloriesPerServing"] ); // Provide non-negative Calories foodItem.CaloriesPerServing = 0; Assert.IsTrue( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.IsNull( foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); }
/// <summary> /// Determine if a food item's name is already being used by another food item. /// </summary> /// <param name="foodItem">The food item</param> /// <returns>True if some other food item is using the name, false otherwise.</returns> public Boolean NameIsDuplicate( FoodItem foodItem ) { return (_foodItems.Find( food => food.Name == foodItem.Name && food.ID != foodItem.ID ) != null); }
public void UndoRedoMealViewModel() { // Create a meal and then navigate to it to bring it into the view model var meal = new Meal( Guid.NewGuid(), new MealType( Guid.NewGuid(), "Test", "", DateTime.Now, false ), DateTime.Now, "Bob", "Battery Operated Buddy" ); var glassOfWater = new FoodItem( Guid.NewGuid(), "Glass of Water", "", 0 ); glassOfWater.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Water", "" ), 1 ) ); meal.FoodItemServings.Add( new Serving<FoodItem>( glassOfWater, 1.0M ) ); var mealViewModel = CreateViewModelForMeal( meal ); Assert.IsFalse( mealViewModel.IsDirty ); Assert.IsFalse( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); // Make changes as such: // o name changed from Bob to Pete // o name changed from Pete to Peter // o Description changed from "Battery Operated Buddy" to "The Rock" // o name changed from Peter to Simon // o name changed from Simon to Saul // o description changed from "The Rock" to "The Persecutor" // o description changed from "The Persecutor" to "The Apostle" // o name changed from Saul to Paul // Verify can undo, cannot redo at each step mealViewModel.Name = "Pete"; Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.Name += "r"; Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.Description = "The Rock"; Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.Name = "Simon"; Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.Name = "Saul"; Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.Description = "The Persecutor"; Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.Description = "The Apostle"; Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.Name = "Paul"; Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); Assert.AreEqual( "Paul", mealViewModel.Name ); Assert.AreEqual( "The Apostle", mealViewModel.Description ); // Undo once. Verify last thing done is undone, and we can redo. mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", mealViewModel.Name ); Assert.AreEqual( "The Apostle", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); // Redo. Verify last thing undone is redone, can no longer redo, can still undo. mealViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Paul", mealViewModel.Name ); Assert.AreEqual( "The Apostle", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( mealViewModel.RedoCommand.CanExecute( null ) ); // Undo 4 times, verify undo worked mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", mealViewModel.Name ); Assert.AreEqual( "The Apostle", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", mealViewModel.Name ); Assert.AreEqual( "The Persecutor", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", mealViewModel.Name ); Assert.AreEqual( "The Rock", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Simon", mealViewModel.Name ); Assert.AreEqual( "The Rock", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); // Redo 2 times, verify mealViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Saul", mealViewModel.Name ); Assert.AreEqual( "The Rock", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Saul", mealViewModel.Name ); Assert.AreEqual( "The Persecutor", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); // Undo 6 times. Back to original, cannot undo, can redo mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", mealViewModel.Name ); Assert.AreEqual( "The Rock", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Simon", mealViewModel.Name ); Assert.AreEqual( "The Rock", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", mealViewModel.Name ); Assert.AreEqual( "The Rock", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", mealViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Pete", mealViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Bob", mealViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", mealViewModel.Description ); Assert.IsFalse( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); // Redo 3 times, verify mealViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Pete", mealViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Peter", mealViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); mealViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Peter", mealViewModel.Name ); Assert.AreEqual( "The Rock", mealViewModel.Description ); Assert.IsTrue( mealViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( mealViewModel.RedoCommand.CanExecute( null ) ); }
/// <summary> /// Remove a food item from the repository /// </summary> /// <param name="foodItem">The food item</param> public void Remove( FoodItem foodItem ) { Debug.Assert( foodItem != null ); if (Contains( foodItem )) { _foodItems.Remove( _foodItems.Find( f => f.ID == foodItem.ID ) ); if (ItemDeleted != null) { ItemDeleted( this, new RepositoryObjectEventArgs( foodItem ) ); } } SaveRepository(); }
public void SaveFoodItem() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); HealthTracker.DataRepository.Services.DataRepository dataRepository = new HealthTracker.DataRepository.Services.DataRepository( configurationMock.Object ); // Get some food groups that we can use FoodGroup meat = dataRepository.FindFoodGroup( f => f.ID == FullTestData.MeatID ); FoodGroup vegetables = dataRepository.FindFoodGroup( f => f.ID == FullTestData.VegetableID ); // Create one new food item, and get a deep copy of one out of the repository. // When the new one is saved, a food item should be added to the repository. // When the deep copy of the existing one is modified, the modifications should not be applied to the repository // until the copy is saved, at which a new food item should not be created, but the food item object with the same // ID should have the modifications applied to it. FoodItem dublinCoddle = new FoodItem( new Guid( "f6ffa83c-5f7a-4961-8571-801f436c0eb9" ), "Dublin Coddle", "Poormans food", 250 ); dublinCoddle.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1 ) ); dublinCoddle.FoodGroupsPerServing.Add( new Serving<FoodGroup>( vegetables, 2 ) ); FoodItem repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID ); Assert.IsNotNull( repositoryChzBurger ); FoodItem chzBurger = new FoodItem(); chzBurger.InitializeData( repositoryChzBurger ); Int32 origFoodItemCount = dataRepository.GetAllFoodItems().Count; // Verify the food groups, etc. Assert.AreEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing ); Assert.AreEqual( repositoryChzBurger.Name, chzBurger.Name ); Assert.AreEqual( repositoryChzBurger.Description, chzBurger.Description ); Assert.AreEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count ); // Remove the vegetables and bacon, and verify the difference chzBurger.Description = "Ground up cow, topped with curdled milk."; chzBurger.Name = "Cheese Burger"; chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity = 1; chzBurger.FoodGroupsPerServing.Remove( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) ); chzBurger.CaloriesPerServing -= 140; repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID ); Assert.AreNotEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing ); Assert.AreNotEqual( repositoryChzBurger.Name, chzBurger.Name ); Assert.AreNotEqual( repositoryChzBurger.Description, chzBurger.Description ); Assert.AreNotEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count ); Assert.AreNotEqual( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ), chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ) ); Assert.IsNotNull( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) ); Assert.IsNull( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) ); // Save the cheese burger, and verify the changes are saved dataRepository.SaveItem( chzBurger ); repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID ); Assert.AreEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing ); Assert.AreEqual( repositoryChzBurger.Name, chzBurger.Name ); Assert.AreEqual( repositoryChzBurger.Description, chzBurger.Description ); Assert.AreEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count ); Assert.AreEqual( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity, chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity ); Assert.IsNull( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) ); Assert.IsNull( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) ); Assert.AreEqual( origFoodItemCount, dataRepository.GetAllFoodItems().Count ); // Add some fruit and verify the difference chzBurger.Name = "Hawian Cheese Burger"; chzBurger.Description = "Ground up cow, topped with curdled milk, smoked pig, and pineapple."; chzBurger.FoodGroupsPerServing.Add( new Serving<FoodGroup>( dataRepository.FindFoodGroup( fg => fg.ID == FullTestData.FruitID ), 0.5M ) ); chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity += 1; chzBurger.CaloriesPerServing += 200; repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID ); Assert.AreNotEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing ); Assert.AreNotEqual( repositoryChzBurger.Name, chzBurger.Name ); Assert.AreNotEqual( repositoryChzBurger.Description, chzBurger.Description ); Assert.AreNotEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count ); Assert.AreNotEqual( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ), chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ) ); Assert.IsNull( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.FruitID ) ); Assert.IsNotNull( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.FruitID ) ); // Save the cheese burger, and verify the changes are saved dataRepository.SaveItem( chzBurger ); repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID ); Assert.AreEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing ); Assert.AreEqual( repositoryChzBurger.Name, chzBurger.Name ); Assert.AreEqual( repositoryChzBurger.Description, chzBurger.Description ); Assert.AreEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count ); Assert.AreEqual( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity, chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity ); Assert.IsNotNull( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.FruitID ) ); Assert.IsNotNull( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.FruitID ) ); Assert.AreEqual( origFoodItemCount, dataRepository.GetAllFoodItems().Count ); // Save the new food item dataRepository.SaveItem( dublinCoddle ); Assert.AreEqual( origFoodItemCount + 1, dataRepository.GetAllFoodItems().Count ); FoodItem repositoryCoddle = dataRepository.FindFoodItem( f => f.ID == new Guid( "f6ffa83c-5f7a-4961-8571-801f436c0eb9" ) ); Assert.IsNotNull( repositoryCoddle ); Assert.AreEqual( "Dublin Coddle", repositoryCoddle.Name ); Assert.AreEqual( "Poormans food", repositoryCoddle.Description ); Assert.AreEqual( 250, repositoryCoddle.CaloriesPerServing ); Assert.AreEqual( 2, repositoryCoddle.FoodGroupsPerServing.Count ); }
// This is really, really ugly. It works, but it is not pretty. Look into changing this. private void LoadRepository( String dataFileName ) { XDocument xmlDoc; using (FileStream stream = new FileStream( dataFileName, FileMode.Open )) { using (XmlReader xmlRdr = new XmlTextReader( stream )) { xmlDoc = XDocument.Load( xmlRdr ); // If we don't have the root element if (xmlDoc.Element( rootNodeTag ) == null) { _foodGroups = new List<FoodGroup>(); _foodItems = new List<FoodItem>(); _mealTypes = new List<MealType>(); _mealTemplates = new List<MealTemplate>(); _meals = new List<Meal>(); } else { if (xmlDoc.Element( rootNodeTag ).Element( foodGroupsNodeTag ) != null) { _foodGroups = (from foodGroupElement in xmlDoc.Element( rootNodeTag ).Element( foodGroupsNodeTag ).Elements( foodGroupNodeTag ) select new FoodGroup( (Guid)foodGroupElement.Attribute( idAttribute ), (String)foodGroupElement.Attribute( nameAttribute ), foodGroupElement.Element( descriptionNodeTag ) == null ? null : (String)foodGroupElement.Element( descriptionNodeTag ).Value )).ToList(); } else { _foodGroups = new List<FoodGroup>(); } // FoodItems _foodItems = new List<FoodItem>(); if (xmlDoc.Element( rootNodeTag ).Element( foodItemsNodeTag ) != null) { foreach (XElement foodItemElement in xmlDoc.Element( rootNodeTag ).Element( foodItemsNodeTag ).Elements( foodItemNodeTag )) { // Construct a food item and add it to the collection FoodItem foodItem = new FoodItem( (Guid)foodItemElement.Attribute( idAttribute ), (String)foodItemElement.Attribute( nameAttribute ), foodItemElement.Element( descriptionNodeTag ) == null ? null : (String)foodItemElement.Element( descriptionNodeTag ).Value, (Decimal)foodItemElement.Attribute( caloriesAttribute ) ); // TODO: Should probably do a tryparse on that... _foodItems.Add( foodItem ); // Loop through the FoodGroupServings to build that list if (foodItemElement.Element( foodGroupServingsNodeTag ) != null) { foreach (XElement servingElement in foodItemElement.Element( foodGroupServingsNodeTag ).Elements( servingNodeTag )) { Serving<FoodGroup> foodGroupServing = new Serving<FoodGroup>( _foodGroups.Find( fg => fg.ID == (Guid)servingElement.Attribute( foodGroupIdAttribute ) ), (Decimal)servingElement.Attribute( quantityAttribute ) ); foodItem.FoodGroupsPerServing.Add( foodGroupServing ); } } } } if (xmlDoc.Element( rootNodeTag ).Element( mealTypesNodeTag ) != null) { _mealTypes = (from mealTypeElement in xmlDoc.Element( rootNodeTag ).Element( mealTypesNodeTag ).Elements( mealTypeNodeTag ) select new MealType( (Guid)mealTypeElement.Attribute( idAttribute ), (String)mealTypeElement.Attribute( nameAttribute ), mealTypeElement.Element( descriptionNodeTag ) == null ? null : (String)mealTypeElement.Element( descriptionNodeTag ).Value, (DateTime)mealTypeElement.Attribute( dateTimeAttribute ), (Boolean)mealTypeElement.Attribute( useDefaultDateTimeAttribute ) )).ToList(); } else { _mealTypes = new List<MealType>(); } // Meal Templates _mealTemplates = new List<MealTemplate>(); if (xmlDoc.Element( rootNodeTag ).Element( mealTemplatesNodeTag ) != null) { foreach (XElement mealTemplateElement in xmlDoc.Element( rootNodeTag ).Element( mealTemplatesNodeTag ).Elements( mealNodeTag )) { // Construct a food item and add it to the collection MealTemplate mealTemplate = new MealTemplate( (Guid)mealTemplateElement.Attribute( idAttribute ), _mealTypes.Find( mt => mt.ID == (Guid)mealTemplateElement.Attribute( mealTypeIdAttribute ) ), (DateTime)mealTemplateElement.Attribute( dateTimeAttribute ), (String)mealTemplateElement.Attribute( nameAttribute ), mealTemplateElement.Element( descriptionNodeTag ) == null ? null : (String)mealTemplateElement.Element( descriptionNodeTag ).Value ); _mealTemplates.Add( mealTemplate ); // Loop through the FoodItemServings to build that list if (mealTemplateElement.Element( foodItemServingsNodeTag ) != null) { foreach (XElement servingElement in mealTemplateElement.Element( foodItemServingsNodeTag ).Elements( servingNodeTag )) { Serving<FoodItem> foodItem = new Serving<FoodItem>( _foodItems.Find( fi => fi.ID == (Guid)servingElement.Attribute( foodItemIdAttribute ) ), (Decimal)servingElement.Attribute( quantityAttribute ) ); mealTemplate.FoodItemServings.Add( foodItem ); } } } } // Meals _meals = new List<Meal>(); if (xmlDoc.Element( rootNodeTag ).Element( mealsNodeTag ) != null) { foreach (XElement mealElement in xmlDoc.Element( rootNodeTag ).Element( mealsNodeTag ).Elements( mealNodeTag )) { // Construct a food item and add it to the collection var meal = new Meal( (Guid)mealElement.Attribute( idAttribute ), _mealTypes.Find( mt => mt.ID == (Guid)mealElement.Attribute( mealTypeIdAttribute ) ), (DateTime)mealElement.Attribute( dateTimeAttribute ), (String)mealElement.Attribute( nameAttribute ), mealElement.Element( descriptionNodeTag ) == null ? null : (String)mealElement.Element( descriptionNodeTag ).Value ); _meals.Add( meal ); // Loop through the FoodItemServings to build that list if (mealElement.Element( foodItemServingsNodeTag ) != null) { foreach (XElement servingElement in mealElement.Element( foodItemServingsNodeTag ).Elements( servingNodeTag )) { Serving<FoodItem> foodItem = new Serving<FoodItem>( _foodItems.Find( fi => fi.ID == (Guid)servingElement.Attribute( foodItemIdAttribute ) ), (Decimal)servingElement.Attribute( quantityAttribute ) ); meal.FoodItemServings.Add( foodItem ); } } } } } } } }
/// <summary> /// Add a food item to the repository /// </summary> /// <param name="foodItem">The food item</param> public void SaveItem( FoodItem foodItem ) { Debug.Assert( foodItem != null ); if (!Contains( foodItem )) { _foodItems.Add( new FoodItem( foodItem ) ); if (ItemAdded != null) { ItemAdded( this, new RepositoryObjectEventArgs( foodItem ) ); } } else { FoodItem repositoryFoodItem = _foodItems.Find( f => f.ID == foodItem.ID ); Debug.Assert( repositoryFoodItem != null ); repositoryFoodItem.InitializeData( foodItem ); if (ItemModified != null) { ItemModified( this, new RepositoryObjectEventArgs( foodItem ) ); } } this.SaveRepository(); }
public void FoodItemNameDescriptionCalories() { FoodItem foodItem = new FoodItem( Guid.NewGuid(), "Apple", "It fell on Newton's head.", 60 ); Assert.IsNotNull( foodItem.ID ); Assert.AreEqual( "Apple", foodItem.Name ); Assert.AreEqual( "It fell on Newton's head.", foodItem.Description ); Assert.AreEqual( 60, foodItem.CaloriesPerServing ); Assert.AreEqual( 0, foodItem.FoodGroupsPerServing.Count ); Assert.IsFalse( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.AreEqual( Messages.Error_No_FoodGroups, foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); // Assign a food group FoodGroup foodGroup = new FoodGroup( Guid.NewGuid(), "Fruit", "" ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( foodGroup, 1 ) ); Assert.IsTrue( foodItem.IsValid ); Assert.IsNull( foodItem["Name"] ); Assert.IsNull( foodItem["FoodGroupsPerServing"] ); Assert.IsNull( foodItem["CaloriesPerServing"] ); }
public void FoodGroupServingsAdjustedAsFoodItemsAdded() { var data = new MockData(); var meal = data.Meals().Find( x => x.ID == MockData.DayTwoBreakfastID ); var viewModel = CreateViewModelForMeal( new Meal( meal ) ); var foodItem = new FoodItem( Guid.NewGuid(), "Test Food Item", "", 1 ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test Food Group", "" ), 1 ) ); viewModel.FoodItemServings.Add( new ServingViewModel<FoodItem>( foodItem, 2 ) ); Assert.AreEqual( meal.FoodGroupServings.Count + 1, viewModel.FoodGroupServings.Count ); }
public void InitializeData() { FoodGroup fruit = new FoodGroup( Guid.NewGuid(), "Fruit", "" ); FoodGroup meat = new FoodGroup( Guid.NewGuid(), "Meat", "" ); FoodGroup vegetable = new FoodGroup( Guid.NewGuid(), "Vegetable", "" ); FoodItem sourceFoodItem = new FoodItem( Guid.NewGuid(), "Banana Burger", "Hamburger meat between banana slices", 235 ); sourceFoodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( fruit, 2 ) ); sourceFoodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1 ) ); Assert.AreEqual( 2, sourceFoodItem.FoodGroupsPerServing.Count ); FoodItem foodItem = new FoodItem(); Assert.IsNotNull( foodItem.ID ); Assert.AreNotEqual( sourceFoodItem.ID, foodItem.ID ); Assert.IsNull( foodItem.Name ); Assert.IsNull( foodItem.Description ); Assert.AreEqual( 0, foodItem.FoodGroupsPerServing.Count ); Assert.AreEqual( 0, foodItem.CaloriesPerServing ); foodItem.InitializeData( sourceFoodItem ); Assert.AreEqual( sourceFoodItem.ID, foodItem.ID ); Assert.AreEqual( sourceFoodItem.Name, foodItem.Name ); Assert.AreEqual( sourceFoodItem.Description, foodItem.Description ); Assert.AreEqual( sourceFoodItem.FoodGroupsPerServing.Count, foodItem.FoodGroupsPerServing.Count ); Serving<FoodGroup> serving = foodItem.FoodGroupsPerServing.Find( fg => fg.Entity.Name == "Fruit" ); Assert.IsNotNull( serving ); Assert.AreEqual( fruit, serving.Entity ); Assert.AreEqual( 2, serving.Quantity ); serving = foodItem.FoodGroupsPerServing.Find( fg => fg.Entity.Name == "Meat" ); Assert.IsNotNull( serving ); Assert.AreEqual( meat, serving.Entity ); Assert.AreEqual( 1, serving.Quantity ); // Create a new source food item, and use it to init the target item sourceFoodItem = new FoodItem( Guid.NewGuid(), "Frog Soup", "Frogs, Aritichokes, and stuff", 250 ); sourceFoodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1 ) ); sourceFoodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( vegetable, 2 ) ); sourceFoodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( fruit, 0.5M ) ); foodItem.InitializeData( sourceFoodItem ); Assert.AreEqual( sourceFoodItem.ID, foodItem.ID ); Assert.AreEqual( sourceFoodItem.Name, foodItem.Name ); Assert.AreEqual( sourceFoodItem.Description, foodItem.Description ); Assert.AreEqual( sourceFoodItem.FoodGroupsPerServing.Count, foodItem.FoodGroupsPerServing.Count ); serving = foodItem.FoodGroupsPerServing.Find( fg => fg.Entity.Name == "Meat" ); Assert.IsNotNull( serving ); Assert.AreEqual( meat, serving.Entity ); Assert.AreEqual( 1, serving.Quantity ); serving = foodItem.FoodGroupsPerServing.Find( fg => fg.Entity.Name == "Vegetable" ); Assert.IsNotNull( serving ); Assert.AreEqual( vegetable, serving.Entity ); Assert.AreEqual( 2, serving.Quantity ); serving = foodItem.FoodGroupsPerServing.Find( fg => fg.Entity.Name == "Fruit" ); Assert.IsNotNull( serving ); Assert.AreEqual( fruit, serving.Entity ); Assert.AreEqual( 0.5M, serving.Quantity ); // We should also be able to take a Food Item and make something else out of it, such as a category FoodGroup category = new FoodGroup(); category.InitializeData( sourceFoodItem ); Assert.AreEqual( sourceFoodItem.ID, category.ID ); Assert.AreEqual( sourceFoodItem.Name, category.Name ); Assert.AreEqual( sourceFoodItem.Description, category.Description ); }
public FoodItem( FoodItem foodItem ) : this() { this.InitializeData( foodItem ); }
public override void OnNavigatedTo( NavigationContext navigationContext ) { if (Model == null) { String idParameter = navigationContext.Parameters["ID"]; Guid myID; FoodItem foodItem = null; if (idParameter != null && Guid.TryParse( idParameter, out myID )) { foodItem = this.DataRepository.GetFoodItem( myID ); } else { foodItem = new FoodItem(); } Model = foodItem; // TODO: Read up and see if there is a better way of diabling the event. // Also, refactor this into a private method. FoodGroupsPerServing.CollectionChanged -= OnFoodGroupServingsChanged; FoodGroupsPerServing.Clear(); foreach (Serving<FoodGroup> serving in foodItem.FoodGroupsPerServing) { ServingViewModel<FoodGroup> servingViewModel = new ServingViewModel<FoodGroup>( serving ); servingViewModel.PropertyChanged += OnFoodGroupServingPropertyChanged; FoodGroupsPerServing.Add( servingViewModel ); } FoodGroupsPerServing.CollectionChanged += OnFoodGroupServingsChanged; base.OnNavigatedTo( navigationContext ); } }
/// <summary> /// Determine if the repository already contains the food item /// </summary> /// <param name="foodItem">The food item</param> /// <returns>Returns true if the food item is alread in the repository, false oterwise.</returns> public Boolean Contains( FoodItem foodItem ) { return (_foodItems.Find( item => item.ID == foodItem.ID ) != null); }
public void AddingMealForOtherDateToRepositoryDoesNotUpdateCaloriesAndFoodGroups() { PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler(); var loggerMock = new Mock<ILoggerFacade>(); var dataRepositoryMock = new Mock<IDataRepository>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var navigationServiceMock = new Mock<IRegionNavigationService>(); var data = new MockData(); var fullMealList = data.Meals(); var todaysMeals = (from meal in fullMealList where meal.DateAndTimeOfMeal.Date == DateTime.Today select meal).ToList(); dataRepositoryMock.Setup( x => x.GetAllMealsForDate( DateTime.Today ) ).Returns( new ReadOnlyCollection<Meal>( todaysMeals ) ); var viewModel = new DailyLogViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); viewModel.CurrentDate = DateTime.Today; viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged; Int32 originalFoodGroupCount = viewModel.FoodGroupServings.Count; Decimal originalFoodGroupServings = (from foodGroupServing in viewModel.FoodGroupServings select foodGroupServing.Quantity).Sum(); Decimal originalCalories = viewModel.Calories; var foodItem = new FoodItem( Guid.NewGuid(), "test", "", 42 ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test1", "" ), 2 ) ); foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test2", "" ), 1 ) ); var newMeal = new Meal( Guid.NewGuid(), new MealType( Guid.NewGuid(), "Doesn't Matter", "", DateTime.Now, false ), DateTime.Now.AddDays( 1 ), "Test Meal", "Just a test" ); newMeal.FoodItemServings.Add( new Serving<FoodItem>( foodItem, 1 ) ); dataRepositoryMock.Raise( e => e.ItemAdded += null, new RepositoryObjectEventArgs( newMeal ) ); Assert.AreEqual( originalCalories, viewModel.Calories ); Assert.AreEqual( originalFoodGroupCount, viewModel.FoodGroupServings.Count ); Assert.AreEqual( originalFoodGroupServings, (from foodGroupServing in viewModel.FoodGroupServings select foodGroupServing.Quantity).Sum() ); Assert.AreEqual( 0, propertyChangedHandler.PropertiesChanged.Count ); }
public List<FoodItem> Foods() { if (_foods == null) { _foods = new List<FoodItem>(); var fruit = FoodGroups().Find( fg => fg.ID == fruitID ); var dairy = FoodGroups().Find( fg => fg.ID == dairyID ); var meat = FoodGroups().Find( fg => fg.ID == meatID ); var vegetable = FoodGroups().Find( fg => fg.ID == vegetableID ); var starch = FoodGroups().Find( fg => fg.ID == starchID ); var food = new FoodItem( milkID, "Milk", "Moo Juice", 90.0M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( dairy, 1.0M ) ); _foods.Add( food ); food = new FoodItem( ojID, "Orange Juice", "Fresh Squeazed, lots of pulp", 100.0M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( fruit, 1.0M ) ); _foods.Add( food ); // breakfast Foods food = new FoodItem( friedEggID, "Egg", "Chicken abortion, fried", 60.0M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1.0M ) ); _foods.Add( food ); food = new FoodItem( hashBrownsID, "Hash Browns", "Shredded tators", 125.5M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( starch, 1.0M ) ); _foods.Add( food ); food = new FoodItem( baconID, "Bacon", "Meat of the gods", 100.0M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1.0M ) ); _foods.Add( food ); food = new FoodItem( cerealID, "Cereal", "Shredded Mini-Wheats", 125.0M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( starch, 2.0M ) ); _foods.Add( food ); // lunch foods food = new FoodItem( hamSandwichID, "Ham Sandwich", "Dead cured pig between bread", 320.0M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1.0M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( vegetable, 0.5M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( dairy, 0.5M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( starch, 2.0M ) ); _foods.Add( food ); food = new FoodItem( baconCheeseBurgerID, "Bacon Cheese Burger", "Burger of the gods", 600.0M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 2.0M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( starch, 1.0M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( vegetable, 0.25M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( dairy, 0.5M ) ); _foods.Add( food ); food = new FoodItem( frenchFriesID, "French Fries", "Skinny deep fried tators", 200.0M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( starch, 1.5M ) ); _foods.Add( food ); food = new FoodItem( carrotSticksID, "carrotSticks", "Orange skinny poser FFs", 40M ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( vegetable, 1.0M ) ); _foods.Add( food ); // Dinner foods - food items dones as full meals. food = new FoodItem( chickenDinnerID, "Chicken Dinner", "Chicken, spuds, and veggies", 700 ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1.0M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( vegetable, 1.0M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( starch, 1.0M ) ); _foods.Add( food ); food = new FoodItem( steakDinnerID, "Steak Dinner", "Cow, spuds, and veggies", 1200 ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1.5M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( vegetable, 1.0M ) ); food.FoodGroupsPerServing.Add( new Serving<FoodGroup>( starch, 1.0M ) ); _foods.Add( food ); } return _foods; }