Skip to content

Commit

Permalink
add resource example in the sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Nov 14, 2024
1 parent 5448bd2 commit 41abcca
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/SIL.Harmony.Sample/Changes/AddWordImageChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using SIL.Harmony.Changes;
using SIL.Harmony.Entities;
using SIL.Harmony.Sample.Models;

namespace SIL.Harmony.Sample.Changes;

public class AddWordImageChange(Guid entityId, Guid imageId) : EditChange<Word>(entityId), ISelfNamedType<AddWordImageChange>
{
public Guid ImageId { get; } = imageId;

public override async ValueTask ApplyChange(Word entity, ChangeContext context)
{
if (!await context.IsObjectDeleted(ImageId)) entity.ImageResourceId = ImageId;
}
}
1 change: 1 addition & 0 deletions src/SIL.Harmony.Sample/CrdtSampleKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static IServiceCollection AddCrdtDataSample(this IServiceCollection servi
.Add<SetWordTextChange>()
.Add<SetWordNoteChange>()
.Add<AddAntonymReferenceChange>()
.Add<AddWordImageChange>()
.Add<SetOrderChange<Definition>>()
.Add<SetDefinitionPartOfSpeechChange>()
.Add<DeleteChange<Word>>()
Expand Down
9 changes: 8 additions & 1 deletion src/SIL.Harmony.Sample/Models/Word.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ public class Word : IObjectBase<Word>
public Guid Id { get; init; }
public DateTimeOffset? DeletedAt { get; set; }
public Guid? AntonymId { get; set; }
public Guid? ImageResourceId { get; set; }

public Guid[] GetReferences()
{
return AntonymId is null ? [] : [AntonymId.Value];
return Refs().ToArray();

IEnumerable<Guid> Refs()
{
if (AntonymId.HasValue) yield return AntonymId.Value;
if (ImageResourceId.HasValue) yield return ImageResourceId.Value;
}
}

public void RemoveReference(Guid id, Commit commit)
Expand Down
41 changes: 41 additions & 0 deletions src/SIL.Harmony.Tests/ResourceTests/WordResourceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Runtime.CompilerServices;
using Microsoft.Extensions.DependencyInjection;
using SIL.Harmony.Sample.Changes;
using SIL.Harmony.Sample.Models;

namespace SIL.Harmony.Tests.ResourceTests;

public class WordResourceTests: DataModelTestBase
{
private RemoteServiceMock _remoteServiceMock = new();
private ResourceService _resourceService => _services.GetRequiredService<ResourceService>();
private readonly Guid _entity1Id = Guid.NewGuid();

private string CreateFile(string contents, [CallerMemberName] string fileName = "")
{
var filePath = Path.GetFullPath(fileName + ".txt");
File.WriteAllText(filePath, contents);
return filePath;
}

[Fact]
public async Task CanReferenceAResourceFromAWord()
{
await WriteNextChange(SetWord(_entity1Id, "test-value"));
var imageFile = CreateFile("not image data");
//set commit date for add local resource
MockTimeProvider.SetNextDateTime(NextDate());
var resourceId = await _resourceService.AddLocalResource(imageFile, Guid.NewGuid(), resourceService: _remoteServiceMock);
await WriteNextChange(new AddWordImageChange(_entity1Id, resourceId));

var word = await DataModel.GetLatest<Word>(_entity1Id);
word.Should().NotBeNull();
word!.ImageResourceId.Should().Be(resourceId);


var localResource = await _resourceService.GetLocalResource(word.ImageResourceId!.Value);
localResource.Should().NotBeNull();
localResource!.LocalPath.Should().Be(imageFile);
(await File.ReadAllTextAsync(localResource.LocalPath)).Should().Be("not image data");
}
}

0 comments on commit 41abcca

Please sign in to comment.