public async void Test_LodgingRepo_GetAsync() { var dbOptions = await NewDb(); using (var ctx = new LodgingContext(dbOptions)) { await ctx.Database.EnsureCreatedAsync(); await ctx.SaveChangesAsync(); // Add repo-specific setup here. await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(dbOptions)) { var repo = new LodgingRepository(ctx); // Add repo-specific method calls here. var actual = await repo.GetAsync(new LodgingSearchFilterModel()); // Add Asserts here. Assert.Empty(actual); } }
public async void Test_ReviewRepo_GetAsyncById() { var dbOptions = await NewDb(); using (var ctx = new LodgingContext(dbOptions)) { await ctx.Database.EnsureCreatedAsync(); await ctx.SaveChangesAsync(); // Add repo-specific setup here. await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(dbOptions)) { var repo = new ReviewRepository(ctx); // Add repo-specific method calls here. var actual = await repo.GetAsync(1); // Add Asserts here. Assert.Null(actual); } }
public async void Test_Seed_HasRentals() { var dbOptions = await NewDb(); var rental = new RentalModel() { Id = 1 }; using (var ctx = new LodgingContext(dbOptions)) { await ctx.Database.EnsureCreatedAsync(); await ctx.Rentals.AddAsync(rental); await ctx.SaveChangesAsync(); // Setup for seeding the database Seed.SeedDatabase(ctx); } using (var ctx = new LodgingContext(dbOptions)) { // Add Asserts here. Assert.False(ctx.Lodgings.Count() > 0); Assert.True(ctx.Rentals.Count() > 0); Assert.False(ctx.Reviews.Count() > 0); } }
public async void Test_UnitOfWork_CommitAsync() { await _connection.OpenAsync(); try { using (var ctx = new LodgingContext(_options)) { await ctx.Database.EnsureCreatedAsync(); } using (var ctx = new LodgingContext(_options)) { var unitOfWork = new UnitOfWork(ctx); var actual = await unitOfWork.CommitAsync(); Assert.NotNull(unitOfWork.Lodging); Assert.NotNull(unitOfWork.Rental); Assert.NotNull(unitOfWork.Review); Assert.Equal(0, actual); } } finally { await _connection.CloseAsync(); } }
public async void Test_LodgingRepo_LodgingByCityAndOccupancy(LodgingModel lodging) { await _connection.OpenAsync(); try { using (var ctx = new LodgingContext(_options)) { await ctx.Database.EnsureCreatedAsync(); await ctx.Lodgings.AddAsync(lodging); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(_options)) { var lodgings = new UnitOfWork(ctx); var actual = await lodgings.Lodging.LodgingByCityAndOccupancy("Austin", 3); Assert.NotEmpty(actual); } } finally { await _connection.CloseAsync(); } }
public UnitOfWork(LodgingContext context) { _context = context; Lodging = new LodgingRepo(context); Rental = new Repository <RentalModel>(context); Review = new Repository <ReviewModel>(context); }
public async void Test_Repository_DeleteAsync(LodgingModel lodging, RentalModel rental, ReviewModel review) { await _connection.OpenAsync(); try { using (var ctx = new LodgingContext(_options)) { await ctx.Database.EnsureCreatedAsync(); await ctx.Lodgings.AddAsync(lodging); await ctx.Rentals.AddAsync(rental); await ctx.Reviews.AddAsync(review); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(_options)) { var lodgings = new Repository <LodgingModel>(ctx); await lodgings.DeleteAsync(1); await ctx.SaveChangesAsync(); Assert.Empty(await ctx.Lodgings.ToListAsync()); } using (var ctx = new LodgingContext(_options)) { var rentals = new Repository <RentalModel>(ctx); await rentals.DeleteAsync(1); await ctx.SaveChangesAsync(); Assert.Empty(await ctx.Rentals.ToListAsync()); } using (var ctx = new LodgingContext(_options)) { var reviews = new Repository <ReviewModel>(ctx); await reviews.DeleteAsync(1); await ctx.SaveChangesAsync(); Assert.Empty(await ctx.Reviews.ToListAsync()); } } finally { await _connection.CloseAsync(); } }
public async void TestRepositoryDeleteAsync(LodgingModel lodging, RentalModel rental, ReviewModel review, ImageModel image) { using (var ctx = new LodgingContext(Options)) { ctx.Rentals.RemoveRange(ctx.Rentals); ctx.Lodgings.RemoveRange(ctx.Lodgings); ctx.Images.RemoveRange(ctx.Images); await ctx.Rentals.AddAsync(rental); await ctx.Reviews.AddAsync(review); await ctx.Images.AddAsync(image); await ctx.Lodgings.AddAsync(lodging); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(Options)) { var lodgings = new Repository <LodgingModel>(ctx); await lodgings.DeleteAsync(lodging.EntityId); Assert.Equal(EntityState.Deleted, ctx.Entry(ctx.Lodgings.Find(lodging.EntityId)).State); } using (var ctx = new LodgingContext(Options)) { var rentals = new Repository <RentalModel>(ctx); await rentals.DeleteAsync(rental.EntityId); Assert.Equal(EntityState.Deleted, ctx.Entry(ctx.Rentals.Find(rental.EntityId)).State); } using (var ctx = new LodgingContext(Options)) { var reviews = new Repository <ReviewModel>(ctx); await reviews.DeleteAsync(review.EntityId); Assert.Equal(EntityState.Deleted, ctx.Entry(ctx.Reviews.Find(review.EntityId)).State); } using (var ctx = new LodgingContext(Options)) { var images = new Repository <ImageModel>(ctx); await images.DeleteAsync(image.EntityId); Assert.Equal(EntityState.Deleted, ctx.Entry(ctx.Images.Find(image.EntityId)).State); } }
public async void Test_Repository_Update(LodgingModel lodging, RentalModel rental, ReviewModel review) { using (var ctx = new LodgingContext(Options)) { ctx.Rentals.RemoveRange(ctx.Rentals); ctx.Lodgings.RemoveRange(ctx.Lodgings); await ctx.Lodgings.AddAsync(lodging); await ctx.Rentals.AddAsync(rental); await ctx.Reviews.AddAsync(review); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(Options)) { var lodgings = new Repository <LodgingModel>(ctx); var lodgingToUpdate = await ctx.Lodgings.FirstAsync(); lodgingToUpdate.Name = "Name"; lodgings.Update(lodgingToUpdate); var result = ctx.Lodgings.Find(lodging.Id); Assert.Equal(lodgingToUpdate.Name, result.Name); Assert.Equal(EntityState.Modified, ctx.Entry(result).State); } using (var ctx = new LodgingContext(Options)) { var rentals = new Repository <RentalModel>(ctx); var rentalToUpdate = await ctx.Rentals.FirstAsync(); rentalToUpdate.LotNumber = "4"; rentals.Update(rentalToUpdate); var result = ctx.Rentals.Find(rental.Id); Assert.Equal(rentalToUpdate.LotNumber, result.LotNumber); Assert.Equal(EntityState.Modified, ctx.Entry(result).State); } using (var ctx = new LodgingContext(Options)) { var reviews = new Repository <ReviewModel>(ctx); var reviewToUpdate = await ctx.Reviews.FirstAsync(); reviewToUpdate.Comment = "Comment"; reviews.Update(reviewToUpdate); var result = ctx.Reviews.Find(review.Id); Assert.Equal(reviewToUpdate.Comment, result.Comment); Assert.Equal(EntityState.Modified, ctx.Entry(result).State); } }
public async void TestUnitOfWorkCommitAsync() { using var ctx = new LodgingContext(Options); var unitOfWork = new UnitOfWork(ctx); var actual = await unitOfWork.CommitAsync(); Assert.NotNull(unitOfWork.Lodging); Assert.NotNull(unitOfWork.Rental); Assert.NotNull(unitOfWork.Review); Assert.Equal(0, actual); }
protected DataTest() { _connection = new SqliteConnection("Data Source=:memory:"); _connection.Open(); Options = new DbContextOptionsBuilder<LodgingContext>() .UseSqlite(_connection) .Options; using var ctx = new LodgingContext(Options); ctx.Database.EnsureCreated(); }
/// <summary> /// /// </summary> /// <param name="applicationBuilder"></param> /// <param name="hostEnvironment"></param> /// <param name="descriptionProvider"></param> /// <param name="context"></param> public void Configure(IApiVersionDescriptionProvider descriptionProvider, IApplicationBuilder applicationBuilder, IWebHostEnvironment hostEnvironment, LodgingContext context) { if (hostEnvironment.IsDevelopment()) { applicationBuilder.UseDeveloperExceptionPage(); Seed.SeedDatabase(context); } applicationBuilder.UseZipkin(); applicationBuilder.UseTracing("lodgingapi.rest"); // Implements a global exception handler applicationBuilder.UseExceptionHandler(a => a.Run(async context => { // creates an empty object for the exception (so as not to leak server data) var result = new ObjectResult(""); // sets the status code for the exception response to server error result.StatusCode = 500; var JsonResult = JsonConvert.SerializeObject(result); context.Response.ContentType = "application/json"; // writes the object into a readable response await context.Response.WriteAsync(JsonResult); })); applicationBuilder.UseHttpsRedirection(); applicationBuilder.UseRouting(); applicationBuilder.UseSwagger(); applicationBuilder.UseSwaggerUI(options => { foreach (var description in descriptionProvider.ApiVersionDescriptions) { options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName); } }); applicationBuilder.UseCors(); applicationBuilder.UseAuthorization(); applicationBuilder.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
/// <summary> /// /// </summary> /// <param name="applicationBuilder"></param> /// <param name="hostEnvironment"></param> /// <param name="descriptionProvider"></param> /// <param name="context"></param> public void Configure(IApiVersionDescriptionProvider descriptionProvider, IApplicationBuilder applicationBuilder, IWebHostEnvironment hostEnvironment, LodgingContext context) { if (hostEnvironment.IsDevelopment()) { applicationBuilder.UseDeveloperExceptionPage(); Seed.SeedDatabase(context); } applicationBuilder.UseZipkin(); applicationBuilder.UseTracing("lodgingapi.rest"); applicationBuilder.UseExceptionHandler(a => a.Run(async context => { var exceptionHandlerPathFeature = context.Features.Get <IExceptionHandlerPathFeature>(); var exception = exceptionHandlerPathFeature.Error; var result = new ObjectResult(""); result.StatusCode = 500; var JsonResult = JsonConvert.SerializeObject(result); context.Response.ContentType = "application/json"; await context.Response.WriteAsync(JsonResult); })); applicationBuilder.UseHttpsRedirection(); applicationBuilder.UseRouting(); applicationBuilder.UseSwagger(); applicationBuilder.UseSwaggerUI(options => { foreach (var description in descriptionProvider.ApiVersionDescriptions) { options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName); } }); applicationBuilder.UseCors(); applicationBuilder.UseAuthorization(); applicationBuilder.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public async void Test_GetAsyncById() { var dbOptions = await NewDb(); using (var ctx = new LodgingContext(dbOptions)) { await ctx.Database.EnsureCreatedAsync(); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(dbOptions)) { var repo = new ConcreteRepository(ctx); var _ = repo.GetAsync(1, new DummyQueryParamsModel()); } }
public async void Test_Repository_SelectAsync_ById() { await _connection.OpenAsync(); try { using (var ctx = new LodgingContext(_options)) { await ctx.Database.EnsureCreatedAsync(); } using (var ctx = new LodgingContext(_options)) { var lodgings = new Repository <LodgingModel>(ctx); var actual = await lodgings.SelectAsync(TestId); Assert.Null(actual); } using (var ctx = new LodgingContext(_options)) { var rentals = new Repository <RentalModel>(ctx); var actual = await rentals.SelectAsync(TestId); Assert.Null(actual); } using (var ctx = new LodgingContext(_options)) { var reviews = new Repository <ReviewModel>(ctx); var actual = await reviews.SelectAsync(TestId); Assert.Null(actual); } } finally { await _connection.CloseAsync(); } }
public async void Test_LodgingRepo_LodgingByLocationAndOccupancy(LodgingModel lodging) { using (var ctx = new LodgingContext(Options)) { await ctx.Lodgings.AddAsync(lodging); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(Options)) { var lodgings = new LodgingRepo(ctx); var actual = await lodgings.LodgingByLocationAndOccupancy(2, "Austin"); Assert.NotEmpty(actual); Assert.True(actual.Count() == 1); } }
public async void Test_Repository_GetAsync_ById() { await _connection.OpenAsync(); try { using (var ctx = new LodgingContext(_options)) { await ctx.Database.EnsureCreatedAsync(); } using (var ctx = new LodgingContext(_options)) { var lodgings = new LodgingRepository(ctx); var actual = await lodgings.GetAsync(1, new LodgingQueryParamsModel()); Assert.Null(actual); } using (var ctx = new LodgingContext(_options)) { var rentals = new RentalRepository(ctx); var actual = await rentals.GetAsync(1, new RentalQueryParamsModel()); Assert.Null(actual); } using (var ctx = new LodgingContext(_options)) { var reviews = new ReviewRepository(ctx); var actual = await reviews.GetAsync(1, new ReviewQueryParamsModel()); Assert.Null(actual); } } finally { await _connection.CloseAsync(); } }
public async void Test_Repository_GetAsync() { await _connection.OpenAsync(); try { using (var ctx = new LodgingContext(_options)) { await ctx.Database.EnsureCreatedAsync(); } using (var ctx = new LodgingContext(_options)) { var lodgings = new LodgingRepository(ctx); var actual = await lodgings.GetAsync(new LodgingSearchFilterModel()); Assert.Empty(actual); } using (var ctx = new LodgingContext(_options)) { var rentals = new RentalRepository(ctx); var actual = await rentals.GetAsync(new RentalSearchFilterModel()); Assert.Empty(actual); } using (var ctx = new LodgingContext(_options)) { var reviews = new ReviewRepository(ctx); var actual = await reviews.GetAsync(new ReviewSearchFilterModel()); Assert.Empty(actual); } } finally { await _connection.CloseAsync(); } }
public async void TestRepositorySelectAsync() { using (var ctx = new LodgingContext(Options)) { ctx.Rentals.RemoveRange(ctx.Rentals); ctx.Lodgings.RemoveRange(ctx.Lodgings); ctx.Images.RemoveRange(ctx.Images); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(Options)) { var lodgings = new Repository <LodgingModel>(ctx); var actual = await lodgings.SelectAsync(); Assert.Empty(actual); } using (var ctx = new LodgingContext(Options)) { var rentals = new Repository <RentalModel>(ctx); var actual = await rentals.SelectAsync(); Assert.Empty(actual); } using (var ctx = new LodgingContext(Options)) { var reviews = new Repository <ReviewModel>(ctx); var actual = await reviews.SelectAsync(); Assert.Empty(actual); } using (var ctx = new LodgingContext(Options)) { var images = new Repository <ImageModel>(ctx); var actual = await images.SelectAsync(); Assert.Empty(actual); } }
public async void Test_LodgingRepo_GetAsyncById() { var dbOptions = await NewDb(); using (var ctx = new LodgingContext(dbOptions)) { await ctx.Database.EnsureCreatedAsync(); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(dbOptions)) { var repo = new LodgingRepository(ctx); var actual = await repo.GetAsync(1, new LodgingQueryParamsModel()); Assert.Null(actual); } }
public async void Test_Apply_Sort_Order() { var dbOptions = await NewDb(); using (var ctx = new LodgingContext(dbOptions)) { await ctx.Database.EnsureCreatedAsync(); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(dbOptions)) { var query = new List <DummyModel>().AsQueryable(); var filters = new List <Expression <Func <DummyModel, bool> > >(); var repo = new ConcreteRepository(ctx); repo.RunApplyTest(query, filters, (e => e.Id), "asc"); repo.RunApplyTest(query, filters, (e => e.Id), "desc"); repo.RunApplyTest(query, filters, (e => e.Id), "default"); } }
public async void Test_LodgingRepo_GetAsync_IncludeImages() { var dbOptions = await NewDb(); using (var ctx = new LodgingContext(dbOptions)) { await ctx.Database.EnsureCreatedAsync(); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(dbOptions)) { var repo = new LodgingRepository(ctx); var queryParams = new LodgingQueryParamsModel(); queryParams.IncludeImages = true; var actual = await repo.GetAsync(queryParams); Assert.Empty(actual); } }
public async void Test_Repository_InsertAsync(LodgingModel lodging, RentalModel rental, ReviewModel review) { using (var ctx = new LodgingContext(Options)) { ctx.Rentals.RemoveRange(ctx.Rentals); ctx.Lodgings.RemoveRange(ctx.Lodgings); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(Options)) { var lodgings = new Repository <LodgingModel>(ctx); await lodgings.InsertAsync(lodging); Assert.Equal(EntityState.Added, ctx.Entry(lodging).State); } using (var ctx = new LodgingContext(Options)) { var rentals = new Repository <RentalModel>(ctx); await rentals.InsertAsync(rental); Assert.Equal(EntityState.Added, ctx.Entry(rental).State); } using (var ctx = new LodgingContext(Options)) { var reviews = new Repository <ReviewModel>(ctx); await reviews.InsertAsync(review); Assert.Equal(EntityState.Added, ctx.Entry(review).State); } }
public async void Test_Repository_SelectAsync_ById() { using (var ctx = new LodgingContext(Options)) { ctx.Rentals.RemoveRange(ctx.Rentals); ctx.Lodgings.RemoveRange(ctx.Lodgings); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(Options)) { var lodgings = new Repository <LodgingModel>(ctx); var actual = await lodgings.SelectAsync(1); Assert.Null(actual); } using (var ctx = new LodgingContext(Options)) { var rentals = new Repository <RentalModel>(ctx); var actual = await rentals.SelectAsync(1); Assert.Null(actual); } using (var ctx = new LodgingContext(Options)) { var reviews = new Repository <ReviewModel>(ctx); var actual = await reviews.SelectAsync(1); Assert.Null(actual); } }
public ReviewRepository(LodgingContext context) { }
public Repository(LodgingContext context) { _db = context.Set <TEntity>(); }
public async void Test_Repository_Update(LodgingModel lodging, RentalModel rental, ReviewModel review) { await _connection.OpenAsync(); try { using (var ctx = new LodgingContext(_options)) { await ctx.Database.EnsureCreatedAsync(); await ctx.Lodging.AddAsync(lodging); await ctx.Rentals.AddAsync(rental); await ctx.Reviews.AddAsync(review); await ctx.SaveChangesAsync(); } using (var ctx = new LodgingContext(_options)) { var lodgings = new Repository <LodgingModel>(ctx); var expected = await ctx.Lodging.FirstAsync(); expected.Name = "name"; lodgings.Update(expected); await ctx.SaveChangesAsync(); var actual = await ctx.Lodging.FirstAsync(); Assert.Equal(expected, actual); } using (var ctx = new LodgingContext(_options)) { var rentals = new Repository <RentalModel>(ctx); var expected = await ctx.Rentals.FirstAsync(); expected.Name = "name"; rentals.Update(expected); await ctx.SaveChangesAsync(); var actual = await ctx.Rentals.FirstAsync(); Assert.Equal(expected, actual); } using (var ctx = new LodgingContext(_options)) { var reviews = new Repository <ReviewModel>(ctx); var expected = await ctx.Reviews.FirstAsync(); expected.Comment = "comment"; reviews.Update(expected); await ctx.SaveChangesAsync(); var actual = await ctx.Reviews.FirstAsync(); Assert.Equal(expected, actual); } } finally { await _connection.CloseAsync(); } }
public void InstantiateLodgingContext() { var _ = new LodgingContext(); }
public ReviewRepository(LodgingContext context) : base(context) { this.dbContext = context; }
public static void SeedDatabase(LodgingContext context) { var lodging1 = new LodgingModel() { Id = 1, Name = "Campsite 1", Bathrooms = 3, Location = new LocationModel() { Id = 1, Address = new AddressModel() { Id = 1, Street = "77 Woodsgate Sq", City = "Baguio", Country = "Philippines", StateProvince = "Benguet", PostalCode = "2600", LocationId = 1 }, Latitude = "16.4023° N", Longitude = "120.5960° E", Locale = "en" }, Rentals = new List <RentalModel>() { new RentalModel { Id = 1, Status = "available", Name = "Baguio House", Occupancy = 6, Price = 100.00, Type = "home", LodgingId = 1 }, new RentalModel { Id = 2, Status = "available", Name = "Baguio Cabin", Occupancy = 4, Price = 100.00, Type = "cabin", LodgingId = 1 }, new RentalModel { Id = 3, Status = "booked", Name = "Waterfront cabin", Occupancy = 2, Price = 100.00, Type = "cabin", LodgingId = 1 }, new RentalModel { Id = 4, Status = "available", Name = "Boat House", Occupancy = 8, Price = 100.00, Type = "home", LodgingId = 1 } }, Reviews = new List <ReviewModel>() { new ReviewModel { Id = 1, AccountId = 1, Comment = "I love it here", Rating = 10, DateCreated = DateTime.Now, LodgingId = 1 }, new ReviewModel { Id = 2, AccountId = 1, Comment = "The flora and fauna is beautiful", Rating = 10, DateCreated = DateTime.Now, LodgingId = 1 }, new ReviewModel { Id = 3, AccountId = 1, Comment = "Nice houses", Rating = 10, DateCreated = DateTime.Now, LodgingId = 1 }, new ReviewModel { Id = 4, AccountId = 1, Comment = "Water is warm", Rating = 10, DateCreated = DateTime.Now, LodgingId = 1 } } }; context.Add(lodging1); context.SaveChanges(); }