Esempio n. 1
0
        public static async Task Main(string[] args)
        {
            IWebHost host = CreateWebHostBuilder(args: args).Build();

            using (IServiceScope scope = host.Services.CreateScope()) {
                IServiceProvider services = scope.ServiceProvider;

                try {
                    var currentParticipantService = (CurrentParticipantService)services.GetRequiredService <ICurrentParticipantService>();
                    currentParticipantService.SetNoHttpContext();

                    PokerTimeDbContext dbContext = services.GetRequiredService <PokerTimeDbContext>();
                    dbContext.Initialize();

                    IMediator mediator = services.GetRequiredService <IMediator>();
                    await mediator.Send(new SeedBaseDataCommand());
                }
                catch (Exception ex) {
                    ILogger logger = scope.ServiceProvider.GetRequiredService <ILoggerFactory>().CreateLogger(nameof(Program));
                    logger.LogError(ex, "An error occurred while migrating or initializing the database.");
                }
            }

            await host.RunAsync();
        }
Esempio n. 2
0
        public static void Destroy(PokerTimeDbContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Database.EnsureDeleted();

            context.Dispose();
        }
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            // Find free TCP port to configure Kestel on
            IPEndPoint endPoint;

            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
                socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
                socket.Listen(1);
                endPoint = (IPEndPoint)socket.LocalEndPoint;
            }

            // Configure testing to use Kestel and test services
            builder
            .ConfigureLogging(lb => {
                lb.SetMinimumLevel(LogLevel.Trace);
                lb.AddProvider(new TestContextLoggerProvider());

                string logFileName = (TestContext.CurrentContext?.Test.ClassName ?? "test-log") + ".log";
                lb.AddFile(Path.Join(Paths.TestArtifactDir, logFileName));
            })
            .ConfigureTestServices(services => {
                // Add a database context using an in-memory database for testing.
                services.RemoveAll <PokerTimeDbContext>();

                services.AddScoped(sp => {
                    DbContextOptions <PokerTimeDbContext> options = new DbContextOptionsBuilder <PokerTimeDbContext>()
                                                                    .UseSqlite(this.ConnectionString)
                                                                    .Options;

                    var context = new PokerTimeDbContext(options);

                    return(context);
                });
                services.ChainInterfaceImplementation <IPokerTimeDbContext, PokerTimeDbContext>();
                services.ChainInterfaceImplementation <IPokerTimeDbContextFactory, PokerTimeDbContext>();

                services.Configure <ServerOptions>(s => {
                    s.BaseUrl = "http://localhost:" + endPoint.Port + "/";
                });
            })
            .UseKestrel(k => k.Listen(endPoint))
            .UseEnvironment(environment: "Test");
        }
Esempio n. 4
0
        public static PokerTimeDbContext Create()
        {
            DbContextOptions <PokerTimeDbContext> options = new DbContextOptionsBuilder <PokerTimeDbContext>()
                                                            .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                                            .Options;

            var context = new PokerTimeDbContext(options);

            context.Database.EnsureCreated();

            new SeedBaseDataCommandHandler(context).Handle(new SeedBaseDataCommand(), CancellationToken.None).
            ConfigureAwait(false).
            GetAwaiter().
            GetResult();

            context.SaveChanges();

            return(context);
        }
Esempio n. 5
0
 public CommandTestBase()
 {
     this.Context = ReturnDbContextFactory.Create();
 }