From 780c79b35690ad95ec2a3abada0f074f124a4564 Mon Sep 17 00:00:00 2001 From: Enkidu93 Date: Wed, 18 Dec 2024 10:29:28 -0500 Subject: [PATCH] Add cleanup service tests for word alignment --- .../Services/BuildCleanupServiceTests.cs | 56 ++++++++++++++++ .../Services/EngineCleanupServiceTests.cs | 64 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/Serval/test/Serval.WordAlignment.Tests/Services/BuildCleanupServiceTests.cs create mode 100644 src/Serval/test/Serval.WordAlignment.Tests/Services/EngineCleanupServiceTests.cs diff --git a/src/Serval/test/Serval.WordAlignment.Tests/Services/BuildCleanupServiceTests.cs b/src/Serval/test/Serval.WordAlignment.Tests/Services/BuildCleanupServiceTests.cs new file mode 100644 index 00000000..db16d100 --- /dev/null +++ b/src/Serval/test/Serval.WordAlignment.Tests/Services/BuildCleanupServiceTests.cs @@ -0,0 +1,56 @@ +namespace Serval.WordAlignment.Services; + +[TestFixture] +public class BuildCleanupServiceTests +{ + [Test] + public async Task CleanupAsync() + { + TestEnvironment env = new(); + Assert.That(env.Builds.Count, Is.EqualTo(2)); + await env.CheckBuildsAsync(); + Assert.That(env.Builds.Count, Is.EqualTo(1)); + Assert.That((await env.Builds.GetAllAsync())[0].Id, Is.EqualTo("build2")); + } + + private class TestEnvironment + { + public MemoryRepository Builds { get; } + + public TestEnvironment() + { + Builds = new MemoryRepository(); + Builds.Add( + new Build + { + Id = "build1", + EngineRef = "engine1", + IsInitialized = false, + DateCreated = DateTime.UtcNow.Subtract(TimeSpan.FromHours(10)) + } + ); + Builds.Add( + new Build + { + Id = "build2", + EngineRef = "engine2", + IsInitialized = true, + DateCreated = DateTime.UtcNow.Subtract(TimeSpan.FromHours(10)) + } + ); + + Service = new BuildCleanupService( + Substitute.For(), + Substitute.For>(), + TimeSpan.Zero + ); + } + + public BuildCleanupService Service { get; } + + public async Task CheckBuildsAsync() + { + await Service.CheckEntitiesAsync(Builds, CancellationToken.None); + } + } +} diff --git a/src/Serval/test/Serval.WordAlignment.Tests/Services/EngineCleanupServiceTests.cs b/src/Serval/test/Serval.WordAlignment.Tests/Services/EngineCleanupServiceTests.cs new file mode 100644 index 00000000..0ed467fb --- /dev/null +++ b/src/Serval/test/Serval.WordAlignment.Tests/Services/EngineCleanupServiceTests.cs @@ -0,0 +1,64 @@ +namespace Serval.WordAlignment.Services; + +[TestFixture] +public class EngineCleanupServiceTests +{ + [Test] + public async Task CleanupAsync() + { + TestEnvironment env = new(); + Assert.That(env.Engines.Count, Is.EqualTo(2)); + await env.CheckEnginesAsync(); + Assert.That(env.Engines.Count, Is.EqualTo(1)); + Assert.That((await env.Engines.GetAllAsync())[0].Id, Is.EqualTo("engine2")); + } + + private class TestEnvironment + { + public MemoryRepository Engines { get; } + + public TestEnvironment() + { + Engines = new MemoryRepository(); + Engines.Add( + new Engine + { + Id = "engine1", + SourceLanguage = "en", + TargetLanguage = "es", + Type = "Nmt", + Owner = "client1", + IsInitialized = false, + DateCreated = DateTime.UtcNow.Subtract(TimeSpan.FromHours(10)), + ParallelCorpora = [] + } + ); + Engines.Add( + new Engine + { + Id = "engine2", + SourceLanguage = "en", + TargetLanguage = "es", + Type = "Nmt", + Owner = "client1", + IsInitialized = true, + DateCreated = DateTime.UtcNow.Subtract(TimeSpan.FromHours(10)), + ParallelCorpora = [] + } + ); + + Service = new EngineCleanupService( + Substitute.For(), + Substitute.For>(), + TimeSpan.Zero + ); + } + + public EngineCleanupService Service { get; } + + public async Task CheckEnginesAsync() + { + await Service.CheckEntitiesAsync(Engines, CancellationToken.None); + } + } +}