public async void TestGenericAdd() { //Arrange var options = new DbContextOptionsBuilder <CookingpapaContext>() .UseInMemoryDatabase(databaseName: "Test1DB") .Options; //Act RecipeOrigin testOrigin; using (var context = new CookingpapaContext(options)) { var _unitOfWork = new UnitOfWork(context); testOrigin = new RecipeOrigin { RecipeOriginName = "Test" }; await _unitOfWork.RecipeOrigins.Add(testOrigin); await _unitOfWork.Complete(); } //Assert using (var context = new CookingpapaContext(options)) { var origin = context.RecipeOrigins .Where(orig => orig.RecipeOriginName == "Test").First(); Assert.Equal(testOrigin.Id, origin.Id); } }
public async Task <Recipe> PostRecipe(PostRecipeVM recipeVM) { AddNewIngredientAndUnit(recipeVM); var recipeOrigins = await _unitOfWork.RecipeOrigins.GetAll(); var recipeOrigin = recipeOrigins.ToList().Find(x => x.RecipeOriginName == recipeVM.RecipeOriginName); if (recipeOrigin == null) { recipeOrigin = new RecipeOrigin() { RecipeOriginName = recipeVM.RecipeOriginName }; _unitOfWork.RecipeOrigins.Add(recipeOrigin); } var user = _unitOfWork.Users.Get(recipeVM.UserId).Result; await _unitOfWork.Complete(); Recipe recipe = new Recipe() { User = user, RecipeOrigin = recipeOrigin, RecipeName = recipeVM.RecipeName, RecipeCookTime = recipeVM.RecipeCookTime, RecipeInstruction = recipeVM.RecipeInstruction }; List <RecipeIngredientGroups> recipeIngredientGroups = new List <RecipeIngredientGroups>(); var ingredientId = _unitOfWork.RecipeIngredients.GetAll().Result.ToList(); var measurementId = _unitOfWork.RecipeMeasurements.GetAll().Result.ToList(); foreach (var x in recipeVM.RecipeIngredientGroupVM) { var ingredientIds = ingredientId.Find(y => y.RecipeIngredientName == x.ingredientName).Id; var measurementIds = measurementId.Find(y => y.RecipeMeasurementName == x.measurementName).Id; recipeIngredientGroups.Add(new RecipeIngredientGroups() { Recipe = recipe, RecipeIngredient = _unitOfWork.RecipeIngredients.Get(ingredientIds).Result, RecipeMeasurement = _unitOfWork.RecipeMeasurements.Get(measurementIds).Result, RecipeIngredientAmount = int.Parse(x.ingredientAmount) }); } _unitOfWork.Recipes.Add(recipe); _unitOfWork.RecipeIngredientGroups.AddRange(recipeIngredientGroups); await _unitOfWork.Complete(); //var newRecipe = _unitOfWork.Recipes.GetAll().Result.Last(); return(recipe); }
public async void TestGenericGetAll() { //Arrange var options = new DbContextOptionsBuilder <CookingpapaContext>() .UseInMemoryDatabase(databaseName: "Test4DB") .Options; //Act List <RecipeOrigin> testOrigins; using (var context = new CookingpapaContext(options)) { var _unitOfWork = new UnitOfWork(context); var testOrigin = new RecipeOrigin { RecipeOriginName = "Test1" }; var testOrigin2 = new RecipeOrigin { RecipeOriginName = "Test2" }; await _unitOfWork.RecipeOrigins.Add(testOrigin); await _unitOfWork.RecipeOrigins.Add(testOrigin2); await _unitOfWork.Complete(); var tempTestOrigins = await _unitOfWork.RecipeOrigins.GetAll(); testOrigins = tempTestOrigins.ToList(); } //Assert using (var context = new CookingpapaContext(options)) { Assert.Equal("Test1", testOrigins[0].RecipeOriginName); Assert.Equal("Test2", testOrigins[1].RecipeOriginName); } }
public async void TestRIGGetByRecipeEager() { //Arrange var options = new DbContextOptionsBuilder <CookingpapaContext>() .UseInMemoryDatabase(databaseName: "Test18DB") .Options; //Act List <RecipeIngredientGroups> testResultsRecipeIngredientGroups; using (var context = new CookingpapaContext(options)) { var _unitOfWork = new UnitOfWork(context); var testUser = new User { Username = "******", Password = "******", Email = "*****@*****.**" }; var testOrigin = new RecipeOrigin { RecipeOriginName = "America" }; var testRecipe = new Recipe { RecipeCookTime = 5, User = testUser, RecipeOrigin = testOrigin }; var testIngredient = new RecipeIngredient { RecipeIngredientName = "TestIngredient" }; var testMeasurement = new RecipeMeasurement { RecipeMeasurementName = "TestMeasurement" }; var testRIG = new RecipeIngredientGroups { Recipe = testRecipe, RecipeIngredient = testIngredient, RecipeMeasurement = testMeasurement, RecipeIngredientAmount = 5 }; var testRIG2 = new RecipeIngredientGroups { Recipe = testRecipe, RecipeIngredient = testIngredient, RecipeMeasurement = testMeasurement, RecipeIngredientAmount = 5 }; await _unitOfWork.RecipeIngredientGroups.Add(testRIG); await _unitOfWork.RecipeIngredientGroups.Add(testRIG2); await _unitOfWork.Complete(); var tempTestRecipeIngredientGroups = await _unitOfWork.RecipeIngredientGroups.GetByRecipeEager(1); testResultsRecipeIngredientGroups = tempTestRecipeIngredientGroups.ToList(); } //Assert using (var context = new CookingpapaContext(options)) { Assert.Equal(1, testResultsRecipeIngredientGroups[0].Recipe.Id); Assert.Equal(5, testResultsRecipeIngredientGroups[0].Recipe.RecipeCookTime); Assert.Equal(1, testResultsRecipeIngredientGroups[1].Recipe.Id); Assert.Equal(5, testResultsRecipeIngredientGroups[1].Recipe.RecipeCookTime); } }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new CookingpapaContext( serviceProvider.GetRequiredService < DbContextOptions <CookingpapaContext> >())) { // look for any product/store in the DB if (context.User.Any()) { return; // DB already has something } #region Add Users var user1 = new User { Email = "*****@*****.**", Username = "******", Password = "******" }; var user2 = new User { Email = "*****@*****.**", Username = "******", Password = "******" }; context.User.AddRange( user1, user2 ); #endregion #region Add Ingredients var salt = new RecipeIngredient { RecipeIngredientName = "Salt" }; var butter = new RecipeIngredient { RecipeIngredientName = "Butter" }; var pepper = new RecipeIngredient { RecipeIngredientName = "Pepper" }; var chicken = new RecipeIngredient { RecipeIngredientName = "Chicken" }; var beer = new RecipeIngredient { RecipeIngredientName = "Beer" }; var water = new RecipeIngredient { RecipeIngredientName = "Water" }; context.AddRange( salt, butter, pepper, chicken, beer, water ); #endregion #region Add Measurements var cups = new RecipeMeasurement { RecipeMeasurementName = "Cups" }; var tblsp = new RecipeMeasurement { RecipeMeasurementName = "Tablespoons" }; var tsp = new RecipeMeasurement { RecipeMeasurementName = "Teaspoons" }; var ounces = new RecipeMeasurement { RecipeMeasurementName = "Ounces" }; context.AddRange( cups, tblsp, tsp, ounces ); #endregion #region Add Recipe Origins var origin1 = new RecipeOrigin { RecipeOriginName = "Italian" }; var origin2 = new RecipeOrigin { RecipeOriginName = "German" }; context.AddRange( origin1, origin2 ); #endregion #region Add Recipes var recipe1 = new Recipe { RecipeCookTime = 45, RecipeName = "Good Food", RecipeInstruction = "Cook the food and stuff", RecipeOrigin = origin1, User = user1 }; var recipe2 = new Recipe { RecipeCookTime = 60, RecipeName = "Super Good Food", RecipeInstruction = "Cook the damn food ", RecipeOrigin = origin2, User = user2 }; context.AddRange( recipe1, recipe2 ); #endregion #region Add RecipeIngrediantGroups context.AddRange( new RecipeIngredientGroups { Recipe = recipe1, RecipeIngredient = beer, RecipeMeasurement = ounces, RecipeIngredientAmount = 36 }, new RecipeIngredientGroups { Recipe = recipe1, RecipeIngredient = salt, RecipeMeasurement = tsp, RecipeIngredientAmount = 1 }, new RecipeIngredientGroups { Recipe = recipe1, RecipeIngredient = pepper, RecipeMeasurement = tblsp, RecipeIngredientAmount = 2 } ); context.AddRange( new RecipeIngredientGroups { Recipe = recipe2, RecipeIngredient = chicken, RecipeMeasurement = ounces, RecipeIngredientAmount = 24 }, new RecipeIngredientGroups { Recipe = recipe2, RecipeIngredient = salt, RecipeMeasurement = tblsp, RecipeIngredientAmount = 1 }, new RecipeIngredientGroups { Recipe = recipe2, RecipeIngredient = butter, RecipeMeasurement = tblsp, RecipeIngredientAmount = 2 }, new RecipeIngredientGroups { Recipe = recipe2, RecipeIngredient = pepper, RecipeMeasurement = tsp, RecipeIngredientAmount = 2 } ); #endregion #region Add RecipeReviews context.AddRange( new RecipeReview { Recipe = recipe1, RecipeReviewComment = "Test Review 1", RecipeReviewRating = 1, User = user1 }, new RecipeReview { Recipe = recipe1, RecipeReviewComment = "Test Review 2", RecipeReviewRating = 2, User = user2 }, new RecipeReview { Recipe = recipe2, RecipeReviewComment = "Test Review 3", RecipeReviewRating = 3, User = user1 }, new RecipeReview { Recipe = recipe2, RecipeReviewComment = "Test Review 4", RecipeReviewRating = 4, User = user2 } ); #endregion #region Add CookBooks var cookBook1 = new Cookbook { User = user1, Recipe = recipe1 }; var cookBook2 = new Cookbook { User = user1, Recipe = recipe2 }; context.AddRange( cookBook1, cookBook2 ); #endregion context.SaveChanges(); } }