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 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 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 ); }