public async Task DeleteCustomer_AcceptedTestAsync() { var queryCustomerById = @"SELECT Id, AddressId, Name, DateCreated FROM dbo.Customer c WHERE c.Id = @Id"; List <Customer> customerModel = CustomerMock.CustomerModelFaker.Generate(1); MockRepositoryBuilder.GetMockDbConnection().SetupDapperAsync(c => c.QueryAsync <Customer>( queryCustomerById, It.IsAny <object>(), null, null, null)).ReturnsAsync(customerModel); MockAuthorizationFactory.AddAdminHeaders(_httpClient); var customer = CustomerMock.CustomerViewModelFaker.Generate(); customer.Id = customerModel[0].Id; var response = await _httpClient.DeleteAsync($"/api/v1/customers/{customerModel[0].Id}"); Assert.Equal(HttpStatusCode.Accepted, response.StatusCode); }
public void Crud_EntityTest() { var customer = CustomerMock.CustomerModelFaker.Generate(); _configurationMock.Setup(x => x.GetSection(It.IsAny <string>())) .Returns(new Mock <IConfigurationSection>().Object); var entityContext = new EntityContext(_entityOptions); var unitOfWork = new UnitOfWork(entityContext); var dapperContext = new DapperContext(MockRepositoryBuilder.GetMockDbConnection().Object); var customerRepository = new CustomerRepository(entityContext, dapperContext); customerRepository.Add(customer); var IsSaveCustomer = unitOfWork.Commit(); customerRepository.Update(customer); var IsUpdateCustomer = unitOfWork.Commit(); customerRepository.Remove(customer); var IsRemoverCustomer = unitOfWork.Commit(); Assert.Equal(1, IsSaveCustomer); Assert.Equal(1, IsUpdateCustomer); Assert.Equal(1, IsRemoverCustomer); }
private void Arrange() { var queryCustomers = @"SELECT c.Id AS id, a.Id AS addressId, c.Name AS name, c.DateCreated AS dateCreated, a.CEP AS cep FROM dbo.Customer c INNER JOIN dbo.Address a ON c.addressId = a.Id"; List <CustomerAddress> customers = CustomerMock.CustomerAddressModelFaker.Generate(6); MockRepositoryBuilder.GetMockDbConnection().SetupDapperAsync(c => c.QueryAsync <CustomerAddress>( queryCustomers, null, null, null, null)).ReturnsAsync(customers); var queryCustomerAddressByName = @"SELECT c.Id AS id, a.Id AS addressId, c.Name AS name, c.DateCreated AS dateCreated, a.CEP AS cep FROM dbo.Customer c INNER JOIN dbo.Address a ON c.addressId = a.Id WHERE c.Name = @Name"; MockRepositoryBuilder.GetMockDbConnection().SetupDapperAsync(c => c.QueryAsync <CustomerAddress>( queryCustomerAddressByName, It.IsAny <object>(), null, null, null)).ReturnsAsync(() => new List <CustomerAddress>()); var queryCustomerById = @"SELECT Id, AddressId, Name, DateCreated FROM dbo.Customer c WHERE c.Id = @Id"; MockRepositoryBuilder.GetMockDbConnection().SetupDapperAsync(c => c.QueryAsync <Customer>( queryCustomerById, It.IsAny <object>(), null, null, null)).ReturnsAsync(new List <Customer>()); }
public async Task PostCustomer_BadRequesTestAsync() { var queryCustomerAddressByName = @"SELECT c.Id AS id, a.Id AS addressId, c.Name AS name, c.DateCreated AS dateCreated, a.CEP AS cep FROM dbo.Customer c INNER JOIN dbo.Address a ON c.addressId = a.Id WHERE c.Name = @Name"; List <CustomerAddress> customerAddressByName = CustomerMock.CustomerAddressModelFaker.Generate(1); MockRepositoryBuilder.GetMockDbConnection().SetupDapperAsync(c => c.QueryAsync <CustomerAddress>( queryCustomerAddressByName, It.IsAny <object>(), null, null, null)).ReturnsAsync(customerAddressByName); MockAuthorizationFactory.AddAdminHeaders(_httpClient); var customer = CustomerMock.CustomerViewModelFaker.Generate(); customer.Id = customerAddressByName[0].Id; customer.Name = customerAddressByName[0].Name; HttpRequestMessage request = new HttpRequestMessage { RequestUri = new System.Uri($"{this._httpClient.BaseAddress.OriginalString}/api/v1/customers"), Method = new HttpMethod("Post"), Headers = { { "User-Agent", "csharp" } }, Content = new StringContent(JsonConvert.SerializeObject(customer), Encoding.UTF8, "application/json") }; var response = await this._httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead); string message = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }
private static IServiceProvider InitializeServiceProvider(IServiceCollection services) { services .AddEntityFrameworkInMemoryDatabase() .AddDbContext <EntityContext>( options => { options.UseInMemoryDatabase("InMemoryDbForTesting"); // .ConfigureWarnings( // x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning) // ); // options.UseInternalServiceProvider(services.BuildServiceProvider()); } ); services.AddSingleton <DapperContext>(sp => { return(new DapperContext(MockRepositoryBuilder.GetMockDbConnection().Object)); }); services.PostConfigure <JwtBearerOptions>( JwtBearerDefaults.AuthenticationScheme, options => { options.TokenValidationParameters = new TokenValidationParameters { SignatureValidator = (token, parameters) => new JwtSecurityToken(token), ValidateIssuer = false, ValidateLifetime = false, ValidateIssuerSigningKey = false, ValidateAudience = false, }; options.Configuration = new OpenIdConnectConfiguration(); }); return(services.BuildServiceProvider());; }
public async Task GetById_OKTestAsync() { var queryCustomerAddressById = @"SELECT c.Id, a.Id AS AddressId, c.Name, c.DateCreated, a.CEP FROM dbo.Customer c INNER JOIN dbo.Address a ON c.AddressId = a.Id WHERE c.Id = @Id"; List <CustomerAddress> customerAddressById = CustomerMock.CustomerAddressModelFaker.Generate(1); MockRepositoryBuilder.GetMockDbConnection().SetupDapperAsync(c => c.QueryAsync <CustomerAddress>( queryCustomerAddressById, It.IsAny <object>(), null, null, null)).ReturnsAsync(customerAddressById); MockAuthorizationFactory.AddAdminHeaders(_httpClient); var response = await _httpClient.GetAsync("/api/v1/customers/1"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }