11using API . JobRuntime . Handlers ;
2+ using API . Schema . ActionsContext ;
23using API . Schema . SeriesContext ;
34using Microsoft . EntityFrameworkCore ;
45using Xunit ;
@@ -22,13 +23,40 @@ public async Task InitializeAsync()
2223 {
2324 if ( ! await _postgres . IsReachableAsync ( ) ) return ;
2425 _dbName = await _postgres . CreateDatabaseAsync ( ) ;
26+ string cs = _postgres . GetConnectionString ( _dbName ) ;
27+ Directory . CreateDirectory ( _libDir ) ;
28+
29+ await MigrateAsync < SeriesContext > ( cs ) ;
30+ await MigrateAsync < global ::API . Schema . JobsContext . JobsContext > ( cs ) ;
31+ await MigrateAsync < ActionsContext > ( cs ) ;
32+
33+ var opts = new DbContextOptionsBuilder < SeriesContext > ( ) . UseNpgsql ( cs ) . Options ;
34+ await using ( var ctx = new SeriesContext ( opts ) )
35+ {
36+ var library = new FileLibrary ( _libDir , "Lib" ) ;
37+ ctx . FileLibraries . Add ( library ) ;
38+ var manga = new Series ( "Test" , "" , "http://x/c.jpg" , SeriesReleaseStatus . Continuing , [ ] , [ ] , [ ] , [ ] , library ) ;
39+ ctx . Series . Add ( manga ) ;
40+ var chapter = new Chapter ( manga , "1" , null , null ) ;
41+ ctx . Chapters . Add ( chapter ) ;
42+ var sourceId = new SourceId < Chapter > ( chapter , "StubConnector" , "site-id-1" , "http://stub.test/1" , true ) ;
43+ ctx . MangaConnectorToChapter . Add ( sourceId ) ;
44+ await ctx . SaveChangesAsync ( ) ;
45+ }
46+
2547 _app = new KenkuApplicationFactory
2648 {
27- PostgresConnectionString = _postgres . GetConnectionString ( _dbName ) ,
49+ PostgresConnectionString = cs ,
2850 RunStartup = true ,
2951 DispatcherCaps = ( 0 , 0 ) ,
3052 } ;
31- Directory . CreateDirectory ( _libDir ) ;
53+ }
54+
55+ private static async Task MigrateAsync < TContext > ( string cs ) where TContext : DbContext
56+ {
57+ var opts = new DbContextOptionsBuilder < TContext > ( ) . UseNpgsql ( cs ) . Options ;
58+ await using var ctx = ( TContext ) Activator . CreateInstance ( typeof ( TContext ) , opts ) ! ;
59+ await ctx . Database . MigrateAsync ( ) ;
3260 }
3361
3462 public async Task DisposeAsync ( )
@@ -44,20 +72,6 @@ public async Task DownloadReconciler_OnFirstTick_EnqueuesDownloadJobForRequested
4472 {
4573 if ( _app is null ) return ; // skip: Postgres not available
4674
47- await _app . WithSeriesContext ( async ctx =>
48- {
49- var library = new FileLibrary ( _libDir , "Lib" ) ;
50- ctx . FileLibraries . Add ( library ) ;
51- var manga = new Series ( "Test" , "" , "http://x/c.jpg" , SeriesReleaseStatus . Continuing , [ ] , [ ] , [ ] , [ ] , library ) ;
52- ctx . Series . Add ( manga ) ;
53- var chapter = new Chapter ( manga , "1" , null , null ) ;
54- ctx . Chapters . Add ( chapter ) ;
55- var sourceId = new SourceId < Chapter > ( chapter , "StubConnector" , "site-id-1" , "http://stub.test/1" , true ) ;
56- ctx . MangaConnectorToChapter . Add ( sourceId ) ;
57- await ctx . SaveChangesAsync ( ) ;
58- return 0 ;
59- } ) ;
60-
6175 bool appeared = false ;
6276 var deadline = DateTime . UtcNow . AddSeconds ( 10 ) ;
6377 while ( DateTime . UtcNow < deadline )
0 commit comments