using Febe.Core.Domain;
using Febe.Data;
using Febe.Tests.Support;
using Microsoft.Data.Sqlite;
namespace Febe.Tests.Data;
///
/// Two accounts with the same post (a post of marta.demo, a tagged post of luca.demo, same SHA-256), the general
/// folder and the own folder of marta.demo: the starting point of the export tests.
///
internal sealed record ExportWorld(long Marta, long Luca, long MartaEntry, long MartaMedia, long LucaEntry, long LucaMedia, long General, long Own)
{
public static async Task CreateAsync(FebeDatabase db, string generalRoot = @"E:\Copie")
{
var marta = await db.Accounts.AddAsync(Make.Account("marta.demo", "100"));
var luca = await db.Accounts.AddAsync(Make.Account("luca.demo", "200"));
var post = await db.Entries.AddAsync(Make.Entry(marta.Id, "555", Make.T0), [Make.Stored(Make.Photo(), Make.Sha(1), "post/a.jpg")]);
var tagged = await db.Entries.AddAsync(Make.Entry(luca.Id, "555", Make.T0, EntryKind.Tagged), [Make.Stored(Make.Photo(), Make.Sha(1), "taggati/a.jpg")]);
var general = await db.Exports.AddDestinationAsync(Destination(ExportScope.General, null, generalRoot));
var own = await db.Exports.AddDestinationAsync(Destination(ExportScope.Account, marta.Id, @"F:\Marta"));
return new ExportWorld(marta.Id, luca.Id, post.Stored.Entry.Id, post.Stored.Media[0].Id, tagged.Stored.Entry.Id, tagged.Stored.Media[0].Id, general.Id, own.Id);
}
public static ExportDestination Destination(ExportScope scope, long? accountId, string root) => new()
{
Scope = scope,
AccountId = accountId,
RootPath = root,
ContentTypes = ContentTypes.All,
CreatedAt = Make.T0,
};
/// A whole copy of the shared photo for marta.demo in the general folder, at .
public ExportCopy WholeCopy(string path, long? destination = null, long? account = null, long? media = null) => new()
{
DestinationId = destination ?? General,
AccountId = account ?? Marta,
MediaId = media ?? MartaMedia,
SourceSha256 = Make.Sha(1),
Variant = ExportVariant.Whole,
State = ExportCopyState.Copied,
RelativePath = path,
Bytes = 1000,
CopySha256 = Make.Sha(1),
CopiedAt = Make.T0.AddHours(1),
};
/// A raw row of export_copy (the whole copy above), with changes.
public Dictionary CopyRow(params (string Column, object? Value)[] changes) => Row(
new()
{
["destination_id"] = General,
["account_id"] = Marta,
["media_id"] = MartaMedia,
["source_sha256"] = Make.Sha(1),
["variant"] = "Whole",
["format"] = "Original",
["frame_at_ms"] = null,
["state"] = "Copied",
["superseded_by_sha256"] = null,
["origin"] = "Written",
["relative_path"] = "marta.demo/a.jpg",
["bytes"] = 1000L,
["copy_sha256"] = Make.Sha(1),
["copied_at"] = 0L,
},
changes);
/// A raw row of export_destination: a retired general folder, with changes.
public static Dictionary DestinationRow(params (string Column, object? Value)[] changes) => Row(
new()
{
["scope"] = "General",
["account_id"] = null,
["removed_account_username"] = null,
["root_path"] = @"G:\Altra",
["content_types"] = 63L,
["photo_format"] = null,
["video_format"] = null,
["automatic"] = 0L,
["automatic_since"] = null,
["created_at"] = 0L,
["removed_at"] = 1L,
},
changes);
public Dictionary RunRow(params (string Column, object? Value)[] changes) => Row(
new() { ["destination_id"] = General, ["kind"] = "Copy", ["origin"] = "Manual", ["state"] = "Running", ["started_at"] = 0L },
changes);
public Dictionary ItemRow(long runId, params (string Column, object? Value)[] changes) => Row(
new()
{
["run_id"] = runId,
["media_id"] = MartaMedia,
["account_id"] = Marta,
["source_sha256"] = Make.Sha(1),
["variant"] = "Whole",
["format"] = "Original",
["frame_at_ms"] = null,
["state"] = "Pending",
},
changes);
public static Dictionary AnalysisRow(params (string Column, object? Value)[] changes) => Row(
new() { ["sha256"] = Make.Sha(5), ["algorithm"] = 1L, ["outcome"] = "Still", ["analyzed_at"] = 0L },
changes);
public Dictionary ConversionRow(params (string Column, object? Value)[] changes) => Row(
new() { ["media_id"] = MartaMedia, ["format"] = "Jpg", ["state"] = "Pending", ["source_sha256"] = Make.Sha(1), ["created_at"] = 0L },
changes);
/// Inserts a raw row and returns its rowid.
public static Task InsertAsync(FebeDatabase db, string table, IReadOnlyDictionary row) =>
db.WriteAsync(
w =>
{
var c = w.Command($"INSERT INTO {table} ({string.Join(", ", row.Keys)}) VALUES ({string.Join(", ", row.Keys.Select(k => "@" + k))})");
foreach (var (column, value) in row)
{
c.With("@" + column, value);
}
c.ExecuteNonQuery();
return w.LastInsertRowId();
},
default);
/// The row breaks a constraint (SQLITE_CONSTRAINT) and is not written.
public static async Task RejectedAsync(FebeDatabase db, string table, IReadOnlyDictionary row, string because)
{
var error = await Assert.ThrowsExactlyAsync(() => InsertAsync(db, table, row), because);
Assert.AreEqual(19, error.SqliteErrorCode, because);
}
public static Task ExecuteAsync(FebeDatabase db, string sql, params (string Name, object? Value)[] parameters) =>
db.WriteAsync(
w =>
{
var c = w.Command(sql);
foreach (var (name, value) in parameters)
{
c.With(name, value);
}
return c.ExecuteNonQuery();
},
default);
public static Task ScalarAsync(FebeDatabase db, string sql, params (string Name, object? Value)[] parameters) =>
db.ReadAsync(
c =>
{
var command = c.Command(sql);
foreach (var (name, value) in parameters)
{
command.With(name, value);
}
return command.ScalarInt64();
},
default);
public static Task ForeignKeyProblemsAsync(FebeDatabase db) => ScalarAsync(db, "SELECT COUNT(*) FROM pragma_foreign_key_check");
private static Dictionary Row(Dictionary row, (string Column, object? Value)[] changes)
{
foreach (var (column, value) in changes)
{
row[column] = value;
}
return row;
}
}