public async Task GetAddressByUserIdShouldGetAddress() { var dbContext = ApplicationDbContextInMemoryFactory.InitializeContext(); var repository = new EfDeletableEntityRepository <UserAddress>(dbContext); var service = new AddressesService(repository); var userManager = MockUserManager.GetUserManager(); var user = new ApplicationUser { Email = "*****@*****.**", UserName = "******", }; await userManager.CreateAsync(user); var userAddress = new UserAddress() { UserId = user.Id, }; await service.CreateAsync(userAddress); var userAddressInvalid = new UserAddress() { UserId = "none", }; await service.CreateAsync(userAddressInvalid); var foundAddress = await service.GetBaseByUserId(user.Id); Assert.Equal(user.Id, foundAddress.UserId); }
public async Task CreateAsyncShouldWorkCorrectlyWithUnexistingCityUsingMoq(string cityName, string cityZIPCode, int countryId) { var addressRepository = new Mock <IDeletableEntityRepository <Address> >(); var cityRepository = new Mock <IRepository <City> >(); var citiesList = new List <City> { new City { Id = 1, Name = "Ruse", ZIPCode = "7000", CountryId = 42 }, new City { Id = 2, Name = "Sofia", ZIPCode = "1000", CountryId = 42 }, }; var addressesList = new List <Address> { new Address { Id = "TestId1", CreatedOn = DateTime.UtcNow, Street = "TestStreet", City = citiesList.ElementAt(0), CityId = 1, Description = "TestDescription", UserId = "TestUserId" }, }; addressRepository.Setup(r => r.AllAsNoTracking()).Returns(addressesList.AsQueryable()); addressRepository.Setup(r => r.AddAsync(It.IsAny <Address>())).Callback((Address item) => addressesList.Add(item)); addressRepository.Setup(r => r.SaveChangesAsync()).Verifiable(); cityRepository.Setup(r => r.All()).Returns(citiesList.AsQueryable()); cityRepository.Setup(r => r.AddAsync(It.IsAny <City>())).Callback((City item) => citiesList.Add(item)); var service = new AddressesService(addressRepository.Object, cityRepository.Object); var model = new AddressInputViewModel { Street = "TestStreet", Description = "TestDescription", UserId = "TestUserId", City = cityName, ZIPCode = cityZIPCode, CountryId = countryId, }; Assert.True(await service.CreateAsync(model)); Assert.Equal(2, addressesList.Count); Assert.Equal("TestStreet", addressesList.ElementAt(1).Street); Assert.Equal("TestDescription", addressesList.ElementAt(1).Description); Assert.Equal("TestUserId", addressesList.ElementAt(1).UserId); Assert.Equal(cityName, addressesList.ElementAt(1).City.Name); Assert.Equal(3, citiesList.Count); Assert.Equal(cityName, citiesList.Last().Name); Assert.Equal(cityZIPCode, citiesList.Last().ZIPCode); Assert.Equal(countryId, citiesList.Last().CountryId); cityRepository.Verify(x => x.All(), Times.Once); cityRepository.Verify(x => x.AddAsync(It.IsAny <City>()), Times.Once); addressRepository.Verify(x => x.AllAsNoTracking(), Times.Once); addressRepository.Verify(x => x.AddAsync(It.IsAny <Address>()), Times.Once); addressRepository.Verify(x => x.SaveChangesAsync()); }
public async Task CreateAddressShouldCreateAddress() { var dbContext = ApplicationDbContextInMemoryFactory.InitializeContext(); var repository = new EfDeletableEntityRepository <UserAddress>(dbContext); var service = new AddressesService(repository); var userManager = MockUserManager.GetUserManager(); var user = new ApplicationUser { Email = "*****@*****.**", UserName = "******", }; await userManager.CreateAsync(user); var userAddress = new UserAddress() { UserId = user.Id, PersonName = "User", Area = "ValidArea", Street = "ValidStreet", Building = "ValidBuilding", Floor = "ValidFloor", Apartment = "ValidApartment", PhoneNumber = "ValidNumber", }; var userAddress2 = new UserAddress() { UserId = user.Id, PersonName = "", Area = "", Street = "", Building = "", Floor = "", Apartment = "", PhoneNumber = "", }; await service.CreateAsync(userAddress); await service.CreateAsync(userAddress2); var addressCount = dbContext.Addresses.ToList().Count; Assert.Equal(2, addressCount); }
public async Task UpdateAddressShouldUpdateAddress() { var dbContext = ApplicationDbContextInMemoryFactory.InitializeContext(); var repository = new EfDeletableEntityRepository <UserAddress>(dbContext); var service = new AddressesService(repository); var userManager = MockUserManager.GetUserManager(); // Create var user = new ApplicationUser { Email = "*****@*****.**", UserName = "******", }; await userManager.CreateAsync(user); var userAddress = new UserAddress() { UserId = user.Id, PersonName = "User", Area = "ValidArea", Street = "ValidStreet", Building = "ValidBuilding", Floor = "ValidFloor", Apartment = "ValidApartment", PhoneNumber = "ValidNumber", }; await service.CreateAsync(userAddress); // Update userAddress.PersonName = "UpdatedUser"; userAddress.Area = "UpdatedValidArea"; userAddress.Street = "UpdatedValidStreet"; userAddress.Building = "UpdatedValidBuilding"; userAddress.Floor = "UpdatedValidFloor"; userAddress.Apartment = "UpdatedValidApartment"; userAddress.PhoneNumber = "UpdatedValidNumber"; await service.UpdateAsync(userAddress); var updatedAddress = dbContext.Addresses.ToList().Find(x => x.Id == userAddress.Id); Assert.Equal("UpdatedUser", updatedAddress.PersonName); Assert.Equal("UpdatedValidArea", updatedAddress.Area); Assert.Equal("UpdatedValidStreet", updatedAddress.Street); Assert.Equal("UpdatedValidBuilding", updatedAddress.Building); Assert.Equal("UpdatedValidFloor", updatedAddress.Floor); Assert.Equal("UpdatedValidApartment", updatedAddress.Apartment); Assert.Equal("UpdatedValidNumber", updatedAddress.PhoneNumber); }
public async Task CreateAsyncWorksCorrectly() { var options = new DbContextOptionsBuilder<ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository<Address>(dbContext); var service = new AddressesService(repository); await service.CreateAsync(new AddressServiceModel()); var addressesInDbCount = repository.All().ToList().Count(); Assert.Equal(1, addressesInDbCount); }
public async Task CreateAsyncShouldReturnFalseIfAddressAlreadyExistsUsingMoq() { var addressRepository = new Mock <IDeletableEntityRepository <Address> >(); var cityRepository = new Mock <IRepository <City> >(); var citiesList = new List <City> { new City { Id = 1, Name = "Ruse", ZIPCode = "7000", CountryId = 42 }, new City { Id = 2, Name = "Sofia", ZIPCode = "1000", CountryId = 42 }, }; var addressesList = new List <Address> { new Address { Id = "TestId1", CreatedOn = DateTime.UtcNow, Street = "TestStreet", City = citiesList.ElementAt(0), CityId = 1, Description = "TestDescription", UserId = "TestUserId" }, }; addressRepository.Setup(r => r.AllAsNoTracking()).Returns(addressesList.AsQueryable()); cityRepository.Setup(r => r.All()).Returns(citiesList.AsQueryable()); var service = new AddressesService(addressRepository.Object, cityRepository.Object); var model = new AddressInputViewModel { Street = "TestStreet", Description = "TestDescription", UserId = "TestUserId", City = "Ruse", ZIPCode = "7000", CountryId = 42, }; Assert.False(await service.CreateAsync(model)); cityRepository.Verify(x => x.All(), Times.Once); addressRepository.Verify(x => x.AllAsNoTracking(), Times.Once); }