|
| 1 | +// OpenTweak - PC Game Optimization Tool |
| 2 | +// Copyright 2024-2025 OpenTweak Contributors |
| 3 | +// Licensed under PolyForm Shield License 1.0.0 |
| 4 | +// See LICENSE.md for full terms. |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using OpenTweak.Models; |
| 10 | +using OpenTweak.Services; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace OpenTweak.Tests.Services; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Integration tests for DatabaseService using a temporary database file. |
| 17 | +/// Tests actual LiteDB operations rather than mocking. |
| 18 | +/// </summary> |
| 19 | +public class DatabaseServiceTests : IDisposable |
| 20 | +{ |
| 21 | + private readonly DatabaseService _service; |
| 22 | + private readonly string _tempDbPath; |
| 23 | + |
| 24 | + public DatabaseServiceTests() |
| 25 | + { |
| 26 | + _tempDbPath = Path.Combine(Path.GetTempPath(), $"opentweak_test_{Guid.NewGuid()}.db"); |
| 27 | + _service = new DatabaseService(_tempDbPath); |
| 28 | + } |
| 29 | + |
| 30 | + public void Dispose() |
| 31 | + { |
| 32 | + _service.Dispose(); |
| 33 | + |
| 34 | + // Clean up test database file |
| 35 | + if (File.Exists(_tempDbPath)) |
| 36 | + { |
| 37 | + try { File.Delete(_tempDbPath); } catch { } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + #region Game Tests |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void UpsertGame_InsertsNewGame() |
| 45 | + { |
| 46 | + var game = CreateTestGame(); |
| 47 | + |
| 48 | + _service.UpsertGame(game); |
| 49 | + var retrieved = _service.GetGame(game.Id); |
| 50 | + |
| 51 | + Assert.NotNull(retrieved); |
| 52 | + Assert.Equal(game.Name, retrieved!.Name); |
| 53 | + Assert.Equal(game.AppId, retrieved.AppId); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void UpsertGame_UpdatesExistingGame() |
| 58 | + { |
| 59 | + var game = CreateTestGame(); |
| 60 | + _service.UpsertGame(game); |
| 61 | + |
| 62 | + game.Name = "Updated Game Name"; |
| 63 | + _service.UpsertGame(game); |
| 64 | + |
| 65 | + var retrieved = _service.GetGame(game.Id); |
| 66 | + Assert.Equal("Updated Game Name", retrieved!.Name); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void GetAllGames_ReturnsAllInsertedGames() |
| 71 | + { |
| 72 | + var games = Enumerable.Range(1, 5).Select(i => CreateTestGame($"Game {i}")).ToList(); |
| 73 | + _service.UpsertGames(games); |
| 74 | + |
| 75 | + var retrieved = _service.GetAllGames().ToList(); |
| 76 | + |
| 77 | + Assert.Equal(5, retrieved.Count); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void GetGameByAppId_FindsCorrectGame() |
| 82 | + { |
| 83 | + var game = CreateTestGame(); |
| 84 | + game.AppId = "123456"; |
| 85 | + game.LauncherType = LauncherType.Steam; |
| 86 | + _service.UpsertGame(game); |
| 87 | + |
| 88 | + var retrieved = _service.GetGameByAppId("123456", LauncherType.Steam); |
| 89 | + |
| 90 | + Assert.NotNull(retrieved); |
| 91 | + Assert.Equal(game.Id, retrieved!.Id); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void GetGameByAppId_ReturnsNullForWrongLauncher() |
| 96 | + { |
| 97 | + var game = CreateTestGame(); |
| 98 | + game.AppId = "123456"; |
| 99 | + game.LauncherType = LauncherType.Steam; |
| 100 | + _service.UpsertGame(game); |
| 101 | + |
| 102 | + var retrieved = _service.GetGameByAppId("123456", LauncherType.Epic); |
| 103 | + |
| 104 | + Assert.Null(retrieved); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void DeleteGame_RemovesGame() |
| 109 | + { |
| 110 | + var game = CreateTestGame(); |
| 111 | + _service.UpsertGame(game); |
| 112 | + |
| 113 | + var deleted = _service.DeleteGame(game.Id); |
| 114 | + var retrieved = _service.GetGame(game.Id); |
| 115 | + |
| 116 | + Assert.True(deleted); |
| 117 | + Assert.Null(retrieved); |
| 118 | + } |
| 119 | + |
| 120 | + #endregion |
| 121 | + |
| 122 | + #region Recipe Tests |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void UpsertRecipe_InsertsNewRecipe() |
| 126 | + { |
| 127 | + var recipe = CreateTestRecipe(); |
| 128 | + |
| 129 | + _service.UpsertRecipe(recipe); |
| 130 | + var retrieved = _service.GetRecipesForGame(recipe.GameId).FirstOrDefault(); |
| 131 | + |
| 132 | + Assert.NotNull(retrieved); |
| 133 | + Assert.Equal(recipe.Description, retrieved!.Description); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public void GetRecipesForGame_ReturnsOnlyMatchingRecipes() |
| 138 | + { |
| 139 | + var gameId1 = Guid.NewGuid(); |
| 140 | + var gameId2 = Guid.NewGuid(); |
| 141 | + |
| 142 | + _service.UpsertRecipe(CreateTestRecipe("Recipe 1", gameId1)); |
| 143 | + _service.UpsertRecipe(CreateTestRecipe("Recipe 2", gameId1)); |
| 144 | + _service.UpsertRecipe(CreateTestRecipe("Recipe 3", gameId2)); |
| 145 | + |
| 146 | + var recipes = _service.GetRecipesForGame(gameId1).ToList(); |
| 147 | + |
| 148 | + Assert.Equal(2, recipes.Count); |
| 149 | + Assert.All(recipes, r => Assert.Equal(gameId1, r.GameId)); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public void DeleteRecipesForGame_RemovesAllRecipesForGame() |
| 154 | + { |
| 155 | + var gameId = Guid.NewGuid(); |
| 156 | + _service.UpsertRecipe(CreateTestRecipe("Recipe 1", gameId)); |
| 157 | + _service.UpsertRecipe(CreateTestRecipe("Recipe 2", gameId)); |
| 158 | + |
| 159 | + _service.DeleteRecipesForGame(gameId); |
| 160 | + |
| 161 | + var recipes = _service.GetRecipesForGame(gameId).ToList(); |
| 162 | + Assert.Empty(recipes); |
| 163 | + } |
| 164 | + |
| 165 | + #endregion |
| 166 | + |
| 167 | + #region Snapshot Tests |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public void UpsertSnapshot_InsertsNewSnapshot() |
| 171 | + { |
| 172 | + var snapshot = CreateTestSnapshot(); |
| 173 | + |
| 174 | + _service.UpsertSnapshot(snapshot); |
| 175 | + var retrieved = _service.GetSnapshotsForGame(snapshot.GameId).FirstOrDefault(); |
| 176 | + |
| 177 | + Assert.NotNull(retrieved); |
| 178 | + Assert.Equal(snapshot.Description, retrieved!.Description); |
| 179 | + } |
| 180 | + |
| 181 | + [Fact] |
| 182 | + public void GetSnapshotsForGame_ReturnsSnapshotsInOrder() |
| 183 | + { |
| 184 | + var gameId = Guid.NewGuid(); |
| 185 | + var older = CreateTestSnapshot(gameId); |
| 186 | + older.Timestamp = DateTime.UtcNow.AddDays(-1); |
| 187 | + var newer = CreateTestSnapshot(gameId); |
| 188 | + newer.Timestamp = DateTime.UtcNow; |
| 189 | + |
| 190 | + _service.UpsertSnapshot(older); |
| 191 | + _service.UpsertSnapshot(newer); |
| 192 | + |
| 193 | + var snapshots = _service.GetSnapshotsForGame(gameId).ToList(); |
| 194 | + |
| 195 | + Assert.Equal(2, snapshots.Count); |
| 196 | + } |
| 197 | + |
| 198 | + [Fact] |
| 199 | + public void DeleteSnapshot_RemovesSnapshot() |
| 200 | + { |
| 201 | + var snapshot = CreateTestSnapshot(); |
| 202 | + _service.UpsertSnapshot(snapshot); |
| 203 | + |
| 204 | + var deleted = _service.DeleteSnapshot(snapshot.Id); |
| 205 | + var retrieved = _service.GetSnapshotsForGame(snapshot.GameId).ToList(); |
| 206 | + |
| 207 | + Assert.True(deleted); |
| 208 | + Assert.Empty(retrieved); |
| 209 | + } |
| 210 | + |
| 211 | + #endregion |
| 212 | + |
| 213 | + #region Helper Methods |
| 214 | + |
| 215 | + private static Game CreateTestGame(string name = "Test Game") |
| 216 | + { |
| 217 | + return new Game |
| 218 | + { |
| 219 | + Id = Guid.NewGuid(), |
| 220 | + Name = name, |
| 221 | + AppId = Guid.NewGuid().ToString(), |
| 222 | + InstallPath = @"C:\Games\TestGame", |
| 223 | + LauncherType = LauncherType.Manual |
| 224 | + }; |
| 225 | + } |
| 226 | + |
| 227 | + private static TweakRecipe CreateTestRecipe(string description = "Test Recipe", Guid? gameId = null) |
| 228 | + { |
| 229 | + return new TweakRecipe |
| 230 | + { |
| 231 | + Id = Guid.NewGuid(), |
| 232 | + GameId = gameId ?? Guid.NewGuid(), |
| 233 | + Description = description, |
| 234 | + FilePath = @"C:\Games\TestGame\config.ini", |
| 235 | + TargetType = TweakTargetType.IniFile, |
| 236 | + Key = "Graphics.Quality", |
| 237 | + Value = "Ultra" |
| 238 | + }; |
| 239 | + } |
| 240 | + |
| 241 | + private static Snapshot CreateTestSnapshot(Guid? gameId = null) |
| 242 | + { |
| 243 | + return new Snapshot |
| 244 | + { |
| 245 | + Id = Guid.NewGuid(), |
| 246 | + GameId = gameId ?? Guid.NewGuid(), |
| 247 | + Timestamp = DateTime.UtcNow, |
| 248 | + Description = "Test snapshot", |
| 249 | + BackupPath = @"C:\Backups\Test" |
| 250 | + }; |
| 251 | + } |
| 252 | + |
| 253 | + #endregion |
| 254 | +} |
0 commit comments