Skip to content

Commit

Permalink
Updated project and test configuration to run migrations automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed May 17, 2021
1 parent fa065c6 commit 5b83868
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Core.Testing/TestWebHostBuilder.cs
Expand Up @@ -15,7 +15,7 @@ public static IWebHostBuilder Create(Dictionary<string, string> configuration, A
configureServices ??= _ => { };

return new WebHostBuilder()
.UseEnvironment("Tests")
.UseEnvironment("Development")
.UseContentRoot(projectDir)
.UseConfiguration(new ConfigurationBuilder()
.SetBasePath(projectDir)
Expand Down
Expand Up @@ -45,7 +45,6 @@ public CreateMeetingTests(CreateMeetingFixture fixture)
}

[Fact]
[Trait("Category", "Exercise")]
public async Task CreateCommand_ShouldReturn_CreatedStatus_With_MeetingId()
{
var commandResponse = fixture.CommandResponse;
Expand All @@ -58,7 +57,6 @@ public async Task CreateCommand_ShouldReturn_CreatedStatus_With_MeetingId()
}

[Fact]
[Trait("Category", "Exercise")]
public void CreateCommand_ShouldPublish_MeetingCreateEvent()
{
// assert MeetingCreated event was produced to external bus
Expand All @@ -70,7 +68,6 @@ public void CreateCommand_ShouldPublish_MeetingCreateEvent()
}

[Fact]
[Trait("Category", "Exercise")]
public async Task CreateCommand_ShouldUpdateReadModel()
{
// prepare query
Expand Down
Expand Up @@ -53,7 +53,6 @@ public ScheduleMeetingTests(ScheduleMeetingFixture fixture)
}

[Fact]
[Trait("Category", "Exercise")]
public async Task CreateMeeting_ShouldReturn_CreatedStatus_With_MeetingId()
{
var commandResponse = fixture.CreateMeetingCommandResponse.EnsureSuccessStatusCode();
Expand All @@ -64,7 +63,6 @@ public async Task CreateMeeting_ShouldReturn_CreatedStatus_With_MeetingId()
}

[Fact]
[Trait("Category", "Exercise")]
public async Task ScheduleMeeting_ShouldSucceed()
{
var commandResponse = fixture.ScheduleMeetingCommandResponse.EnsureSuccessStatusCode();
Expand All @@ -75,7 +73,6 @@ public async Task ScheduleMeeting_ShouldSucceed()
}

[Fact]
[Trait("Category", "Exercise")]
public async Task ScheduleMeeting_ShouldUpdateReadModel()
{
//send query
Expand Down
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Core.Testing;
using FluentAssertions;
using Microsoft.AspNetCore.Hosting;
using Warehouse.Products.GettingProductDetails;
using Warehouse.Products.GettingProducts;
using Warehouse.Products.RegisteringProduct;
using Xunit;

Expand Down
@@ -1,5 +1,4 @@
using Castle.Core.Configuration;
using Core.WebApi.Middlewares.ExceptionHandling;
using Core.WebApi.Middlewares.ExceptionHandling;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
Expand All @@ -18,8 +17,6 @@ public static IWebHostBuilder Configure(IWebHostBuilder webHostBuilder, string s
.ConfigureServices(services =>
{
services.AddRouting()
.AddAuthorization()
.AddCors()
.AddWarehouseServices()
.AddTransient<DbContextOptions<WarehouseDBContext>>(s =>
{
Expand All @@ -33,15 +30,13 @@ public static IWebHostBuilder Configure(IWebHostBuilder webHostBuilder, string s
})
.Configure(app =>
{
app.UseHttpsRedirection()
.UseMiddleware(typeof(ExceptionHandlingMiddleware))
app.UseMiddleware(typeof(ExceptionHandlingMiddleware))
.UseRouting()
.UseAuthorization()
.UseEndpoints(endpoints => { endpoints.UseWarehouseEndpoints(); });
.UseEndpoints(endpoints => { endpoints.UseWarehouseEndpoints(); })
.ConfigureWarehouse();
// Kids, do not try this at home!
var database = app.ApplicationServices.GetRequiredService<WarehouseDBContext>().Database;
database.Migrate();
database.ExecuteSqlRaw("TRUNCATE TABLE \"Product\"");
});

Expand Down
9 changes: 3 additions & 6 deletions Sample/Warehouse/Warehouse.Api/Program.cs
Expand Up @@ -12,20 +12,17 @@
.ConfigureServices(services =>
{
services.AddRouting()
.AddCors()
.AddAuthorization()
.AddWarehouseServices();
})
.Configure(app =>
{
app.UseHttpsRedirection()
.UseMiddleware(typeof(ExceptionHandlingMiddleware))
app.UseMiddleware(typeof(ExceptionHandlingMiddleware))
.UseRouting()
.UseAuthorization()
.UseEndpoints(endpoints =>
{
endpoints.UseWarehouseEndpoints();
});
})
.ConfigureWarehouse();
});
})
.Build();
Expand Down
16 changes: 15 additions & 1 deletion Sample/Warehouse/Warehouse/Configuration.cs
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Routing;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Warehouse.Products;
Expand All @@ -16,5 +18,17 @@ public static IServiceCollection AddWarehouseServices(this IServiceCollection se

public static IEndpointRouteBuilder UseWarehouseEndpoints(this IEndpointRouteBuilder endpoints)
=> endpoints.UseProductsEndpoints();

public static IApplicationBuilder ConfigureWarehouse(this IApplicationBuilder app)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

if (environment == "Development")
{
app.ApplicationServices.GetRequiredService<WarehouseDBContext>().Database.Migrate();
}

return app;
}
}
}
@@ -1,6 +1,5 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
Expand Down
3 changes: 1 addition & 2 deletions Sample/Warehouse/Warehouse/Products/GettingProducts/Route.cs
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Warehouse.Core.Extensions;
Expand Down
1 change: 0 additions & 1 deletion Sample/Warehouse/Warehouse/Products/Product.cs
@@ -1,5 +1,4 @@
using System;
using System.Text.Json.Serialization;
using Warehouse.Products.Primitives;

namespace Warehouse.Products
Expand Down

0 comments on commit 5b83868

Please sign in to comment.