public void MealTemplateAddedToChildrenWhenAddedToRepository() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); FullTestData.Reset(); DataRepository dataRepository = new DataRepository( configurationMock.Object ); MealTemplateNodeViewModel mealTemplateNodeViewModel = new MealTemplateNodeViewModel( dataRepository, null ); Int32 originalChildCount = mealTemplateNodeViewModel.Children.Count; MealTemplate newMealTemplate = new MealTemplate( Guid.NewGuid(), dataRepository.GetAllMealTypes()[0], DateTime.Now, "Test Meal Template", "Just a test" ); newMealTemplate.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.GetAllFoodItems()[0], 1 ) ); dataRepository.SaveItem( newMealTemplate ); Assert.AreEqual( originalChildCount + 1, mealTemplateNodeViewModel.Children.Count ); foreach (ClickableTreeNodeViewModel node in mealTemplateNodeViewModel.Children) { MealTemplate mealTemplate = dataRepository.GetMealTemplate( (Guid)node.Parameter ); Assert.IsNotNull( mealTemplate ); Assert.AreEqual( mealTemplate.Name, node.Name ); } }
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 FoodItemNodeVewModelModifyFoodItem() { PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler(); 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 foodItem = dataRepository.GetAllFoodItems().ElementAt( 0 ); String originalName = foodItem.Name; ClickableTreeNodeViewModel foodItemInTree = null; foreach (ClickableTreeNodeViewModel child in foodItemNodeViewModel.Children) { child.PropertyChanged += propertyChangedHandler.OnPropertyChanged; if ((Guid)child.Parameter == foodItem.ID) { foodItemInTree = child; } } Assert.IsNotNull( foodItemInTree ); Assert.AreEqual( originalName, foodItemInTree.Name ); foodItem.Name += "Modified"; dataRepository.SaveItem( foodItem ); Assert.AreEqual( originalName + "Modified", foodItemInTree.Name ); Assert.AreEqual( 1, propertyChangedHandler.PropertiesChanged.Count ); Assert.AreEqual( foodItemInTree, propertyChangedHandler.Sender ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) ); }
public void MealTypeAddedToChildrenWhenAddedToRepository() { 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 ); MealTypeNodeViewModel mealTypeNodeViewModel = new MealTypeNodeViewModel( dataRepository, null ); Int32 originalChildCount = mealTypeNodeViewModel.Children.Count; MealType newMealType = new MealType( Guid.NewGuid(), "New Meal Type", "Some Description", DateTime.Now, false ); dataRepository.SaveItem( newMealType ); Assert.AreEqual( originalChildCount + 1, mealTypeNodeViewModel.Children.Count ); foreach (ClickableTreeNodeViewModel node in mealTypeNodeViewModel.Children) { MealType mealType = dataRepository.GetMealType( (Guid)node.Parameter ); Assert.IsNotNull( mealType ); Assert.AreEqual( mealType.Name, node.Name ); } }
public void MealCopy() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); DateTime theTime = DateTime.Now; Meal mealModel = new Meal( new Guid( "d88a6942-663d-4f6f-831d-ea9127ccc0e9" ), dataRepository.FindMealType( mt => mt.Name == "Lunch" ), theTime, "Lunch", null ); mealModel.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Baby Carrots" ), 2.5M ) ); mealModel.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Deluxe Bacon Cheese Burger" ), 1 ) ); mealModel.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Glass of Skim Milk" ), 2 ) ); Assert.IsTrue( mealModel.IsValid ); Meal meal = new Meal( mealModel ); Assert.IsNotNull( meal.ID ); Assert.AreEqual( mealModel.ID, meal.ID ); Assert.IsTrue( meal.IsValid ); Assert.AreEqual( mealModel.TypeOfMeal, meal.TypeOfMeal ); Assert.AreEqual( mealModel.DateAndTimeOfMeal, meal.DateAndTimeOfMeal ); Assert.AreEqual( mealModel.Calories, meal.Calories ); Assert.AreEqual( mealModel.FoodItemServings.Count, meal.FoodItemServings.Count ); for (Int32 idx = 0; idx < mealModel.FoodItemServings.Count; idx++) { Assert.AreEqual( mealModel.FoodItemServings[idx].Quantity, meal.FoodItemServings[idx].Quantity ); Assert.AreEqual( mealModel.FoodItemServings[idx].Entity, meal.FoodItemServings[idx].Entity ); } Assert.AreEqual( mealModel.FoodGroupServings.Count, meal.FoodGroupServings.Count ); }
public void FoodItemViewModelUndoRedo() { var loggerMock = new Mock<ILoggerFacade>(); Mock<IRegionNavigationService> regionNavigationServiceMock = new Mock<IRegionNavigationService>(); Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>(); Mock<IInteractionService> interactionServiceMock = new Mock<IInteractionService>(); 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 ); // TODO: Update this test when the undo and redo is expanded. // TODO: Update this test to include Calories (can be done before expansion) // Should not be able to undo or redo on a new object. ServingViewModel<FoodGroup> waterServing = new ServingViewModel<FoodGroup>( dataRepository.GetFoodGroup( FullTestData.WaterID ), 1.0M ); ServingViewModel<FoodGroup> meatServing = new ServingViewModel<FoodGroup>( dataRepository.GetFoodGroup( FullTestData.MeatID ), 1.5M ); ServingViewModel<FoodGroup> fruitServing = new ServingViewModel<FoodGroup>( dataRepository.GetFoodGroup( FullTestData.FruitID ), 2.5M ); FoodItemViewModel foodItemViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView", UriKind.Relative ) ); foodItemViewModel.OnNavigatedTo( navigationContext ); Assert.IsFalse( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); // Assign values to properties, save, and the load into new view model to reset the undo/redo log foodItemViewModel.Name = "Bob"; foodItemViewModel.Description = "Battery Operated Buddy"; foodItemViewModel.CaloriesPerServing = 42.0M; foodItemViewModel.FoodGroupsPerServing.Add( waterServing ); navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItemViewModel.ID.ToString(), UriKind.Relative ) ); foodItemViewModel.SaveCommand.Execute( null ); foodItemViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); foodItemViewModel.OnNavigatedTo( navigationContext ); Assert.IsFalse( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.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 Add Meat food group serving (1.5) // o Change Calroies to 69 // o Add Fruit food group serving (2.5) // o Change Meat servings to 3 // o Remove the Fruit food serving // 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 foodItemViewModel.Name = "Pete"; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.Name += "r"; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.Description = "The Rock"; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.FoodGroupsPerServing.Add( meatServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.CaloriesPerServing = 69; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.FoodGroupsPerServing.Add( fruitServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); meatServing.Quantity = 3; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.FoodGroupsPerServing.Remove( fruitServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.Name = "Simon"; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.Name = "Saul"; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.Description = "The Persecutor"; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.Description = "The Apostle"; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.Name = "Paul"; Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); Assert.AreEqual( "Paul", foodItemViewModel.Name ); Assert.AreEqual( "The Apostle", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); // Undo once. Verify last thing done is undone, and we can redo. foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodItemViewModel.Name ); Assert.AreEqual( "The Apostle", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); // Redo. Verify last thing undone is redone, can no longer redo, can still undo. foodItemViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Paul", foodItemViewModel.Name ); Assert.AreEqual( "The Apostle", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodItemViewModel.RedoCommand.CanExecute( null ) ); // Undo 4 times, verify undo worked foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodItemViewModel.Name ); Assert.AreEqual( "The Apostle", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodItemViewModel.Name ); Assert.AreEqual( "The Persecutor", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Simon", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); // Redo 2 times, verify foodItemViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Saul", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Saul", foodItemViewModel.Name ); Assert.AreEqual( "The Persecutor", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); // Undo until back to original, cannot undo, can redo foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 3, meatServing.Quantity ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Simon", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 3, meatServing.Quantity ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 3, meatServing.Quantity ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 3, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( fruitServing ) ); Assert.AreEqual( 3, meatServing.Quantity ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 3, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( fruitServing ) ); Assert.AreEqual( 1.5M, meatServing.Quantity ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 1.5M, meatServing.Quantity ); Assert.AreEqual( 69, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 1.5M, meatServing.Quantity ); Assert.AreEqual( 42, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 1, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.AreEqual( 1.5M, meatServing.Quantity ); Assert.AreEqual( 42, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodItemViewModel.Description ); Assert.AreEqual( 1, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.AreEqual( 1.5M, meatServing.Quantity ); Assert.AreEqual( 42, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Pete", foodItemViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodItemViewModel.Description ); Assert.AreEqual( 1, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.AreEqual( 1.5M, meatServing.Quantity ); Assert.AreEqual( 42, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Bob", foodItemViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodItemViewModel.Description ); Assert.AreEqual( 1, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.AreEqual( 1.5M, meatServing.Quantity ); Assert.AreEqual( 42, foodItemViewModel.CaloriesPerServing ); Assert.IsFalse( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); // Redo 4 times, verify foodItemViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Pete", foodItemViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodItemViewModel.Description ); Assert.AreEqual( 1, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.AreEqual( 42, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodItemViewModel.Description ); Assert.AreEqual( 1, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.AreEqual( 42, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 1, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.AreEqual( 42, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); foodItemViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Peter", foodItemViewModel.Name ); Assert.AreEqual( "The Rock", foodItemViewModel.Description ); Assert.AreEqual( 2, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.FoodGroupsPerServing.Contains( meatServing ) ); Assert.AreEqual( 42, foodItemViewModel.CaloriesPerServing ); Assert.IsTrue( foodItemViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodItemViewModel.RedoCommand.CanExecute( null ) ); }
public void FoodItemViewModelTitle() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); 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 ); FoodItemViewModel foodItemViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); Assert.AreEqual( DisplayStrings.NewFoodItemTitle, foodItemViewModel.Title ); // Need to navigate to a new food group in order to makes changes and save them. NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView", UriKind.Relative ) ); foodItemViewModel.OnNavigatedTo( navigationContext ); Assert.AreEqual( DisplayStrings.NewFoodItemTitle, foodItemViewModel.Title ); foodItemViewModel.Name = "Test the Title"; foodItemViewModel.Description = "This is just to test the title"; foodItemViewModel.CaloriesPerServing = 90; foodItemViewModel.FoodGroupsPerServing.Add( new ServingViewModel<FoodGroup>( dataRepository.GetAllFoodGroups()[0], 1 ) ); Assert.AreEqual( DisplayStrings.NewFoodItemTitle, foodItemViewModel.Title ); Assert.IsTrue( foodItemViewModel.SaveCommand.CanExecute( null ) ); foodItemViewModel.SaveCommand.Execute( null ); Assert.AreEqual( "Test the Title", foodItemViewModel.Title ); }
public void FoodGroupViewModelIsDirty() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); 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 ); // Existing Food group // Starts clean // Change Name - dirty // Undo - clean // Change Description - dirty // Undo - clean FoodGroup vegetableFoodGroup = dataRepository.GetFoodGroup( FullTestData.VegetableID ); Assert.IsNotNull( vegetableFoodGroup ); String originalName = vegetableFoodGroup.Name; String originalDescription = vegetableFoodGroup.Description; FoodGroupViewModel vegetableFoodGroupViewModel = new FoodGroupViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "DoesItMatter?ID=" + vegetableFoodGroup.ID.ToString(), UriKind.Relative ) ); vegetableFoodGroupViewModel.OnNavigatedTo( navigationContext ); Assert.IsFalse( vegetableFoodGroupViewModel.IsDirty ); vegetableFoodGroupViewModel.Name += "Modified"; Assert.IsTrue( vegetableFoodGroupViewModel.IsDirty ); vegetableFoodGroupViewModel.Name = originalName; Assert.IsFalse( vegetableFoodGroupViewModel.IsDirty ); vegetableFoodGroupViewModel.Description += "Different"; Assert.IsTrue( vegetableFoodGroupViewModel.IsDirty ); vegetableFoodGroupViewModel.Description = originalDescription; Assert.IsFalse( vegetableFoodGroupViewModel.IsDirty ); vegetableFoodGroupViewModel.Name = null; Assert.IsTrue( vegetableFoodGroupViewModel.IsDirty ); vegetableFoodGroupViewModel.Name = originalName; Assert.IsFalse( vegetableFoodGroupViewModel.IsDirty ); }
public void NonMealTemplateNotAddedToChildrenWhenAddedToRepository() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); FullTestData.Reset(); DataRepository dataRepository = new DataRepository( configurationMock.Object ); MealTemplateNodeViewModel mealTemplateNodeViewModel = new MealTemplateNodeViewModel( dataRepository, null ); Int32 originalChildCount = mealTemplateNodeViewModel.Children.Count; var mealType = new MealType( Guid.NewGuid(), "A New Meal Type", "This should not already exist", DateTime.Now, false ); Assert.IsTrue( mealType.IsValid ); dataRepository.SaveItem( mealType ); Assert.AreEqual( originalChildCount, mealTemplateNodeViewModel.Children.Count ); foreach (ClickableTreeNodeViewModel node in mealTemplateNodeViewModel.Children) { MealTemplate mealTemplate = dataRepository.GetMealTemplate( (Guid)node.Parameter ); Assert.IsNotNull( mealTemplate ); Assert.AreEqual( mealTemplate.Name, node.Name ); } }
public void FoodItemNodeViewModelDefault() { 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 ); Assert.AreEqual( DisplayStrings.AdminFoodItemsTitle, foodItemNodeViewModel.Name ); // If the counts are the same, and every child is in the repository, then the data should be fine. Assert.AreEqual( dataRepository.GetAllFoodItems().Count, 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 MealTemplateName() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); MealTemplate mealTemplate = new MealTemplate( Guid.NewGuid(), dataRepository.FindMealType( mt => mt.Name == "Dinner" ), DateTime.Now, "Just a test mealTemplate", "" ); Assert.IsNull( mealTemplate["Name"] ); // Use String w/ just whitespace mealTemplate.Name = " "; Assert.AreEqual( Messages.Error_No_Name, mealTemplate["Name"] ); Assert.IsNull( mealTemplate.Name ); // Whitespace on front mealTemplate.Name = " The Name"; Assert.IsNull( mealTemplate["Name"] ); Assert.AreEqual( "The Name", mealTemplate.Name ); // Whitespace on end mealTemplate.Name = "The Name "; Assert.IsNull( mealTemplate["Name"] ); Assert.AreEqual( "The Name", mealTemplate.Name ); // Whitespace on front and end mealTemplate.Name = " The Name "; Assert.IsNull( mealTemplate["Name"] ); Assert.AreEqual( "The Name", mealTemplate.Name ); // Only whitespace in middle mealTemplate.Name = "The Name"; Assert.IsNull( mealTemplate["Name"] ); Assert.AreEqual( "The Name", mealTemplate.Name ); }
public void MealTemplateIDTypeTimeNameDescription() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); DateTime theTime = DateTime.Now; MealTemplate mealTemplate = new MealTemplate( new Guid( "1c456eca-17c6-4395-a14f-85800a5c9d35" ), dataRepository.FindMealType( mt => mt.Name == "Dinner" ), theTime, "Just a test mealTemplate", "And this is the description" ); // Should be invalid when instantiated since there is a lot of missing data Assert.IsFalse( mealTemplate.IsValid ); Assert.IsNull( mealTemplate["TypeOfMeal"] ); Assert.IsNull( mealTemplate["TimeOfMeal"] ); Assert.AreEqual( Messages.Error_No_FoodItems, mealTemplate["FoodItemServings"] ); Assert.IsNull( mealTemplate["Name"] ); Assert.IsNull( mealTemplate["ID"] ); Assert.AreEqual( new Guid( "1c456eca-17c6-4395-a14f-85800a5c9d35" ), mealTemplate.ID ); Assert.AreEqual( "Dinner", mealTemplate.TypeOfMeal.Name ); Assert.ReferenceEquals( dataRepository.FindMealType( mt => mt.Name == "Dinner" ), mealTemplate.TypeOfMeal ); Assert.AreEqual( theTime, mealTemplate.DateAndTimeOfMeal ); Assert.AreEqual( "Just a test mealTemplate", mealTemplate.Name ); Assert.AreEqual( "And this is the description", mealTemplate.Description ); Assert.AreEqual( 0, mealTemplate.FoodItemServings.Count ); Assert.AreEqual( 0, mealTemplate.Calories ); Assert.AreEqual( 0, mealTemplate.FoodGroupServings.Count ); // Add missing data, show that it is now valid mealTemplate.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Baby Carrots" ), 2.5M ) ); mealTemplate.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Deluxe Bacon Cheese Burger" ), 1 ) ); mealTemplate.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Glass of Skim Milk" ), 2 ) ); mealTemplate.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Fruit Salad" ), 1.5M ) ); Assert.IsTrue( mealTemplate.IsValid ); Assert.IsNull( mealTemplate["TypeOfMeal"] ); Assert.IsNull( mealTemplate["TimeOfMeal"] ); Assert.IsNull( mealTemplate["FoodItemServings"] ); Assert.IsNull( mealTemplate["Name"] ); Assert.AreEqual( (2.5M * 40) + 650 + (2 * 90) + (1.5M * 150), mealTemplate.Calories ); Assert.AreEqual( 5, mealTemplate.FoodGroupServings.Count ); Assert.AreEqual( 3, mealTemplate.FoodGroupServings.Find( fg => fg.Entity.Name == "Vegetable" ).Quantity ); Assert.AreEqual( 1, mealTemplate.FoodGroupServings.Find( fg => fg.Entity.Name == "Grain" ).Quantity ); Assert.AreEqual( 1.5M, mealTemplate.FoodGroupServings.Find( fg => fg.Entity.Name == "Meat" ).Quantity ); Assert.AreEqual( 5, mealTemplate.FoodGroupServings.Find( fg => fg.Entity.Name == "Dairy" ).Quantity ); Assert.AreEqual( 3, mealTemplate.FoodGroupServings.Find( fg => fg.Entity.Name == "Fruit" ).Quantity ); }
public void FoodItemViewModelDefault() { var loggerMock = new Mock<ILoggerFacade>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); FoodItemViewModel foodItemViewModel = new FoodItemViewModel( dataRepository, null, null, loggerMock.Object ); Assert.AreEqual( DisplayStrings.NewFoodItemTitle, foodItemViewModel.Title ); }
public void FoodItemViewModelCaloriesPerServing() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler(); // Construct a known food group mealType and make sure the food group servings makes sense. FoodItem foodItem = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID ); FoodItemViewModel foodItemViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItem.ID.ToString(), UriKind.Relative ) ); foodItemViewModel.OnNavigatedTo( navigationContext ); foodItemViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged; Assert.AreEqual( foodItem.CaloriesPerServing, foodItemViewModel.CaloriesPerServing ); Assert.IsFalse( foodItemViewModel.IsDirty ); Assert.IsTrue( foodItemViewModel.IsValid ); // Change the calories foodItemViewModel.CaloriesPerServing = 700; Assert.IsTrue( foodItemViewModel.IsDirty ); Assert.IsTrue( foodItemViewModel.IsValid ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "CaloriesPerServing" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) ); Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count ); foodItemViewModel.CaloriesPerServing = 650; Assert.IsFalse( foodItemViewModel.IsDirty ); Assert.IsTrue( foodItemViewModel.IsValid ); propertyChangedHandler.Reset(); // Make the calories invalid foodItemViewModel.CaloriesPerServing = -700; Assert.IsTrue( foodItemViewModel.IsDirty ); Assert.IsFalse( foodItemViewModel.IsValid ); Assert.AreEqual( Messages.Error_Negative_Calories, foodItemViewModel.Error ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "CaloriesPerServing" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) ); Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count ); }
public void NavigateToNewFoodItem() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); var viewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemViewModel", UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); foreach (var foodItem in dataRepository.GetAllFoodItems()) { Assert.AreNotEqual( foodItem.ID, viewModel.ID ); } Assert.IsNull( viewModel.Name ); Assert.IsNull( viewModel.Description ); Assert.AreEqual( 0, viewModel.FoodGroupsPerServing.Count ); Assert.IsFalse( viewModel.IsDirty ); }
public void NavigateToFoodItem() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); FoodItemViewModel viewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "Something?ID=" + FullTestData.CheeseBurgerID, UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); Assert.AreEqual( FullTestData.CheeseBurgerID, viewModel.ID ); Assert.AreEqual( "Deluxe Bacon Cheese Burger", viewModel.Name ); Assert.AreEqual( "Ground up cow, topped with curdled milk and salted pig fat. Add lettuce, tomato, and onion for health.", viewModel.Description ); Assert.AreEqual( 4, viewModel.FoodGroupsPerServing.Count ); Assert.IsFalse( viewModel.IsDirty ); }
public void FoodItemViewModelValidFoodGroups() { var loggerMock = new Mock<ILoggerFacade>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); FoodItemViewModel foodItemViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); Assert.IsNotNull( foodItemViewModel.ValidFoodGroups ); Assert.AreEqual( dataRepository.GetAllFoodGroups().Count, foodItemViewModel.ValidFoodGroups.Items.Count ); }
public void NavigateToExistingFoodGroup() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); var viewModel = new FoodGroupViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodGroupView?ID=" + FullTestData.DairyID, UriKind.Relative ) ); viewModel.OnNavigatedTo( navigationContext ); Assert.AreEqual( FullTestData.DairyID, viewModel.ID ); Assert.AreEqual( "Dairy", viewModel.Name ); Assert.AreEqual( "Mostly stuff from cows.", viewModel.Description ); Assert.IsFalse( viewModel.IsDirty ); }
public void FoodGroupViewModelUndoRedo() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); 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 ); // Should not be able to undo or redo on a new object. FoodGroupViewModel foodGroupViewModel = new FoodGroupViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodGroupView", UriKind.Relative ) ); foodGroupViewModel.OnNavigatedTo( navigationContext ); Assert.IsFalse( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); // Set initial data and save then load into new view model to reset the undo/redo cache foodGroupViewModel.Name = "Bob"; foodGroupViewModel.Description = "Battery Operated Buddy"; foodGroupViewModel.SaveCommand.Execute( null ); navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodGroupView?ID=" + foodGroupViewModel.ID.ToString(), UriKind.Relative ) ); foodGroupViewModel = new FoodGroupViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); foodGroupViewModel.OnNavigatedTo( navigationContext ); Assert.IsFalse( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.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 foodGroupViewModel.Name = "Pete"; Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.Name += "r"; Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.Description = "The Rock"; Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.Name = "Simon"; Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.Name = "Saul"; Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.Description = "The Persecutor"; Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.Description = "The Apostle"; Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.Name = "Paul"; Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); Assert.AreEqual( "Paul", foodGroupViewModel.Name ); Assert.AreEqual( "The Apostle", foodGroupViewModel.Description ); // Undo once. Verify last thing done is undone, and we can redo. foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodGroupViewModel.Name ); Assert.AreEqual( "The Apostle", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); // Redo. Verify last thing undone is redone, can no longer redo, can still undo. foodGroupViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Paul", foodGroupViewModel.Name ); Assert.AreEqual( "The Apostle", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsFalse( foodGroupViewModel.RedoCommand.CanExecute( null ) ); // Undo 4 times, verify undo worked foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodGroupViewModel.Name ); Assert.AreEqual( "The Apostle", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodGroupViewModel.Name ); Assert.AreEqual( "The Persecutor", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodGroupViewModel.Name ); Assert.AreEqual( "The Rock", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Simon", foodGroupViewModel.Name ); Assert.AreEqual( "The Rock", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); // Redo 2 times, verify foodGroupViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Saul", foodGroupViewModel.Name ); Assert.AreEqual( "The Rock", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Saul", foodGroupViewModel.Name ); Assert.AreEqual( "The Persecutor", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); // Undo 6 times. Back to original, cannot undo, can redo foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Saul", foodGroupViewModel.Name ); Assert.AreEqual( "The Rock", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Simon", foodGroupViewModel.Name ); Assert.AreEqual( "The Rock", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", foodGroupViewModel.Name ); Assert.AreEqual( "The Rock", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Peter", foodGroupViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Pete", foodGroupViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.UndoCommand.Execute( null ); Assert.AreEqual( "Bob", foodGroupViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodGroupViewModel.Description ); Assert.IsFalse( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); // Redo 3 times, verify foodGroupViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Pete", foodGroupViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Peter", foodGroupViewModel.Name ); Assert.AreEqual( "Battery Operated Buddy", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); foodGroupViewModel.RedoCommand.Execute( null ); Assert.AreEqual( "Peter", foodGroupViewModel.Name ); Assert.AreEqual( "The Rock", foodGroupViewModel.Description ); Assert.IsTrue( foodGroupViewModel.UndoCommand.CanExecute( null ) ); Assert.IsTrue( foodGroupViewModel.RedoCommand.CanExecute( null ) ); }
public void FoodItemViewModelFoodGroupServings() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler(); // Navigate to a known food group mealType and make sure the food group servings makes sense. // NOTE: No need to get too fancy, the model tests makes sure the data loaded properly. FoodItem foodItem = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID ); FoodItemViewModel foodItemViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItem.ID.ToString(), UriKind.Relative ) ); foodItemViewModel.OnNavigatedTo( navigationContext ); foodItemViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged; Assert.AreEqual( foodItem.FoodGroupsPerServing.Count, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsFalse( foodItemViewModel.IsDirty ); Assert.IsTrue( foodItemViewModel.IsValid ); // Add a food group ServingViewModel<FoodGroup> testFoodServings = new ServingViewModel<FoodGroup>( dataRepository.GetFoodGroup( FullTestData.JunkFoodID ), 1 ); foodItemViewModel.FoodGroupsPerServing.Add( testFoodServings ); Assert.IsTrue( foodItemViewModel.IsDirty ); Assert.IsTrue( foodItemViewModel.IsValid ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) ); Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count ); foodItemViewModel.FoodGroupsPerServing.Remove( testFoodServings ); Assert.IsTrue( foodItemViewModel.IsValid ); Assert.IsFalse( foodItemViewModel.IsDirty ); propertyChangedHandler.Reset(); // Delete a food group testFoodServings = foodItemViewModel.FoodGroupsPerServing.ElementAt( 0 ); foodItemViewModel.FoodGroupsPerServing.Remove( testFoodServings ); Assert.IsTrue( foodItemViewModel.IsDirty ); Assert.IsTrue( foodItemViewModel.IsValid ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) ); Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count ); foodItemViewModel.FoodGroupsPerServing.Add( testFoodServings ); Assert.IsTrue( foodItemViewModel.IsValid ); Assert.IsFalse( foodItemViewModel.IsDirty ); propertyChangedHandler.Reset(); // Modify a food group foodItemViewModel.FoodGroupsPerServing[0].Quantity += 1; Assert.AreEqual( foodItem.FoodGroupsPerServing.Count, foodItemViewModel.FoodGroupsPerServing.Count ); Serving<FoodGroup> modelServing = foodItem.FoodGroupsPerServing.Find( f => f.Entity.ID == foodItemViewModel.FoodGroupsPerServing[0].Entity.ID ); Assert.IsNotNull( modelServing ); Assert.IsTrue( foodItemViewModel.IsDirty ); Assert.IsTrue( foodItemViewModel.IsValid ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) ); Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count ); foodItemViewModel.FoodGroupsPerServing[0].Quantity -= 1; Assert.IsTrue( foodItemViewModel.IsValid ); Assert.IsFalse( foodItemViewModel.IsDirty ); propertyChangedHandler.Reset(); // Make a serving invalid. foodItemViewModel.FoodGroupsPerServing[0].Quantity *= -1; Assert.AreEqual( foodItem.FoodGroupsPerServing.Count, foodItemViewModel.FoodGroupsPerServing.Count ); Assert.IsTrue( foodItemViewModel.IsDirty ); Assert.IsFalse( foodItemViewModel.IsValid ); Assert.AreEqual( Messages.Error_No_Quantity, foodItemViewModel.Error ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) ); Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count ); foodItemViewModel.FoodGroupsPerServing[0].Quantity *= -1; Assert.IsTrue( foodItemViewModel.IsValid ); Assert.IsFalse( foodItemViewModel.IsDirty ); propertyChangedHandler.Reset(); // Remove all food groups foodItemViewModel.FoodGroupsPerServing.Clear(); Assert.IsTrue( foodItemViewModel.IsDirty ); Assert.IsFalse( foodItemViewModel.IsValid ); Assert.AreEqual( Messages.Error_No_FoodGroups, foodItemViewModel.Error ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) ); Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count ); }
public void MealIdTypeAndTime() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); DateTime theTime = DateTime.Now; Meal meal = new Meal( new Guid( "d678325f-6bd0-4fae-8847-ebb07323b9bc" ), dataRepository.FindMealType( mt => mt.Name == "Lunch" ), theTime, "Lunch", null ); // Should be invalid when instantiated since there is a lot of missing data Assert.IsFalse( meal.IsValid ); Assert.IsNull( meal["TypeOfMeal"] ); Assert.IsNull( meal["TimeOfMeal"] ); Assert.AreEqual( Messages.Error_No_FoodItems, meal["FoodItemServings"] ); Assert.AreEqual( new Guid( "d678325f-6bd0-4fae-8847-ebb07323b9bc" ), meal.ID ); Assert.AreEqual( "Lunch", meal.TypeOfMeal.Name ); Assert.ReferenceEquals( dataRepository.FindMealType( mt => mt.Name == "Lunch" ), meal.TypeOfMeal ); Assert.AreEqual( theTime, meal.DateAndTimeOfMeal ); Assert.AreEqual( 0, meal.FoodItemServings.Count ); Assert.AreEqual( 0, meal.Calories ); Assert.AreEqual( 0, meal.FoodGroupServings.Count ); // Add missing data, show that it is now valid meal.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Baby Carrots" ), 2.5M ) ); meal.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Deluxe Bacon Cheese Burger" ), 1 ) ); meal.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Glass of Skim Milk" ), 2 ) ); Assert.IsTrue( meal.IsValid ); Assert.IsNull( meal["TypeOfMeal"] ); Assert.IsNull( meal["TimeOfMeal"] ); Assert.IsNull( meal["FoodItemServings"] ); Assert.AreEqual( (2.5M * 40) + 650 + (2 * 90), meal.Calories ); Assert.AreEqual( 4, meal.FoodGroupServings.Count ); Assert.AreEqual( 3, meal.FoodGroupServings.Find( fg => fg.Entity.Name == "Vegetable" ).Quantity ); Assert.AreEqual( 1, meal.FoodGroupServings.Find( fg => fg.Entity.Name == "Grain" ).Quantity ); Assert.AreEqual( 1.5M, meal.FoodGroupServings.Find( fg => fg.Entity.Name == "Meat" ).Quantity ); Assert.AreEqual( 5, meal.FoodGroupServings.Find( fg => fg.Entity.Name == "Dairy" ).Quantity ); }
public void FoodItemViewModelID() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); FoodItemViewModel testFoodItemViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + FullTestData.FruitSaladID, UriKind.Relative ) ); testFoodItemViewModel.OnNavigatedTo( navigationContext ); Assert.AreEqual( FullTestData.FruitSaladID, testFoodItemViewModel.ID ); }
public void MealTemplateMeal() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); DateTime theTime = DateTime.Now; Meal meal = new Meal( Guid.NewGuid(), dataRepository.FindMealType( mt => mt.Name == "Lunch" ), theTime, "Lunch", null ); meal.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Baby Carrots" ), 2.5M ) ); meal.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Deluxe Bacon Cheese Burger" ), 1 ) ); Assert.IsTrue( meal.IsValid ); Assert.AreEqual( 750, meal.Calories ); MealTemplate mealTemplate = new MealTemplate( meal ); Assert.AreEqual( meal.ID, mealTemplate.ID ); Assert.AreEqual( meal.TypeOfMeal, mealTemplate.TypeOfMeal ); Assert.AreEqual( meal.DateAndTimeOfMeal, mealTemplate.DateAndTimeOfMeal ); Assert.AreEqual( 2, mealTemplate.FoodItemServings.Count ); Assert.IsNotNull( mealTemplate.FoodItemServings.Find( s => s.Entity.Name == "Baby Carrots" ) ); Assert.IsNotNull( mealTemplate.FoodItemServings.Find( s => s.Entity.Name == "Deluxe Bacon Cheese Burger" ) ); Assert.IsTrue( mealTemplate.IsValid ); Assert.IsNull( mealTemplate["Name"] ); }
public void MealTemplateNodeViewModelRemoveMealTemplate() { 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 ); MealTemplateNodeViewModel mealTemplateNodeViewModel = new MealTemplateNodeViewModel( dataRepository, null ); Int32 originalChildCount = mealTemplateNodeViewModel.Children.Count; MealTemplate removedMealTemplate = dataRepository.GetAllMealTemplates().ElementAt( 0 ); dataRepository.Remove( removedMealTemplate ); Assert.AreEqual( originalChildCount - 1, mealTemplateNodeViewModel.Children.Count ); foreach (ClickableTreeNodeViewModel node in mealTemplateNodeViewModel.Children) { MealTemplate mealTemplate = dataRepository.GetMealTemplate( (Guid)node.Parameter ); Assert.IsNotNull( mealTemplate ); Assert.AreEqual( mealTemplate.Name, node.Name ); } }
public void MealTemplateValidationTests() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); DateTime theTime = DateTime.Now; MealTemplate mealTemplate = new MealTemplate( Guid.NewGuid(), default( MealType ), default( DateTime ), null, null ); Assert.IsFalse( mealTemplate.IsValid ); Assert.IsNull( mealTemplate["ID"] ); Assert.AreEqual( Messages.Error_No_MealType, mealTemplate["TypeOfMeal"] ); Assert.IsNull( mealTemplate["TimeOfMeal"] ); Assert.AreEqual( Messages.Error_No_FoodItems, mealTemplate["FoodItemServings"] ); Assert.AreEqual( Messages.Error_No_Name, mealTemplate["Name"] ); // Add a valid mealTemplate type mealTemplate.TypeOfMeal = dataRepository.FindMealType( mt => mt.Name == "Lunch" ); Assert.IsFalse( mealTemplate.IsValid ); Assert.IsNull( mealTemplate["ID"] ); Assert.IsNull( mealTemplate["TypeOfMeal"] ); Assert.IsNull( mealTemplate["TimeOfMeal"] ); Assert.AreEqual( Messages.Error_No_FoodItems, mealTemplate["FoodItemServings"] ); Assert.AreEqual( Messages.Error_No_Name, mealTemplate["Name"] ); // Add a valid time for the mealTemplate mealTemplate.DateAndTimeOfMeal = theTime; Assert.IsFalse( mealTemplate.IsValid ); Assert.IsNull( mealTemplate["ID"] ); Assert.IsNull( mealTemplate["TypeOfMeal"] ); Assert.IsNull( mealTemplate["TimeOfMeal"] ); Assert.AreEqual( Messages.Error_No_FoodItems, mealTemplate["FoodItemServings"] ); Assert.AreEqual( Messages.Error_No_Name, mealTemplate["Name"] ); // Add a food item serving mealTemplate.FoodItemServings.Add( new Serving<FoodItem>( dataRepository.FindFoodItem( food => food.Name == "Baby Carrots" ), 2.5M ) ); Assert.IsFalse( mealTemplate.IsValid ); Assert.IsNull( mealTemplate["ID"] ); Assert.IsNull( mealTemplate["TypeOfMeal"] ); Assert.IsNull( mealTemplate["TimeOfMeal"] ); Assert.IsNull( mealTemplate["FoodItemServings"] ); Assert.AreEqual( Messages.Error_No_Name, mealTemplate["Name"] ); // Assign a valid name mealTemplate.Name = "A test mealTemplate template"; Assert.IsTrue( mealTemplate.IsValid ); Assert.IsNull( mealTemplate["ID"] ); Assert.IsNull( mealTemplate["TypeOfMeal"] ); Assert.IsNull( mealTemplate["TimeOfMeal"] ); Assert.IsNull( mealTemplate["FoodItemServings"] ); Assert.IsNull( mealTemplate["Name"] ); }
public void FoodItemViewModelIsValid() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); FoodItemViewModel testFoodItemViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView", UriKind.Relative ) ); testFoodItemViewModel.OnNavigatedTo( navigationContext ); Assert.IsFalse( testFoodItemViewModel.IsValid ); Assert.AreEqual( Messages.Error_No_Name, testFoodItemViewModel["Name"] ); Assert.AreEqual( Messages.Error_No_FoodGroups, testFoodItemViewModel["FoodGroupsPerServing"] ); Assert.IsTrue( String.IsNullOrEmpty( testFoodItemViewModel["Description"] ) ); testFoodItemViewModel.Name = "Fruit Salad"; testFoodItemViewModel.FoodGroupsPerServing.Add( new ServingViewModel<FoodGroup>( dataRepository.FindFoodGroup( fg => fg.Name == "Fruit" ), 1 ) ); Assert.IsFalse( testFoodItemViewModel.IsValid ); Assert.AreEqual( Messages.Error_FoodItem_Exists, testFoodItemViewModel["Name"] ); Assert.IsTrue( String.IsNullOrEmpty( testFoodItemViewModel["FoodGroupsPerServing"] ) ); Assert.IsTrue( String.IsNullOrEmpty( testFoodItemViewModel["Description"] ) ); testFoodItemViewModel.Name = "Some Unique Name"; Assert.IsTrue( testFoodItemViewModel.IsValid ); Assert.IsTrue( String.IsNullOrEmpty( testFoodItemViewModel.Error ) ); Assert.IsTrue( String.IsNullOrEmpty( testFoodItemViewModel["Name"] ) ); Assert.IsTrue( String.IsNullOrEmpty( testFoodItemViewModel["FoodGroupsPerServing"] ) ); Assert.IsTrue( String.IsNullOrEmpty( testFoodItemViewModel["Description"] ) ); }
public void FoodItemNodeViewModelRemoveFoodItem() { 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 removedFoodItem = dataRepository.GetAllFoodItems().ElementAt( 0 ); dataRepository.Remove( removedFoodItem ); 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 FoodItemViewModelName() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler(); FoodItem testFoodItem = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID ); FoodItemViewModel testFoodItemViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); // Tests: // o Name is the same as the object from the repository // o Changing the name causes appropriate properties to be marked as changed NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + testFoodItem.ID.ToString(), UriKind.Relative ) ); testFoodItemViewModel.OnNavigatedTo( navigationContext ); testFoodItemViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged; Assert.AreEqual( testFoodItem.Name, testFoodItemViewModel.Name ); testFoodItemViewModel.Name = "Some other name"; Assert.AreEqual( "Some other name", testFoodItemViewModel.Name ); Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) ); Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) ); }
public void FoodItemViewModelIsDirty() { var loggerMock = new Mock<ILoggerFacade>(); var regionNavigationServiceMock = new Mock<IRegionNavigationService>(); var regionManagerMock = new Mock<IRegionManager>(); var interactionServiceMock = new Mock<IInteractionService>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); // Existing Food Item // Starts clean // Change Name - dirty // Undo - clean // Change Description - dirty // Undo - clean // Change Calories - dirty // Undo - clean // Change Food Group Serving - dirty // Undo - clean // Add food group - dirty // undo - clean // FoodItem cheeseBurger = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID ); Assert.IsNotNull( cheeseBurger ); String originalName = cheeseBurger.Name; String originalDescription = cheeseBurger.Description; FoodItemViewModel cheeseBurgerViewModel = new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object ); NavigationContext navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + cheeseBurger.ID.ToString(), UriKind.Relative ) ); cheeseBurgerViewModel.OnNavigatedTo( navigationContext ); Assert.IsFalse( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.Name += "Modified"; Assert.IsTrue( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.Name = originalName; Assert.IsFalse( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.Description += "Different"; Assert.IsTrue( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.Description = originalDescription; Assert.IsFalse( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.Name = null; Assert.IsTrue( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.Name = originalName; Assert.IsFalse( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.CaloriesPerServing += 90; Assert.IsTrue( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.CaloriesPerServing -= 90; Assert.IsFalse( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.FoodGroupsPerServing[0].Quantity += 1; Assert.IsTrue( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.FoodGroupsPerServing[0].Quantity -= 1; Assert.IsFalse( cheeseBurgerViewModel.IsDirty ); ServingViewModel<FoodGroup> serving = cheeseBurgerViewModel.FoodGroupsPerServing[0]; cheeseBurgerViewModel.FoodGroupsPerServing.Remove( serving ); Assert.IsTrue( cheeseBurgerViewModel.IsDirty ); // Create an exact duplicate of the servings. ServingViewModel<FoodGroup> copyOfServing = new ServingViewModel<FoodGroup>( serving.Entity, serving.Quantity ); cheeseBurgerViewModel.FoodGroupsPerServing.Add( copyOfServing ); Assert.IsFalse( cheeseBurgerViewModel.IsDirty ); serving = new ServingViewModel<FoodGroup>( dataRepository.FindFoodGroup( fg => fg.Name == "Water" ), 1 ); cheeseBurgerViewModel.FoodGroupsPerServing.Add( serving ); Assert.IsTrue( cheeseBurgerViewModel.IsDirty ); cheeseBurgerViewModel.FoodGroupsPerServing.Remove( serving ); Assert.IsFalse( cheeseBurgerViewModel.IsDirty ); }
public void FoodGroupViewModelDefault() { var loggerMock = new Mock<ILoggerFacade>(); var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new DataRepository( configurationMock.Object ); FoodGroupViewModel foodGroupViewModel = new FoodGroupViewModel( dataRepository, null, null, loggerMock.Object ); // This is really the only property that should be set at this point. // There is no model set until the view/view model is navigated to. Assert.AreEqual( DisplayStrings.NewFoodGroupTitle, foodGroupViewModel.Title ); }