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 ) ); }
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 ); }
public void FindFoodGroup() { 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 FoodGroup item = dataRepository.FindFoodGroup( fg => fg.Name == "Does Not Exist" ); Assert.IsNull( item ); // Find a food group that does exist, show that a deep copy is returned item = dataRepository.FindFoodGroup( fg => fg.Name == "Meat" ); Assert.AreEqual( "Meat", item.Name ); FoodGroup sameItem = dataRepository.FindFoodGroup( fg => fg.Name == "Meat" ); 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 = "Chicken"; Assert.AreEqual( "Chicken", item.Name ); Assert.AreEqual( "Meat", sameItem.Name ); }
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 ); }