public void FindMeal() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new HealthTracker.DataRepository.Services.DataRepository( configurationMock.Object ); // Try to find a currentMealTemplate that does not exist, should be null var item = dataRepository.FindMeal( x => x.Name == "Does Not Exist" ); Assert.IsNull( item ); // Find a currentMealTemplate that does exist, show that a deep copy is returned item = dataRepository.FindMeal( x => x.Name == "Breakfast #4" ); Assert.AreEqual( "Breakfast #4", item.Name ); var sameItem = dataRepository.FindMeal( x => x.Name == "Breakfast #4" ); Assert.AreEqual( item.ID, sameItem.ID ); Assert.AreNotEqual( item, sameItem ); // Show deep copy // Verify that changes to the first found currentMealTemplate do not affect changes to what is logically // the same currentMealTemplate returned by the second find (IOW, show they are deep copies). item.Name = "tutti-frutti"; Assert.AreEqual( "tutti-frutti", item.Name ); Assert.AreEqual( "Breakfast #4", sameItem.Name ); }
public void DeleteMeal() { var configurationMock = new Mock<IConfiguration>(); configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile ); configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName ); FullTestData.Reset(); var dataRepository = new HealthTracker.DataRepository.Services.DataRepository( configurationMock.Object ); // Make a deep copy of a meal var chzBurgerMealID = new Guid( "FEB4A1B2-796F-11E0-BB69-31B74724019B" ); var chzBurgerMeal = new Meal(); chzBurgerMeal.InitializeData( dataRepository.FindMeal( m => m.ID == chzBurgerMealID ) ); Assert.IsNotNull( chzBurgerMeal ); // Delete the meal. Show that the meal 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( chzBurgerMeal ); Assert.AreEqual( origFoodGroupCount, dataRepository.GetAllFoodGroups().Count ); Assert.AreEqual( origFoodItemCount, dataRepository.GetAllFoodItems().Count ); Assert.AreEqual( origMealTypeCount, dataRepository.GetAllMealTypes().Count ); Assert.AreEqual( origMealTemplateCount, dataRepository.GetAllMealTemplates().Count ); Assert.AreEqual( origMealCount - 1, dataRepository.GetAllMeals().Count ); Assert.IsNotNull( chzBurgerMeal.ID ); Assert.IsNull( dataRepository.FindMealTemplate( mt => mt.ID == chzBurgerMeal.ID ) ); }
public void SaveMeal() { 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. 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, and get a deep copy of one out of the repository. // When the new one is saved, a meal 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 time a new meal should not be created, but the meal object with the same // ID should have the modifications applied to it. Guid breakfastMealID = new Guid( "12ed06f0-2251-44c3-bced-b656e6fbe558" ); Guid lunchMealID = new Guid( "53edb94e-3b15-497e-9907-34de41c9bc8d" ); Meal fruitBreakfast = new Meal( breakfastMealID, 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 ); Meal repositoryChzBurgerLunch = dataRepository.FindMeal( m => m.ID == lunchMealID ); Assert.IsNotNull( repositoryChzBurgerLunch ); Meal chzBurgerLunch = new Meal(); chzBurgerLunch.InitializeData( repositoryChzBurgerLunch ); Int32 origMealCount = dataRepository.GetAllMeals().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.FindMeal( m => m.ID == lunchMealID ); 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.FindMeal( m => m.ID == lunchMealID ); 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( origMealCount, dataRepository.GetAllMeals().Count ); // Save the breakfast dataRepository.SaveItem( fruitBreakfast ); Assert.AreEqual( origMealCount + 1, dataRepository.GetAllMeals().Count ); var repositoryFruitBreakfast = dataRepository.FindMeal( f => f.ID == breakfastMealID ); 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 ); }