public void AddRestrauntShouldAddCorrectRestauranttoDB(string Id, bool useAsync) { //Arrange var options = new DbContextOptionsBuilder <Project2DBContext>() .UseInMemoryDatabase(databaseName: "EmptyAddRestaurantTesting3DB") .Options; Restaurant r = new Restaurant { Id = Id, Name = Id + " Diner", Lat = "loc", Lon = "loc" }; RestaurantRepo rRepo; Restaurant result; //Act using (var context = new Project2DBContext(options)) { rRepo = new RestaurantRepo(context); if (useAsync) { rRepo.AddRestaurantAsync(r).Wait(); } else { rRepo.AddRestaurant(r); } result = context.Restaurant.Find(r.Id); } //Assert Assert.Equal(r, result); }
public void AddRestaurantShouldThrowExceptionIfIdAlreadyInDB(string Id, bool useAsync) { //Arrange string dbName; if (useAsync) { dbName = "EmptyAddRestaurantAsyncTesting1DB"; } else { dbName = "EmptyAddRestaurantTesting1DB"; } var options = new DbContextOptionsBuilder <Project2DBContext>() .UseInMemoryDatabase(databaseName: dbName) .Options; Restaurant r = new Restaurant { Id = Id, Name = Id + " Diner", Lat = "loc", Lon = "loc" }; Restaurant r2 = new Restaurant { Id = Id, Name = Id + " Diner", Lat = "loc", Lon = "loc" }; RestaurantRepo rRepo; bool result = false; using (var context = new Project2DBContext(options)) { context.Restaurant.Add(r2); context.SaveChanges(); } //Act using (var context = new Project2DBContext(options)) { rRepo = new RestaurantRepo(context); try { if (useAsync) { rRepo.AddRestaurantAsync(r).Wait(); } else { rRepo.AddRestaurant(r); } } catch (DbUpdateException) { result = true; } catch (AggregateException) { result = true; } } //Assert Assert.True(result); }
public void AddRestaurantShouldThrowExceptionIfLocationIsNull(string Id, bool useAsync) { //Arrange var options = new DbContextOptionsBuilder <Project2DBContext>() .UseInMemoryDatabase(databaseName: "EmptyAddRestaurantTesting2DB") .Options; Restaurant r = new Restaurant { Id = Id, Name = Id + " Diner" }; RestaurantRepo rRepo; bool result = false; //Act using (var context = new Project2DBContext(options)) { rRepo = new RestaurantRepo(context); try { if (useAsync) { rRepo.AddRestaurantAsync(r).Wait(); } else { rRepo.AddRestaurant(r); } } catch (DbUpdateException) { result = true; } catch (AggregateException) { result = true; } } //Assert Assert.True(result); }