public async Task CreateAsync_WithValidModel_WorksCorrectly() { // Arrange var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); var user = new PoolItUser { UserName = "******" }; var car = new Car { Model = new CarModel { Model = "Test Model", Manufacturer = new CarManufacturer() }, Owner = user, Colour = "TestColour" }; await context.Cars.AddAsync(car); await context.SaveChangesAsync(); var serviceModel = new RideServiceModel { Title = "Test Ride", CarId = car.Id, From = "Test From", To = "Test To", Date = DateTime.UtcNow, AvailableSeats = 2 }; var ridesService = new RidesService(new EfRepository <Ride>(context), null, new EfRepository <Car>(context), null); // Act var result = await ridesService.CreateAsync(serviceModel); // Assert Assert.NotNull(result); var dbModel = await context.Rides.SingleOrDefaultAsync(); Assert.NotNull(dbModel); // - Check correct id is returned Assert.Equal(dbModel.Id, result); // - Check Conversation is created Assert.NotNull(dbModel.Conversation); // - Check organiser is added as participant var userRideExists = await context.UserRides.AnyAsync(u => u.UserId == user.Id && u.RideId == dbModel.Id); Assert.True(userRideExists); }
public async Task CreateAsync_WithNonExistentCar_ReturnsNull() { // Arrange var testCarId = Guid.NewGuid().ToString(); var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); var serviceModel = new RideServiceModel { Title = "Test Ride", CarId = testCarId, From = "Test From", To = "Test To", Date = DateTime.UtcNow, AvailableSeats = 2 }; var ridesService = new RidesService(new EfRepository <Ride>(context), null, new EfRepository <Car>(context), null); // Act var result = await ridesService.CreateAsync(serviceModel); // Assert Assert.Null(result); var dbModel = await context.Rides.AnyAsync(); Assert.False(dbModel); }
public async Task CreateAsync_WithInvalidModel_ReturnsNull() { // Arrange var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); var car = new Car { Model = new CarModel { Model = "Test Model", Manufacturer = new CarManufacturer() }, Owner = new PoolItUser { UserName = "******" }, Colour = "TestColour" }; await context.Cars.AddAsync(car); await context.SaveChangesAsync(); var serviceModel = new RideServiceModel { Title = "Test Ride", CarId = car.Id }; var ridesService = new RidesService(new EfRepository <Ride>(context), null, new EfRepository <Car>(context), null); // Act var result = await ridesService.CreateAsync(serviceModel); // Assert Assert.Null(result); var dbModel = await context.Rides.AnyAsync(); Assert.False(dbModel); }