Files
Explorer-Workbench/tests/Explorer.Storage.Tests/SqliteDatabaseSessionTests.cs

52 lines
2.3 KiB
C#

using Explorer.Application;
using Explorer.Storage.Sqlite;
using Microsoft.Extensions.Logging.Abstractions;
namespace Explorer.Storage.Tests;
public class SqliteDatabaseSessionTests
{
[Fact]
public async Task Can_browse_and_edit_a_standalone_database()
{
var dir = Path.Combine(Path.GetTempPath(), "ew-sql", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(dir);
var path = Path.Combine(dir, "sample.db");
var indexPath = Path.Combine(dir, "index.db");
await using var writer = new SqliteIndexStore(indexPath, NullLogger<SqliteIndexStore>.Instance);
await writer.OpenAsync();
await writer.CloseAsync();
await using var session = new SqliteDatabaseSession(indexPath);
await session.OpenAsync(path, preferWrite: true);
Assert.True(session.CanWrite);
await session.ExecuteAsync("CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT);");
await session.InsertRowAsync("notes", new Dictionary<string, object?> { ["body"] = "hello" });
var page = await session.ReadTableAsync("notes", 0, 50);
Assert.Contains("body", page.Columns);
Assert.Single(page.Rows);
var rowId = Convert.ToInt64(page.Rows[0][0]);
await session.UpdateCellAsync("notes", rowId, "body", "world");
var query = await session.ExecuteAsync("SELECT body FROM notes");
Assert.Equal("world", query.Rows[0][0]?.ToString());
await session.DeleteRowAsync("notes", rowId);
Assert.Equal(0, (await session.ReadTableAsync("notes", 0, 10)).TotalRows);
}
[Fact]
public async Task Index_opens_read_only_while_writer_holds_lock()
{
var dir = Path.Combine(Path.GetTempPath(), "ew-sql", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(dir);
var path = Path.Combine(dir, "index.db");
await using var writer = new SqliteIndexStore(path, NullLogger<SqliteIndexStore>.Instance);
await writer.OpenAsync();
await using var session = new SqliteDatabaseSession(path);
await session.OpenAsync(path, preferWrite: true);
Assert.False(session.CanWrite);
Assert.Contains("Read-only", session.ModeLabel, StringComparison.OrdinalIgnoreCase);
var tables = await session.ListTablesAsync();
Assert.Contains("sources", tables);
}
}