using System.Data; using System.Transactions; using Dapper; using Microsoft.Data.SqlClient; using PlotLine.Data; using PlotLine.Services; internal static class NarrativeFocusDatabaseTests { private sealed record Fixture(int UserID,int ProjectID,int BookID,int ChapterID,int SceneID); private sealed class Factory(string connectionString):ISqlConnectionFactory {public IDbConnection CreateConnection()=>new SqlConnection(connectionString);} public static async Task Run(string environmentFile) { var line=File.ReadLines(environmentFile).Single(x=>x.StartsWith("ConnectionStrings__DefaultConnection=")); var value=line[(line.IndexOf('=')+1)..].Trim(); if(value.Length>1&&((value[0]=='"'&&value[^1]=='"')||(value[0]=='\''&&value[^1]=='\'')))value=value[1..^1]; var builder=new SqlConnectionStringBuilder(value); if(builder.InitialCatalog!="PlotDirector_Development")throw new InvalidOperationException("Refusing a non-Development database"); var sql=File.ReadAllText(Path.Combine("PlotLine.Tests","Sql","NarrativeFocusRepositoryFixture.sql")); Fixture fixture; // Sequential pooled connections enlist in one ambient transaction. Never Complete it. using(var rollback=new TransactionScope(TransactionScopeOption.RequiresNew,new TransactionOptions{IsolationLevel=System.Transactions.IsolationLevel.ReadCommitted,Timeout=TimeSpan.FromMinutes(2)},TransactionScopeAsyncFlowOption.Enabled)) { using(var db=new SqlConnection(builder.ConnectionString))fixture=await db.QuerySingleAsync(sql,new{ExistingBook=(int?)null}); var factory=new Factory(builder.ConnectionString);var focus=new NarrativeFocus(factory);var objectives=new WriterObjectiveRepository(factory); var before=(await objectives.Read(fixture.UserID,fixture.BookID)).Objective!; var state=await focus.Read(fixture.UserID,fixture.ProjectID,fixture.BookID); if(state.SceneID!=fixture.SceneID||state.Positions.Count!=1||state.Recent.Single().SceneID!=fixture.SceneID)throw new Exception("Repository seed/position/history mapping failed"); state=await focus.Set(fixture.UserID,fixture.ProjectID,fixture.BookID,fixture.ChapterID,null,state.Version); if(state.SceneID!=null||state.ChapterID!=fixture.ChapterID)throw new Exception("Repository chapter navigation failed"); if(await focus.Open(fixture.UserID,null,fixture.SceneID,fixture.ProjectID,fixture.BookID+1)!=null)throw new Exception("Repository accepted an explicit destination in the wrong book"); state=(await focus.Open(fixture.UserID,null,fixture.SceneID,fixture.ProjectID,fixture.BookID))!; if(state.SceneID!=fixture.SceneID||state.Recent.Count!=2)throw new Exception("Repository explicit destination/history deduplication failed"); var after=(await objectives.Read(fixture.UserID,fixture.BookID)).Objective!; if(before.Activity!=after.Activity||before.Intention!=after.Intention||before.ChapterID!=after.ChapterID||before.SceneID!=after.SceneID)throw new Exception("Navigation altered an objective"); } using(var db=new SqlConnection(builder.ConnectionString)) if(await db.QuerySingleAsync(sql,new{ExistingBook=fixture.BookID})!=0)throw new Exception("Repository fixture did not roll back"); Console.WriteLine("PASS Development Dapper repository: seed and mapped results, Set/Open, scope validation, history, independent objectives and fixture rollback"); } }