Skip to content

Commit

Permalink
Verify corpora files are deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
johnml1135 committed Jan 9, 2025
1 parent f8d09ee commit a94c042
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ this IMediatorRegistrationConfigurator configurator
configurator.AddConsumer<GetCorpusConsumer>();
configurator.AddConsumer<GetDataFileConsumer>();
configurator.AddConsumer<DeleteDataFileConsumer>();
configurator.AddConsumer<DataFileDeletedConsumer>();
return configurator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Serval.DataFiles.Consumers;

public class DataFileDeletedConsumer(ICorpusService corpusService) : IConsumer<DataFileDeleted>
{
private readonly ICorpusService _corpusService = corpusService;

public async Task Consume(ConsumeContext<DataFileDeleted> context)
{
await _corpusService.DeleteAllCorpusFilesAsync(context.Message.DataFileId, context.CancellationToken);
}
}
12 changes: 12 additions & 0 deletions src/Serval/src/Serval.DataFiles/Services/CorpusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ await _mediator.Publish(
);
}

public Task DeleteAllCorpusFilesAsync(string fileId, CancellationToken cancellationToken = default)
{
return Entities.UpdateAllAsync(
e => e.Files.Any(f => f.FileRef == fileId),
u =>
{
u.RemoveAll(e => e.Files, f => f.FileRef == fileId);
},
cancellationToken: cancellationToken
);
}

private static DataFileResult Map(DataFile dataFile)
{
return new DataFileResult
Expand Down
1 change: 1 addition & 0 deletions src/Serval/src/Serval.DataFiles/Services/ICorpusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface ICorpusService
Task<Corpus> CreateAsync(Corpus corpus, CancellationToken cancellationToken = default);
Task<Corpus> UpdateAsync(string id, IReadOnlyList<CorpusFile> files, CancellationToken cancellationToken = default);
Task DeleteAsync(string id, CancellationToken cancellationToken = default);
Task DeleteAllCorpusFilesAsync(string fileId, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public async Task CreateAsync(IEnumerable<string> scope, int expectedStatusCode)
var newCorpus = new CorpusConfig { Language = "es", Files = new List<CorpusFileConfig>() };
var created = await corporaClient.CreateAsync(newCorpus);
Assert.That(created, Is.Not.Null);
var allCorpora = await corporaClient.GetAllAsync();
Assert.That(allCorpora.Count, Is.EqualTo(3));
break;
case 403:
default:
Expand Down Expand Up @@ -235,6 +237,19 @@ public async Task DeleteAsync(IEnumerable<string> scope, int expectedStatusCode,
}
}

[Test]
public async Task PropagateFileDeletedToCorpus()
{
var scope = new[] { Scopes.DeleteFiles, Scopes.ReadFiles };
var corporaClient = _env.CreateCorporaClient(scope);
var dataFilesClient = _env.CreateDataFilesClient(scope);
var originalCorpus1 = await corporaClient.GetAsync(CORPUS_ID1);
Assert.That(originalCorpus1.Files.Count, Is.EqualTo(1));
await dataFilesClient.DeleteAsync(FILE_ID1);
var updatedCorpus1 = await corporaClient.GetAsync(CORPUS_ID1);
Assert.That(updatedCorpus1.Files.Count, Is.EqualTo(0));
}

[TearDown]
public void TearDown()
{
Expand Down Expand Up @@ -262,7 +277,7 @@ public TestEnvironment()
public IRepository<DataFiles.Models.DataFile> DataFiles { get; }
public IRepository<DataFiles.Models.Corpus> Corpora { get; }

public DataFilesClient CreateClient(IEnumerable<string> scope)
public DataFilesClient CreateDataFilesClient(IEnumerable<string> scope)
{
HttpClient httpClient = Factory.WithWebHostBuilder(_ => { }).CreateClient();
if (scope is not null)
Expand Down

0 comments on commit a94c042

Please sign in to comment.