Esempio n. 1
0
        public void GetValueToReplace_ParametersMatchExpectedValues()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <ValueToReplaceDbContext>()
                            .UseInMemoryDatabase(databaseName: $"ValueToReplaceDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeValueToReplace = new FakeValueToReplace {
            }.Generate();

            //Act
            using (var context = new ValueToReplaceDbContext(dbOptions))
            {
                context.ValueToReplaces.AddRange(fakeValueToReplace);
                context.SaveChanges();

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

                //Assert
                var valueToReplaceById = service.GetValueToReplace(fakeValueToReplace.ValueToReplaceId);

                valueToReplaceById.Should().BeEquivalentTo(fakeValueToReplace);
                valueToReplaceById.ValueToReplaceId.Should().Be(fakeValueToReplace.ValueToReplaceId);
                valueToReplaceById.ValueToReplaceTextField1.Should().Be(fakeValueToReplace.ValueToReplaceTextField1);
                valueToReplaceById.ValueToReplaceTextField2.Should().Be(fakeValueToReplace.ValueToReplaceTextField2);
                valueToReplaceById.ValueToReplaceDateField1.Should().Be(fakeValueToReplace.ValueToReplaceDateField1);
            }
        }
Esempio n. 2
0
        public void AddValueToReplace_NewRecordAddedWithProperValues()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <ValueToReplaceDbContext>()
                            .UseInMemoryDatabase(databaseName: $"ValueToReplaceDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeValueToReplace = new FakeValueToReplace {
            }.Generate();

            //Act
            using (var context = new ValueToReplaceDbContext(dbOptions))
            {
                var service = new ValueToReplaceRepository(context, new SieveProcessor(sieveOptions));

                service.AddValueToReplace(fakeValueToReplace);

                context.SaveChanges();
            }

            //Assert
            using (var context = new ValueToReplaceDbContext(dbOptions))
            {
                context.ValueToReplaces.Count().Should().Be(1);

                var valueToReplaceById = context.ValueToReplaces.FirstOrDefault(lambdaInitialsToReplace => lambdaInitialsToReplace.ValueToReplaceId == fakeValueToReplace.ValueToReplaceId);

                valueToReplaceById.Should().BeEquivalentTo(fakeValueToReplace);
                valueToReplaceById.ValueToReplaceId.Should().Be(fakeValueToReplace.ValueToReplaceId);
                valueToReplaceById.ValueToReplaceTextField1.Should().Be(fakeValueToReplace.ValueToReplaceTextField1);
                valueToReplaceById.ValueToReplaceTextField2.Should().Be(fakeValueToReplace.ValueToReplaceTextField2);
                valueToReplaceById.ValueToReplaceDateField1.Should().Be(fakeValueToReplace.ValueToReplaceDateField1);
            }
        }
        public async Task BadPatchValueToReplaceReturns400BadRequest()
        {
            //Arrange
            var lookupVal = "Easily Identified Value For Test"; // don't know the id at this scope, so need to have another value to lookup
            var fakeValueToReplaceOne = new FakeValueToReplace {
            }.Generate();

            var appFactory = _factory;

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

                context.ValueToReplaces.RemoveRange(context.ValueToReplaces);
                context.ValueToReplaces.AddRange(fakeValueToReplaceOne);
                context.SaveChanges();
            }

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

            var manuallyCreatedInvalidPatchDoc = "[{\"value\":\"" + lookupVal + "\",\"path\":\"/ValueToReplaceIntField1\",\"op\":\"replace\"}]";

            // Act
            // get the value i want to update. assumes I can use sieve for this field. if this is not an option, just use something else
            var getResult = await client.GetAsync($"api/ValueToReplaceLowers/?filters=ValueToReplaceTextField1=={fakeValueToReplaceOne.ValueToReplaceTextField1}")
                            .ConfigureAwait(false);

            var getResponseContent = await getResult.Content.ReadAsStringAsync()
                                     .ConfigureAwait(false);

            var getResponse = JsonConvert.DeserializeObject <IEnumerable <ValueToReplaceDto> >(getResponseContent);
            var id          = getResponse.FirstOrDefault().ValueToReplaceId;

            // patch it
            var method       = new HttpMethod("PATCH");
            var patchRequest = new HttpRequestMessage(method, $"api/ValueToReplaceLowers/{id}")
            {
                Content = new StringContent(manuallyCreatedInvalidPatchDoc,
                                            Encoding.Unicode, "application/json")
            };
            var patchResult = await client.SendAsync(patchRequest)
                              .ConfigureAwait(false);

            // Assert
            patchResult.StatusCode.Should().Be(400);
        }
Esempio n. 4
0
        public void GetValueToReplaces_FilterListWithExact(string filters)
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <ValueToReplaceDbContext>()
                            .UseInMemoryDatabase(databaseName: $"ValueToReplaceDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeValueToReplaceOne = new FakeValueToReplace {
            }.Generate();

            fakeValueToReplaceOne.ValueToReplaceTextField1 = "Alpha";
            fakeValueToReplaceOne.ValueToReplaceTextField2 = "Bravo";
            fakeValueToReplaceOne.ValueToReplaceIntField1  = 5;

            var fakeValueToReplaceTwo = new FakeValueToReplace {
            }.Generate();

            fakeValueToReplaceTwo.ValueToReplaceTextField1 = "Charlie";
            fakeValueToReplaceTwo.ValueToReplaceTextField2 = "Delta";
            fakeValueToReplaceTwo.ValueToReplaceIntField1  = 6;

            var fakeValueToReplaceThree = new FakeValueToReplace {
            }.Generate();

            fakeValueToReplaceThree.ValueToReplaceTextField1 = "Echo";
            fakeValueToReplaceThree.ValueToReplaceTextField2 = "Foxtrot";
            fakeValueToReplaceThree.ValueToReplaceIntField1  = 7;

            //Act
            using (var context = new ValueToReplaceDbContext(dbOptions))
            {
                context.ValueToReplaces.AddRange(fakeValueToReplaceOne, fakeValueToReplaceTwo, fakeValueToReplaceThree);
                context.SaveChanges();

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

                var valueToReplaceRepo = service.GetValueToReplaces(new ValueToReplaceParametersDto {
                    Filters = filters
                });

                //Assert
                valueToReplaceRepo.Should()
                .HaveCount(1);

                context.Database.EnsureDeleted();
            }
        }
Esempio n. 5
0
        public void GetValueToReplaces_SearchQueryReturnsExpectedRecordCount(string queryString, int expectedCount)
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <ValueToReplaceDbContext>()
                            .UseInMemoryDatabase(databaseName: $"ValueToReplaceDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeValueToReplaceOne = new FakeValueToReplace {
            }.Generate();

            fakeValueToReplaceOne.ValueToReplaceTextField1 = "Alpha";
            fakeValueToReplaceOne.ValueToReplaceTextField2 = "Bravo";

            var fakeValueToReplaceTwo = new FakeValueToReplace {
            }.Generate();

            fakeValueToReplaceTwo.ValueToReplaceTextField1 = "Hartsfield";
            fakeValueToReplaceTwo.ValueToReplaceTextField2 = "White";

            var fakeValueToReplaceThree = new FakeValueToReplace {
            }.Generate();

            fakeValueToReplaceThree.ValueToReplaceTextField1 = "Bravehart";
            fakeValueToReplaceThree.ValueToReplaceTextField2 = "Jonfav";

            //Act
            using (var context = new ValueToReplaceDbContext(dbOptions))
            {
                context.ValueToReplaces.AddRange(fakeValueToReplaceOne, fakeValueToReplaceTwo, fakeValueToReplaceThree);
                context.SaveChanges();

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

                var valueToReplaceRepo = service.GetValueToReplaces(new ValueToReplaceParametersDto {
                    QueryString = queryString
                });

                //Assert
                valueToReplaceRepo.Should()
                .HaveCount(expectedCount);

                context.Database.EnsureDeleted();
            }
        }
Esempio n. 6
0
        public void GetValueToReplaces_ListSortedInDescOrder()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <ValueToReplaceDbContext>()
                            .UseInMemoryDatabase(databaseName: $"ValueToReplaceDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeValueToReplaceOne = new FakeValueToReplace {
            }.Generate();

            fakeValueToReplaceOne.ValueToReplaceTextField1 = "Bravo";

            var fakeValueToReplaceTwo = new FakeValueToReplace {
            }.Generate();

            fakeValueToReplaceTwo.ValueToReplaceTextField1 = "Alpha";

            var fakeValueToReplaceThree = new FakeValueToReplace {
            }.Generate();

            fakeValueToReplaceThree.ValueToReplaceTextField1 = "Charlie";

            //Act
            using (var context = new ValueToReplaceDbContext(dbOptions))
            {
                context.ValueToReplaces.AddRange(fakeValueToReplaceOne, fakeValueToReplaceTwo, fakeValueToReplaceThree);
                context.SaveChanges();

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

                var valueToReplaceRepo = service.GetValueToReplaces(new ValueToReplaceParametersDto {
                    SortOrder = "-ValueToReplaceTextField1"
                });

                //Assert
                valueToReplaceRepo.Should()
                .ContainInOrder(fakeValueToReplaceThree, fakeValueToReplaceOne, fakeValueToReplaceTwo);

                context.Database.EnsureDeleted();
            }
        }
        public async Task GetValueToReplaces_ReturnsSuccessCodeAndResourceWithAccurateFields()
        {
            var fakeValueToReplaceOne = new FakeValueToReplace {
            }.Generate();
            var fakeValueToReplaceTwo = new FakeValueToReplace {
            }.Generate();

            var appFactory = _factory;

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

                //context.ValueToReplaces.RemoveRange(context.ValueToReplaces);
                context.ValueToReplaces.AddRange(fakeValueToReplaceOne, fakeValueToReplaceTwo);
                context.SaveChanges();
            }

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

            var result = await client.GetAsync($"api/v1/ValueToReplaceLowers")
                         .ConfigureAwait(false);

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

            var response = JsonConvert.DeserializeObject <IEnumerable <ValueToReplaceDto> >(responseContent);

            // Assert
            result.StatusCode.Should().Be(200);
            response.Should().ContainEquivalentOf(fakeValueToReplaceOne, options =>
                                                  options.ExcludingMissingMembers());
            response.Should().ContainEquivalentOf(fakeValueToReplaceTwo, options =>
                                                  options.ExcludingMissingMembers());
        }
Esempio n. 8
0
        public void DeleteValueToReplace_ReturnsProperCount()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <ValueToReplaceDbContext>()
                            .UseInMemoryDatabase(databaseName: $"ValueToReplaceDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeValueToReplaceOne = new FakeValueToReplace {
            }.Generate();
            var fakeValueToReplaceTwo = new FakeValueToReplace {
            }.Generate();
            var fakeValueToReplaceThree = new FakeValueToReplace {
            }.Generate();

            //Act
            using (var context = new ValueToReplaceDbContext(dbOptions))
            {
                context.ValueToReplaces.AddRange(fakeValueToReplaceOne, fakeValueToReplaceTwo, fakeValueToReplaceThree);

                var service = new ValueToReplaceRepository(context, new SieveProcessor(sieveOptions));
                service.DeleteValueToReplace(fakeValueToReplaceTwo);

                context.SaveChanges();

                //Assert
                var valueToReplaceList = context.ValueToReplaces.ToList();

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

                valueToReplaceList.Should().ContainEquivalentOf(fakeValueToReplaceOne);
                valueToReplaceList.Should().ContainEquivalentOf(fakeValueToReplaceThree);
                Assert.DoesNotContain(valueToReplaceList, lambdaInitialsToReplace => lambdaInitialsToReplace == fakeValueToReplaceTwo);

                context.Database.EnsureDeleted();
            }
        }
Esempio n. 9
0
        public void GetValueToReplaces_ReturnExpectedPageSize()
        {
            //Arrange
            var dbOptions = new DbContextOptionsBuilder <ValueToReplaceDbContext>()
                            .UseInMemoryDatabase(databaseName: $"ValueToReplaceDb{Guid.NewGuid()}")
                            .Options;
            var sieveOptions = Options.Create(new SieveOptions());

            var fakeValueToReplaceOne = new FakeValueToReplace {
            }.Generate();
            var fakeValueToReplaceTwo = new FakeValueToReplace {
            }.Generate();
            var fakeValueToReplaceThree = new FakeValueToReplace {
            }.Generate();

            //Act
            using (var context = new ValueToReplaceDbContext(dbOptions))
            {
                context.ValueToReplaces.AddRange(fakeValueToReplaceOne, fakeValueToReplaceTwo, fakeValueToReplaceThree);
                context.SaveChanges();

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

                var valueToReplaceRepo = service.GetValueToReplaces(new ValueToReplaceParametersDto {
                    PageSize = 2
                });

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

                valueToReplaceRepo.Should().ContainEquivalentOf(fakeValueToReplaceOne);
                valueToReplaceRepo.Should().ContainEquivalentOf(fakeValueToReplaceTwo);

                context.Database.EnsureDeleted();
            }
        }
        public async Task PatchValueToReplaceReturns204AndFieldsWereSuccessfullyUpdated()
        {
            //Arrange
            var mapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <ValueToReplaceProfile>();
            }).CreateMapper();

            var lookupVal = "Easily Identified Value For Test"; // don't know the id at this scope, so need to have another value to lookup
            var fakeValueToReplaceOne = new FakeValueToReplace {
            }.Generate();
            var expectedFinalObject = mapper.Map <ValueToReplaceDto>(fakeValueToReplaceOne);

            expectedFinalObject.ValueToReplaceTextField1 = lookupVal;

            var appFactory = _factory;

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

                context.ValueToReplaces.RemoveRange(context.ValueToReplaces);
                context.ValueToReplaces.AddRange(fakeValueToReplaceOne);
                context.SaveChanges();
            }

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

            var patchDoc = new JsonPatchDocument <ValueToReplaceForUpdateDto>();

            patchDoc.Replace(lambdaInitialsToReplace => lambdaInitialsToReplace.ValueToReplaceTextField1, lookupVal);
            var serializedValueToReplaceToUpdate = JsonConvert.SerializeObject(patchDoc);

            // Act
            // get the value i want to update. assumes I can use sieve for this field. if this is not an option, just use something else
            var getResult = await client.GetAsync($"api/ValueToReplaceLowers/?filters=ValueToReplaceTextField1=={fakeValueToReplaceOne.ValueToReplaceTextField1}")
                            .ConfigureAwait(false);

            var getResponseContent = await getResult.Content.ReadAsStringAsync()
                                     .ConfigureAwait(false);

            var getResponse = JsonConvert.DeserializeObject <IEnumerable <ValueToReplaceDto> >(getResponseContent);
            var id          = getResponse.FirstOrDefault().ValueToReplaceId;

            // patch it
            var method       = new HttpMethod("PATCH");
            var patchRequest = new HttpRequestMessage(method, $"api/ValueToReplaceLowers/{id}")
            {
                Content = new StringContent(serializedValueToReplaceToUpdate,
                                            Encoding.Unicode, "application/json")
            };
            var patchResult = await client.SendAsync(patchRequest)
                              .ConfigureAwait(false);

            // get it again to confirm updates
            var checkResult = await client.GetAsync($"api/ValueToReplaceLowers/{id}")
                              .ConfigureAwait(false);

            var checkResponseContent = await checkResult.Content.ReadAsStringAsync()
                                       .ConfigureAwait(false);

            var checkResponse = JsonConvert.DeserializeObject <ValueToReplaceDto>(checkResponseContent);

            // Assert
            patchResult.StatusCode.Should().Be(204);
            checkResponse.Should().BeEquivalentTo(expectedFinalObject, options =>
                                                  options.ExcludingMissingMembers());
        }
        public async Task PutValueToReplaceReturnsBodyAndFieldsWereSuccessfullyUpdated()
        {
            //Arrange
            var mapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <ValueToReplaceProfile>();
            }).CreateMapper();

            var lookupVal = "Easily Identified Value For Test"; // don't know the id at this scope, so need to have another value to lookup
            var newString = "New Val";
            var newInt = 12;
            var newDate = new Faker("en").Date.Past();
            var fakeValueToReplaceOne = new FakeValueToReplace {
            }.Generate();
            var expectedFinalObject = mapper.Map <ValueToReplaceDto>(fakeValueToReplaceOne);

            expectedFinalObject.ValueToReplaceTextField1 = lookupVal;
            expectedFinalObject.ValueToReplaceTextField2 = newString;
            expectedFinalObject.ValueToReplaceIntField1  = newInt;
            expectedFinalObject.ValueToReplaceDateField1 = newDate;

            var appFactory = _factory;

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

                context.ValueToReplaces.RemoveRange(context.ValueToReplaces);
                context.ValueToReplaces.AddRange(fakeValueToReplaceOne);
                context.SaveChanges();
            }

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

            var serializedValueToReplaceToUpdate = JsonConvert.SerializeObject(expectedFinalObject);

            // Act
            // get the value i want to update. assumes I can use sieve for this field. if this is not an option, just use something else
            var getResult = await client.GetAsync($"api/v1/ValueToReplaceLowers/?filters=ValueToReplaceTextField1=={fakeValueToReplaceOne.ValueToReplaceTextField1}")
                            .ConfigureAwait(false);

            var getResponseContent = await getResult.Content.ReadAsStringAsync()
                                     .ConfigureAwait(false);

            var getResponse = JsonConvert.DeserializeObject <IEnumerable <ValueToReplaceDto> >(getResponseContent);
            var id          = getResponse.FirstOrDefault().ValueToReplaceId;

            // put it
            var patchResult = await client.PutAsJsonAsync($"api/v1/ValueToReplaceLowers/{id}", expectedFinalObject)
                              .ConfigureAwait(false);

            // get it again to confirm updates
            var checkResult = await client.GetAsync($"api/v1/ValueToReplaceLowers/{id}")
                              .ConfigureAwait(false);

            var checkResponseContent = await checkResult.Content.ReadAsStringAsync()
                                       .ConfigureAwait(false);

            var checkResponse = JsonConvert.DeserializeObject <ValueToReplaceDto>(checkResponseContent);

            // Assert
            patchResult.StatusCode.Should().Be(204);
            checkResponse.Should().BeEquivalentTo(expectedFinalObject, options =>
                                                  options.ExcludingMissingMembers());
        }