Skip to content

Commit

Permalink
Create and initialize the TestStore inside InitializeAsync
Browse files Browse the repository at this point in the history
Before this commit, it was impossible to create a concrete implementation of SharedStoreFixtureBase that requires a `TestStoreFactory` which is initialized asynchronously (the purpose of `IAsyncLifetime`).

After this commit, it becomes possible because the `TestStoreFactory` and the `StoreName` properties are not accessed in the constructor anymore.
  • Loading branch information
0xced committed Sep 8, 2020
1 parent c3065ff commit cbe5fca
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
20 changes: 12 additions & 8 deletions test/EFCore.Specification.Tests/SharedStoreFixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ public abstract class SharedStoreFixtureBase<TContext> : FixtureBase, IDisposabl
where TContext : DbContext
{
protected virtual Type ContextType { get; } = typeof(TContext);
public IServiceProvider ServiceProvider { get; }

private IServiceProvider _serviceProvider;
public IServiceProvider ServiceProvider
=> _serviceProvider ?? throw new InvalidOperationException($"You must override the {nameof(InitializeAsync)} method and call `await base.{nameof(InitializeAsync)}();`. At this point the {nameof(ServiceProvider)} property will be available.");

protected abstract string StoreName { get; }
protected abstract ITestStoreFactory TestStoreFactory { get; }
public TestStore TestStore { get; }

private TestStore _testStore;
public TestStore TestStore
=> _testStore ?? throw new InvalidOperationException($"You must override the {nameof(InitializeAsync)} method and call `await base.{nameof(InitializeAsync)}();`. At this point the {nameof(TestStore)} property will be available.");

protected virtual bool UsePooling
=> true;
Expand All @@ -35,9 +42,9 @@ private IDbContextPool ContextPool
public ListLoggerFactory ListLoggerFactory
=> _listLoggerFactory ??= (ListLoggerFactory)ServiceProvider.GetRequiredService<ILoggerFactory>();

protected SharedStoreFixtureBase()
public virtual Task InitializeAsync()
{
TestStore = TestStoreFactory.GetOrCreate(StoreName);
_testStore = TestStoreFactory.GetOrCreate(StoreName);

var services = AddServices(TestStoreFactory.AddProviderServices(new ServiceCollection()));
if (UsePooling)
Expand All @@ -53,13 +60,10 @@ protected SharedStoreFixtureBase()
ServiceLifetime.Singleton);
}

ServiceProvider = services.BuildServiceProvider(validateScopes: true);
_serviceProvider = services.BuildServiceProvider(validateScopes: true);

TestStore.Initialize(ServiceProvider, CreateContext, c => Seed((TContext)c), c => Clean(c));
}

public virtual Task InitializeAsync()
{
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1399,9 +1399,10 @@ public class MigrationsInfrastructureSqlServerFixture : MigrationsInfrastructure
protected override ITestStoreFactory TestStoreFactory
=> SqlServerTestStoreFactory.Instance;

public MigrationsInfrastructureSqlServerFixture()
public override async Task InitializeAsync()
{
((SqlServerTestStore)TestStore).ExecuteNonQuery(
await base.InitializeAsync();
await ((SqlServerTestStore)TestStore).ExecuteNonQueryAsync(
@"USE master
IF EXISTS(select * from sys.databases where name='TransactionSuppressed')
DROP DATABASE TransactionSuppressed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Internal;
Expand Down Expand Up @@ -634,10 +635,10 @@ Name nvarchar(100) NOT NULL,
);
EXECUTE sys.sp_addextendedproperty @name = N'MS_Description', @value = N'Blog table comment.
On multiple lines.',
@level0type = N'SCHEMA', @level0name = 'dbo',
@level0type = N'SCHEMA', @level0name = 'dbo',
@level1type = N'TABLE', @level1name = 'Blogs';
EXECUTE sys.sp_addextendedproperty @name = N'MS_Description', @value = N'Blog.Id column comment.',
@level0type = N'SCHEMA', @level0name = 'dbo',
@level0type = N'SCHEMA', @level0name = 'dbo',
@level1type = N'TABLE', @level1name = 'Blogs',
@level2type = N'COLUMN', @level2name = 'Id';
",
Expand Down Expand Up @@ -2303,10 +2304,11 @@ protected override ITestStoreFactory TestStoreFactory
public new SqlServerTestStore TestStore
=> (SqlServerTestStore)base.TestStore;

public SqlServerDatabaseModelFixture()
public override async Task InitializeAsync()
{
TestStore.ExecuteNonQuery("CREATE SCHEMA db2");
TestStore.ExecuteNonQuery("CREATE SCHEMA [db.2]");
await base.InitializeAsync();
await TestStore.ExecuteNonQueryAsync("CREATE SCHEMA db2");
await TestStore.ExecuteNonQueryAsync("CREATE SCHEMA [db.2]");
}

protected override bool ShouldLogCategory(string logCategory)
Expand Down

0 comments on commit cbe5fca

Please sign in to comment.