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:

```csharp
public class MySharedStoreFixture<TContext> : SharedStoreFixtureBase<TContext> where TContext : DbContext
{
    private string? _storeName;
    private ITestStoreFactory? _testStoreFactory;

    protected override string StoreName => _storeName ?? throw new InvalidOperationException($"The {nameof(StoreName)} property is only available after {nameof(InitializeAsync)} has executed.");
    protected override ITestStoreFactory TestStoreFactory => _testStoreFactory ?? throw new InvalidOperationException($"The {nameof(TestStoreFactory)} property is only available after {nameof(InitializeAsync)} has executed.");

    public override async Task InitializeAsync()
    {
        var someInfo = await GetSomeInfoAsync();

        _storeName = someInfo.Name;
        _testStoreFactory = new MyTestStoreFactory(someInfo);

        await base.InitializeAsync();
    }
}
```
  • Loading branch information
0xced committed Sep 8, 2020
1 parent 9ad82d2 commit 01dc10a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 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($"The {nameof(ServiceProvider)} is only available after {nameof(InitializeAsync)} has executed.");

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

private TestStore _testStore;
public TestStore TestStore
=> _testStore ?? throw new InvalidOperationException($"The {nameof(TestStore)} is only available after {nameof(InitializeAsync)} has executed.");

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

0 comments on commit 01dc10a

Please sign in to comment.