@@ -12,24 +12,32 @@ namespace CommunityToolkit.Datasync.Server.Test.Live;
12
12
13
13
[ ExcludeFromCodeCoverage ]
14
14
[ Collection ( "LiveTestsCollection" ) ]
15
- public class PgSQL_Controller_Tests : LiveControllerTests < PgEntityMovie >
15
+ public class PgSQL_Controller_Tests ( DatabaseFixture fixture , ITestOutputHelper output ) : LiveControllerTests < PgEntityMovie > , IAsyncLifetime
16
16
{
17
17
#region Setup
18
- private readonly DatabaseFixture _fixture ;
19
18
private readonly Random random = new ( ) ;
20
- private readonly string connectionString ;
21
- private readonly List < PgEntityMovie > movies ;
19
+ private readonly string connectionString = Environment . GetEnvironmentVariable ( "DATASYNC_PGSQL_CONNECTIONSTRING" ) ;
20
+ private List < PgEntityMovie > movies = [ ] ;
22
21
23
- public PgSQL_Controller_Tests ( DatabaseFixture fixture , ITestOutputHelper output ) : base ( )
22
+ public async Task InitializeAsync ( )
24
23
{
25
- this . _fixture = fixture ;
26
- this . connectionString = Environment . GetEnvironmentVariable ( "DATASYNC_PGSQL_CONNECTIONSTRING" ) ;
27
24
if ( ! string . IsNullOrEmpty ( this . connectionString ) )
28
25
{
29
- output . WriteLine ( $ "PgIsInitialized = { this . _fixture . PgIsInitialized } ") ;
30
- Context = PgDbContext . CreateContext ( this . connectionString , output , clearEntities : ! this . _fixture . PgIsInitialized ) ;
26
+ // Note: we don't clear entities on every run to speed up the test runs. This can only be done because
27
+ // the tests are read-only (associated with the query and get capabilities). If the test being run writes
28
+ // to the database then change clearEntities to true.
29
+ output . WriteLine ( $ "PgIsInitialized = { fixture . PgIsInitialized } ") ;
30
+ Context = await PgDbContext . CreateContextAsync ( this . connectionString , output , clearEntities : ! fixture . PgIsInitialized ) ;
31
31
this . movies = Context . Movies . AsNoTracking ( ) . ToList ( ) ;
32
- this . _fixture . PgIsInitialized = true ;
32
+ fixture . PgIsInitialized = true ;
33
+ }
34
+ }
35
+
36
+ public async Task DisposeAsync ( )
37
+ {
38
+ if ( Context is not null )
39
+ {
40
+ await Context . DisposeAsync ( ) ;
33
41
}
34
42
}
35
43
@@ -39,11 +47,11 @@ public PgSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output)
39
47
40
48
protected override bool CanRunLiveTests ( ) => ! string . IsNullOrEmpty ( this . connectionString ) ;
41
49
42
- protected override Task < PgEntityMovie > GetEntityAsync ( string id )
43
- => Task . FromResult ( Context . Movies . AsNoTracking ( ) . SingleOrDefault ( m => m . Id == id ) ) ;
50
+ protected override async Task < PgEntityMovie > GetEntityAsync ( string id )
51
+ => await Context . Movies . AsNoTracking ( ) . SingleOrDefaultAsync ( m => m . Id == id ) ;
44
52
45
- protected override Task < int > GetEntityCountAsync ( )
46
- => Task . FromResult ( Context . Movies . Count ( ) ) ;
53
+ protected override async Task < int > GetEntityCountAsync ( )
54
+ => await Context . Movies . CountAsync ( ) ;
47
55
48
56
protected override Task < IRepository < PgEntityMovie > > GetPopulatedRepositoryAsync ( )
49
57
=> Task . FromResult < IRepository < PgEntityMovie > > ( new EntityTableRepository < PgEntityMovie > ( Context ) ) ;
0 commit comments