-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add resource example in the sample project
- Loading branch information
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |