예제 #1
0
        public void TimeMatchesParameterWithFullConstructor()
        {
            var targetTime = DateTime.Now.AddHours( 1.0 );
             var mealType = new MealType( Guid.NewGuid(), "New Meal Type", "This is a description for the meal type", targetTime, true );

             Assert.AreEqual( targetTime, mealType.DefaultTimeOfMeal );
        }
예제 #2
0
        public void UseDefaultTimeMatchesParameterWithFullConstructor()
        {
            var mealType = new MealType( Guid.NewGuid(), "Test", "Test Description", DateTime.Now, false );
             Assert.IsFalse( mealType.UseDefaultMealTime );

             mealType = new MealType( Guid.NewGuid(), "Test", "Test Description", DateTime.Now, true );
             Assert.IsTrue( mealType.UseDefaultMealTime );
        }
예제 #3
0
        public void TimeDefaultsToNowWithDefaultConstructor()
        {
            var currentTime = DateTime.Now;
             var mealType = new MealType();

             Assert.IsTrue( currentTime <= mealType.DefaultTimeOfMeal );
             Assert.IsTrue( currentTime.AddSeconds( 1.0 ) >= mealType.DefaultTimeOfMeal );
        }
예제 #4
0
        public void TimeCopiedFromSourceWithCopyConstructor()
        {
            var targetTime = DateTime.Now.AddHours( 1.0 );
             var sourceMealType = new MealType( Guid.NewGuid(), "New Meal Type", "This is a description for the meal type", targetTime, true );
             var targetMealType = new MealType( sourceMealType );

             Assert.AreEqual( targetTime, targetMealType.DefaultTimeOfMeal );
        }
예제 #5
0
        /// <summary>
        /// Create a new meal using the specified ID, type, and time.  Use this constructor when the ID of the 
        /// meal is already known, such as when loading the meal from a file or database.  The FoodItemServings
        /// collection will be instantiated, but needs to be populated seperately.
        /// </summary>
        /// <param name="id">The ID for the meal</param>
        /// <param name="typeOfMeal">The type of meal</param>
        /// <param name="timeOfMeal">The time the meal was eaten</param>
        public MealBase( Guid id, MealType typeOfMeal, DateTime timeOfMeal, String name, String description )
            : base(id, name, description)
        {
            TypeOfMeal = typeOfMeal;
             DateAndTimeOfMeal = timeOfMeal;

             FoodItemServings = new List<Serving<FoodItem>>();
        }
예제 #6
0
        public void UseDefaultTimeCopiedFromSourceWithCopyConstructor()
        {
            var mealType = new MealType( Guid.NewGuid(), "Test", "Test Description", DateTime.Now, false );
             var targetMealType = new MealType( mealType );
             Assert.IsFalse( targetMealType.UseDefaultMealTime );

             mealType = new MealType( Guid.NewGuid(), "Test", "Test Description", DateTime.Now, true );
             targetMealType = new MealType( mealType );
             Assert.IsTrue( targetMealType.UseDefaultMealTime );
        }
        public void MealTypeAddedToViewModelWhenAddedToRepository()
        {
            var data = new MockData();
             var dataRespositoryMock = CreateMockDataRepository( data.MealTypes() );

             var viewModel = new AllMealTypesViewModel( dataRespositoryMock.Object );
             var mealType = new MealType( Guid.NewGuid(), "New Type", "For a unit test", DateTime.Now, false );
             data.MealTypes().Add( mealType );
             dataRespositoryMock.Raise( e => e.ItemAdded += null, new RepositoryObjectEventArgs( mealType ) );

             AssertViewModelContents( viewModel, data.MealTypes() );
        }
예제 #8
0
        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 ChangingMealTypeDescriptionMarksDescriptionIsDirtyChanged()
        {
            var mealType = new MealType( Guid.NewGuid(), "Peanut", "Butter Jelly Time", DateTime.Now, false );
             var viewModel = CreateViewModelForMealType( mealType );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             viewModel.Description = "with a baseball bat";

             Assert.AreEqual( 2, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Description" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
        }
        public void CannotDeleteMealTypeIfUsed()
        {
            var dataRespositoryMock = new Mock<IDataRepository>();
             var mealType = new MealType( Guid.NewGuid(), "This is a test", "This is a test meal type", DateTime.Now, false );
             var viewModel = CreateViewModelForMealType( mealType, dataRespositoryMock );

             dataRespositoryMock.Setup( x => x.Contains( mealType ) ).Returns( true );
             dataRespositoryMock.Setup( x => x.ItemIsUsed( mealType ) ).Returns( true );

             Assert.IsFalse( viewModel.IsNew );
             Assert.IsFalse( viewModel.DeleteCommand.CanExecute( null ) );
             dataRespositoryMock.VerifyAll();
        }
예제 #11
0
        public void IntializeDataSetsAllData()
        {
            var sourceMealType = new MealType( Guid.NewGuid(), "Test", "Test Description", DateTime.Today.AddHours( 14.25 ), false );
             var targetMealType = new MealType();

             targetMealType.InitializeData( sourceMealType );

             Assert.AreEqual( sourceMealType.ID, targetMealType.ID );
             Assert.AreEqual( sourceMealType.Name, targetMealType.Name );
             Assert.AreEqual( sourceMealType.Description, targetMealType.Description );
             Assert.AreEqual( sourceMealType.DefaultTimeOfMeal, targetMealType.DefaultTimeOfMeal );
             Assert.AreEqual( sourceMealType.UseDefaultMealTime, targetMealType.UseDefaultMealTime );
        }
        public void ChangingMealTypeNameMarksNameIsDirtyIsValidChanged()
        {
            var mealType = new MealType( Guid.NewGuid(), "Peanut", "Butter Jelly Time", DateTime.Now, false );
             var viewModel = CreateViewModelForMealType( mealType );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             viewModel.Name = "Jelly";

             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 ServingViewModelEditting()
        {
            MealType pi = new MealType( Guid.NewGuid(), "pi", "not the tasty kind", DateTime.Now, false );
             MealType unity = new MealType( Guid.NewGuid(), "unity", "it is itself", DateTime.Now, false );
             MealType golden = new MealType( Guid.NewGuid(), "golden", "it is very nice", DateTime.Now, false );

             ServingViewModel<MealType> servingViewModel = new ServingViewModel<MealType>( pi, 3.14159M );

             // Cancel Test.  Begin the edit, change the data, then cancel.
             servingViewModel.BeginEdit();
             servingViewModel.Entity = unity;
             servingViewModel.Quantity = 1;
             Assert.AreEqual( unity, servingViewModel.Entity );
             Assert.AreEqual( 1, servingViewModel.Quantity );
             servingViewModel.CancelEdit();
             Assert.AreEqual( pi, servingViewModel.Entity );
             Assert.AreEqual( 3.14159M, servingViewModel.Quantity );

             // In Transaction Test.  Begin the edit, change the data, begin again, cancel.  Data should still match original.
             servingViewModel.BeginEdit();
             servingViewModel.Entity = unity;
             servingViewModel.Quantity = 1;
             Assert.AreEqual( unity, servingViewModel.Entity );
             Assert.AreEqual( 1, servingViewModel.Quantity );
             servingViewModel.BeginEdit();
             servingViewModel.Entity = golden;
             servingViewModel.Quantity = 1.61803M;
             Assert.AreEqual( golden, servingViewModel.Entity );
             Assert.AreEqual( 1.61803M, servingViewModel.Quantity );
             servingViewModel.CancelEdit();
             Assert.AreEqual( pi, servingViewModel.Entity );
             Assert.AreEqual( 3.14159M, servingViewModel.Quantity );

             // End Edit Test.  Begin the edit, change the data, end the edit, change should stick
             servingViewModel.BeginEdit();
             servingViewModel.Entity = golden;
             servingViewModel.Quantity = 1.61803M;
             Assert.AreEqual( golden, servingViewModel.Entity );
             Assert.AreEqual( 1.61803M, servingViewModel.Quantity );
             servingViewModel.EndEdit();
             Assert.AreEqual( golden, servingViewModel.Entity );
             Assert.AreEqual( 1.61803M, servingViewModel.Quantity );
        }
        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 MealTypeIsDeletedIfAnswerIsYes()
        {
            var dataRepositoryMock = new Mock<IDataRepository>();
             var regionManagerMock = new Mock<IRegionManager>();
             var regionMock = new Mock<IRegion>();
             var interactionServiceMock = new Mock<IInteractionService>();
             var mealType = new MealType( Guid.NewGuid(), "Test Meal Type", "This is a test meal type", DateTime.Now, false );
             var viewModel = CreateViewModelForMealType( mealType, dataRepositoryMock, regionManagerMock, interactionServiceMock );
             var view = CreateViewInRegion( viewModel, regionMock, regionManagerMock );

             dataRepositoryMock.Setup( x => x.Contains( mealType ) ).Returns( true );
             dataRepositoryMock.Setup( x => x.ItemIsUsed( mealType ) ).Returns( false );
             dataRepositoryMock.Setup( x => x.Remove( mealType ) );

             interactionServiceMock
            .Setup( x => x.ShowMessageBox( Messages.Question_MealType_Delete, DisplayStrings.DeleteCaption, MessageBoxButton.YesNo, MessageBoxImage.Question ) )
            .Returns( MessageBoxResult.Yes );

             Assert.IsTrue( viewModel.DeleteCommand.CanExecute( null ) );
             viewModel.DeleteCommand.Execute( null );

             dataRepositoryMock.VerifyAll();
             dataRepositoryMock.Verify( x => x.Remove( mealType ) );
             interactionServiceMock.VerifyAll();
             regionMock.Verify( x => x.Remove( view ), Times.Exactly( 1 ) );
             regionMock.Verify( x => x.Remove( It.IsAny<Object>() ), Times.Exactly( 1 ) );
        }
        private MealTypeViewModel CreateViewModelForMealType(
         MealType mealType, Mock<IDataRepository> dataRepositoryMock, Mock<IRegionManager> regionManagerMock, Mock<IInteractionService> interactionServiceMock )
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();

             var viewModel =
            new MealTypeViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );

             dataRepositoryMock.Setup( x => x.GetMealType( mealType.ID ) ).Returns( mealType );

             var navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "MealTypeView?ID=" + mealType.ID.ToString(), UriKind.Relative ) );
             viewModel.OnNavigatedTo( navigationContext );

             return viewModel;
        }
예제 #17
0
        public override void InitializeData( RepositoryObjectBase source )
        {
            // If Initializing something to itself, just bail.
             if (this == source)
             {
            return;
             }

             base.InitializeData( source );

             if (source is MealBase)
             {
            MealBase meal = source as MealBase;
            this.TypeOfMeal = meal.TypeOfMeal;
            this.DateAndTimeOfMeal = meal.DateAndTimeOfMeal;

            this.FoodItemServings.Clear();
            foreach (Serving<FoodItem> foodItemServing in meal.FoodItemServings)
            {
               this.FoodItemServings.Add( new Serving<FoodItem>( foodItemServing ) );
            }
             }
        }
        private MealTypeViewModel CreateViewModelForMealType( MealType mealType, Mock<IDataRepository> dataRepositoryMock )
        {
            var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             return CreateViewModelForMealType( mealType, dataRepositoryMock, regionManagerMock, interactionServiceMock );
        }
예제 #19
0
        public override void OnNavigatedTo( NavigationContext navigationContext )
        {
            if (Model == null)
             {
            String idParameter = navigationContext.Parameters["ID"];
            Guid myID;
            MealType mealType = null;

            if (idParameter != null && Guid.TryParse( idParameter, out myID ))
            {
               mealType = this.DataRepository.GetMealType( myID );
            }
            else
            {
               mealType = new MealType();
            }
            Model = mealType;

            base.OnNavigatedTo( navigationContext );
             }
        }
 private MealTypeViewModel CreateViewModelForMealType( MealType mealType )
 {
     var dataRepositoryMock = new Mock<IDataRepository>();
      return CreateViewModelForMealType( mealType, dataRepositoryMock );
 }
예제 #21
0
 public MealTemplate( Guid id, MealType mealType, DateTime timeOfMeal, String name, String description )
     : base(id, mealType, timeOfMeal, name, description)
 {
 }
예제 #22
0
 private void SetCacheValues()
 {
     _previousFoodItemCount = FoodItemServings.Count;
      _previousTypeOfMeal = TypeOfMeal;
      _previousTimeOfMeal = TimeOfMeal;
 }
        public void ServingViewModelPropertyChanged()
        {
            MealType pi = new MealType( Guid.NewGuid(), "pi", "not the tasty kind", DateTime.Now, false );
             MealType e = new MealType( Guid.NewGuid(), "e", "exponent", DateTime.Now, false );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             Serving<MealType> serving = new Serving<MealType>( pi, 3.14159M );
             ServingViewModel<MealType> servingViewModel = new ServingViewModel<MealType>( serving );
             servingViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;

             Assert.AreEqual( pi, servingViewModel.Entity );
             Assert.AreEqual( 3.14159M, servingViewModel.Quantity );
             Assert.IsNull( propertyChangedHandler.Sender );
             Assert.AreEqual( 0, propertyChangedHandler.PropertiesChanged.Count );

             servingViewModel.Entity = e;
             Assert.AreEqual( servingViewModel, propertyChangedHandler.Sender );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Entity" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( e, serving.Entity );
             Assert.AreEqual( 3.14159M, serving.Quantity );

             propertyChangedHandler.Reset();
             servingViewModel.Quantity = 2.71828183M;
             Assert.AreEqual( servingViewModel, propertyChangedHandler.Sender );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Quantity" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( e, serving.Entity );
             Assert.AreEqual( 2.71828183M, serving.Quantity );
        }
예제 #24
0
        public void TimeOfMealForExistingMealResetBasedOnMealTypeIfTimeNeverManuallySet()
        {
            double mealHour = 12.5;
             var mealType = new MealType( Guid.NewGuid(), "test", "test", DateTime.Today.AddDays( -42 ).AddHours( mealHour ), true );
             var meal = new Meal( Guid.NewGuid(), mealType, DateTime.Today.AddHours( mealHour ), "test", "test" );
             var viewModel = CreateViewModelForMeal( new Meal( meal ) );

             viewModel.TypeOfMeal = new MealType( Guid.NewGuid(), "Another Test", "d00d", DateTime.Today.AddDays( -345 ).AddHours( mealHour + 2.0 ), true );

             Assert.AreEqual( DateTime.Today.AddHours( mealHour + 2.0 ), viewModel.TimeOfMeal );
        }
        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 );
             }
        }
예제 #26
0
        /// <summary>
        /// Close test meal.  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 testFoodItem = new FoodItem( Guid.NewGuid(), "Test Food Item", "Test Food Item Description", 42.0M );
             testFoodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( testFoodGroup, 5.0M ) );
             Assert.IsTrue( testFoodItem.IsValid );
             List<FoodItem> foodItems = new List<FoodItem>();
             foodItems.Add( testFoodItem );
             dataRepositoryMock.Setup( x => x.GetAllFoodItems() ).Returns( new ReadOnlyCollection<FoodItem>( foodItems ) );
             MealType testMealType = new MealType( Guid.NewGuid(), "Lunch", "A lunch for testing", DateTime.Now, false );
             Assert.IsTrue( testMealType.IsValid );
             List<MealType> mealTypes = new List<MealType>();
             mealTypes.Add( testMealType );
             dataRepositoryMock.Setup( x => x.GetAllMealTypes() ).Returns( new ReadOnlyCollection<MealType>( mealTypes ) );

             var meal = new Meal( Guid.NewGuid(), testMealType, DateTime.Now, "test meal", "This is a test" );
             meal.FoodItemServings.Add( new Serving<FoodItem>( testFoodItem, 1.5M ) );

             dataRepositoryMock.Setup( x => x.GetMeal( meal.ID ) ).Returns( meal );
             dataRepositoryMock.Setup( x => x.Contains( meal ) ).Returns( true );

             // Create the view model under test and associate it with a view
             var viewModel = new MealViewModel( 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 foodGroup.  This loads the view model
             NavigationContext navigationContext =
            new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealView?ID=" + meal.ID.ToString(), UriKind.Relative ) );
             viewModel.OnNavigatedTo( navigationContext );

             if (makeDirty)
             {
            if (makeInvalid)
            {
               interactionServiceMock
                  .Setup( x => x.ShowMessageBox( Messages.Question_Meal_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_Meal_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( meal ), Times.Exactly( 1 ) );
            dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<Meal>() ), Times.Exactly( 1 ) );
             }
             else
             {
            dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<Meal>() ), Times.Never() );
             }
        }
예제 #27
0
        public void DeleteMealType()
        {
            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 currentMealTemplate type
             MealType lunch = new MealType();
             lunch.InitializeData( dataRepository.FindMealType( mt => mt.Name == "Lunch" ) );
             Assert.IsNotNull( lunch );

             // Delete the meal type.  Show that the meal type 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( lunch );
             Assert.AreEqual( origFoodGroupCount, dataRepository.GetAllFoodGroups().Count );
             Assert.AreEqual( origFoodItemCount, dataRepository.GetAllFoodItems().Count );
             Assert.AreEqual( origMealTypeCount - 1, dataRepository.GetAllMealTypes().Count );
             Assert.AreEqual( origMealTemplateCount, dataRepository.GetAllMealTemplates().Count );
             Assert.AreEqual( origMealCount, dataRepository.GetAllMeals().Count );
             Assert.IsNotNull( lunch.ID );
             Assert.IsNull( dataRepository.FindMealTemplate( mt => mt.ID == lunch.ID ) );
        }
예제 #28
0
        public void SaveMealType()
        {
            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 );

             MealType forthMeal = new MealType( new Guid( "18bbf0b4-9f2e-4477-97c9-d2b9419fe57f" ), "Forth Meal", "Going to Taco Bell", DateTime.Now, false );

             MealType repositoryBreakfast = dataRepository.FindMealType( m => m.ID == FullTestData.BreakfastID );
             MealType breakfast = new MealType();
             breakfast.InitializeData( repositoryBreakfast );

             Int32 origMealTypeCount = dataRepository.GetAllMealTypes().Count;

             // Verfiy the breakfasts are the same
             Assert.AreEqual( breakfast.ID, repositoryBreakfast.ID );
             Assert.AreEqual( breakfast.Name, repositoryBreakfast.Name );
             Assert.AreEqual( breakfast.Description, repositoryBreakfast.Description );
             Assert.AreEqual( breakfast.DefaultTimeOfMeal, repositoryBreakfast.DefaultTimeOfMeal );
             Assert.AreEqual( breakfast.UseDefaultMealTime, repositoryBreakfast.UseDefaultMealTime );

             // Change the breakfast, verify the repository has not changed
             breakfast.Name = "Big Breakfast";
             breakfast.Description = "Bacon, Eggs, Pancakes, stuff like that";
             breakfast.DefaultTimeOfMeal = DateTime.Now.AddHours( 2.5 );
             breakfast.UseDefaultMealTime = false;

             repositoryBreakfast = dataRepository.FindMealType( m => m.ID == FullTestData.BreakfastID );
             Assert.AreEqual( breakfast.ID, repositoryBreakfast.ID );
             Assert.AreNotEqual( breakfast.Name, repositoryBreakfast.Name );
             Assert.AreNotEqual( breakfast.Description, repositoryBreakfast.Description );
             Assert.AreNotEqual( breakfast.DefaultTimeOfMeal, repositoryBreakfast.DefaultTimeOfMeal );
             Assert.AreNotEqual( breakfast.UseDefaultMealTime, repositoryBreakfast.UseDefaultMealTime );

             Assert.AreEqual( origMealTypeCount, dataRepository.GetAllMealTypes().Count );

             // Save the change, verify the repository item has been changed, and no new currentMealTemplate types have been added
             dataRepository.SaveItem( breakfast );
             repositoryBreakfast = dataRepository.FindMealType( m => m.ID == FullTestData.BreakfastID );
             Assert.AreEqual( breakfast.ID, repositoryBreakfast.ID );
             Assert.AreEqual( breakfast.Name, repositoryBreakfast.Name );
             Assert.AreEqual( breakfast.Description, repositoryBreakfast.Description );
             Assert.AreEqual( breakfast.DefaultTimeOfMeal, repositoryBreakfast.DefaultTimeOfMeal );
             Assert.AreEqual( breakfast.UseDefaultMealTime, repositoryBreakfast.UseDefaultMealTime );

             Assert.AreEqual( origMealTypeCount, dataRepository.GetAllMealTypes().Count );

             // Save the new Meal type
             dataRepository.SaveItem( forthMeal );
             MealType repositoryForthMeal = dataRepository.FindMealType( m => m.ID == forthMeal.ID );
             Assert.IsNotNull( repositoryForthMeal );
             Assert.AreNotEqual( forthMeal, repositoryForthMeal );
             Assert.AreEqual( forthMeal.ID, repositoryForthMeal.ID );
             Assert.AreEqual( forthMeal.Name, repositoryForthMeal.Name );
             Assert.AreEqual( forthMeal.Description, repositoryForthMeal.Description );
             Assert.AreEqual( forthMeal.DefaultTimeOfMeal, repositoryForthMeal.DefaultTimeOfMeal );
             Assert.AreEqual( forthMeal.UseDefaultMealTime, repositoryForthMeal.UseDefaultMealTime );

             Assert.AreEqual( origMealTypeCount + 1, dataRepository.GetAllMealTypes().Count );
        }
        /// <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>();

             MealTypeViewModel viewModel = new MealTypeViewModel(
            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() );

             // Setup a mealTemplate type in the mock repository, navigate to it
             MealType mealType = new MealType( Guid.NewGuid(), "Test Meal Type", "Test Meal Type Description", DateTime.Now, false );
             dataRepositoryMock.Setup( x => x.GetMealType( mealType.ID ) ).Returns( mealType );
             dataRepositoryMock.Setup( x => x.Contains( mealType ) ).Returns( true );
             NavigationContext navigationContext =
            new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealTypeView?ID=" + mealType.ID.ToString(), UriKind.Relative ) );
             viewModel.OnNavigatedTo( navigationContext );

             if (makeDirty)
             {
            if (makeInvalid)
            {
               interactionServiceMock
                  .Setup( x => x.ShowMessageBox( Messages.Question_MealType_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_MealType_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( mealType ), Times.Exactly( 1 ) );
            dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<MealType>() ), Times.Exactly( 1 ) );
             }
             else
             {
            dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<MealType>() ), Times.Never() );
             }
        }
        public void ExistingMealTypeViewModelIsNotNew()
        {
            var dataRepositoryMock = new Mock<IDataRepository>();
             var mealType = new MealType( Guid.NewGuid(), "Meal Type", "This is a test meal type", DateTime.Now, false );
             dataRepositoryMock.Setup( x => x.Contains( mealType ) ).Returns( true );
             var viewModel = CreateViewModelForMealType( mealType, dataRepositoryMock );

             Assert.IsFalse( viewModel.IsNew );
        }