From c6f3d1da473e3c0ef12c4dc3262dea36c784971a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Bl=C3=A1zquez?= Date: Wed, 14 Aug 2024 22:10:12 +0200 Subject: [PATCH] Fix tests --- Console/Commands/MainCommand.cs | 10 ++-------- Console/FodyWeavers.xml | 3 +++ Console/Utils.cs | 20 +++++++++++++++++++- Tests/PlayerTests.cs | 13 +++++-------- 4 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 Console/FodyWeavers.xml diff --git a/Console/Commands/MainCommand.cs b/Console/Commands/MainCommand.cs index 2ad06b9..1130dfc 100644 --- a/Console/Commands/MainCommand.cs +++ b/Console/Commands/MainCommand.cs @@ -32,14 +32,8 @@ public async ValueTask ExecuteAsync(IConsole console) Utils.ConfigurePlatformDependencies(); await console.Output.WriteLineAsync("Ignore any warnings above this message"); await console.Output.WriteLineAsync("[PortAudio] Initialized corretly"); - - var upgrader = DeployChanges - .To.SQLiteDatabase(connectionString) - .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly()) - .LogToConsole() - .Build(); - - var result = upgrader.PerformUpgrade(); + Utils.PerformMigrations(connectionString); + await console.Output.WriteLineAsync("[Db] Initialized corretly"); var top = new Toplevel(); diff --git a/Console/FodyWeavers.xml b/Console/FodyWeavers.xml new file mode 100644 index 0000000..6ef7058 --- /dev/null +++ b/Console/FodyWeavers.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Console/Utils.cs b/Console/Utils.cs index 79993d5..c56852e 100644 --- a/Console/Utils.cs +++ b/Console/Utils.cs @@ -1,4 +1,6 @@ -using PortAudioSharp; +using System.Reflection; +using DbUp; +using PortAudioSharp; using Terminal.Gui; namespace Console; @@ -13,6 +15,22 @@ public static void ConfigurePlatformDependencies() PortAudio.Initialize(); } + public static void PerformMigrations(string connectionString) + { + var upgrader = DeployChanges + .To.SQLiteDatabase(connectionString) + .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly()) + .LogToConsole() + .Build(); + + var result = upgrader.PerformUpgrade(); + + if (!result.Successful) + { + throw result.Error; + } + } + public static string? ShowInputDialog(string title, string prompt, ColorScheme colorScheme) { if (_isShowing) diff --git a/Tests/PlayerTests.cs b/Tests/PlayerTests.cs index ff42c7b..5c40956 100644 --- a/Tests/PlayerTests.cs +++ b/Tests/PlayerTests.cs @@ -4,6 +4,7 @@ using Console.Database; using Console.Repositories; using FluentAssertions; +using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using YoutubeExplode; @@ -13,17 +14,13 @@ public class PlayerTests : IAsyncDisposable { private readonly PlayerController _player; - private DbContextOptions CreateInMemoryOptions() => - new DbContextOptionsBuilder() - .UseInMemoryDatabase(Guid.NewGuid().ToString()) // Use a unique database name - .Options; - public PlayerTests() { - var options = CreateInMemoryOptions(); - var context = new MyDbContext(options); - var settings = new SettingsRepository(context); + const string connectionString = "Data Source=test.db"; + Utils.PerformMigrations(connectionString); Utils.ConfigurePlatformDependencies(); + var connection = new SqliteConnection(connectionString); + var settings = new SettingsRepository(connection); settings.InitializeAsync().GetAwaiter().GetResult(); _player = new(new YoutubeClient(), settings) { Volume = 0 }; }