public void GetPet_ParametersMatchExpectedValues()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <VetClinicDbContext>()
                            .UseInMemoryDatabase(databaseName: $"PetDb{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 fakePet = new FakePet {
            }.Generate();

            //Act
            using (var context = new VetClinicDbContext(dbOptions, currentUserService, new DateTimeService()))
            {
                context.Pets.AddRange(fakePet);
                context.SaveChanges();

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

                //Assert
                var petById = service.GetPet(fakePet.PetId);
                petById.PetId.Should().Be(fakePet.PetId);
                petById.Name.Should().Be(fakePet.Name);
                petById.Type.Should().Be(fakePet.Type);
            }
        }
        public void DeletePet_ReturnsProperCount()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <VetClinicDbContext>()
                            .UseInMemoryDatabase(databaseName: $"PetDb{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 fakePetOne = new FakePet {
            }.Generate();
            var fakePetTwo = new FakePet {
            }.Generate();
            var fakePetThree = new FakePet {
            }.Generate();

            //Act
            using (var context = new VetClinicDbContext(dbOptions, currentUserService, new DateTimeService()))
            {
                context.Pets.AddRange(fakePetOne, fakePetTwo, fakePetThree);

                var service = new PetRepository(context, new SieveProcessor(sieveOptions));
                service.DeletePet(fakePetTwo);

                context.SaveChanges();

                //Assert
                var petList = context.Pets.ToList();

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

                petList.Should().ContainEquivalentOf(fakePetOne);
                petList.Should().ContainEquivalentOf(fakePetThree);
                Assert.DoesNotContain(petList, p => p == fakePetTwo);

                context.Database.EnsureDeleted();
            }
        }
        public async void GetPetsAsync_CountMatchesAndContainsEquivalentObjects()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <VetClinicDbContext>()
                            .UseInMemoryDatabase(databaseName: $"PetDb{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 fakePetOne = new FakePet {
            }.Generate();
            var fakePetTwo = new FakePet {
            }.Generate();
            var fakePetThree = new FakePet {
            }.Generate();

            //Act
            using (var context = new VetClinicDbContext(dbOptions, currentUserService, new DateTimeService()))
            {
                context.Pets.AddRange(fakePetOne, fakePetTwo, fakePetThree);
                context.SaveChanges();

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

                var petRepo = await service.GetPetsAsync(new PetParametersDto());

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

                petRepo.Should().ContainEquivalentOf(fakePetOne);
                petRepo.Should().ContainEquivalentOf(fakePetTwo);
                petRepo.Should().ContainEquivalentOf(fakePetThree);

                context.Database.EnsureDeleted();
            }
        }
        public async void GetPetsAsync_ReturnExpectedPageSize()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <VetClinicDbContext>()
                            .UseInMemoryDatabase(databaseName: $"PetDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakePetOne = new FakePet {
            }.Generate();
            var fakePetTwo = new FakePet {
            }.Generate();
            var fakePetThree = new FakePet {
            }.Generate();

            // need id's due to default sorting
            fakePetOne.PetId   = Guid.Parse("547ee3d9-5241-4ce3-93f6-a65700bd36ca");
            fakePetTwo.PetId   = Guid.Parse("621fab6d-2487-43f4-aec2-354fa54089da");
            fakePetThree.PetId = Guid.Parse("f9335b96-63dd-412e-935b-102463b9f245");

            //Act
            using (var context = new VetClinicDbContext(dbOptions))
            {
                context.Pets.AddRange(fakePetOne, fakePetTwo, fakePetThree);
                context.SaveChanges();

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

                var petRepo = await service.GetPetsAsync(new PetParametersDto { PageSize = 2 });

                //Assert
                petRepo.Should()
                .NotBeEmpty()
                .And.HaveCount(2);

                petRepo.Should().ContainEquivalentOf(fakePetOne);
                petRepo.Should().ContainEquivalentOf(fakePetTwo);

                context.Database.EnsureDeleted();
            }
        }
Example #5
0
        public async Task GetPets_ReturnsSuccessCodeAndResourceWithAccurateFields()
        {
            var fakePetOne = new FakePet {
            }.Generate();
            var fakePetTwo = new FakePet {
            }.Generate();

            var appFactory = _factory;

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

                //context.Pets.RemoveRange(context.Pets);
                context.Pets.AddRange(fakePetOne, fakePetTwo);
                context.SaveChanges();
            }

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

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

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

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

            // Assert
            result.StatusCode.Should().Be(200);
            response.Should().ContainEquivalentOf(fakePetOne, options =>
                                                  options.ExcludingMissingMembers());
            response.Should().ContainEquivalentOf(fakePetTwo, options =>
                                                  options.ExcludingMissingMembers());
        }