public void DeleteFoodGroup()
        {
            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 group.
             FoodGroup meat = new FoodGroup();
             meat.InitializeData( dataRepository.FindFoodGroup( fg => fg.Name == "Meat" ) );

             // Delete the food group.  Show that the food group 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( meat );
             Assert.AreEqual( origFoodGroupCount - 1, dataRepository.GetAllFoodGroups().Count );
             Assert.AreEqual( origFoodItemCount, dataRepository.GetAllFoodItems().Count );
             Assert.AreEqual( origMealTypeCount, dataRepository.GetAllMealTypes().Count );
             Assert.AreEqual( origMealTemplateCount, dataRepository.GetAllMealTemplates().Count );
             Assert.AreEqual( origMealCount, dataRepository.GetAllMeals().Count );
             Assert.IsNotNull( meat.ID );
             Assert.IsNull( dataRepository.FindFoodGroup( f => f.ID == meat.ID ) );
        }
Example #2
0
        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"] );
        }
Example #3
0
        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 FoodGroupAddedToViewModelWhenAddedToRepository()
        {
            var data = new MockData();
             var dataRespositoryMock = CreateMockDataRepository( data.FoodGroups() );

             var viewModel = new AllFoodGroupsViewModel( dataRespositoryMock.Object );
             var foodGroup = new FoodGroup( Guid.NewGuid(), "New Group", "Some newly dreamed up food group" );
             data.FoodGroups().Add( foodGroup );
             dataRespositoryMock.Raise( e => e.ItemAdded += null, new RepositoryObjectEventArgs( foodGroup ) );

             AssertViewModelContents( viewModel, data.FoodGroups() );
        }
        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 ServingViewModelDefault()
        {
            FoodGroup foodGroupOne = new FoodGroup( Guid.NewGuid(), "FoodGroupOne", "Test Food Group One" );

             ServingViewModel<FoodGroup> servingViewModel = new ServingViewModel<FoodGroup>();
             Assert.IsFalse( servingViewModel.IsValid );
             Assert.IsNull( servingViewModel.Entity );
             Assert.AreEqual( 0, servingViewModel.Quantity );
             Assert.AreEqual( Messages.Error_No_Quantity, servingViewModel["Quantity"] );
             Assert.AreEqual( Messages.Error_No_ServingItem, servingViewModel["Entity"] );

             servingViewModel.Quantity = 42;
             Assert.IsFalse( servingViewModel.IsValid );
             Assert.IsNull( servingViewModel["Quantity"] );
             Assert.AreEqual( Messages.Error_No_ServingItem, servingViewModel["Entity"] );

             servingViewModel.Entity = foodGroupOne;
             Assert.IsTrue( servingViewModel.IsValid );
             Assert.IsNull( servingViewModel["Quantity"] );
             Assert.IsNull( servingViewModel["Entity"] );
        }
        public override void OnNavigatedTo( NavigationContext navigationContext )
        {
            if (Model == null)
             {
            String idParameter = navigationContext.Parameters["ID"];
            Guid myID;
            FoodGroup foodGroup = null;

            if (idParameter != null && Guid.TryParse( idParameter, out myID ))
            {
               foodGroup = this.DataRepository.GetFoodGroup( myID );
            }
            else
            {
               foodGroup = new FoodGroup();
            }
            Model = foodGroup;

            base.OnNavigatedTo( navigationContext );
             }
        }
Example #8
0
        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 FoodGroupAddedToChildrenWhenAddedToRepository()
        {
            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 );

             FoodGroupNodeViewModel foodGroupNodeViewModel = new FoodGroupNodeViewModel( dataRepository, null );

             Int32 originalChildCount = foodGroupNodeViewModel.Children.Count;

             var newFoodGroup = new FoodGroup( Guid.NewGuid(), "New Food Group", "Some Description" );
             dataRepository.SaveItem( newFoodGroup );
             Assert.AreEqual( originalChildCount + 1, foodGroupNodeViewModel.Children.Count );

             foreach (ClickableTreeNodeViewModel node in foodGroupNodeViewModel.Children)
             {
            FoodGroup foodGroup = dataRepository.GetFoodGroup( (Guid)node.Parameter );
            Assert.IsNotNull( foodGroup );
            Assert.AreEqual( foodGroup.Name, node.Name );
             }
        }
Example #10
0
 /// <summary>
 /// Determine if a food group's name is being used by some other food group 
 /// in the repository.
 /// </summary>
 /// <param name="foodGroup">The food group</param>
 /// <returns>True if some other food group in the repository has the same name, false otherwise.</returns>
 public Boolean NameIsDuplicate( FoodGroup foodGroup )
 {
     return (_foodGroups.Find( fg => fg.Name == foodGroup.Name && fg.ID != foodGroup.ID ) != null);
 }
Example #11
0
        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"] );
        }
Example #12
0
        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>
        /// 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 ServingViewModelEntityQuantity()
 {
     FoodGroup foodGroupOne = new FoodGroup( Guid.NewGuid(), "Food Group One", "Test Food Group One" );
      ServingViewModel<FoodGroup> servingViewModel = new ServingViewModel<FoodGroup>( foodGroupOne, 43 );
      Assert.IsTrue( servingViewModel.IsValid );
      Assert.AreEqual( foodGroupOne, servingViewModel.Entity );
      Assert.AreEqual( 43, servingViewModel.Quantity );
      Assert.IsNull( servingViewModel["Quantity"] );
      Assert.IsNull( servingViewModel["Entity"] );
 }
Example #15
0
        /// <summary>
        /// Remove a food group from the repository
        /// </summary>
        /// <param name="foodGroup">The food group to delete</param>
        public void Remove( FoodGroup foodGroup )
        {
            if (foodGroup == null)
             {
            throw new ArgumentNullException( "foodGroup" );
             }

             if (Contains( foodGroup ))
             {
            // foodGroup is likely a deep copy, find by ID and remove.
            _foodGroups.Remove( _foodGroups.Find( fg => fg.ID == foodGroup.ID ) );
            if (ItemDeleted != null)
            {
               ItemDeleted( this, new RepositoryObjectEventArgs( foodGroup ) );
            }

            SaveRepository();
             }
        }
        public void ServingViewModelIsDirty()
        {
            FoodGroup foodGroupOne = new FoodGroup( Guid.NewGuid(), "FoodGroupOne", "Test Food Group #1" );
             FoodGroup foodGroupTwo = new FoodGroup( Guid.NewGuid(), "FoodGroupTwo", "Test Food Group #2" );
             FoodGroup foodGroupThree = new FoodGroup( Guid.NewGuid(), "FoodGroupThree", "Test Food Group #3" );

             // Upon creation, the view model should be clean.
             ServingViewModel<FoodGroup> servingViewModel = new ServingViewModel<FoodGroup>( foodGroupOne, 2 );
             Assert.IsFalse( servingViewModel.IsDirty );

             // Change the Entity, the view model should be dirty
             servingViewModel.Entity = foodGroupTwo;
             Assert.IsTrue( servingViewModel.IsDirty );

             // Change the Entity back to the original value, the view model should no longer be dirty
             servingViewModel.Entity = foodGroupOne;
             Assert.IsFalse( servingViewModel.IsDirty );

             // Change the Quantity, the view model should be dirty
             servingViewModel.Quantity = 3;
             Assert.IsTrue( servingViewModel.IsDirty );

             // Change the Quantity back to the original value, the view model should no lonber be dirty
             servingViewModel.Quantity = 2;
             Assert.IsFalse( servingViewModel.IsDirty );

             // Make it dirty again, then reset the cache, the flag should fo false.
             servingViewModel.Entity = foodGroupThree;
             servingViewModel.Quantity = 4.5M;
             Assert.IsTrue( servingViewModel.IsDirty );

             servingViewModel.ResetPreviousValueCache();
             Assert.IsFalse( servingViewModel.IsDirty );
        }
Example #17
0
        /// <summary>
        /// Determine if a food group is referenenced somewhere.
        /// </summary>
        /// <param name="foodGroup">The food group to look for</param>
        /// <returns>True if the food group is in use, false otherwise</returns>
        public Boolean ItemIsUsed( FoodGroup foodGroup )
        {
            foreach (FoodItem foodItem in _foodItems)
             {
            if (foodItem.FoodGroupsPerServing.Find( fg => fg.Entity.ID == foodGroup.ID ) != null)
            {
               return true;
            }
             }

             return false;
        }
        public void UsedFoodGroupIsUsed()
        {
            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 foodGroup = new FoodGroup( Guid.NewGuid(), "Test", "" );

             dataRepositoryMock.Setup( x => x.Contains( foodGroup ) ).Returns( true );
             dataRepositoryMock.Setup( x => x.GetFoodGroup( foodGroup.ID ) ).Returns( foodGroup );
             dataRepositoryMock.Setup( x => x.ItemIsUsed( foodGroup ) ).Returns( true );

             var testFoodGroupViewModel =
            new FoodGroupViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );

             var navigationContext = new NavigationContext( regionNavigationServiceMock.Object,
            new Uri( "FoodGroupView?ID=" + foodGroup.ID.ToString(), UriKind.Relative ) );
             testFoodGroupViewModel.OnNavigatedTo( navigationContext );
             Assert.IsTrue( testFoodGroupViewModel.IsUsed );
        }
Example #19
0
        /// <summary>
        /// Add a food group to the repository.
        /// </summary>
        /// <param name="foodGroup">The food group to add</param>
        public void SaveItem( FoodGroup foodGroup )
        {
            if (foodGroup == null)
             {
            throw new ArgumentNullException( "foodGroup" );
             }

             if (!Contains( foodGroup ))
             {
            _foodGroups.Add( new FoodGroup( foodGroup ) );
            if (ItemAdded != null)
            {
               ItemAdded( this, new RepositoryObjectEventArgs( foodGroup ) );
            }
             }
             else
             {
            FoodGroup repositoryFoodGroup = _foodGroups.Find( item => item.ID == foodGroup.ID );
            Debug.Assert( repositoryFoodGroup != null );
            repositoryFoodGroup.InitializeData( foodGroup );
            if (ItemModified != null)
            {
               ItemModified( this, new RepositoryObjectEventArgs( foodGroup ) );
            }
             }

             this.SaveRepository();
        }
        public void SaveFoodGroup()
        {
            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 );

             // Create one new food group, and get a deep copy of one out of the repository.
             // When the new one is saved, a food group 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 group should not be created, but the food group object with the same
             // ID should have the modifications applied to it.

             // TODO: This needs to change
             FoodGroup stuffing = new FoodGroup( new Guid( "5ff4f935-00b9-482b-a86a-3ffd53de9dda" ), "Stuffing", "The stuff that goes in a turkey" );
             FoodGroup repositoryMeat = dataRepository.FindFoodGroup( fg => fg.ID == FullTestData.MeatID );
             FoodGroup meat = new FoodGroup();
             meat.InitializeData( repositoryMeat );

             Assert.IsNotNull( meat );
             Assert.AreEqual( meat.ID, FullTestData.MeatID );
             Assert.AreEqual( meat.Name, "Meat" );
             Assert.AreEqual( meat.Description, "Lean meat is generally the best." );

             // Saving the one that already exists should result in the existing item being changed.
             Int32 origFoodGroupCount = dataRepository.GetAllFoodGroups().Count;
             meat.Name = "Meat and Poultry";
             meat.Description = "Eat more chicken.";
             repositoryMeat = dataRepository.FindFoodGroup( fg => fg.ID == FullTestData.MeatID );
             Assert.AreNotEqual( meat.Name, repositoryMeat.Name );
             Assert.AreNotEqual( meat.Description, repositoryMeat.Description );
             dataRepository.SaveItem( meat );
             repositoryMeat = dataRepository.FindFoodGroup( fg => fg.ID == FullTestData.MeatID );
             Assert.AreEqual( origFoodGroupCount, dataRepository.GetAllFoodGroups().Count );
             Assert.AreEqual( meat.Name, repositoryMeat.Name );
             Assert.AreEqual( meat.Description, repositoryMeat.Description );

             // Saving the one that does not exist, should result in the new item being added.
             dataRepository.SaveItem( stuffing );
             Assert.AreEqual( origFoodGroupCount + 1, dataRepository.GetAllFoodGroups().Count );
             FoodGroup repositoryStuffing = dataRepository.FindFoodGroup( fg => fg.ID == new Guid( "5ff4f935-00b9-482b-a86a-3ffd53de9dda" ) );
             Assert.AreEqual( stuffing.Name, repositoryStuffing.Name );
             Assert.AreEqual( stuffing.Description, repositoryStuffing.Description );
        }
Example #21
0
 public FoodGroup( FoodGroup item )
     : this(item.ID, item.Name, item.Description)
 {
 }
Example #22
0
        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 void ServingViewModelServing()
        {
            FoodGroup foodGroupOne = new FoodGroup( Guid.NewGuid(), "FoodGroupOne", "Test Food Group #1" );
             Serving<FoodGroup> serving = new Serving<FoodGroup>( foodGroupOne, 2.5M );
             ServingViewModel<FoodGroup> servingViewModel = new ServingViewModel<FoodGroup>( serving );

             Assert.IsTrue( servingViewModel.IsValid );
             Assert.AreEqual( foodGroupOne, servingViewModel.Entity );
             Assert.AreEqual( 2.5M, servingViewModel.Quantity );
             Assert.IsNull( servingViewModel["Quantity"] );
             Assert.IsNull( servingViewModel["Entity"] );
        }
        public void CannotDeleteFoodGroupIfUsed()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var dataRepositoryMock = new Mock<IDataRepository>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var foodGroup = new FoodGroup( Guid.NewGuid(), "Test Me", "This is a test" );
             dataRepositoryMock.Setup( x => x.Contains( foodGroup ) ).Returns( true );
             dataRepositoryMock.Setup( x => x.GetFoodGroup( foodGroup.ID ) ).Returns( foodGroup );
             dataRepositoryMock.Setup( x => x.ItemIsUsed( foodGroup ) ).Returns( true );

             var viewModel =
             new FoodGroupViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             var navigationContext =
            new NavigationContext( regionNavigationServiceMock.Object, new Uri( "FoodGroupView?ID=" + foodGroup.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<UserControl>() ), Times.Never() );
        }
        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 SaveNotCalledForNonChangedFoodGroup()
        {
            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 foodGroup = new FoodGroup( Guid.NewGuid(), "FoodGroup", "" );

             dataRepositoryMock.Setup( x => x.Contains( foodGroup ) ).Returns( true );
             dataRepositoryMock.Setup( x => x.GetFoodGroup( foodGroup.ID ) ).Returns( foodGroup );

             var foodGroupViewModel =
            new FoodGroupViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             var navigationContext = new NavigationContext( regionNavigationServiceMock.Object,
            new Uri( "FoodGroupView?ID=" + foodGroup.ID.ToString(), UriKind.Relative ) );
             foodGroupViewModel.OnNavigatedTo( navigationContext );

             Assert.IsTrue( foodGroupViewModel.IsValid );
             Assert.IsFalse( foodGroupViewModel.IsDirty );
             Assert.IsFalse( foodGroupViewModel.IsNew );

             foodGroupViewModel.SaveCommand.Execute( null );

             dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<FoodGroup>() ), Times.Never() );
        }
Example #27
0
 /// <summary>
 /// Determine if the repository contains a food group with the same ID.
 /// </summary>
 /// <param name="foodGroup">The food group</param>
 /// <returns>True of the food group is in the repository, false otherwise</returns>
 public Boolean Contains( FoodGroup foodGroup )
 {
     return (_foodGroups.Find( item => item.ID == foodGroup.ID ) != null);
 }