Пример #1
0
        public async void DeleteApartmentByIdAsync_PositiveAndNegative_TestAsync()
        {
            var options = new DbContextOptionsBuilder <ApartmentContext>()
                          .UseInMemoryDatabase(databaseName: "DeleteApartmentByIdAsync_PositiveAndNegative_TestAsync")
                          .Options;

            using (var context = new ApartmentContext(options))
            {
                await context.AddRangeAsync(_users);

                await context.SaveChangesAsync();

                var userWithApartments = context.Users.AsNoTracking().FirstOrDefault();

                foreach (var item in _addresses)
                {
                    item.CountryId = context.Countries.FirstOrDefault().Id;
                }

                for (int i = 0; i < 2; i++)
                {
                    _apartments[i].OwnerId = userWithApartments.Id;
                    _apartments[i].Address = _addresses[i];
                }

                await context.AddRangeAsync(_apartments);

                await context.SaveChangesAsync();
            }

            using (var context = new ApartmentContext(options))
            {
                var apartmenr = await context.Apartments.AsNoTracking().FirstOrDefaultAsync();

                var service = new ApartmentUserService(context, _mapper);

                var resultPositive = await service.DeleteApartmentByIdAsync(apartmenr.Id.ToString(), apartmenr.OwnerId.ToString());

                var resultNegative = await service.DeleteApartmentByIdAsync(new Guid().ToString(), new Guid().ToString());

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Message.Should().BeNull();

                resultNegative.IsSuccess.Should().BeFalse();
                resultNegative.Message.Should().Contain("Apartment was not found");
            }
        }
Пример #2
0
        public async void UpdateApartmentAsync_Positive_TestAsync()
        {
            var options = new DbContextOptionsBuilder <ApartmentContext>()
                          .UseInMemoryDatabase(databaseName: "UpdateApartmentAsync_PositiveAndNegative_TestAsync")
                          .Options;

            using (var context = new ApartmentContext(options))
            {
                await context.AddRangeAsync(_users);

                await context.SaveChangesAsync();

                var userWithApartments = context.Users.AsNoTracking().FirstOrDefault();

                foreach (var item in _addresses)
                {
                    item.CountryId = context.Countries.FirstOrDefault().Id;
                }

                for (int i = 0; i < 2; i++)
                {
                    _apartments[i].OwnerId = userWithApartments.Id;
                    _apartments[i].Address = _addresses[i];
                }

                await context.AddRangeAsync(_apartments);

                await context.SaveChangesAsync();
            }

            using (var context = new ApartmentContext(options))
            {
                var apartment = await context.Apartments
                                .Include(_ => _.Address.Country).Include(_ => _.Address)
                                .AsNoTracking().FirstOrDefaultAsync();

                var newCountry = await context.Countries.Where(_ => _.Id != apartment.Address.CountryId).FirstOrDefaultAsync();

                ApartmentView view = new ApartmentView()
                {
                    Apartment = _mapper.Map <ApartmentDTO>(apartment),

                    Address = _mapper.Map <AddressDTO>(apartment.Address),

                    Country = _mapper.Map <CountryDTO>(apartment.Address.Country)
                };

                view.Address.City    = "Updated";
                view.Apartment.Title = "Updated";
                view.Country         = _mapper.Map <CountryDTO>(newCountry);

                var service = new ApartmentUserService(context, _mapper);

                var resultPositive = await service.UpdateApartmentAsync(view);

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.Apartment.Title.Should().BeEquivalentTo("Updated");
                resultPositive.Data.Address.City.Should().BeEquivalentTo("Updated");
                resultPositive.Data.Country.Name.Should().BeEquivalentTo(newCountry.Name);
            }
        }