|
| 1 | +using FluentAssertions; |
| 2 | +using Moq; |
| 3 | +using GenAIDBExplorer.Core.Models.Project; |
| 4 | +using GenAIDBExplorer.Core.Models.SemanticModel; |
| 5 | +using GenAIDBExplorer.Core.Models.Database; |
| 6 | +using GenAIDBExplorer.Core.SemanticModelProviders; |
| 7 | +using GenAIDBExplorer.Core.Data.DatabaseProviders; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | + |
| 10 | +namespace GenAIDBExplorer.Core.Tests.SemanticModelProviders; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Unit tests for ISchemaRepository interface compliance and behavior. |
| 14 | +/// Tests REQ-001, REQ-002, SEC-002, PER-001 from specification. |
| 15 | +/// </summary> |
| 16 | +[TestClass] |
| 17 | +public class SchemaRepositoryTests |
| 18 | +{ |
| 19 | + private Mock<ISchemaRepository> _mockSchemaRepository = null!; |
| 20 | + private ISchemaRepository _schemaRepository = null!; |
| 21 | + |
| 22 | + [TestInitialize] |
| 23 | + public void TestInitialize() |
| 24 | + { |
| 25 | + // Arrange - Create mock for interface testing |
| 26 | + _mockSchemaRepository = new Mock<ISchemaRepository>(); |
| 27 | + _schemaRepository = _mockSchemaRepository.Object; |
| 28 | + } |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + public async Task GetTablesAsync_ShouldReturnDictionary_OfTableInfo() |
| 32 | + { |
| 33 | + // Arrange |
| 34 | + var expectedTables = new Dictionary<string, TableInfo> |
| 35 | + { |
| 36 | + { "dbo.Customer", new TableInfo("dbo", "Customer") }, |
| 37 | + { "Sales.Orders", new TableInfo("Sales", "Orders") } |
| 38 | + }; |
| 39 | + |
| 40 | + _mockSchemaRepository.Setup(r => r.GetTablesAsync(null)) |
| 41 | + .ReturnsAsync(expectedTables); |
| 42 | + |
| 43 | + // Act |
| 44 | + var result = await _schemaRepository.GetTablesAsync(); |
| 45 | + |
| 46 | + // Assert |
| 47 | + result.Should().NotBeNull(); |
| 48 | + result.Should().HaveCount(2); |
| 49 | + result.Should().ContainKey("dbo.Customer"); |
| 50 | + result.Should().ContainKey("Sales.Orders"); |
| 51 | + result["dbo.Customer"].TableName.Should().Be("Customer"); |
| 52 | + result["Sales.Orders"].SchemaName.Should().Be("Sales"); |
| 53 | + } |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public async Task GetTablesAsync_WithSchema_ShouldFilterBySchema() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var expectedTables = new Dictionary<string, TableInfo> |
| 60 | + { |
| 61 | + { "Sales.Customer", new TableInfo("Sales", "Customer") }, |
| 62 | + { "Sales.Orders", new TableInfo("Sales", "Orders") } |
| 63 | + }; |
| 64 | + |
| 65 | + _mockSchemaRepository.Setup(r => r.GetTablesAsync("Sales")) |
| 66 | + .ReturnsAsync(expectedTables); |
| 67 | + |
| 68 | + // Act |
| 69 | + var result = await _schemaRepository.GetTablesAsync("Sales"); |
| 70 | + |
| 71 | + // Assert |
| 72 | + result.Should().NotBeNull(); |
| 73 | + result.Should().HaveCount(2); |
| 74 | + result.Values.Should().AllSatisfy(t => t.SchemaName.Should().Be("Sales")); |
| 75 | + } |
| 76 | + |
| 77 | + [TestMethod] |
| 78 | + public async Task GetViewsAsync_ShouldReturnDictionary_OfViewInfo() |
| 79 | + { |
| 80 | + // Arrange |
| 81 | + var expectedViews = new Dictionary<string, ViewInfo> |
| 82 | + { |
| 83 | + { "dbo.CustomerView", new ViewInfo("dbo", "CustomerView") }, |
| 84 | + { "Sales.OrderSummary", new ViewInfo("Sales", "OrderSummary") } |
| 85 | + }; |
| 86 | + |
| 87 | + _mockSchemaRepository.Setup(r => r.GetViewsAsync(null)) |
| 88 | + .ReturnsAsync(expectedViews); |
| 89 | + |
| 90 | + // Act |
| 91 | + var result = await _schemaRepository.GetViewsAsync(); |
| 92 | + |
| 93 | + // Assert |
| 94 | + result.Should().NotBeNull(); |
| 95 | + result.Should().HaveCount(2); |
| 96 | + result.Should().ContainKey("dbo.CustomerView"); |
| 97 | + result.Should().ContainKey("Sales.OrderSummary"); |
| 98 | + } |
| 99 | + |
| 100 | + [TestMethod] |
| 101 | + public async Task GetStoredProceduresAsync_ShouldReturnDictionary_OfStoredProcedureInfo() |
| 102 | + { |
| 103 | + // Arrange |
| 104 | + var expectedProcedures = new Dictionary<string, StoredProcedureInfo> |
| 105 | + { |
| 106 | + { "dbo.GetCustomer", new StoredProcedureInfo("dbo", "GetCustomer", "PROCEDURE", null, "CREATE PROCEDURE...") }, |
| 107 | + { "Sales.ProcessOrder", new StoredProcedureInfo("Sales", "ProcessOrder", "PROCEDURE", null, "CREATE PROCEDURE...") } |
| 108 | + }; |
| 109 | + |
| 110 | + _mockSchemaRepository.Setup(r => r.GetStoredProceduresAsync(null)) |
| 111 | + .ReturnsAsync(expectedProcedures); |
| 112 | + |
| 113 | + // Act |
| 114 | + var result = await _schemaRepository.GetStoredProceduresAsync(); |
| 115 | + |
| 116 | + // Assert |
| 117 | + result.Should().NotBeNull(); |
| 118 | + result.Should().HaveCount(2); |
| 119 | + result.Should().ContainKey("dbo.GetCustomer"); |
| 120 | + result.Should().ContainKey("Sales.ProcessOrder"); |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public async Task GetColumnsForTableAsync_ShouldReturnList_OfSemanticModelColumns() |
| 125 | + { |
| 126 | + // Arrange |
| 127 | + var tableInfo = new TableInfo("dbo", "Customer"); |
| 128 | + var expectedColumns = new List<SemanticModelColumn> |
| 129 | + { |
| 130 | + new("dbo", "CustomerID", "Primary key") { Type = "int" }, |
| 131 | + new("dbo", "CustomerName", "Customer name") { Type = "nvarchar(100)" } |
| 132 | + }; |
| 133 | + |
| 134 | + _mockSchemaRepository.Setup(r => r.GetColumnsForTableAsync(tableInfo)) |
| 135 | + .ReturnsAsync(expectedColumns); |
| 136 | + |
| 137 | + // Act |
| 138 | + var result = await _schemaRepository.GetColumnsForTableAsync(tableInfo); |
| 139 | + |
| 140 | + // Assert |
| 141 | + result.Should().NotBeNull(); |
| 142 | + result.Should().HaveCount(2); |
| 143 | + result[0].Name.Should().Be("CustomerID"); |
| 144 | + result[1].Name.Should().Be("CustomerName"); |
| 145 | + } |
| 146 | + |
| 147 | + [TestMethod] |
| 148 | + public async Task GetSampleTableDataAsync_ShouldReturnSampleData() |
| 149 | + { |
| 150 | + // Arrange |
| 151 | + var tableInfo = new TableInfo("dbo", "Customer"); |
| 152 | + var expectedData = new List<Dictionary<string, object>> |
| 153 | + { |
| 154 | + new() { { "CustomerID", 1 }, { "CustomerName", "John Doe" } }, |
| 155 | + new() { { "CustomerID", 2 }, { "CustomerName", "Jane Smith" } } |
| 156 | + }; |
| 157 | + |
| 158 | + _mockSchemaRepository.Setup(r => r.GetSampleTableDataAsync(tableInfo, 5, false)) |
| 159 | + .ReturnsAsync(expectedData); |
| 160 | + |
| 161 | + // Act |
| 162 | + var result = await _schemaRepository.GetSampleTableDataAsync(tableInfo, 5, false); |
| 163 | + |
| 164 | + // Assert |
| 165 | + result.Should().NotBeNull(); |
| 166 | + result.Should().HaveCount(2); |
| 167 | + result[0].Should().ContainKey("CustomerID"); |
| 168 | + result[0].Should().ContainKey("CustomerName"); |
| 169 | + } |
| 170 | + |
| 171 | + [TestMethod] |
| 172 | + public async Task CreateSemanticModelTableAsync_ShouldCreateTable_WithColumnsAndIndexes() |
| 173 | + { |
| 174 | + // Arrange |
| 175 | + var tableInfo = new TableInfo("dbo", "Customer"); |
| 176 | + var expectedTable = new SemanticModelTable("dbo", "Customer"); |
| 177 | + expectedTable.Columns.Add(new SemanticModelColumn("dbo", "CustomerID", "Primary key") { Type = "int", IsPrimaryKey = true }); |
| 178 | + expectedTable.Indexes.Add(new SemanticModelIndex("dbo", "IX_Customer_Name", "Index on name") { Type = "NonClustered" }); |
| 179 | + |
| 180 | + _mockSchemaRepository.Setup(r => r.CreateSemanticModelTableAsync(tableInfo)) |
| 181 | + .ReturnsAsync(expectedTable); |
| 182 | + |
| 183 | + // Act |
| 184 | + var result = await _schemaRepository.CreateSemanticModelTableAsync(tableInfo); |
| 185 | + |
| 186 | + // Assert |
| 187 | + result.Should().NotBeNull(); |
| 188 | + result.Schema.Should().Be("dbo"); |
| 189 | + result.Name.Should().Be("Customer"); |
| 190 | + result.Columns.Should().HaveCount(1); |
| 191 | + result.Indexes.Should().HaveCount(1); |
| 192 | + result.Columns[0].IsPrimaryKey.Should().BeTrue(); |
| 193 | + } |
| 194 | + |
| 195 | + [TestMethod] |
| 196 | + public async Task CreateSemanticModelViewAsync_ShouldCreateView_WithDefinition() |
| 197 | + { |
| 198 | + // Arrange |
| 199 | + var viewInfo = new ViewInfo("dbo", "CustomerView"); |
| 200 | + var expectedView = new SemanticModelView("dbo", "CustomerView"); |
| 201 | + expectedView.Columns.Add(new SemanticModelColumn("dbo", "CustomerID", "Customer identifier") { Type = "int" }); |
| 202 | + expectedView.Definition = "SELECT CustomerID, CustomerName FROM Customer"; |
| 203 | + |
| 204 | + _mockSchemaRepository.Setup(r => r.CreateSemanticModelViewAsync(viewInfo)) |
| 205 | + .ReturnsAsync(expectedView); |
| 206 | + |
| 207 | + // Act |
| 208 | + var result = await _schemaRepository.CreateSemanticModelViewAsync(viewInfo); |
| 209 | + |
| 210 | + // Assert |
| 211 | + result.Should().NotBeNull(); |
| 212 | + result.Schema.Should().Be("dbo"); |
| 213 | + result.Name.Should().Be("CustomerView"); |
| 214 | + result.Columns.Should().HaveCount(1); |
| 215 | + result.Definition.Should().Be("SELECT CustomerID, CustomerName FROM Customer"); |
| 216 | + } |
| 217 | + |
| 218 | + [TestMethod] |
| 219 | + public async Task CreateSemanticModelStoredProcedureAsync_ShouldCreateStoredProcedure_WithParameters() |
| 220 | + { |
| 221 | + // Arrange |
| 222 | + var procedureInfo = new StoredProcedureInfo("dbo", "GetCustomer", "PROCEDURE", "@CustomerID int", "CREATE PROCEDURE GetCustomer..."); |
| 223 | + var expectedProcedure = new SemanticModelStoredProcedure("dbo", "GetCustomer", "CREATE PROCEDURE GetCustomer @CustomerID int AS SELECT * FROM Customer WHERE CustomerID = @CustomerID", "@CustomerID int"); |
| 224 | + |
| 225 | + _mockSchemaRepository.Setup(r => r.CreateSemanticModelStoredProcedureAsync(procedureInfo)) |
| 226 | + .ReturnsAsync(expectedProcedure); |
| 227 | + |
| 228 | + // Act |
| 229 | + var result = await _schemaRepository.CreateSemanticModelStoredProcedureAsync(procedureInfo); |
| 230 | + |
| 231 | + // Assert |
| 232 | + result.Should().NotBeNull(); |
| 233 | + result.Schema.Should().Be("dbo"); |
| 234 | + result.Name.Should().Be("GetCustomer"); |
| 235 | + result.Definition.Should().Contain("CREATE PROCEDURE GetCustomer"); |
| 236 | + } |
| 237 | + |
| 238 | + [TestMethod] |
| 239 | + public async Task GetTablesAsync_ShouldHandleException_Gracefully() |
| 240 | + { |
| 241 | + // Arrange |
| 242 | + _mockSchemaRepository.Setup(r => r.GetTablesAsync(null)) |
| 243 | + .ThrowsAsync(new InvalidOperationException("SQL connection failed")); |
| 244 | + |
| 245 | + // Act & Assert |
| 246 | + await Assert.ThrowsExceptionAsync<InvalidOperationException>( |
| 247 | + () => _schemaRepository.GetTablesAsync()); |
| 248 | + } |
| 249 | + |
| 250 | + [TestMethod] |
| 251 | + public async Task GetSampleTableDataAsync_WithRandomSelection_ShouldReturnRandomData() |
| 252 | + { |
| 253 | + // Arrange |
| 254 | + var tableInfo = new TableInfo("dbo", "Customer"); |
| 255 | + var expectedData = new List<Dictionary<string, object>> |
| 256 | + { |
| 257 | + new() { { "CustomerID", 1 }, { "CustomerName", "Random Customer" } } |
| 258 | + }; |
| 259 | + |
| 260 | + _mockSchemaRepository.Setup(r => r.GetSampleTableDataAsync(tableInfo, 1, true)) |
| 261 | + .ReturnsAsync(expectedData); |
| 262 | + |
| 263 | + // Act |
| 264 | + var result = await _schemaRepository.GetSampleTableDataAsync(tableInfo, 1, true); |
| 265 | + |
| 266 | + // Assert |
| 267 | + result.Should().NotBeNull(); |
| 268 | + result.Should().HaveCount(1); |
| 269 | + |
| 270 | + // Verify random selection was requested |
| 271 | + _mockSchemaRepository.Verify(r => r.GetSampleTableDataAsync(tableInfo, 1, true), Times.Once); |
| 272 | + } |
| 273 | + |
| 274 | + [TestMethod] |
| 275 | + public async Task ConcurrentOperations_ShouldNotCause_DataCorruption() |
| 276 | + { |
| 277 | + // Arrange - Test PER-001: Concurrent operations without corruption |
| 278 | + var tableInfo = new TableInfo("dbo", "Customer"); |
| 279 | + var expectedTable = new SemanticModelTable("dbo", "Customer"); |
| 280 | + expectedTable.Columns.Add(new SemanticModelColumn("dbo", "CustomerID", "Primary key") { Type = "int" }); |
| 281 | + |
| 282 | + _mockSchemaRepository.Setup(r => r.CreateSemanticModelTableAsync(tableInfo)) |
| 283 | + .ReturnsAsync(expectedTable); |
| 284 | + |
| 285 | + // Act - Execute multiple concurrent operations |
| 286 | + var tasks = Enumerable.Range(0, 10) |
| 287 | + .Select(_ => _schemaRepository.CreateSemanticModelTableAsync(tableInfo)) |
| 288 | + .ToArray(); |
| 289 | + |
| 290 | + var results = await Task.WhenAll(tasks); |
| 291 | + |
| 292 | + // Assert |
| 293 | + results.Should().AllSatisfy(result => |
| 294 | + { |
| 295 | + result.Should().NotBeNull(); |
| 296 | + result.Schema.Should().Be("dbo"); |
| 297 | + result.Name.Should().Be("Customer"); |
| 298 | + result.Columns.Should().HaveCount(1); |
| 299 | + }); |
| 300 | + |
| 301 | + // Verify method was called correct number of times |
| 302 | + _mockSchemaRepository.Verify(r => r.CreateSemanticModelTableAsync(tableInfo), Times.Exactly(10)); |
| 303 | + } |
| 304 | + |
| 305 | + [TestMethod] |
| 306 | + public async Task GetViewDefinitionAsync_ShouldReturnViewDefinition() |
| 307 | + { |
| 308 | + // Arrange |
| 309 | + var viewInfo = new ViewInfo("dbo", "CustomerView"); |
| 310 | + var expectedDefinition = "SELECT CustomerID, CustomerName FROM Customer WHERE Active = 1"; |
| 311 | + |
| 312 | + _mockSchemaRepository.Setup(r => r.GetViewDefinitionAsync(viewInfo)) |
| 313 | + .ReturnsAsync(expectedDefinition); |
| 314 | + |
| 315 | + // Act |
| 316 | + var result = await _schemaRepository.GetViewDefinitionAsync(viewInfo); |
| 317 | + |
| 318 | + // Assert |
| 319 | + result.Should().NotBeNull(); |
| 320 | + result.Should().Be(expectedDefinition); |
| 321 | + result.Should().Contain("SELECT"); |
| 322 | + result.Should().Contain("FROM Customer"); |
| 323 | + } |
| 324 | + |
| 325 | + [TestMethod] |
| 326 | + public async Task GetColumnsForViewAsync_ShouldReturnViewColumns() |
| 327 | + { |
| 328 | + // Arrange |
| 329 | + var viewInfo = new ViewInfo("dbo", "CustomerView"); |
| 330 | + var expectedColumns = new List<SemanticModelColumn> |
| 331 | + { |
| 332 | + new("dbo", "CustomerID", "Customer identifier") { Type = "int" }, |
| 333 | + new("dbo", "CustomerName", "Customer name") { Type = "nvarchar(100)" } |
| 334 | + }; |
| 335 | + |
| 336 | + _mockSchemaRepository.Setup(r => r.GetColumnsForViewAsync(viewInfo)) |
| 337 | + .ReturnsAsync(expectedColumns); |
| 338 | + |
| 339 | + // Act |
| 340 | + var result = await _schemaRepository.GetColumnsForViewAsync(viewInfo); |
| 341 | + |
| 342 | + // Assert |
| 343 | + result.Should().NotBeNull(); |
| 344 | + result.Should().HaveCount(2); |
| 345 | + result[0].Name.Should().Be("CustomerID"); |
| 346 | + result[1].Name.Should().Be("CustomerName"); |
| 347 | + } |
| 348 | + |
| 349 | + [TestMethod] |
| 350 | + public async Task GetSampleViewDataAsync_ShouldReturnViewSampleData() |
| 351 | + { |
| 352 | + // Arrange |
| 353 | + var viewInfo = new ViewInfo("dbo", "CustomerView"); |
| 354 | + var expectedData = new List<Dictionary<string, object>> |
| 355 | + { |
| 356 | + new() { { "CustomerID", 1 }, { "CustomerName", "Active Customer" } } |
| 357 | + }; |
| 358 | + |
| 359 | + _mockSchemaRepository.Setup(r => r.GetSampleViewDataAsync(viewInfo, 5, false)) |
| 360 | + .ReturnsAsync(expectedData); |
| 361 | + |
| 362 | + // Act |
| 363 | + var result = await _schemaRepository.GetSampleViewDataAsync(viewInfo, 5, false); |
| 364 | + |
| 365 | + // Assert |
| 366 | + result.Should().NotBeNull(); |
| 367 | + result.Should().HaveCount(1); |
| 368 | + result[0].Should().ContainKey("CustomerID"); |
| 369 | + result[0]["CustomerName"].Should().Be("Active Customer"); |
| 370 | + } |
| 371 | + |
| 372 | + [TestMethod] |
| 373 | + public async Task RepositoryOperations_ShouldComplete_WithinPerformanceThresholds() |
| 374 | + { |
| 375 | + // Arrange - Test PER-002: Entity loading ≤5s for 1000 entities (scaled down for unit test) |
| 376 | + var tables = Enumerable.Range(1, 100) |
| 377 | + .ToDictionary(i => $"dbo.Table{i}", i => new TableInfo("dbo", $"Table{i}")); |
| 378 | + |
| 379 | + _mockSchemaRepository.Setup(r => r.GetTablesAsync(null)) |
| 380 | + .ReturnsAsync(tables); |
| 381 | + |
| 382 | + // Act |
| 383 | + var startTime = DateTime.UtcNow; |
| 384 | + var result = await _schemaRepository.GetTablesAsync(); |
| 385 | + var duration = DateTime.UtcNow - startTime; |
| 386 | + |
| 387 | + // Assert |
| 388 | + result.Should().HaveCount(100); |
| 389 | + duration.Should().BeLessThan(TimeSpan.FromSeconds(1)); // Mock should be very fast |
| 390 | + } |
| 391 | +} |
0 commit comments