FileDatabase Refactor and normalization

Test cleanup
This commit is contained in:
daniel-c-harvey
2025-09-04 17:27:28 -04:00
parent 6fefcbcfb5
commit 97ad4e3ac7
20 changed files with 373 additions and 393 deletions
@@ -8,7 +8,6 @@ namespace DeepDrftContent.Controllers;
[Route("api/[controller]")] [Route("api/[controller]")]
public class TrackController : ControllerBase public class TrackController : ControllerBase
{ {
private readonly EntryKey _vaultKey = new("tracks", MediaVaultType.Audio);
private readonly FileDatabase.Services.FileDatabase _fileDatabase; private readonly FileDatabase.Services.FileDatabase _fileDatabase;
public TrackController(FileDatabase.Services.FileDatabase fileDatabase) public TrackController(FileDatabase.Services.FileDatabase fileDatabase)
@@ -19,8 +18,12 @@ public class TrackController : ControllerBase
[HttpGet("{trackId}")] [HttpGet("{trackId}")]
public async Task<ActionResult<AudioBinaryDto>> GetTrack([FromQuery] string trackId) public async Task<ActionResult<AudioBinaryDto>> GetTrack([FromQuery] string trackId)
{ {
if (_fileDatabase.GetVault(_vaultKey) is not AudioVault vault) { return NotFound(); } // BEFORE: Complex with EntryKey objects and redundant MediaVaultType
var file = await vault.GetEntryAsync<AudioBinary>(MediaVaultType.Audio, new EntryKey(trackId, MediaVaultType.Audio)); // var entryKey = new EntryKey(trackId, MediaVaultTypeMap.GetVaultType<AudioBinary>());
// var file = await _fileDatabase.LoadResourceAsync<AudioBinary>(_vaultKey, entryKey);
// AFTER: Ultra clean - just string identifiers, types inferred
var file = await _fileDatabase.LoadResourceAsync<AudioBinary>("tracks", trackId);
if (file == null) { return NotFound(); } if (file == null) { return NotFound(); }
return File(file.Buffer, MimeTypeExtensions.GetMimeType(file.Extension)); return File(file.Buffer, MimeTypeExtensions.GetMimeType(file.Extension));
} }
@@ -8,20 +8,30 @@ namespace DeepDrftContent.FileDatabase.Abstractions;
/// </summary> /// </summary>
public interface IIndexFactory public interface IIndexFactory
{ {
/// <summary>
/// Creates an index of the specified type
/// </summary>
Task<IIndex?> CreateIndexAsync(IndexType type, string rootPath);
/// <summary> /// <summary>
/// Loads an existing index of the specified type /// Loads an existing index of the specified type
/// </summary> /// </summary>
Task<IIndex?> LoadIndexAsync(IndexType type, string rootPath); Task<IIndex?> LoadIndexAsync(IndexType type, string rootPath);
/// <summary> /// <summary>
/// Loads existing index or creates new one if loading fails /// Creates a directory index
/// </summary> /// </summary>
Task<IIndex?> LoadOrCreateIndexAsync(IndexType type, string rootPath); Task<IDirectoryIndex?> CreateDirectoryIndexAsync(string rootPath);
/// <summary>
/// Loads existing directory index or creates new one if loading fails
/// </summary>
Task<IDirectoryIndex?> LoadOrCreateDirectoryIndexAsync(string rootPath);
/// <summary>
/// Creates a vault index with the specified vault type
/// </summary>
Task<IVaultIndex?> CreateVaultIndexAsync(string rootPath, MediaVaultType vaultType);
/// <summary>
/// Loads existing vault index or creates new one with the specified vault type if loading fails
/// </summary>
Task<IVaultIndex?> LoadOrCreateVaultIndexAsync(string rootPath, MediaVaultType vaultType);
} }
/// <summary> /// <summary>
@@ -66,4 +66,14 @@ public interface IMediaTypeRegistry
/// Get the metadata type for a vault type /// Get the metadata type for a vault type
/// </summary> /// </summary>
Type GetMetaDataType(MediaVaultType vaultType); Type GetMetaDataType(MediaVaultType vaultType);
/// <summary>
/// Get the vault type for a binary type (reverse mapping)
/// </summary>
MediaVaultType GetVaultType(Type binaryType);
/// <summary>
/// Get the vault type for a binary type using generics (reverse mapping)
/// </summary>
MediaVaultType GetVaultType<T>() where T : FileBinary;
} }
@@ -1,9 +0,0 @@
namespace DeepDrftContent.FileDatabase.Models;
/// <summary>
/// Represents a key for entries in the file database system.
/// Combines a string key with a media vault type for type-safe operations.
/// </summary>
/// <param name="Key">The string identifier for the entry</param>
/// <param name="Type">The media vault type this entry belongs to</param>
public record EntryKey(string Key, MediaVaultType Type);
@@ -17,9 +17,9 @@ public interface IIndex
public interface IEntryQueryable : IIndex public interface IEntryQueryable : IIndex
{ {
/// <summary> /// <summary>
/// Gets all entry keys in this index /// Gets all entry IDs in this index
/// </summary> /// </summary>
IReadOnlyList<EntryKey> GetEntries(); IReadOnlyList<string> GetEntries();
/// <summary> /// <summary>
/// Gets the number of entries in this index /// Gets the number of entries in this index
@@ -27,9 +27,9 @@ public interface IEntryQueryable : IIndex
int GetEntriesSize(); int GetEntriesSize();
/// <summary> /// <summary>
/// Checks if the index contains the specified entry key /// Checks if the index contains the specified entry ID
/// </summary> /// </summary>
bool HasEntry(EntryKey entryKey); bool HasEntry(string entryId);
} }
/// <summary> /// <summary>
@@ -40,7 +40,7 @@ public interface IDirectoryIndex : IEntryQueryable
/// <summary> /// <summary>
/// Adds an entry to the directory index /// Adds an entry to the directory index
/// </summary> /// </summary>
void PutEntry(EntryKey entryKey); void PutEntry(string entryId);
} }
/// <summary> /// <summary>
@@ -51,10 +51,10 @@ public interface IVaultIndex : IEntryQueryable
/// <summary> /// <summary>
/// Gets metadata for a specific entry /// Gets metadata for a specific entry
/// </summary> /// </summary>
MetaData? GetEntry(EntryKey entryKey); MetaData? GetEntry(string entryId);
/// <summary> /// <summary>
/// Adds an entry with metadata to the vault index /// Adds an entry with metadata to the vault index
/// </summary> /// </summary>
void PutEntry(EntryKey entryKey, MetaData metaData); void PutEntry(string entryId, MetaData metaData);
} }
@@ -1,4 +1,5 @@
using DeepDrftContent.FileDatabase.Utils; using DeepDrftContent.FileDatabase.Utils;
using System.Text.Json.Serialization;
namespace DeepDrftContent.FileDatabase.Models; namespace DeepDrftContent.FileDatabase.Models;
@@ -20,7 +21,7 @@ public abstract class IndexData
/// </summary> /// </summary>
public class DirectoryIndexData : IndexData public class DirectoryIndexData : IndexData
{ {
public List<EntryKey> Entries { get; set; } = new(); public List<string> Entries { get; set; } = new();
public DirectoryIndexData(string indexKey) : base(indexKey) { } public DirectoryIndexData(string indexKey) : base(indexKey) { }
@@ -39,7 +40,7 @@ public class DirectoryIndexData : IndexData
/// </summary> /// </summary>
public class VaultEntryData public class VaultEntryData
{ {
public EntryKey Key { get; set; } = null!; public string Key { get; set; } = null!;
public MetaData Value { get; set; } = null!; public MetaData Value { get; set; } = null!;
} }
@@ -49,12 +50,22 @@ public class VaultEntryData
public class VaultIndexData : IndexData public class VaultIndexData : IndexData
{ {
public List<VaultEntryData> Entries { get; set; } = new(); public List<VaultEntryData> Entries { get; set; } = new();
public MediaVaultType VaultType { get; set; }
public VaultIndexData(string indexKey) : base(indexKey) { } public VaultIndexData(string indexKey) : base(indexKey)
{
VaultType = MediaVaultType.Media; // Default vault type for legacy compatibility
}
[JsonConstructor]
public VaultIndexData(string indexKey, MediaVaultType vaultType) : base(indexKey)
{
VaultType = vaultType;
}
public static VaultIndexData FromIndex(VaultIndex index) public static VaultIndexData FromIndex(VaultIndex index)
{ {
var data = new VaultIndexData(index.GetKey()) var data = new VaultIndexData(index.GetKey(), index.VaultType)
{ {
Entries = index.Entries.Select(kvp => new VaultEntryData { Key = kvp.Key, Value = kvp.Value }).ToList() Entries = index.Entries.Select(kvp => new VaultEntryData { Key = kvp.Key, Value = kvp.Value }).ToList()
}; };
@@ -67,11 +78,11 @@ public class VaultIndexData : IndexData
/// </summary> /// </summary>
public class DirectoryIndex : IndexData, IDirectoryIndex public class DirectoryIndex : IndexData, IDirectoryIndex
{ {
public StructuralSet<EntryKey> Entries { get; } public StructuralSet<string> Entries { get; }
public DirectoryIndex(DirectoryIndexData indexData) : base(indexData.IndexKey) public DirectoryIndex(DirectoryIndexData indexData) : base(indexData.IndexKey)
{ {
Entries = new StructuralSet<EntryKey>(); Entries = new StructuralSet<string>();
// Load entries from data // Load entries from data
foreach (var entry in indexData.Entries) foreach (var entry in indexData.Entries)
{ {
@@ -81,13 +92,13 @@ public class DirectoryIndex : IndexData, IDirectoryIndex
public string GetKey() => IndexKey; public string GetKey() => IndexKey;
public IReadOnlyList<EntryKey> GetEntries() => Entries.ToList().AsReadOnly(); public IReadOnlyList<string> GetEntries() => Entries.ToList().AsReadOnly();
public int GetEntriesSize() => Entries.Size; public int GetEntriesSize() => Entries.Size;
public bool HasEntry(EntryKey entryKey) => Entries.Has(entryKey); public bool HasEntry(string entryId) => Entries.Has(entryId);
public void PutEntry(EntryKey entryKey) => Entries.Add(entryKey); public void PutEntry(string entryId) => Entries.Add(entryId);
} }
/// <summary> /// <summary>
@@ -95,11 +106,13 @@ public class DirectoryIndex : IndexData, IDirectoryIndex
/// </summary> /// </summary>
public class VaultIndex : IndexData, IVaultIndex public class VaultIndex : IndexData, IVaultIndex
{ {
public StructuralMap<EntryKey, MetaData> Entries { get; } public StructuralMap<string, MetaData> Entries { get; }
public MediaVaultType VaultType { get; }
public VaultIndex(VaultIndexData indexData) : base(indexData.IndexKey) public VaultIndex(VaultIndexData indexData) : base(indexData.IndexKey)
{ {
Entries = new StructuralMap<EntryKey, MetaData>(); Entries = new StructuralMap<string, MetaData>();
VaultType = indexData.VaultType;
// Load entries from data // Load entries from data
foreach (var entry in indexData.Entries) foreach (var entry in indexData.Entries)
{ {
@@ -109,13 +122,13 @@ public class VaultIndex : IndexData, IVaultIndex
public string GetKey() => IndexKey; public string GetKey() => IndexKey;
public IReadOnlyList<EntryKey> GetEntries() => Entries.Keys.ToList().AsReadOnly(); public IReadOnlyList<string> GetEntries() => Entries.Keys.ToList().AsReadOnly();
public int GetEntriesSize() => Entries.Size; public int GetEntriesSize() => Entries.Size;
public bool HasEntry(EntryKey entryKey) => Entries.Has(entryKey); public bool HasEntry(string entryId) => Entries.Has(entryId);
public MetaData? GetEntry(EntryKey entryKey) => Entries.Get(entryKey); public MetaData? GetEntry(string entryId) => Entries.Get(entryId);
public void PutEntry(EntryKey entryKey, MetaData metaData) => Entries.Set(entryKey, metaData); public void PutEntry(string entryId, MetaData metaData) => Entries.Set(entryId, metaData);
} }
@@ -17,6 +17,16 @@ public static class MediaVaultTypeMap
public static Type GetParamsType(MediaVaultType vaultType) => _registry.GetParamsType(vaultType); public static Type GetParamsType(MediaVaultType vaultType) => _registry.GetParamsType(vaultType);
public static Type GetMetaDataType(MediaVaultType vaultType) => _registry.GetMetaDataType(vaultType); public static Type GetMetaDataType(MediaVaultType vaultType) => _registry.GetMetaDataType(vaultType);
/// <summary>
/// Get the vault type for a binary type (reverse mapping)
/// </summary>
public static MediaVaultType GetVaultType(Type binaryType) => _registry.GetVaultType(binaryType);
/// <summary>
/// Get the vault type for a binary type using generics (reverse mapping)
/// </summary>
public static MediaVaultType GetVaultType<T>() where T : FileBinary => _registry.GetVaultType<T>();
} }
/// <summary> /// <summary>
+10 -12
View File
@@ -10,7 +10,7 @@ The C# port preserves the original three-layer architecture:
- **Location**: `Services/FileDatabase.cs` - **Location**: `Services/FileDatabase.cs`
- **Purpose**: Root-level manager coordinating multiple media vaults - **Purpose**: Root-level manager coordinating multiple media vaults
- **Key Features**: - **Key Features**:
- Manages collection of `MediaVault` instances using `StructuralMap<EntryKey, MediaVault>` - Manages collection of `MediaVault` instances using `StructuralMap<string, MediaVault>`
- Provides async factory method `FromAsync()` for initialization - Provides async factory method `FromAsync()` for initialization
- Handles vault creation, resource loading, and resource registration - Handles vault creation, resource loading, and resource registration
@@ -38,7 +38,7 @@ The C# port preserves the original three-layer architecture:
## Key Components ## Key Components
### Models (`Models/` directory) ### Models (`Models/` directory)
- **`EntryKey`**: Composite key structure with string key + MediaVaultType - **String-based keys**: Simple string identifiers for vault and entry management
- **`MetaData` hierarchy**: Base metadata → `ImageMetaData` with aspect ratio - **`MetaData` hierarchy**: Base metadata → `ImageMetaData` with aspect ratio
- **Media Types**: `FileBinary``MediaBinary``ImageBinary` - **Media Types**: `FileBinary``MediaBinary``ImageBinary`
- **Factory Classes**: Type-safe creation of media objects and metadata - **Factory Classes**: Type-safe creation of media objects and metadata
@@ -58,7 +58,7 @@ The C# port preserves the original three-layer architecture:
5. **Dependency Inversion**: Abstract base classes and interfaces 5. **Dependency Inversion**: Abstract base classes and interfaces
### C# Language Features ### C# Language Features
- **Records**: Immutable data structures for `EntryKey`, `MetaData` - **Records**: Immutable data structures for `MetaData` hierarchy
- **Pattern Matching**: Switch expressions for type-safe factory methods - **Pattern Matching**: Switch expressions for type-safe factory methods
- **Nullable Reference Types**: Explicit nullability handling - **Nullable Reference Types**: Explicit nullability handling
- **Async/Await**: Full async support with `Task<T>` and `ValueTask<T>` - **Async/Await**: Full async support with `Task<T>` and `ValueTask<T>`
@@ -76,17 +76,15 @@ The C# port preserves the original three-layer architecture:
var database = await FileDatabase.FromAsync("/path/to/database"); var database = await FileDatabase.FromAsync("/path/to/database");
// Create a vault // Create a vault
var vaultKey = new EntryKey("images", MediaVaultType.Image); var vaultId = "images";
await database.CreateVaultAsync(vaultKey); await database.CreateVaultAsync(vaultId, MediaVaultType.Image);
// Store an image // Store an image (MediaVaultType inferred from ImageBinary)
var entryKey = new EntryKey("photo1", MediaVaultType.Image);
var imageData = new ImageBinary(new ImageBinaryParams(buffer, size, ".jpg", 1.5)); var imageData = new ImageBinary(new ImageBinaryParams(buffer, size, ".jpg", 1.5));
await database.RegisterResourceAsync(MediaVaultType.Image, vaultKey, entryKey, imageData); await database.RegisterResourceAsync("gallery", "photo1", imageData);
// Load an image // Load an image (MediaVaultType inferred from ImageBinary generic type)
var loadedImage = await database.LoadResourceAsync<ImageBinary>( var loadedImage = await database.LoadResourceAsync<ImageBinary>("gallery", "photo1");
MediaVaultType.Image, vaultKey, entryKey);
``` ```
## Project Structure ## Project Structure
@@ -94,7 +92,7 @@ var loadedImage = await database.LoadResourceAsync<ImageBinary>(
``` ```
FileDatabase/ FileDatabase/
├── Models/ ├── Models/
│ ├── EntryKey.cs # Composite key structure │ ├── [EntryKey removed] # Now using simple string keys
│ ├── MetaData.cs # Metadata hierarchy │ ├── MetaData.cs # Metadata hierarchy
│ ├── MediaModels.cs # Binary data classes │ ├── MediaModels.cs # Binary data classes
│ ├── MediaFactories.cs # Factory pattern implementations │ ├── MediaFactories.cs # Factory pattern implementations
@@ -1,5 +1,6 @@
using DeepDrftContent.FileDatabase.Models; using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Utils; using DeepDrftContent.FileDatabase.Utils;
using IndexType = DeepDrftContent.FileDatabase.Services.IndexType;
namespace DeepDrftContent.FileDatabase.Services; namespace DeepDrftContent.FileDatabase.Services;
@@ -8,19 +9,19 @@ namespace DeepDrftContent.FileDatabase.Services;
/// </summary> /// </summary>
public class FileDatabase : DirectoryIndexDirectory public class FileDatabase : DirectoryIndexDirectory
{ {
private readonly StructuralMap<EntryKey, MediaVault> _vaults; private readonly StructuralMap<string, MediaVault> _vaults;
/// <summary> /// <summary>
/// Factory method to create a FileDatabase instance /// Factory method to create a FileDatabase instance
/// </summary> /// </summary>
public static async Task<FileDatabase?> FromAsync(string rootPath) public static async Task<FileDatabase?> FromAsync(string rootPath)
{ {
var factory = new IndexFactory(rootPath, IndexType.Directory); var factoryService = new IndexFactoryService();
var rootIndex = await factory.BuildIndexAsync(); var rootIndex = await factoryService.LoadOrCreateDirectoryIndexAsync(rootPath);
if (rootIndex is DirectoryIndex directoryIndex) if (rootIndex != null)
{ {
var db = new FileDatabase(rootPath, directoryIndex); var db = new FileDatabase(rootPath, (DirectoryIndex)rootIndex);
await db.InitVaultsAsync(); await db.InitVaultsAsync();
return db; return db;
} }
@@ -30,7 +31,7 @@ public class FileDatabase : DirectoryIndexDirectory
private FileDatabase(string rootPath, DirectoryIndex index) : base(rootPath, index) private FileDatabase(string rootPath, DirectoryIndex index) : base(rootPath, index)
{ {
_vaults = new StructuralMap<EntryKey, MediaVault>(); _vaults = new StructuralMap<string, MediaVault>();
} }
/// <summary> /// <summary>
@@ -38,56 +39,86 @@ public class FileDatabase : DirectoryIndexDirectory
/// </summary> /// </summary>
private async Task InitVaultsAsync() private async Task InitVaultsAsync()
{ {
foreach (var vaultKey in GetIndexEntries()) foreach (var vaultId in GetIndexEntries())
{ {
await InitVaultAsync(vaultKey); var vaultType = await GetVaultTypeFromIndex(vaultId);
if (vaultType.HasValue)
{
await InitVaultAsync(vaultId, vaultType.Value);
}
} }
} }
/// <summary> /// <summary>
/// Initializes a specific vault /// Initializes a specific vault
/// </summary> /// </summary>
private async Task InitVaultAsync(EntryKey vaultKey) private async Task InitVaultAsync(string vaultId, MediaVaultType vaultType)
{ {
var path = Path.Combine(RootPath, vaultKey.Key); var path = Path.Combine(RootPath, vaultId);
var directoryVault = await MediaVaultFactory.From(path, vaultKey.Type); var directoryVault = await MediaVaultFactory.From(path, vaultType);
if (directoryVault != null) if (directoryVault != null)
{ {
_vaults.Set(vaultKey, directoryVault); _vaults.Set(vaultId, directoryVault);
} }
} }
/// <summary> /// <summary>
/// Checks if a vault exists for the given key /// Gets vault type from the vault's index file
/// </summary> /// </summary>
public bool HasVault(EntryKey vaultKey) private async Task<MediaVaultType?> GetVaultTypeFromIndex(string vaultId)
{ {
return _vaults.Has(vaultKey); try
{
var factoryService = new IndexFactoryService();
var vaultPath = Path.Combine(RootPath, vaultId);
var index = await factoryService.LoadIndexAsync(IndexType.Vault, vaultPath);
if (index is VaultIndex vaultIndex)
{
return vaultIndex.VaultType;
}
}
catch
{
// If we can't load the index, we can't determine the vault type
// This might happen for legacy vaults or corrupted indexes
}
return null;
} }
/// <summary> /// <summary>
/// Gets a vault by key /// Checks if a vault exists for the given vault ID
/// </summary> /// </summary>
public MediaVault? GetVault(EntryKey vaultKey) public bool HasVault(string vaultId)
{ {
return HasVault(vaultKey) ? _vaults.Get(vaultKey) : null; return _vaults.Has(vaultId);
}
/// <summary>
/// Gets a vault by vault ID
/// </summary>
public MediaVault? GetVault(string vaultId)
{
return HasVault(vaultId) ? _vaults.Get(vaultId) : null;
} }
/// <summary> /// <summary>
/// Creates a new vault /// Creates a new vault
/// </summary> /// </summary>
public async Task CreateVaultAsync(EntryKey vaultKey) public async Task CreateVaultAsync(string vaultId, MediaVaultType vaultType)
{ {
try try
{ {
var path = Path.Combine(RootPath, vaultKey.Key); var path = Path.Combine(RootPath, vaultId);
var directoryVault = await MediaVaultFactory.From(path, vaultKey.Type); var directoryVault = await MediaVaultFactory.From(path, vaultType);
if (directoryVault != null) if (directoryVault != null)
{ {
_vaults.Set(vaultKey, directoryVault); _vaults.Set(vaultId, directoryVault);
await AddToIndexAsync(vaultKey); // Now using string-based index
await AddToIndexAsync(vaultId);
} }
} }
catch catch
@@ -97,17 +128,17 @@ public class FileDatabase : DirectoryIndexDirectory
} }
/// <summary> /// <summary>
/// Loads a resource from a specific vault /// Loads a resource from a specific vault (MediaVaultType inferred from T)
/// </summary> /// </summary>
public async Task<T?> LoadResourceAsync<T>(MediaVaultType vaultType, EntryKey vaultKey, EntryKey entryKey) public async Task<T?> LoadResourceAsync<T>(string vaultId, string entryId)
where T : FileBinary where T : FileBinary
{ {
try try
{ {
var vault = _vaults.Get(vaultKey); var vault = _vaults.Get(vaultId);
if (vault != null) if (vault != null)
{ {
return await vault.GetEntryAsync<T>(vaultType, entryKey); return await vault.GetEntryAsync<T>(entryId);
} }
} }
catch catch
@@ -119,16 +150,16 @@ public class FileDatabase : DirectoryIndexDirectory
} }
/// <summary> /// <summary>
/// Registers a resource in a specific vault /// Registers a resource in a specific vault (MediaVaultType inferred from media type)
/// </summary> /// </summary>
public async Task<bool> RegisterResourceAsync(MediaVaultType vaultType, EntryKey vaultKey, EntryKey entryKey, object media) public async Task<bool> RegisterResourceAsync(string vaultId, string entryId, FileBinary media)
{ {
try try
{ {
var directoryVault = _vaults.Get(vaultKey); var directoryVault = _vaults.Get(vaultId);
if (directoryVault != null) if (directoryVault != null)
{ {
await directoryVault.AddEntryAsync(vaultType, entryKey, media); await directoryVault.AddEntryAsync(entryId, media);
return true; return true;
} }
} }
@@ -141,9 +172,9 @@ public class FileDatabase : DirectoryIndexDirectory
} }
/// <summary> /// <summary>
/// Gets all vault keys managed by this database /// Gets all vault IDs managed by this database
/// </summary> /// </summary>
public IReadOnlyList<EntryKey> GetVaultKeys() public IReadOnlyList<string> GetVaultIds()
{ {
return _vaults.Keys.ToList().AsReadOnly(); return _vaults.Keys.ToList().AsReadOnly();
} }
@@ -155,4 +186,5 @@ public class FileDatabase : DirectoryIndexDirectory
{ {
return _vaults.Size; return _vaults.Size;
} }
} }
@@ -10,18 +10,11 @@ namespace DeepDrftContent.FileDatabase.Services;
/// </summary> /// </summary>
public class IndexFactoryService : IIndexFactory, IIndexDataFactory public class IndexFactoryService : IIndexFactory, IIndexDataFactory
{ {
private readonly Dictionary<IndexType, Func<string, IIndex>> _indexCreators;
private readonly Dictionary<IndexType, Func<object, IIndex>> _indexFromDataCreators; private readonly Dictionary<IndexType, Func<object, IIndex>> _indexFromDataCreators;
private readonly Dictionary<IndexType, Func<IIndex, object>> _indexDataCreators; private readonly Dictionary<IndexType, Func<IIndex, object>> _indexDataCreators;
public IndexFactoryService() public IndexFactoryService()
{ {
_indexCreators = new Dictionary<IndexType, Func<string, IIndex>>
{
{ IndexType.Directory, rootPath => new DirectoryIndex(new DirectoryIndexData(Path.GetFileName(rootPath))) },
{ IndexType.Vault, rootPath => new VaultIndex(new VaultIndexData(Path.GetFileName(rootPath))) }
};
_indexFromDataCreators = new Dictionary<IndexType, Func<object, IIndex>> _indexFromDataCreators = new Dictionary<IndexType, Func<object, IIndex>>
{ {
{ IndexType.Directory, data => new DirectoryIndex((DirectoryIndexData)data) }, { IndexType.Directory, data => new DirectoryIndex((DirectoryIndexData)data) },
@@ -35,18 +28,14 @@ public class IndexFactoryService : IIndexFactory, IIndexDataFactory
}; };
} }
public async Task<IIndex?> CreateIndexAsync(IndexType type, string rootPath) public async Task<IDirectoryIndex?> CreateDirectoryIndexAsync(string rootPath)
{ {
if (!_indexCreators.TryGetValue(type, out var creator)) var indexData = new DirectoryIndexData(Path.GetFileName(rootPath));
{ var index = new DirectoryIndex(indexData);
throw new ArgumentException($"Unknown index type: {type}");
}
var index = creator(rootPath);
// Ensure directory exists and save the index // Ensure directory exists and save the index
await FileUtils.MakeVaultDirectoryAsync(rootPath); await FileUtils.MakeVaultDirectoryAsync(rootPath);
await SaveIndexAsync(rootPath, type, index); await SaveIndexAsync(rootPath, IndexType.Directory, index);
return index; return index;
} }
@@ -70,15 +59,41 @@ public class IndexFactoryService : IIndexFactory, IIndexDataFactory
return creator(indexData); return creator(indexData);
} }
public async Task<IIndex?> LoadOrCreateIndexAsync(IndexType type, string rootPath) public async Task<IDirectoryIndex?> LoadOrCreateDirectoryIndexAsync(string rootPath)
{ {
try try
{ {
return await LoadIndexAsync(type, rootPath); var index = await LoadIndexAsync(IndexType.Directory, rootPath);
return index as IDirectoryIndex;
} }
catch catch
{ {
return await CreateIndexAsync(type, rootPath); return await CreateDirectoryIndexAsync(rootPath);
}
}
public async Task<IVaultIndex?> CreateVaultIndexAsync(string rootPath, MediaVaultType vaultType)
{
var vaultIndexData = new VaultIndexData(Path.GetFileName(rootPath), vaultType);
var index = new VaultIndex(vaultIndexData);
// Ensure directory exists and save the index
await FileUtils.MakeVaultDirectoryAsync(rootPath);
await SaveIndexAsync(rootPath, IndexType.Vault, index);
return index;
}
public async Task<IVaultIndex?> LoadOrCreateVaultIndexAsync(string rootPath, MediaVaultType vaultType)
{
try
{
var index = await LoadIndexAsync(IndexType.Vault, rootPath);
return index as IVaultIndex;
}
catch
{
return await CreateVaultIndexAsync(rootPath, vaultType);
} }
} }
@@ -39,27 +39,6 @@ public abstract class AbstractIndexContainer
} }
} }
/// <summary>
/// Factory for creating and loading indexes - delegates to IIndexFactory
/// </summary>
public class IndexFactory : AbstractIndexContainer
{
private readonly IIndexFactory _factoryService;
public IndexFactory(string path, IndexType type, IIndexFactory? factoryService = null, IIndexDataFactory? indexDataFactory = null)
: base(path, type, indexDataFactory)
{
_factoryService = factoryService ?? new IndexFactoryService();
}
/// <summary>
/// Builds an index by loading existing or creating new
/// </summary>
public async Task<IIndex?> BuildIndexAsync()
{
return await _factoryService.LoadOrCreateIndexAsync(Type, RootPath);
}
}
/// <summary> /// <summary>
/// Abstract base class for directory containers that manage indexes /// Abstract base class for directory containers that manage indexes
@@ -74,11 +53,11 @@ public abstract class IndexDirectory : AbstractIndexContainer
Index = index; Index = index;
} }
protected IReadOnlyList<EntryKey> GetIndexEntries() => Index.GetEntries(); protected IReadOnlyList<string> GetIndexEntries() => Index.GetEntries();
public int GetIndexSize() => Index.GetEntriesSize(); public int GetIndexSize() => Index.GetEntriesSize();
public bool HasIndexEntry(EntryKey entryKey) => Index.HasEntry(entryKey); public bool HasIndexEntry(string entryId) => Index.HasEntry(entryId);
} }
/// <summary> /// <summary>
@@ -94,9 +73,9 @@ public class DirectoryIndexDirectory : IndexDirectory
_directoryIndex = index; _directoryIndex = index;
} }
protected async Task AddToIndexAsync(EntryKey entryKey) protected async Task AddToIndexAsync(string entryId)
{ {
_directoryIndex.PutEntry(entryKey); _directoryIndex.PutEntry(entryId);
await SaveIndexAsync(_directoryIndex); await SaveIndexAsync(_directoryIndex);
} }
} }
@@ -114,9 +93,9 @@ public class VaultIndexDirectory : IndexDirectory
_vaultIndex = index; _vaultIndex = index;
} }
protected async Task AddToIndexAsync(EntryKey entryKey, MetaData metaData) protected async Task AddToIndexAsync(string entryId, MetaData metaData)
{ {
_vaultIndex.PutEntry(entryKey, metaData); _vaultIndex.PutEntry(entryId, metaData);
await SaveIndexAsync(_vaultIndex); await SaveIndexAsync(_vaultIndex);
} }
} }
@@ -37,32 +37,39 @@ public abstract class MediaVault : VaultIndexDirectory
} }
/// <summary> /// <summary>
/// Adds a new entry to the vault with the specified media data /// Adds a new entry to the vault with the specified media data (MediaVaultType inferred from media type)
/// </summary> /// </summary>
public async Task AddEntryAsync(MediaVaultType vaultType, EntryKey entryKey, object media) public async Task AddEntryAsync(string entryId, FileBinary media)
{ {
// Extract properties from media object based on type // Extract properties from media object based on type
var (buffer, extension) = ExtractMediaProperties(media); var (buffer, extension) = ExtractMediaProperties(media);
var mediaPath = GetMediaPathFromEntryKey(entryKey.Key, extension); // Infer MediaVaultType from the media object type
var metaData = MetaDataFactory.CreateFromMedia(vaultType, entryKey.Key, extension, media); var vaultType = MediaVaultTypeMap.GetVaultType(media.GetType());
await AddToIndexAsync(entryKey, metaData); var mediaPath = GetMediaPathFromEntryKey(entryId, extension);
var metaData = MetaDataFactory.CreateFromMedia(vaultType, entryId, extension, media);
// Use string-based index operations
await AddToIndexAsync(entryId, metaData);
await FileUtils.PutFileAsync(mediaPath, buffer); await FileUtils.PutFileAsync(mediaPath, buffer);
} }
/// <summary> /// <summary>
/// Retrieves an entry from the vault /// Retrieves an entry from the vault (MediaVaultType inferred from T)
/// </summary> /// </summary>
public async Task<T?> GetEntryAsync<T>(MediaVaultType vaultType, EntryKey entryKey) where T : FileBinary public async Task<T?> GetEntryAsync<T>(string entryId) where T : FileBinary
{ {
if (!HasIndexEntry(entryKey)) // Infer MediaVaultType from the generic type T
var vaultType = MediaVaultTypeMap.GetVaultType<T>();
if (!HasIndexEntry(entryId))
return null; return null;
if (Index is not VaultIndex vaultIndex) if (Index is not VaultIndex vaultIndex)
return null; return null;
var metaData = vaultIndex.GetEntry(entryKey); var metaData = vaultIndex.GetEntry(entryId);
if (metaData == null) if (metaData == null)
return null; return null;
@@ -79,15 +86,16 @@ public abstract class MediaVault : VaultIndexDirectory
} }
/// <summary> /// <summary>
/// Extracts buffer and extension from a media object /// Extracts buffer and extension from a media binary
/// </summary> /// </summary>
private static (byte[] buffer, string extension) ExtractMediaProperties(object media) private static (byte[] buffer, string extension) ExtractMediaProperties(FileBinary media)
{ {
return media switch return media switch
{ {
ImageBinary imageBinary => (imageBinary.Buffer, imageBinary.Extension), ImageBinary imageBinary => (imageBinary.Buffer, imageBinary.Extension),
AudioBinary audioBinary => (audioBinary.Buffer, audioBinary.Extension), AudioBinary audioBinary => (audioBinary.Buffer, audioBinary.Extension),
MediaBinary mediaBinary => (mediaBinary.Buffer, mediaBinary.Extension), MediaBinary mediaBinary => (mediaBinary.Buffer, mediaBinary.Extension),
FileBinary fileBinary => throw new ArgumentException($"FileBinary must be a specific media type (ImageBinary, AudioBinary, or MediaBinary), not base FileBinary"),
_ => throw new ArgumentException($"Unsupported media type: {media.GetType()}") _ => throw new ArgumentException($"Unsupported media type: {media.GetType()}")
}; };
} }
@@ -105,12 +113,12 @@ public class ImageVault : MediaVault
/// </summary> /// </summary>
public static async Task<ImageVault?> FromAsync(string rootPath) public static async Task<ImageVault?> FromAsync(string rootPath)
{ {
var factory = new IndexFactory(rootPath, IndexType.Vault); var factoryService = new IndexFactoryService();
var index = await factory.BuildIndexAsync(); var index = await factoryService.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Image);
if (index is VaultIndex vaultIndex) if (index != null)
{ {
return new ImageVault(rootPath, vaultIndex); return new ImageVault(rootPath, (VaultIndex)index);
} }
return null; return null;
@@ -123,12 +131,12 @@ public class AudioVault : MediaVault
public static async Task<AudioVault?> FromAsync(string rootPath) public static async Task<AudioVault?> FromAsync(string rootPath)
{ {
var factory = new IndexFactory(rootPath, IndexType.Vault); var factoryService = new IndexFactoryService();
var index = await factory.BuildIndexAsync(); var index = await factoryService.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Audio);
if (index is VaultIndex vaultIndex) if (index != null)
{ {
return new AudioVault(rootPath, vaultIndex); return new AudioVault(rootPath, (VaultIndex)index);
} }
return null; return null;
@@ -18,6 +18,9 @@ public class SimpleMediaTypeRegistry : IMediaTypeRegistry
private readonly Dictionary<MediaVaultType, Type> _dtoTypes = new(); private readonly Dictionary<MediaVaultType, Type> _dtoTypes = new();
private readonly Dictionary<MediaVaultType, Type> _paramsTypes = new(); private readonly Dictionary<MediaVaultType, Type> _paramsTypes = new();
private readonly Dictionary<MediaVaultType, Type> _metaDataTypes = new(); private readonly Dictionary<MediaVaultType, Type> _metaDataTypes = new();
// Reverse mapping: Type -> MediaVaultType
private readonly Dictionary<Type, MediaVaultType> _typeToVaultType = new();
public SimpleMediaTypeRegistry() public SimpleMediaTypeRegistry()
{ {
@@ -76,6 +79,9 @@ public class SimpleMediaTypeRegistry : IMediaTypeRegistry
_paramsTypes[vaultType] = typeof(TParams); _paramsTypes[vaultType] = typeof(TParams);
_metaDataTypes[vaultType] = typeof(TMetaData); _metaDataTypes[vaultType] = typeof(TMetaData);
// Populate reverse mapping
_typeToVaultType[typeof(TBinary)] = vaultType;
if (vaultFactory != null) if (vaultFactory != null)
_vaultFactories[vaultType] = vaultFactory; _vaultFactories[vaultType] = vaultFactory;
} }
@@ -146,4 +152,22 @@ public class SimpleMediaTypeRegistry : IMediaTypeRegistry
public Type GetMetaDataType(MediaVaultType vaultType) => public Type GetMetaDataType(MediaVaultType vaultType) =>
_metaDataTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}"); _metaDataTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
public MediaVaultType GetVaultType(Type binaryType)
{
if (_typeToVaultType.TryGetValue(binaryType, out var vaultType))
return vaultType;
// Check inheritance hierarchy for derived types
foreach (var kvp in _typeToVaultType)
{
if (kvp.Key.IsAssignableFrom(binaryType))
return kvp.Value;
}
throw new ArgumentException($"Cannot infer MediaVaultType for {binaryType.Name}. Type not registered.");
}
public MediaVaultType GetVaultType<T>() where T : FileBinary
=> GetVaultType(typeof(T));
} }
+3 -3
View File
@@ -21,10 +21,10 @@ namespace DeepDrftContent
private static async Task InitializeTrackVault(FileDatabase.Services.FileDatabase fileDatabase) private static async Task InitializeTrackVault(FileDatabase.Services.FileDatabase fileDatabase)
{ {
var vaultKey = new EntryKey("tracks", MediaVaultType.Audio); const string vaultId = "tracks";
if (!fileDatabase.HasVault(vaultKey)) if (!fileDatabase.HasVault(vaultId))
{ {
await fileDatabase.CreateVaultAsync(vaultKey); await fileDatabase.CreateVaultAsync(vaultId, MediaVaultType.Audio);
} }
} }
} }
+6 -13
View File
@@ -64,12 +64,12 @@ public class FileDatabaseTests
Assert.That(_fileDatabase, Is.Not.Null); Assert.That(_fileDatabase, Is.Not.Null);
// Act // Act
await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey); await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultType);
// Assert // Assert
Assert.That(_fileDatabase.GetIndexSize(), Is.EqualTo(1), "Index should contain one element"); Assert.That(_fileDatabase.GetIndexSize(), Is.EqualTo(1), "Index should contain one element");
var vaultDirectory = Path.Combine(_testDatabasePath, TestData.TestKeys.ImageVaultKey.Key); var vaultDirectory = Path.Combine(_testDatabasePath, TestData.TestKeys.ImageVaultKey);
Assert.That(Directory.Exists(vaultDirectory), Is.True, "Vault directory should exist"); Assert.That(Directory.Exists(vaultDirectory), Is.True, "Vault directory should exist");
} }
@@ -80,12 +80,11 @@ public class FileDatabaseTests
_fileDatabase = await FileDatabase.FromAsync(_testDatabasePath); _fileDatabase = await FileDatabase.FromAsync(_testDatabasePath);
Assert.That(_fileDatabase, Is.Not.Null); Assert.That(_fileDatabase, Is.Not.Null);
await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey); await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultType);
var testImage = TestData.CreateTestImageBinary(1.0); var testImage = TestData.CreateTestImageBinary(1.0);
// Act // Act
await _fileDatabase.RegisterResourceAsync( await _fileDatabase.RegisterResourceAsync(
MediaVaultType.Image,
TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultKey,
TestData.TestKeys.TestImageEntry, TestData.TestKeys.TestImageEntry,
testImage); testImage);
@@ -104,18 +103,16 @@ public class FileDatabaseTests
_fileDatabase = await FileDatabase.FromAsync(_testDatabasePath); _fileDatabase = await FileDatabase.FromAsync(_testDatabasePath);
Assert.That(_fileDatabase, Is.Not.Null); Assert.That(_fileDatabase, Is.Not.Null);
await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey); await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultType);
var testImage = TestData.CreateTestImageBinary(1.0); var testImage = TestData.CreateTestImageBinary(1.0);
await _fileDatabase.RegisterResourceAsync( await _fileDatabase.RegisterResourceAsync(
MediaVaultType.Image,
TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultKey,
TestData.TestKeys.TestImageEntry, TestData.TestKeys.TestImageEntry,
testImage); testImage);
// Act // Act
var loadedMedia = await _fileDatabase.LoadResourceAsync<ImageBinary>( var loadedMedia = await _fileDatabase.LoadResourceAsync<ImageBinary>(
MediaVaultType.Image,
TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultKey,
TestData.TestKeys.TestImageEntry); TestData.TestKeys.TestImageEntry);
@@ -139,7 +136,6 @@ public class FileDatabaseTests
Assert.DoesNotThrowAsync(async () => Assert.DoesNotThrowAsync(async () =>
{ {
await _fileDatabase.LoadResourceAsync<ImageBinary>( await _fileDatabase.LoadResourceAsync<ImageBinary>(
MediaVaultType.Image,
TestData.TestKeys.NonExistentVaultKey, TestData.TestKeys.NonExistentVaultKey,
TestData.TestKeys.NonExistentEntryKey); TestData.TestKeys.NonExistentEntryKey);
}, "Should not throw exceptions when accessing nonexistent vault"); }, "Should not throw exceptions when accessing nonexistent vault");
@@ -152,13 +148,12 @@ public class FileDatabaseTests
_fileDatabase = await FileDatabase.FromAsync(_testDatabasePath); _fileDatabase = await FileDatabase.FromAsync(_testDatabasePath);
Assert.That(_fileDatabase, Is.Not.Null); Assert.That(_fileDatabase, Is.Not.Null);
await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey); await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultType);
// Act & Assert - Should not throw exception when accessing nonexistent resource // Act & Assert - Should not throw exception when accessing nonexistent resource
Assert.DoesNotThrowAsync(async () => Assert.DoesNotThrowAsync(async () =>
{ {
await _fileDatabase.LoadResourceAsync<ImageBinary>( await _fileDatabase.LoadResourceAsync<ImageBinary>(
MediaVaultType.Image,
TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultKey,
TestData.TestKeys.NonExistentEntryKey); TestData.TestKeys.NonExistentEntryKey);
}, "Should not throw exceptions when accessing nonexistent resource"); }, "Should not throw exceptions when accessing nonexistent resource");
@@ -171,11 +166,10 @@ public class FileDatabaseTests
_fileDatabase = await FileDatabase.FromAsync(_testDatabasePath); _fileDatabase = await FileDatabase.FromAsync(_testDatabasePath);
Assert.That(_fileDatabase, Is.Not.Null); Assert.That(_fileDatabase, Is.Not.Null);
await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey); await _fileDatabase.CreateVaultAsync(TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultType);
var testImage = TestData.CreateTestImageBinary(1.0); var testImage = TestData.CreateTestImageBinary(1.0);
await _fileDatabase.RegisterResourceAsync( await _fileDatabase.RegisterResourceAsync(
MediaVaultType.Image,
TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultKey,
TestData.TestKeys.TestImageEntry, TestData.TestKeys.TestImageEntry,
testImage); testImage);
@@ -198,7 +192,6 @@ public class FileDatabaseTests
// Verify resource can be loaded // Verify resource can be loaded
var loadedMedia = await reloadedDatabase.LoadResourceAsync<ImageBinary>( var loadedMedia = await reloadedDatabase.LoadResourceAsync<ImageBinary>(
MediaVaultType.Image,
TestData.TestKeys.ImageVaultKey, TestData.TestKeys.ImageVaultKey,
TestData.TestKeys.TestImageEntry); TestData.TestKeys.TestImageEntry);
+36 -113
View File
@@ -38,10 +38,10 @@ public class IndexSystemTests
} }
/// <summary> /// <summary>
/// Helper method to create test entry keys - DRY principle /// Helper method to create test entry IDs - DRY principle
/// </summary> /// </summary>
protected static EntryKey CreateTestEntryKey(string key, MediaVaultType type = MediaVaultType.Image) protected static string CreateTestEntryId(string key)
=> new(key, type); => key;
/// <summary> /// <summary>
/// Helper method to create test metadata - DRY principle /// Helper method to create test metadata - DRY principle
@@ -69,7 +69,7 @@ public class IndexSystemTests
public async Task CreateIndexAsync_DirectoryType_CreatesDirectoryIndex() public async Task CreateIndexAsync_DirectoryType_CreatesDirectoryIndex()
{ {
// Act // Act
var index = await _factory.CreateIndexAsync(IndexType.Directory, TestDirectory); var index = await _factory.CreateDirectoryIndexAsync(TestDirectory);
// Assert // Assert
Assert.That(index, Is.Not.Null, "Index should be created"); Assert.That(index, Is.Not.Null, "Index should be created");
@@ -81,7 +81,7 @@ public class IndexSystemTests
public async Task CreateIndexAsync_VaultType_CreatesVaultIndex() public async Task CreateIndexAsync_VaultType_CreatesVaultIndex()
{ {
// Act // Act
var index = await _factory.CreateIndexAsync(IndexType.Vault, TestDirectory); var index = await _factory.CreateVaultIndexAsync(TestDirectory, MediaVaultType.Media);
// Assert // Assert
Assert.That(index, Is.Not.Null, "Index should be created"); Assert.That(index, Is.Not.Null, "Index should be created");
@@ -89,23 +89,12 @@ public class IndexSystemTests
Assert.That(File.Exists(IndexPath), Is.True, "Index file should be created"); Assert.That(File.Exists(IndexPath), Is.True, "Index file should be created");
} }
[Test]
public void CreateIndexAsync_InvalidType_ThrowsArgumentException()
{
// Arrange
var invalidType = (IndexType)999;
// Act & Assert
Assert.ThrowsAsync<ArgumentException>(async () =>
await _factory.CreateIndexAsync(invalidType, TestDirectory),
"Should throw for invalid index type");
}
[Test] [Test]
public async Task LoadIndexAsync_ExistingDirectoryIndex_LoadsSuccessfully() public async Task LoadIndexAsync_ExistingDirectoryIndex_LoadsSuccessfully()
{ {
// Arrange - Create an index first // Arrange - Create an index first
await _factory.CreateIndexAsync(IndexType.Directory, TestDirectory); await _factory.CreateDirectoryIndexAsync(TestDirectory);
// Act // Act
var loadedIndex = await _factory.LoadIndexAsync(IndexType.Directory, TestDirectory); var loadedIndex = await _factory.LoadIndexAsync(IndexType.Directory, TestDirectory);
@@ -119,7 +108,7 @@ public class IndexSystemTests
public async Task LoadIndexAsync_ExistingVaultIndex_LoadsSuccessfully() public async Task LoadIndexAsync_ExistingVaultIndex_LoadsSuccessfully()
{ {
// Arrange - Create an index first // Arrange - Create an index first
await _factory.CreateIndexAsync(IndexType.Vault, TestDirectory); await _factory.CreateVaultIndexAsync(TestDirectory, MediaVaultType.Media);
// Act // Act
var loadedIndex = await _factory.LoadIndexAsync(IndexType.Vault, TestDirectory); var loadedIndex = await _factory.LoadIndexAsync(IndexType.Vault, TestDirectory);
@@ -142,11 +131,11 @@ public class IndexSystemTests
public async Task LoadOrCreateIndexAsync_ExistingIndex_LoadsExisting() public async Task LoadOrCreateIndexAsync_ExistingIndex_LoadsExisting()
{ {
// Arrange - Create an index with data // Arrange - Create an index with data
var originalIndex = await _factory.CreateIndexAsync(IndexType.Directory, TestDirectory); var originalIndex = await _factory.CreateDirectoryIndexAsync(TestDirectory);
Assert.That(originalIndex, Is.TypeOf<DirectoryIndex>(), "Should create DirectoryIndex"); Assert.That(originalIndex, Is.TypeOf<DirectoryIndex>(), "Should create DirectoryIndex");
var directoryIndex = (DirectoryIndex)originalIndex!; var directoryIndex = (DirectoryIndex)originalIndex!;
var testKey = CreateTestEntryKey("test-entry"); var testKey = CreateTestEntryId("test-entry");
directoryIndex.PutEntry(testKey); directoryIndex.PutEntry(testKey);
// Save the modified index // Save the modified index
@@ -154,30 +143,28 @@ public class IndexSystemTests
await FileUtils.PutObjectAsync(IndexPath, indexData); await FileUtils.PutObjectAsync(IndexPath, indexData);
// Act // Act
var loadedIndex = await _factory.LoadOrCreateIndexAsync(IndexType.Directory, TestDirectory); var loadedIndex = await _factory.LoadOrCreateDirectoryIndexAsync(TestDirectory);
// Assert // Assert
Assert.That(loadedIndex, Is.Not.Null, "Index should be loaded"); Assert.That(loadedIndex, Is.Not.Null, "Index should be loaded");
Assert.That(loadedIndex, Is.TypeOf<DirectoryIndex>(), "Should load DirectoryIndex"); Assert.That(loadedIndex, Is.TypeOf<DirectoryIndex>(), "Should load DirectoryIndex");
Assert.That(loadedIndex, Is.InstanceOf<IEntryQueryable>(), "Should implement IEntryQueryable"); Assert.That(loadedIndex, Is.InstanceOf<IEntryQueryable>(), "Should implement IEntryQueryable");
var queryableIndex = (IEntryQueryable)loadedIndex!; Assert.That(loadedIndex.GetEntriesSize(), Is.EqualTo(1), "Should preserve existing entries");
Assert.That(queryableIndex.GetEntriesSize(), Is.EqualTo(1), "Should preserve existing entries");
} }
[Test] [Test]
public async Task LoadOrCreateIndexAsync_NonExistentIndex_CreatesNew() public async Task LoadOrCreateIndexAsync_NonExistentIndex_CreatesNew()
{ {
// Act // Act
var index = await _factory.LoadOrCreateIndexAsync(IndexType.Directory, TestDirectory); var index = await _factory.LoadOrCreateDirectoryIndexAsync(TestDirectory);
// Assert // Assert
Assert.That(index, Is.Not.Null, "Index should be created"); Assert.That(index, Is.Not.Null, "Index should be created");
Assert.That(index, Is.TypeOf<DirectoryIndex>(), "Should create DirectoryIndex"); Assert.That(index, Is.TypeOf<DirectoryIndex>(), "Should create DirectoryIndex");
Assert.That(index, Is.InstanceOf<IEntryQueryable>(), "Should implement IEntryQueryable"); Assert.That(index, Is.InstanceOf<IEntryQueryable>(), "Should implement IEntryQueryable");
var queryableIndex = (IEntryQueryable)index!; Assert.That(index.GetEntriesSize(), Is.EqualTo(0), "New index should be empty");
Assert.That(queryableIndex.GetEntriesSize(), Is.EqualTo(0), "New index should be empty");
} }
[Test] [Test]
@@ -185,7 +172,7 @@ public class IndexSystemTests
{ {
// Arrange // Arrange
var directoryIndex = new DirectoryIndex(new DirectoryIndexData("test")); var directoryIndex = new DirectoryIndex(new DirectoryIndexData("test"));
var testKey = CreateTestEntryKey("test-entry"); var testKey = CreateTestEntryId("test-entry");
directoryIndex.PutEntry(testKey); directoryIndex.PutEntry(testKey);
// Act // Act
@@ -202,7 +189,7 @@ public class IndexSystemTests
{ {
// Arrange // Arrange
var vaultIndex = new VaultIndex(new VaultIndexData("test")); var vaultIndex = new VaultIndex(new VaultIndexData("test"));
var testKey = CreateTestEntryKey("test-entry"); var testKey = CreateTestEntryId("test-entry");
var testMetaData = CreateTestMetaData("test-entry"); var testMetaData = CreateTestMetaData("test-entry");
vaultIndex.PutEntry(testKey, testMetaData); vaultIndex.PutEntry(testKey, testMetaData);
@@ -273,7 +260,7 @@ public class IndexSystemTests
public void PutEntry_NewEntry_AddsSuccessfully() public void PutEntry_NewEntry_AddsSuccessfully()
{ {
// Arrange // Arrange
var testKey = CreateTestEntryKey("new-entry"); var testKey = CreateTestEntryId("new-entry");
// Act // Act
_directoryIndex.PutEntry(testKey); _directoryIndex.PutEntry(testKey);
@@ -288,7 +275,7 @@ public class IndexSystemTests
public void PutEntry_DuplicateEntry_DoesNotDuplicate() public void PutEntry_DuplicateEntry_DoesNotDuplicate()
{ {
// Arrange // Arrange
var testKey = CreateTestEntryKey("duplicate-entry"); var testKey = CreateTestEntryId("duplicate-entry");
_directoryIndex.PutEntry(testKey); _directoryIndex.PutEntry(testKey);
// Act // Act
@@ -304,9 +291,9 @@ public class IndexSystemTests
// Arrange // Arrange
var keys = new[] var keys = new[]
{ {
CreateTestEntryKey("entry1"), CreateTestEntryId("entry1"),
CreateTestEntryKey("entry2"), CreateTestEntryId("entry2"),
CreateTestEntryKey("entry3") CreateTestEntryId("entry3")
}; };
// Act // Act
@@ -327,7 +314,7 @@ public class IndexSystemTests
public void HasEntry_ExistingEntry_ReturnsTrue() public void HasEntry_ExistingEntry_ReturnsTrue()
{ {
// Arrange // Arrange
var testKey = CreateTestEntryKey("existing-entry"); var testKey = CreateTestEntryId("existing-entry");
_directoryIndex.PutEntry(testKey); _directoryIndex.PutEntry(testKey);
// Act & Assert // Act & Assert
@@ -338,7 +325,7 @@ public class IndexSystemTests
public void HasEntry_NonExistentEntry_ReturnsFalse() public void HasEntry_NonExistentEntry_ReturnsFalse()
{ {
// Arrange // Arrange
var testKey = CreateTestEntryKey("non-existent"); var testKey = CreateTestEntryId("non-existent");
// Act & Assert // Act & Assert
Assert.That(_directoryIndex.HasEntry(testKey), Is.False, "Should not find non-existent entry"); Assert.That(_directoryIndex.HasEntry(testKey), Is.False, "Should not find non-existent entry");
@@ -350,9 +337,9 @@ public class IndexSystemTests
// Arrange // Arrange
var keys = new[] var keys = new[]
{ {
CreateTestEntryKey("entry1"), CreateTestEntryId("entry1"),
CreateTestEntryKey("entry2"), CreateTestEntryId("entry2"),
CreateTestEntryKey("entry3") CreateTestEntryId("entry3")
}; };
foreach (var key in keys) foreach (var key in keys)
@@ -399,7 +386,7 @@ public class IndexSystemTests
public void PutEntry_NewEntryWithMetadata_AddsSuccessfully() public void PutEntry_NewEntryWithMetadata_AddsSuccessfully()
{ {
// Arrange // Arrange
var testKey = CreateTestEntryKey("new-entry"); var testKey = CreateTestEntryId("new-entry");
var testMetaData = CreateTestMetaData("new-entry"); var testMetaData = CreateTestMetaData("new-entry");
// Act // Act
@@ -415,7 +402,7 @@ public class IndexSystemTests
public void PutEntry_DuplicateEntry_UpdatesMetadata() public void PutEntry_DuplicateEntry_UpdatesMetadata()
{ {
// Arrange // Arrange
var testKey = CreateTestEntryKey("duplicate-entry"); var testKey = CreateTestEntryId("duplicate-entry");
var originalMetaData = CreateTestMetaData("original"); var originalMetaData = CreateTestMetaData("original");
var updatedMetaData = CreateTestMetaData("updated"); var updatedMetaData = CreateTestMetaData("updated");
@@ -433,7 +420,7 @@ public class IndexSystemTests
public void GetEntry_ExistingEntry_ReturnsMetadata() public void GetEntry_ExistingEntry_ReturnsMetadata()
{ {
// Arrange // Arrange
var testKey = CreateTestEntryKey("existing-entry"); var testKey = CreateTestEntryId("existing-entry");
var testMetaData = CreateTestMetaData("existing-entry"); var testMetaData = CreateTestMetaData("existing-entry");
_vaultIndex.PutEntry(testKey, testMetaData); _vaultIndex.PutEntry(testKey, testMetaData);
@@ -448,7 +435,7 @@ public class IndexSystemTests
public void GetEntry_NonExistentEntry_ReturnsNull() public void GetEntry_NonExistentEntry_ReturnsNull()
{ {
// Arrange // Arrange
var testKey = CreateTestEntryKey("non-existent"); var testKey = CreateTestEntryId("non-existent");
// Act // Act
var retrievedMetaData = _vaultIndex.GetEntry(testKey); var retrievedMetaData = _vaultIndex.GetEntry(testKey);
@@ -463,9 +450,9 @@ public class IndexSystemTests
// Arrange // Arrange
var entries = new[] var entries = new[]
{ {
(CreateTestEntryKey("entry1"), CreateTestMetaData("entry1", ".png")), (CreateTestEntryId("entry1"), CreateTestMetaData("entry1", ".png")),
(CreateTestEntryKey("entry2"), CreateTestMetaData("entry2", ".jpg")), (CreateTestEntryId("entry2"), CreateTestMetaData("entry2", ".jpg")),
(CreateTestEntryKey("entry3"), CreateTestMetaData("entry3", ".gif")) (CreateTestEntryId("entry3"), CreateTestMetaData("entry3", ".gif"))
}; };
// Act // Act
@@ -484,70 +471,6 @@ public class IndexSystemTests
} }
} }
/// <summary>
/// Tests for IndexFactory - Single Responsibility Principle
/// </summary>
[TestFixture]
public class IndexFactoryTests : IndexTestBase
{
[Test]
public async Task IndexFactory_DirectoryType_BuildsDirectoryIndex()
{
// Arrange
var factory = new IndexFactory(TestDirectory, IndexType.Directory);
// Act
var index = await factory.BuildIndexAsync();
// Assert
Assert.That(index, Is.Not.Null, "Index should be built");
Assert.That(index, Is.TypeOf<DirectoryIndex>(), "Should build DirectoryIndex");
Assert.That(File.Exists(IndexPath), Is.True, "Index file should be created");
}
[Test]
public async Task IndexFactory_VaultType_BuildsVaultIndex()
{
// Arrange
var factory = new IndexFactory(TestDirectory, IndexType.Vault);
// Act
var index = await factory.BuildIndexAsync();
// Assert
Assert.That(index, Is.Not.Null, "Index should be built");
Assert.That(index, Is.TypeOf<VaultIndex>(), "Should build VaultIndex");
Assert.That(File.Exists(IndexPath), Is.True, "Index file should be created");
}
[Test]
public async Task IndexFactory_ExistingIndex_LoadsExistingData()
{
// Arrange - Create index with data
var factory = new IndexFactory(TestDirectory, IndexType.Directory);
var originalIndex = await factory.BuildIndexAsync();
var directoryIndex = (DirectoryIndex)originalIndex!;
var testKey = CreateTestEntryKey("persisted-entry");
directoryIndex.PutEntry(testKey);
// Save the index manually
var factoryService = new IndexFactoryService();
var indexData = factoryService.CreateIndexData(IndexType.Directory, directoryIndex);
await FileUtils.PutObjectAsync(IndexPath, indexData);
// Act - Create new factory and build
var newFactory = new IndexFactory(TestDirectory, IndexType.Directory);
var loadedIndex = await newFactory.BuildIndexAsync();
// Assert
Assert.That(loadedIndex, Is.Not.Null, "Index should be loaded");
Assert.That(loadedIndex, Is.InstanceOf<IEntryQueryable>(), "Should implement IEntryQueryable");
var queryableIndex = (IEntryQueryable)loadedIndex!;
Assert.That(queryableIndex.GetEntriesSize(), Is.EqualTo(1), "Should load existing entry");
Assert.That(queryableIndex.HasEntry(testKey), Is.True, "Should contain persisted entry");
}
}
/// <summary> /// <summary>
/// Integration tests for IndexDirectory classes - Open/Closed Principle /// Integration tests for IndexDirectory classes - Open/Closed Principle
@@ -563,10 +486,10 @@ public class IndexSystemTests
// Arrange // Arrange
var database = await FileDatabase.FromAsync(TestDirectory); var database = await FileDatabase.FromAsync(TestDirectory);
var testVaultKey = CreateTestEntryKey("test-vault"); var testVaultKey = CreateTestEntryId("test-vault");
// Act - This internally uses DirectoryIndexDirectory.AddToIndexAsync // Act - This internally uses DirectoryIndexDirectory.AddToIndexAsync
await database!.CreateVaultAsync(testVaultKey); await database!.CreateVaultAsync(testVaultKey, MediaVaultType.Image);
// Assert // Assert
Assert.That(database.HasIndexEntry(testVaultKey), Is.True, "Should contain added entry"); Assert.That(database.HasIndexEntry(testVaultKey), Is.True, "Should contain added entry");
@@ -586,11 +509,11 @@ public class IndexSystemTests
// Arrange // Arrange
var vault = await ImageVault.FromAsync(TestDirectory); var vault = await ImageVault.FromAsync(TestDirectory);
var testKey = CreateTestEntryKey("test-entry"); var testKey = CreateTestEntryId("test-entry");
var testImage = TestData.CreateTestImageBinary(1.0); var testImage = TestData.CreateTestImageBinary(1.0);
// Act - This internally uses VaultIndexDirectory.AddToIndexAsync // Act - This internally uses VaultIndexDirectory.AddToIndexAsync
await vault!.AddEntryAsync(MediaVaultType.Image, testKey, testImage); await vault!.AddEntryAsync(testKey, testImage);
// Assert // Assert
Assert.That(vault.HasIndexEntry(testKey), Is.True, "Should contain added entry"); Assert.That(vault.HasIndexEntry(testKey), Is.True, "Should contain added entry");
+55 -55
View File
@@ -38,10 +38,10 @@ public class MediaVaultTests
} }
/// <summary> /// <summary>
/// Helper method to create test entry keys - DRY principle /// Helper method to create test entry IDs - DRY principle
/// </summary> /// </summary>
protected static EntryKey CreateTestEntryKey(string key, MediaVaultType type = MediaVaultType.Image) protected static string CreateTestEntryId(string key)
=> new(key, type); => key;
/// <summary> /// <summary>
/// Helper method to create test media files - DRY principle /// Helper method to create test media files - DRY principle
@@ -113,11 +113,11 @@ public class MediaVaultTests
public async Task AddEntryAsync_ImageBinary_AddsToIndexAndCreatesFile() public async Task AddEntryAsync_ImageBinary_AddsToIndexAndCreatesFile()
{ {
// Arrange // Arrange
var entryKey = CreateTestEntryKey("test-image"); var entryKey = CreateTestEntryId("test-image");
var imageBinary = TestData.CreateTestImageBinary(1.5); var imageBinary = TestData.CreateTestImageBinary(1.5);
// Act // Act
await _imageVault.AddEntryAsync(MediaVaultType.Image, entryKey, imageBinary); await _imageVault.AddEntryAsync(entryKey, imageBinary);
// Assert // Assert
Assert.That(_imageVault.HasIndexEntry(entryKey), Is.True, "Should add to index"); Assert.That(_imageVault.HasIndexEntry(entryKey), Is.True, "Should add to index");
@@ -132,15 +132,15 @@ public class MediaVaultTests
// Arrange // Arrange
var entries = new[] var entries = new[]
{ {
(CreateTestEntryKey("image1"), TestData.CreateTestImageBinary(1.0)), (CreateTestEntryId("image1"), TestData.CreateTestImageBinary(1.0)),
(CreateTestEntryKey("image2"), TestData.CreateTestImageBinary(1.5)), (CreateTestEntryId("image2"), TestData.CreateTestImageBinary(1.5)),
(CreateTestEntryKey("image3"), TestData.CreateTestImageBinary(2.0)) (CreateTestEntryId("image3"), TestData.CreateTestImageBinary(2.0))
}; };
// Act // Act
foreach (var (key, binary) in entries) foreach (var (key, binary) in entries)
{ {
await _imageVault.AddEntryAsync(MediaVaultType.Image, key, binary); await _imageVault.AddEntryAsync(key, binary);
} }
// Assert // Assert
@@ -149,7 +149,7 @@ public class MediaVaultTests
foreach (var (key, binary) in entries) foreach (var (key, binary) in entries)
{ {
Assert.That(_imageVault.HasIndexEntry(key), Is.True, $"Should contain {key} in index"); Assert.That(_imageVault.HasIndexEntry(key), Is.True, $"Should contain {key} in index");
var expectedFilePath = Path.Combine(TestDirectory, $"{key.Key}.png"); var expectedFilePath = Path.Combine(TestDirectory, $"{key}.png");
AssertMediaFileExists(expectedFilePath, binary.Buffer); AssertMediaFileExists(expectedFilePath, binary.Buffer);
} }
} }
@@ -158,12 +158,12 @@ public class MediaVaultTests
public async Task GetEntryAsync_ExistingImage_ReturnsImageBinary() public async Task GetEntryAsync_ExistingImage_ReturnsImageBinary()
{ {
// Arrange // Arrange
var entryKey = CreateTestEntryKey("existing-image"); var entryKey = CreateTestEntryId("existing-image");
var originalImage = TestData.CreateTestImageBinary(1.77); var originalImage = TestData.CreateTestImageBinary(1.77);
await _imageVault.AddEntryAsync(MediaVaultType.Image, entryKey, originalImage); await _imageVault.AddEntryAsync(entryKey, originalImage);
// Act // Act
var retrievedImage = await _imageVault.GetEntryAsync<ImageBinary>(MediaVaultType.Image, entryKey); var retrievedImage = await _imageVault.GetEntryAsync<ImageBinary>(entryKey);
// Assert // Assert
Assert.That(retrievedImage, Is.Not.Null, "Should retrieve image"); Assert.That(retrievedImage, Is.Not.Null, "Should retrieve image");
@@ -176,10 +176,10 @@ public class MediaVaultTests
public async Task GetEntryAsync_NonExistentImage_ReturnsNull() public async Task GetEntryAsync_NonExistentImage_ReturnsNull()
{ {
// Arrange // Arrange
var nonExistentKey = CreateTestEntryKey("non-existent"); var nonExistentKey = CreateTestEntryId("non-existent");
// Act // Act
var retrievedImage = await _imageVault.GetEntryAsync<ImageBinary>(MediaVaultType.Image, nonExistentKey); var retrievedImage = await _imageVault.GetEntryAsync<ImageBinary>(nonExistentKey);
// Assert // Assert
Assert.That(retrievedImage, Is.Null, "Should return null for non-existent image"); Assert.That(retrievedImage, Is.Null, "Should return null for non-existent image");
@@ -189,16 +189,16 @@ public class MediaVaultTests
public async Task GetEntryAsync_IndexEntryExistsButFileDeleted_ReturnsNull() public async Task GetEntryAsync_IndexEntryExistsButFileDeleted_ReturnsNull()
{ {
// Arrange // Arrange
var entryKey = CreateTestEntryKey("deleted-file"); var entryKey = CreateTestEntryId("deleted-file");
var imageBinary = TestData.CreateTestImageBinary(1.0); var imageBinary = TestData.CreateTestImageBinary(1.0);
await _imageVault.AddEntryAsync(MediaVaultType.Image, entryKey, imageBinary); await _imageVault.AddEntryAsync(entryKey, imageBinary);
// Delete the physical file but leave index entry // Delete the physical file but leave index entry
var filePath = Path.Combine(TestDirectory, "deleted-file.png"); var filePath = Path.Combine(TestDirectory, "deleted-file.png");
File.Delete(filePath); File.Delete(filePath);
// Act // Act
var retrievedImage = await _imageVault.GetEntryAsync<ImageBinary>(MediaVaultType.Image, entryKey); var retrievedImage = await _imageVault.GetEntryAsync<ImageBinary>(entryKey);
// Assert // Assert
Assert.That(retrievedImage, Is.Null, "Should return null when file is missing"); Assert.That(retrievedImage, Is.Null, "Should return null when file is missing");
@@ -208,18 +208,18 @@ public class MediaVaultTests
public async Task AddEntryAsync_DuplicateKey_UpdatesExistingEntry() public async Task AddEntryAsync_DuplicateKey_UpdatesExistingEntry()
{ {
// Arrange // Arrange
var entryKey = CreateTestEntryKey("duplicate-key"); var entryKey = CreateTestEntryId("duplicate-key");
var originalImage = TestData.CreateTestImageBinary(1.0); var originalImage = TestData.CreateTestImageBinary(1.0);
var updatedImage = TestData.CreateTestImageBinary(2.0); var updatedImage = TestData.CreateTestImageBinary(2.0);
// Act // Act
await _imageVault.AddEntryAsync(MediaVaultType.Image, entryKey, originalImage); await _imageVault.AddEntryAsync(entryKey, originalImage);
await _imageVault.AddEntryAsync(MediaVaultType.Image, entryKey, updatedImage); await _imageVault.AddEntryAsync(entryKey, updatedImage);
// Assert // Assert
Assert.That(_imageVault.GetIndexSize(), Is.EqualTo(1), "Should still have only one entry"); Assert.That(_imageVault.GetIndexSize(), Is.EqualTo(1), "Should still have only one entry");
var retrievedImage = await _imageVault.GetEntryAsync<ImageBinary>(MediaVaultType.Image, entryKey); var retrievedImage = await _imageVault.GetEntryAsync<ImageBinary>(entryKey);
Assert.That(retrievedImage, Is.Not.Null, "Should retrieve updated image"); Assert.That(retrievedImage, Is.Not.Null, "Should retrieve updated image");
Assert.That(retrievedImage!.AspectRatio, Is.EqualTo(2.0), "Should have updated aspect ratio"); Assert.That(retrievedImage!.AspectRatio, Is.EqualTo(2.0), "Should have updated aspect ratio");
} }
@@ -257,11 +257,11 @@ public class MediaVaultTests
public async Task AddEntryAsync_AudioBinary_AddsToIndexAndCreatesFile() public async Task AddEntryAsync_AudioBinary_AddsToIndexAndCreatesFile()
{ {
// Arrange // Arrange
var entryKey = CreateTestEntryKey("test-audio", MediaVaultType.Audio); var entryKey = CreateTestEntryId("test-audio");
var audioBinary = TestData.CreateTestAudioBinary(120.0, 320); var audioBinary = TestData.CreateTestAudioBinary(120.0, 320);
// Act // Act
await _audioVault.AddEntryAsync(MediaVaultType.Audio, entryKey, audioBinary); await _audioVault.AddEntryAsync(entryKey, audioBinary);
// Assert // Assert
Assert.That(_audioVault.HasIndexEntry(entryKey), Is.True, "Should add to index"); Assert.That(_audioVault.HasIndexEntry(entryKey), Is.True, "Should add to index");
@@ -274,12 +274,12 @@ public class MediaVaultTests
public async Task GetEntryAsync_ExistingAudio_ReturnsAudioBinary() public async Task GetEntryAsync_ExistingAudio_ReturnsAudioBinary()
{ {
// Arrange // Arrange
var entryKey = CreateTestEntryKey("existing-audio", MediaVaultType.Audio); var entryKey = CreateTestEntryId("existing-audio");
var originalAudio = TestData.CreateTestAudioBinary(180.5, 256); var originalAudio = TestData.CreateTestAudioBinary(180.5, 256);
await _audioVault.AddEntryAsync(MediaVaultType.Audio, entryKey, originalAudio); await _audioVault.AddEntryAsync(entryKey, originalAudio);
// Act // Act
var retrievedAudio = await _audioVault.GetEntryAsync<AudioBinary>(MediaVaultType.Audio, entryKey); var retrievedAudio = await _audioVault.GetEntryAsync<AudioBinary>(entryKey);
// Assert // Assert
Assert.That(retrievedAudio, Is.Not.Null, "Should retrieve audio"); Assert.That(retrievedAudio, Is.Not.Null, "Should retrieve audio");
@@ -337,9 +337,9 @@ public class MediaVaultTests
return (string)method!.Invoke(_vault, new object[] { mediaKey })!; return (string)method!.Invoke(_vault, new object[] { mediaKey })!;
} }
public bool HasIndexEntry(EntryKey entryKey) => _vault.HasIndexEntry(entryKey); public bool HasIndexEntry(string entryId) => _vault.HasIndexEntry(entryId);
public Task AddEntryAsync(MediaVaultType vaultType, EntryKey entryKey, object media) => public Task AddEntryAsync(string entryId, FileBinary media) =>
_vault.AddEntryAsync(vaultType, entryKey, media); _vault.AddEntryAsync(entryId, media);
} }
[Test] [Test]
@@ -394,19 +394,19 @@ public class MediaVaultTests
} }
[Test] [Test]
public async Task AddEntryAsync_UnsupportedMediaType_ThrowsArgumentException() public async Task AddEntryAsync_BaseFileBinary_ThrowsArgumentException()
{ {
// Arrange // Arrange
var vault = await TestMediaVaultWrapper.FromAsync(TestDirectory); var vault = await TestMediaVaultWrapper.FromAsync(TestDirectory);
Assert.That(vault, Is.Not.Null, "Vault should be created"); Assert.That(vault, Is.Not.Null, "Vault should be created");
var entryKey = CreateTestEntryKey("test"); var entryKey = CreateTestEntryId("test");
var unsupportedMedia = new object(); // Not a supported media type var baseFileBinary = new FileBinary(new FileBinaryParams(TestData.TestPngBytes, TestData.TestPngBytes.Length)); // Base FileBinary, not a specific media type
// Act & Assert // Act & Assert
Assert.ThrowsAsync<ArgumentException>(async () => Assert.ThrowsAsync<ArgumentException>(async () =>
await vault!.AddEntryAsync(MediaVaultType.Image, entryKey, unsupportedMedia), await vault!.AddEntryAsync(entryKey, baseFileBinary),
"Should throw for unsupported media type"); "Should throw for base FileBinary type - must be specific media type");
} }
[Test] [Test]
@@ -416,11 +416,11 @@ public class MediaVaultTests
var vault = await TestMediaVaultWrapper.FromAsync(TestDirectory); var vault = await TestMediaVaultWrapper.FromAsync(TestDirectory);
Assert.That(vault, Is.Not.Null, "Vault should be created"); Assert.That(vault, Is.Not.Null, "Vault should be created");
var entryKey = CreateTestEntryKey("test-media"); var entryKey = CreateTestEntryId("test-media");
var imageBinary = TestData.CreateTestImageBinary(1.0); // Use existing test data helper var imageBinary = TestData.CreateTestImageBinary(1.0); // Use existing test data helper
// Act // Act
await vault!.AddEntryAsync(MediaVaultType.Image, entryKey, imageBinary); await vault!.AddEntryAsync(entryKey, imageBinary);
// Assert // Assert
Assert.That(vault.HasIndexEntry(entryKey), Is.True, "Should add entry to index"); Assert.That(vault.HasIndexEntry(entryKey), Is.True, "Should add entry to index");
@@ -456,10 +456,10 @@ public class MediaVaultTests
{ {
// Arrange // Arrange
var vault = await ImageVault.FromAsync(TestDirectory); var vault = await ImageVault.FromAsync(TestDirectory);
var entryKey = CreateTestEntryKey("corrupted-file"); var entryKey = CreateTestEntryId("corrupted-file");
var imageBinary = TestData.CreateTestImageBinary(1.0); var imageBinary = TestData.CreateTestImageBinary(1.0);
await vault!.AddEntryAsync(MediaVaultType.Image, entryKey, imageBinary); await vault!.AddEntryAsync(entryKey, imageBinary);
// Corrupt the media file // Corrupt the media file
var filePath = Path.Combine(TestDirectory, "corrupted-file.png"); var filePath = Path.Combine(TestDirectory, "corrupted-file.png");
@@ -468,7 +468,7 @@ public class MediaVaultTests
// Act & Assert - Should not throw, but behavior may vary // Act & Assert - Should not throw, but behavior may vary
Assert.DoesNotThrowAsync(async () => Assert.DoesNotThrowAsync(async () =>
{ {
await vault.GetEntryAsync<ImageBinary>(MediaVaultType.Image, entryKey); await vault.GetEntryAsync<ImageBinary>(entryKey);
}, "Should handle corrupted files gracefully"); }, "Should handle corrupted files gracefully");
} }
@@ -480,7 +480,7 @@ public class MediaVaultTests
// Arrange // Arrange
var vault = await ImageVault.FromAsync(TestDirectory); var vault = await ImageVault.FromAsync(TestDirectory);
var entryKey = CreateTestEntryKey("large-file"); var entryKey = CreateTestEntryId("large-file");
// Create a reasonably large buffer (not too large to cause test issues) // Create a reasonably large buffer (not too large to cause test issues)
var largeBuffer = new byte[1024 * 1024]; // 1MB var largeBuffer = new byte[1024 * 1024]; // 1MB
@@ -491,7 +491,7 @@ public class MediaVaultTests
// Act & Assert - Should not throw exceptions // Act & Assert - Should not throw exceptions
Assert.DoesNotThrowAsync(async () => Assert.DoesNotThrowAsync(async () =>
{ {
await vault!.AddEntryAsync(MediaVaultType.Image, entryKey, largeBinary); await vault!.AddEntryAsync(entryKey, largeBinary);
}, "Should handle large files gracefully"); }, "Should handle large files gracefully");
} }
@@ -500,16 +500,16 @@ public class MediaVaultTests
{ {
// Arrange // Arrange
var vault = await ImageVault.FromAsync(TestDirectory); var vault = await ImageVault.FromAsync(TestDirectory);
var entryKey = CreateTestEntryKey("concurrent-test"); var entryKey = CreateTestEntryId("concurrent-test");
var imageBinary = TestData.CreateTestImageBinary(1.0); var imageBinary = TestData.CreateTestImageBinary(1.0);
await vault!.AddEntryAsync(MediaVaultType.Image, entryKey, imageBinary); await vault!.AddEntryAsync(entryKey, imageBinary);
// Act - Multiple concurrent reads // Act - Multiple concurrent reads
var tasks = new List<Task<ImageBinary?>>(); var tasks = new List<Task<ImageBinary?>>();
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
tasks.Add(vault.GetEntryAsync<ImageBinary>(MediaVaultType.Image, entryKey)); tasks.Add(vault.GetEntryAsync<ImageBinary>(entryKey));
} }
var results = await Task.WhenAll(tasks); var results = await Task.WhenAll(tasks);
@@ -537,14 +537,14 @@ public class MediaVaultTests
// Arrange // Arrange
var database = await FileDatabase.FromAsync(TestDirectory); var database = await FileDatabase.FromAsync(TestDirectory);
var vaultKey = new EntryKey("test-vault", MediaVaultType.Image); var vaultKey = "test-vault";
var entryKey = new EntryKey("test-image", MediaVaultType.Image); var entryKey = "test-image";
var imageBinary = TestData.CreateTestImageBinary(1.5); var imageBinary = TestData.CreateTestImageBinary(1.5);
// Act // Act
await database!.CreateVaultAsync(vaultKey); await database!.CreateVaultAsync(vaultKey, MediaVaultType.Image);
await database.RegisterResourceAsync(MediaVaultType.Image, vaultKey, entryKey, imageBinary); await database.RegisterResourceAsync(vaultKey, entryKey, imageBinary);
var retrievedImage = await database.LoadResourceAsync<ImageBinary>(MediaVaultType.Image, vaultKey, entryKey); var retrievedImage = await database.LoadResourceAsync<ImageBinary>(vaultKey, entryKey);
// Assert // Assert
Assert.That(retrievedImage, Is.Not.Null, "Should retrieve image through database"); Assert.That(retrievedImage, Is.Not.Null, "Should retrieve image through database");
@@ -564,16 +564,16 @@ public class MediaVaultTests
// Arrange - Create and populate vault // Arrange - Create and populate vault
var database1 = await FileDatabase.FromAsync(TestDirectory); var database1 = await FileDatabase.FromAsync(TestDirectory);
var vaultKey = new EntryKey("persistent-vault", MediaVaultType.Image); var vaultKey = "persistent-vault";
var entryKey = new EntryKey("persistent-image", MediaVaultType.Image); var entryKey = "persistent-image";
var imageBinary = TestData.CreateTestImageBinary(2.0); var imageBinary = TestData.CreateTestImageBinary(2.0);
await database1!.CreateVaultAsync(vaultKey); await database1!.CreateVaultAsync(vaultKey, MediaVaultType.Image);
await database1.RegisterResourceAsync(MediaVaultType.Image, vaultKey, entryKey, imageBinary); await database1.RegisterResourceAsync(vaultKey, entryKey, imageBinary);
// Act - Reload database // Act - Reload database
var database2 = await FileDatabase.FromAsync(TestDirectory); var database2 = await FileDatabase.FromAsync(TestDirectory);
var retrievedImage = await database2!.LoadResourceAsync<ImageBinary>(MediaVaultType.Image, vaultKey, entryKey); var retrievedImage = await database2!.LoadResourceAsync<ImageBinary>(vaultKey, entryKey);
// Assert // Assert
Assert.That(retrievedImage, Is.Not.Null, "Should retrieve image after database reload"); Assert.That(retrievedImage, Is.Not.Null, "Should retrieve image after database reload");
-32
View File
@@ -8,38 +8,6 @@ namespace DeepDrftTests;
[TestFixture] [TestFixture]
public class ModelTests public class ModelTests
{ {
[TestFixture]
public class EntryKeyTests
{
[Test]
public void EntryKey_CanBeCreated()
{
// Arrange
var key = "test-key";
var type = MediaVaultType.Image;
// Act
var entryKey = new EntryKey(key, type);
// Assert
Assert.That(entryKey.Key, Is.EqualTo(key), "Key should match");
Assert.That(entryKey.Type, Is.EqualTo(type), "Type should match");
}
[Test]
public void EntryKey_SupportsStructuralEquality()
{
// Arrange
var key1 = new EntryKey("test", MediaVaultType.Image);
var key2 = new EntryKey("test", MediaVaultType.Image);
var key3 = new EntryKey("different", MediaVaultType.Image);
// Act & Assert
Assert.That(key1, Is.EqualTo(key2), "Structurally equal keys should be equal");
Assert.That(key1, Is.Not.EqualTo(key3), "Different keys should not be equal");
Assert.That(key1.GetHashCode(), Is.EqualTo(key2.GetHashCode()), "Equal keys should have same hash code");
}
}
[TestFixture] [TestFixture]
public class MediaModelTests public class MediaModelTests
+8 -5
View File
@@ -51,14 +51,17 @@ public static class TestData
} }
/// <summary> /// <summary>
/// Test entry keys used across tests /// Test keys used across tests - now using simple strings and separate vault types
/// </summary> /// </summary>
public static class TestKeys public static class TestKeys
{ {
public static readonly EntryKey TestImageEntry = new("test", MediaVaultType.Image); public const string TestImageEntry = "test";
public static readonly EntryKey ImageVaultKey = new("img", MediaVaultType.Image); public const string ImageVaultKey = "img";
public static readonly EntryKey NonExistentVaultKey = new("i-do-not-exist", MediaVaultType.Image); public const string NonExistentVaultKey = "i-do-not-exist";
public static readonly EntryKey NonExistentEntryKey = new("i-do-not-exist", MediaVaultType.Image); public const string NonExistentEntryKey = "i-do-not-exist";
// Vault type for image-related tests
public const MediaVaultType ImageVaultType = MediaVaultType.Image;
} }
/// <summary> /// <summary>
+22 -22
View File
@@ -16,8 +16,8 @@ public class UtilityTests
public void StructuralMap_CanAddAndRetrieveEntries() public void StructuralMap_CanAddAndRetrieveEntries()
{ {
// Arrange // Arrange
var map = new StructuralMap<EntryKey, string>(); var map = new StructuralMap<string, string>();
var key = new EntryKey("test", MediaVaultType.Image); var key = "test";
var value = "test-value"; var value = "test-value";
// Act // Act
@@ -33,9 +33,9 @@ public class UtilityTests
public void StructuralMap_HandlesStructuralEquality() public void StructuralMap_HandlesStructuralEquality()
{ {
// Arrange // Arrange
var map = new StructuralMap<EntryKey, string>(); var map = new StructuralMap<string, string>();
var key1 = new EntryKey("test", MediaVaultType.Image); var key1 = "test";
var key2 = new EntryKey("test", MediaVaultType.Image); // Same values, different instance var key2 = "test"; // Same values
var value = "test-value"; var value = "test-value";
// Act // Act
@@ -50,8 +50,8 @@ public class UtilityTests
public void StructuralMap_CanRemoveEntries() public void StructuralMap_CanRemoveEntries()
{ {
// Arrange // Arrange
var map = new StructuralMap<EntryKey, string>(); var map = new StructuralMap<string, string>();
var key = new EntryKey("test", MediaVaultType.Image); var key = "test";
var value = "test-value"; var value = "test-value";
map.Set(key, value); map.Set(key, value);
@@ -68,12 +68,12 @@ public class UtilityTests
public void StructuralMap_CanEnumerateEntries() public void StructuralMap_CanEnumerateEntries()
{ {
// Arrange // Arrange
var map = new StructuralMap<EntryKey, string>(); var map = new StructuralMap<string, string>();
var entries = new[] var entries = new[]
{ {
(new EntryKey("key1", MediaVaultType.Image), "value1"), ("key1", "value1"),
(new EntryKey("key2", MediaVaultType.Media), "value2"), ("key2", "value2"),
(new EntryKey("key3", MediaVaultType.Image), "value3") ("key3", "value3")
}; };
foreach (var (key, value) in entries) foreach (var (key, value) in entries)
@@ -102,8 +102,8 @@ public class UtilityTests
public void StructuralSet_CanAddAndContainEntries() public void StructuralSet_CanAddAndContainEntries()
{ {
// Arrange // Arrange
var set = new StructuralSet<EntryKey>(); var set = new StructuralSet<string>();
var key = new EntryKey("test", MediaVaultType.Image); var key = "test";
// Act // Act
set.Add(key); set.Add(key);
@@ -117,9 +117,9 @@ public class UtilityTests
public void StructuralSet_HandlesStructuralEquality() public void StructuralSet_HandlesStructuralEquality()
{ {
// Arrange // Arrange
var set = new StructuralSet<EntryKey>(); var set = new StructuralSet<string>();
var key1 = new EntryKey("test", MediaVaultType.Image); var key1 = "test";
var key2 = new EntryKey("test", MediaVaultType.Image); // Same values, different instance var key2 = "test"; // Same values
// Act // Act
set.Add(key1); set.Add(key1);
@@ -134,8 +134,8 @@ public class UtilityTests
public void StructuralSet_CanRemoveEntries() public void StructuralSet_CanRemoveEntries()
{ {
// Arrange // Arrange
var set = new StructuralSet<EntryKey>(); var set = new StructuralSet<string>();
var key = new EntryKey("test", MediaVaultType.Image); var key = "test";
set.Add(key); set.Add(key);
// Act // Act
@@ -151,12 +151,12 @@ public class UtilityTests
public void StructuralSet_CanEnumerateEntries() public void StructuralSet_CanEnumerateEntries()
{ {
// Arrange // Arrange
var set = new StructuralSet<EntryKey>(); var set = new StructuralSet<string>();
var keys = new[] var keys = new[]
{ {
new EntryKey("key1", MediaVaultType.Image), "key1",
new EntryKey("key2", MediaVaultType.Media), "key2",
new EntryKey("key3", MediaVaultType.Image) "key3"
}; };
foreach (var key in keys) foreach (var key in keys)