Ejemplo n.º 1
0
        public async Task DeleteSample204AndFieldsWereSuccessfullyUpdated()
        {
            //Arrange
            var fakeSampleOne = new FakeSample {
            }.Generate();

            var appFactory = _factory;

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

                context.Samples.RemoveRange(context.Samples);
                context.Samples.AddRange(fakeSampleOne);
                context.SaveChanges();
            }

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

            // 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/Samples/?filters=ExternalId=={fakeSampleOne.ExternalId}")
                            .ConfigureAwait(false);

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

            var getResponse = JsonConvert.DeserializeObject <Response <IEnumerable <SampleDto> > >(getResponseContent);
            var id          = getResponse.Data.FirstOrDefault().SampleId;

            // delete it
            var method        = new HttpMethod("DELETE");
            var deleteRequest = new HttpRequestMessage(method, $"api/Samples/{id}");
            var deleteResult  = await client.SendAsync(deleteRequest)
                                .ConfigureAwait(false);

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

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

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

            // Assert
            deleteResult.StatusCode.Should().Be(204);
            checkResponse.Data.Should().Be(null);
        }
        public async Task GetSamples_ReturnsSuccessCodeAndResourceWithAccurateFields()
        {
            var fakeSampleOne = new FakeSample {
            }.Generate();
            var fakeSampleTwo = new FakeSample {
            }.Generate();

            var appFactory = _factory;

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

                //context.Samples.RemoveRange(context.Samples);
                context.Samples.AddRange(fakeSampleOne, fakeSampleTwo);
                context.SaveChanges();
            }

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

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

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

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

            // Assert
            result.StatusCode.Should().Be(200);
            response.Should().ContainEquivalentOf(fakeSampleOne, options =>
                                                  options.ExcludingMissingMembers());
            response.Should().ContainEquivalentOf(fakeSampleTwo, options =>
                                                  options.ExcludingMissingMembers());
        }
Ejemplo n.º 3
0
        public async Task DeleteSampleCommand_Deletes_Sample_From_Db()
        {
            // Arrange
            var fakeSampleOne = new FakeSample {
            }.Generate();

            await InsertAsync(fakeSampleOne);

            var sample = await ExecuteDbContextAsync(db => db.Samples.SingleOrDefaultAsync());

            var sampleId = sample.SampleId;

            // Act
            var command        = new DeleteSampleCommand(sampleId);
            var sampleReturned = await SendAsync(command);

            await SendAsync(command);

            var samples = await ExecuteDbContextAsync(db => db.Samples.ToListAsync());

            // Assert
            samples.Count.Should().Be(0);
        }
Ejemplo n.º 4
0
        public async Task PatchSample204AndFieldsWereSuccessfullyUpdated()
        {
            //Arrange
            var mapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <SampleProfile>();
            }).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 fakeSampleOne = new FakeSample {
            }.Generate();

            var expectedFinalObject = mapper.Map <SampleDto>(fakeSampleOne);

            expectedFinalObject.ExternalId = lookupVal;

            var appFactory = _factory;

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

                context.Samples.RemoveRange(context.Samples);
                context.Samples.AddRange(fakeSampleOne);
                context.SaveChanges();
            }

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

            var patchDoc = new JsonPatchDocument <SampleForUpdateDto>();

            patchDoc.Replace(s => s.ExternalId, lookupVal);
            var serializedSampleToUpdate = 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/Samples/?filters=ExternalId=={fakeSampleOne.ExternalId}")
                            .ConfigureAwait(false);

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

            var getResponse = JsonConvert.DeserializeObject <Response <IEnumerable <SampleDto> > >(getResponseContent);
            var id          = getResponse.Data.FirstOrDefault().SampleId;

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

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

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

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

            // Assert
            patchResult.StatusCode.Should().Be(204);
            checkResponse.Should().BeEquivalentTo(expectedFinalObject, options =>
                                                  options.ExcludingMissingMembers());
        }
Ejemplo n.º 5
0
        public async Task PutSampleReturnsBodyAndFieldsWereSuccessfullyUpdated()
        {
            //Arrange
            var mapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <SampleProfile>();
            }).CreateMapper();

            var fakeSampleOne = new FakeSample {
            }.Generate();
            var expectedFinalObject = mapper.Map <SampleDto>(fakeSampleOne);

            expectedFinalObject.ExternalId = "Easily Identified Value For Test";

            var appFactory = _factory;

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

                context.Samples.RemoveRange(context.Samples);
                context.Samples.AddRange(fakeSampleOne);
                context.SaveChanges();
            }

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

            var serializedSampleToUpdate = 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/Samples/?filters=ExternalId=={fakeSampleOne.ExternalId}")
                            .ConfigureAwait(false);

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

            var getResponse = JsonConvert.DeserializeObject <Response <IEnumerable <SampleDto> > >(getResponseContent);
            var id          = getResponse?.Data.FirstOrDefault().SampleId;

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

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

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

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

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