|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System.Data; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using SharpCoreDB.Data.Provider; |
| 7 | + |
| 8 | +namespace SharpCoreDB.Tests.DataProvider; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Integration-style tests for SharpCoreDBCommand exercising the ADO.NET provider surface. |
| 12 | +/// These run under the main SharpCoreDB.Tests coverage collection (unlike the minimal |
| 13 | +/// tests in Provider.Sync.Tests) and specifically cover the Guid normalization path |
| 14 | +/// added for EF Core GUID foreign key round-tripping. |
| 15 | +/// </summary> |
| 16 | +public sealed class SharpCoreDBCommandTests : IDisposable |
| 17 | +{ |
| 18 | + private readonly string _dbPath; |
| 19 | + private readonly SharpCoreDBConnection _connection; |
| 20 | + |
| 21 | + public SharpCoreDBCommandTests() |
| 22 | + { |
| 23 | + _dbPath = $"./test_cmd_{Guid.NewGuid():N}.scdb"; |
| 24 | + _connection = new SharpCoreDBConnection($"Data Source={_dbPath};Password=TestPassword123;Cache=Shared"); |
| 25 | + _connection.Open(); |
| 26 | + |
| 27 | + // Ensure base table for GUID + DML tests (exercises ExecuteNonQuery + flush path) |
| 28 | + using var cmd = _connection.CreateCommand(); |
| 29 | + cmd.CommandText = "CREATE TABLE IF NOT EXISTS GuidTest (Id TEXT PRIMARY KEY, Name TEXT)"; |
| 30 | + cmd.ExecuteNonQuery(); |
| 31 | + } |
| 32 | + |
| 33 | + public void Dispose() |
| 34 | + { |
| 35 | + _connection?.Dispose(); |
| 36 | + try |
| 37 | + { |
| 38 | + if (File.Exists(_dbPath)) |
| 39 | + File.Delete(_dbPath); |
| 40 | + } |
| 41 | + catch (IOException) |
| 42 | + { |
| 43 | + // Acceptable in test cleanup - file may be locked briefly |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void Constructor_Default_ShouldInitializeDefaults() |
| 49 | + { |
| 50 | + // Arrange & Act |
| 51 | + var cmd = new SharpCoreDBCommand(); |
| 52 | + |
| 53 | + // Assert |
| 54 | + Assert.Equal(CommandType.Text, cmd.CommandType); |
| 55 | + Assert.Equal(30, cmd.CommandTimeout); |
| 56 | + Assert.NotNull(cmd.Parameters); |
| 57 | + Assert.False(cmd.DesignTimeVisible); // default |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void Constructor_WithCommandText_ShouldSetCommandText() |
| 62 | + { |
| 63 | + // Arrange & Act |
| 64 | + var cmd = new SharpCoreDBCommand("SELECT 1"); |
| 65 | + |
| 66 | + // Assert |
| 67 | + Assert.Equal("SELECT 1", cmd.CommandText); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void Constructor_WithConnection_ShouldSetConnection() |
| 72 | + { |
| 73 | + // Arrange & Act |
| 74 | + var cmd = new SharpCoreDBCommand("SELECT 1", _connection); |
| 75 | + |
| 76 | + // Assert |
| 77 | + Assert.Same(_connection, cmd.Connection); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void ExecuteNonQuery_InsertWithGuidParam_ShouldNormalizeGuidAndFlush() |
| 82 | + { |
| 83 | + // Arrange |
| 84 | + var testGuid = Guid.NewGuid(); |
| 85 | + using var cmd = _connection.CreateCommand(); |
| 86 | + cmd.CommandText = "INSERT INTO GuidTest (Id, Name) VALUES (@id, @name)"; |
| 87 | + cmd.Parameters.Add(new SharpCoreDBParameter("@id", testGuid)); |
| 88 | + cmd.Parameters.Add(new SharpCoreDBParameter("@name", "GuidRow")); |
| 89 | + |
| 90 | + // Act |
| 91 | + var affected = cmd.ExecuteNonQuery(); |
| 92 | + |
| 93 | + // Assert |
| 94 | + Assert.Equal(-1, affected); // convention for this provider |
| 95 | + |
| 96 | + // Verify round-trip via reader (also exercises BuildParameterDictionary on SELECT) |
| 97 | + using var verify = _connection.CreateCommand(); |
| 98 | + verify.CommandText = "SELECT Id, Name FROM GuidTest WHERE Id = @id"; |
| 99 | + verify.Parameters.Add(new SharpCoreDBParameter("@id", testGuid)); |
| 100 | + using var reader = verify.ExecuteReader(); |
| 101 | + Assert.True(reader.Read()); |
| 102 | + // The stored value is the normalized "D" string form |
| 103 | + var storedId = reader.GetString(0); |
| 104 | + Assert.Equal(testGuid.ToString("D"), storedId); |
| 105 | + Assert.Equal("GuidRow", reader.GetString(1)); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public async Task ExecuteScalarAsync_SimpleConstantQuery_ShouldReturnValue() |
| 110 | + { |
| 111 | + // Arrange - simple constant query (no table/params) to reliably exercise |
| 112 | + // the full async scalar path (Task.Run + BuildParameterDictionary + result extraction) |
| 113 | + // while the Guid/string param paths are already covered by the reader-based tests. |
| 114 | + using var cmd = _connection.CreateCommand(); |
| 115 | + cmd.CommandText = "SELECT 'ScalarAsyncOK' AS Result"; |
| 116 | + |
| 117 | + // Act |
| 118 | + var result = await cmd.ExecuteScalarAsync(); |
| 119 | + |
| 120 | + // Assert |
| 121 | + Assert.Equal("ScalarAsyncOK", result); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void ExecuteReader_SystemTableQuery_ShouldReturnTablesViaMetadata() |
| 126 | + { |
| 127 | + // Arrange - this hits the SQLITE_MASTER special path + ExecuteSystemTableQuery |
| 128 | + using var cmd = _connection.CreateCommand(); |
| 129 | + cmd.CommandText = "SELECT name, type FROM sqlite_master WHERE type = 'table'"; |
| 130 | + |
| 131 | + // Act |
| 132 | + using var reader = cmd.ExecuteReader(); |
| 133 | + |
| 134 | + // Assert - at minimum the GuidTest table we created should appear |
| 135 | + var tableNames = new List<string>(); |
| 136 | + while (reader.Read()) |
| 137 | + { |
| 138 | + tableNames.Add(reader.GetString(0)); |
| 139 | + } |
| 140 | + Assert.Contains("GuidTest", tableNames); |
| 141 | + } |
| 142 | + |
| 143 | + [Fact] |
| 144 | + public async Task ExecuteReaderAsync_Typed_ShouldReturnSharpCoreDBDataReader() |
| 145 | + { |
| 146 | + // Arrange |
| 147 | + using var cmd = _connection.CreateCommand(); |
| 148 | + cmd.CommandText = "SELECT 42 AS Answer"; |
| 149 | + |
| 150 | + // Act |
| 151 | + var reader = await cmd.ExecuteReaderAsync(); |
| 152 | + |
| 153 | + // Assert |
| 154 | + Assert.IsType<SharpCoreDBDataReader>(reader); |
| 155 | + Assert.True(await reader.ReadAsync()); |
| 156 | + Assert.Equal(42, reader.GetInt32(0)); |
| 157 | + await reader.DisposeAsync(); |
| 158 | + } |
| 159 | + |
| 160 | + [Fact] |
| 161 | + public void Cancel_And_Prepare_ShouldNotThrow() |
| 162 | + { |
| 163 | + // Arrange |
| 164 | + var cmd = new SharpCoreDBCommand("SELECT 1"); |
| 165 | + |
| 166 | + // Act & Assert - no-op methods |
| 167 | + cmd.Cancel(); |
| 168 | + cmd.Prepare(); |
| 169 | + Assert.True(true); |
| 170 | + } |
| 171 | +} |
0 commit comments