예제 #1
0
        public void DeleteFoodItem()
        {
            var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             HealthTracker.DataRepository.Services.DataRepository dataRepository = new HealthTracker.DataRepository.Services.DataRepository( configurationMock.Object );

             // Make a deep copy of a food item.
             FoodItem fruitSalad = new FoodItem();
             fruitSalad.InitializeData( dataRepository.FindFoodItem( f => f.Name == "Fruit Salad" ) );

             // Delete the food item.  Show that the food item has been deleted, but none of the
             // other data repository items have not been effected.
             Int32 origFoodGroupCount = dataRepository.GetAllFoodGroups().Count;
             Int32 origFoodItemCount = dataRepository.GetAllFoodItems().Count;
             Int32 origMealTypeCount = dataRepository.GetAllMealTypes().Count;
             Int32 origMealTemplateCount = dataRepository.GetAllMealTemplates().Count;
             Int32 origMealCount = dataRepository.GetAllMeals().Count;
             dataRepository.Remove( fruitSalad );
             Assert.AreEqual( origFoodGroupCount, dataRepository.GetAllFoodGroups().Count );
             Assert.AreEqual( origFoodItemCount - 1, dataRepository.GetAllFoodItems().Count );
             Assert.AreEqual( origMealTypeCount, dataRepository.GetAllMealTypes().Count );
             Assert.AreEqual( origMealTemplateCount, dataRepository.GetAllMealTemplates().Count );
             Assert.AreEqual( origMealCount, dataRepository.GetAllMeals().Count );
             Assert.IsNotNull( fruitSalad.ID );
             Assert.IsNull( dataRepository.FindFoodItem( f => f.ID == fruitSalad.ID ) );
        }
예제 #2
0
        public void SaveMealTemplate()
        {
            var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             HealthTracker.DataRepository.Services.DataRepository dataRepository = new HealthTracker.DataRepository.Services.DataRepository( configurationMock.Object );

             // Get some food items that we can use for the new Meal Template.
             FoodItem fruitSalad = dataRepository.FindFoodItem( f => f.ID == FullTestData.FruitSaladID );
             FoodItem glassOfMilk = dataRepository.FindFoodItem( f => f.ID == FullTestData.GlassOfMilkID );
             FoodItem glassOfWater = dataRepository.FindFoodItem( f => f.ID == FullTestData.GlassOfWaterID );

             MealType breakfast = dataRepository.FindMealType( f => f.ID == FullTestData.BreakfastID );

             // Create one new Meal Template, and get a deep copy of one out of the repository.
             // When the new one is saved, a meal template 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 meal template should not be created, but the meal template object with
             // the same ID should have the modifications applied to it.
             MealTemplate fruitBreakfast = new MealTemplate( new Guid( "63a29a24-d2cb-40b1-9826-b355943817ab" ),
            breakfast, DateTime.Now, "Fruit Breakfast", "Fruit Salad and some milk" );
             fruitBreakfast.FoodItemServings.Add( new Serving<FoodItem>( fruitSalad, 1 ) );
             fruitBreakfast.FoodItemServings.Add( new Serving<FoodItem>( glassOfMilk, 1 ) );
             Assert.IsTrue( fruitBreakfast.IsValid );

             MealTemplate repositoryChzBurgerLunch = dataRepository.FindMealTemplate( m => m.ID == FullTestData.CheeseBurgerLunchID );
             Assert.IsNotNull( repositoryChzBurgerLunch );
             MealTemplate chzBurgerLunch = new MealTemplate();
             chzBurgerLunch.InitializeData( repositoryChzBurgerLunch );
             Int32 origMealTemplateCount = dataRepository.GetAllMealTemplates().Count;

             // Verifiy the copies are the same
             Assert.AreEqual( repositoryChzBurgerLunch.ID, chzBurgerLunch.ID );
             Assert.AreEqual( repositoryChzBurgerLunch.Name, chzBurgerLunch.Name );
             Assert.AreEqual( repositoryChzBurgerLunch.Description, chzBurgerLunch.Description );
             Assert.AreEqual( repositoryChzBurgerLunch.Calories, chzBurgerLunch.Calories );
             Assert.AreEqual( repositoryChzBurgerLunch.FoodItemServings.Count, chzBurgerLunch.FoodItemServings.Count );

             // Replace the glasses of milk with a glass of water, and only have one burger
             chzBurgerLunch.FoodItemServings.Remove( chzBurgerLunch.FoodItemServings.Find( f => f.Entity.ID == FullTestData.GlassOfMilkID ) );
             chzBurgerLunch.FoodItemServings.Add( new Serving<FoodItem>( glassOfWater, 1.5M ) );
             chzBurgerLunch.FoodItemServings.Find( f => f.Entity.ID == FullTestData.CheeseBurgerID ).Quantity = 1;
             chzBurgerLunch.Description = "A typical cheese burger lunch, made a bit healthier";

             repositoryChzBurgerLunch = dataRepository.FindMealTemplate( m => m.ID == FullTestData.CheeseBurgerLunchID );
             Assert.AreEqual( repositoryChzBurgerLunch.ID, chzBurgerLunch.ID );
             Assert.AreEqual( repositoryChzBurgerLunch.Name, chzBurgerLunch.Name );
             Assert.AreNotEqual( repositoryChzBurgerLunch.Description, chzBurgerLunch.Description );
             Assert.AreNotEqual( repositoryChzBurgerLunch.Calories, chzBurgerLunch.Calories );

             // Save the changes and verify the changes were saved
             dataRepository.SaveItem( chzBurgerLunch );
             repositoryChzBurgerLunch = dataRepository.FindMealTemplate( m => m.ID == FullTestData.CheeseBurgerLunchID );
             Assert.AreEqual( chzBurgerLunch.Name, repositoryChzBurgerLunch.Name );
             Assert.AreEqual( chzBurgerLunch.Description, repositoryChzBurgerLunch.Description );
             Assert.AreEqual( chzBurgerLunch.FoodItemServings.Count, repositoryChzBurgerLunch.FoodItemServings.Count );
             Assert.AreEqual( chzBurgerLunch.Calories, repositoryChzBurgerLunch.Calories );
             Assert.AreEqual( origMealTemplateCount, dataRepository.GetAllMealTemplates().Count );

             // Save the breakfast
             dataRepository.SaveItem( fruitBreakfast );
             Assert.AreEqual( origMealTemplateCount + 1, dataRepository.GetAllMealTemplates().Count );
             MealTemplate repositoryFruitBreakfast = dataRepository.FindMealTemplate( f => f.ID == new Guid( "63a29a24-d2cb-40b1-9826-b355943817ab" ) );
             Assert.IsNotNull( repositoryFruitBreakfast );
             Assert.AreEqual( fruitBreakfast.ID, repositoryFruitBreakfast.ID );
             Assert.AreEqual( fruitBreakfast.Name, repositoryFruitBreakfast.Name );
             Assert.AreEqual( fruitBreakfast.Description, repositoryFruitBreakfast.Description );
             Assert.AreEqual( fruitBreakfast.FoodItemServings.Count, repositoryFruitBreakfast.FoodItemServings.Count );
             Assert.AreEqual( fruitBreakfast.Calories, repositoryFruitBreakfast.Calories );
        }
예제 #3
0
        public void FindFoodItem()
        {
            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 );

             // Try to find a food group that does not exist, should be null
             FoodItem item = dataRepository.FindFoodItem( x => x.Name == "Does Not Exist" );
             Assert.IsNull( item );

             // Find a food group that does exist, show that a deep copy is returned
             item = dataRepository.FindFoodItem( x => x.Name == "Fruit Salad" );
             Assert.AreEqual( "Fruit Salad", item.Name );
             FoodItem sameItem = dataRepository.FindFoodItem( x => x.Name == "Fruit Salad" );
             Assert.AreEqual( item.ID, sameItem.ID );
             Assert.AreNotEqual( item, sameItem );  // Show deep copy

             // Verify that changes to the first found food group do not affect changes to what is logically
             // the same food group returned by the second find (IOW, show they are deep copies).
             item.Name = "Sex Organ Salad";
             Assert.AreEqual( "Sex Organ Salad", item.Name );
             Assert.AreEqual( "Fruit Salad", sameItem.Name );
        }
예제 #4
0
        public void SaveFoodItem()
        {
            var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             HealthTracker.DataRepository.Services.DataRepository dataRepository = new HealthTracker.DataRepository.Services.DataRepository( configurationMock.Object );

             // Get some food groups that we can use
             FoodGroup meat = dataRepository.FindFoodGroup( f => f.ID == FullTestData.MeatID );
             FoodGroup vegetables = dataRepository.FindFoodGroup( f => f.ID == FullTestData.VegetableID );

             // Create one new food item, and get a deep copy of one out of the repository.
             // When the new one is saved, a food item should be added to the repository.
             // When the deep copy of the existing one is modified, the modifications should not be applied to the repository
             // until the copy is saved, at which a new food item should not be created, but the food item object with the same
             // ID should have the modifications applied to it.
             FoodItem dublinCoddle = new FoodItem( new Guid( "f6ffa83c-5f7a-4961-8571-801f436c0eb9" ), "Dublin Coddle", "Poormans food", 250 );
             dublinCoddle.FoodGroupsPerServing.Add( new Serving<FoodGroup>( meat, 1 ) );
             dublinCoddle.FoodGroupsPerServing.Add( new Serving<FoodGroup>( vegetables, 2 ) );

             FoodItem repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID );
             Assert.IsNotNull( repositoryChzBurger );
             FoodItem chzBurger = new FoodItem();
             chzBurger.InitializeData( repositoryChzBurger );
             Int32 origFoodItemCount = dataRepository.GetAllFoodItems().Count;

             // Verify the food groups, etc.
             Assert.AreEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing );
             Assert.AreEqual( repositoryChzBurger.Name, chzBurger.Name );
             Assert.AreEqual( repositoryChzBurger.Description, chzBurger.Description );
             Assert.AreEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count );

             // Remove the vegetables and bacon, and verify the difference
             chzBurger.Description = "Ground up cow, topped with curdled milk.";
             chzBurger.Name = "Cheese Burger";
             chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity = 1;
             chzBurger.FoodGroupsPerServing.Remove( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) );
             chzBurger.CaloriesPerServing -= 140;

             repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID );
             Assert.AreNotEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing );
             Assert.AreNotEqual( repositoryChzBurger.Name, chzBurger.Name );
             Assert.AreNotEqual( repositoryChzBurger.Description, chzBurger.Description );
             Assert.AreNotEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count );
             Assert.AreNotEqual(
            repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ),
            chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ) );
             Assert.IsNotNull( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) );
             Assert.IsNull( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) );

             // Save the cheese burger, and verify the changes are saved
             dataRepository.SaveItem( chzBurger );
             repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID );
             Assert.AreEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing );
             Assert.AreEqual( repositoryChzBurger.Name, chzBurger.Name );
             Assert.AreEqual( repositoryChzBurger.Description, chzBurger.Description );
             Assert.AreEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count );
             Assert.AreEqual(
            repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity,
            chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity );
             Assert.IsNull( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) );
             Assert.IsNull( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.VegetableID ) );

             Assert.AreEqual( origFoodItemCount, dataRepository.GetAllFoodItems().Count );

             // Add some fruit and verify the difference
             chzBurger.Name = "Hawian Cheese Burger";
             chzBurger.Description = "Ground up cow, topped with curdled milk, smoked pig, and pineapple.";
             chzBurger.FoodGroupsPerServing.Add( new Serving<FoodGroup>(
            dataRepository.FindFoodGroup( fg => fg.ID == FullTestData.FruitID ), 0.5M ) );
             chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity += 1;
             chzBurger.CaloriesPerServing += 200;

             repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID );
             Assert.AreNotEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing );
             Assert.AreNotEqual( repositoryChzBurger.Name, chzBurger.Name );
             Assert.AreNotEqual( repositoryChzBurger.Description, chzBurger.Description );
             Assert.AreNotEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count );
             Assert.AreNotEqual(
            repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ),
            chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ) );
             Assert.IsNull( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.FruitID ) );
             Assert.IsNotNull( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.FruitID ) );

             // Save the cheese burger, and verify the changes are saved
             dataRepository.SaveItem( chzBurger );
             repositoryChzBurger = dataRepository.FindFoodItem( f => f.ID == FullTestData.CheeseBurgerID );
             Assert.AreEqual( repositoryChzBurger.CaloriesPerServing, chzBurger.CaloriesPerServing );
             Assert.AreEqual( repositoryChzBurger.Name, chzBurger.Name );
             Assert.AreEqual( repositoryChzBurger.Description, chzBurger.Description );
             Assert.AreEqual( repositoryChzBurger.FoodGroupsPerServing.Count, chzBurger.FoodGroupsPerServing.Count );
             Assert.AreEqual(
            repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity,
            chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.MeatID ).Quantity );
             Assert.IsNotNull( repositoryChzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.FruitID ) );
             Assert.IsNotNull( chzBurger.FoodGroupsPerServing.Find( f => f.Entity.ID == FullTestData.FruitID ) );

             Assert.AreEqual( origFoodItemCount, dataRepository.GetAllFoodItems().Count );

             // Save the new food item
             dataRepository.SaveItem( dublinCoddle );
             Assert.AreEqual( origFoodItemCount + 1, dataRepository.GetAllFoodItems().Count );
             FoodItem repositoryCoddle = dataRepository.FindFoodItem( f => f.ID == new Guid( "f6ffa83c-5f7a-4961-8571-801f436c0eb9" ) );
             Assert.IsNotNull( repositoryCoddle );
             Assert.AreEqual( "Dublin Coddle", repositoryCoddle.Name );
             Assert.AreEqual( "Poormans food", repositoryCoddle.Description );
             Assert.AreEqual( 250, repositoryCoddle.CaloriesPerServing );
             Assert.AreEqual( 2, repositoryCoddle.FoodGroupsPerServing.Count );
        }