Exemplo n.º 1
0
        public async void GetCitiesAsync_ReturnExpectedPageNumberAndSize()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <VetClinicDbContext>()
                            .UseInMemoryDatabase(databaseName: $"CityDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var currentUser = new Mock <ICurrentUserService>();

            currentUser.SetupGet(c => c.UserId).Returns("testuser");
            var currentUserService = currentUser.Object;

            var fakeCityOne = new FakeCity {
            }.Generate();
            var fakeCityTwo = new FakeCity {
            }.Generate();
            var fakeCityThree = new FakeCity {
            }.Generate();

            //Act
            using (var context = new VetClinicDbContext(dbOptions, currentUserService, new DateTimeService()))
            {
                context.Cities.AddRange(fakeCityOne, fakeCityTwo, fakeCityThree);
                context.SaveChanges();

                var service = new CityRepository(context, new SieveProcessor(sieveOptions));

                var cityRepo = await service.GetCitiesAsync(new CityParametersDto { PageSize = 1, PageNumber = 2 });

                //Assert
                cityRepo.Should()
                .NotBeEmpty()
                .And.HaveCount(1);

                cityRepo.Should().ContainEquivalentOf(fakeCityTwo);

                context.Database.EnsureDeleted();
            }
        }
Exemplo n.º 2
0
        public void DeleteCity_ReturnsProperCount()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <VetClinicDbContext>()
                            .UseInMemoryDatabase(databaseName: $"CityDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeCityOne = new FakeCity {
            }.Generate();
            var fakeCityTwo = new FakeCity {
            }.Generate();
            var fakeCityThree = new FakeCity {
            }.Generate();

            //Act
            using (var context = new VetClinicDbContext(dbOptions))
            {
                context.Cities.AddRange(fakeCityOne, fakeCityTwo, fakeCityThree);

                var service = new CityRepository(context, new SieveProcessor(sieveOptions));
                service.DeleteCity(fakeCityTwo);

                context.SaveChanges();

                //Assert
                var cityList = context.Cities.ToList();

                cityList.Should()
                .NotBeEmpty()
                .And.HaveCount(2);

                cityList.Should().ContainEquivalentOf(fakeCityOne);
                cityList.Should().ContainEquivalentOf(fakeCityThree);
                Assert.DoesNotContain(cityList, c => c == fakeCityTwo);

                context.Database.EnsureDeleted();
            }
        }
        public async Task GetCities_ReturnsSuccessCodeAndResourceWithAccurateFields()
        {
            var fakeCityOne = new FakeCity {
            }.Generate();
            var fakeCityTwo = new FakeCity {
            }.Generate();

            var appFactory = _factory;

            using (var scope = appFactory.Services.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <VetClinicDbContext>();
                context.Database.EnsureCreated();

                //context.Cities.RemoveRange(context.Cities);
                context.Cities.AddRange(fakeCityOne, fakeCityTwo);
                context.SaveChanges();
            }

            var client = appFactory.CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            var result = await client.GetAsync("api/Cities")
                         .ConfigureAwait(false);

            var responseContent = await result.Content.ReadAsStringAsync()
                                  .ConfigureAwait(false);

            var response = JsonConvert.DeserializeObject <Response <IEnumerable <CityDto> > >(responseContent).Data;

            // Assert
            result.StatusCode.Should().Be(200);
            response.Should().ContainEquivalentOf(fakeCityOne, options =>
                                                  options.ExcludingMissingMembers());
            response.Should().ContainEquivalentOf(fakeCityTwo, options =>
                                                  options.ExcludingMissingMembers());
        }
        public async void GetCitiesAsync_CountMatchesAndContainsEquivalentObjects()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <VetClinicDbContext>()
                            .UseInMemoryDatabase(databaseName: $"CityDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeCityOne = new FakeCity {
            }.Generate();
            var fakeCityTwo = new FakeCity {
            }.Generate();
            var fakeCityThree = new FakeCity {
            }.Generate();

            //Act
            using (var context = new VetClinicDbContext(dbOptions))
            {
                context.Cities.AddRange(fakeCityOne, fakeCityTwo, fakeCityThree);
                context.SaveChanges();

                var service = new CityRepository(context, new SieveProcessor(sieveOptions));

                var cityRepo = await service.GetCitiesAsync(new CityParametersDto());

                //Assert
                cityRepo.Should()
                .NotBeEmpty()
                .And.HaveCount(3);

                cityRepo.Should().ContainEquivalentOf(fakeCityOne);
                cityRepo.Should().ContainEquivalentOf(fakeCityTwo);
                cityRepo.Should().ContainEquivalentOf(fakeCityThree);

                context.Database.EnsureDeleted();
            }
        }