Skip to content

Commit

Permalink
get sena 3 sync tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Nov 22, 2024
1 parent ceb7ade commit 01e27b3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ namespace FwDataMiniLcmBridge.Tests.Fixtures;

public static class FwDataTestsKernel
{
public static IServiceCollection AddTestFwDataBridge(this IServiceCollection services)
public static IServiceCollection AddTestFwDataBridge(this IServiceCollection services, bool mockProjectLoader = true)
{
services.AddFwDataBridge();
services.AddSingleton<IConfiguration>(_ => new ConfigurationRoot([]));
services.AddSingleton<MockFwProjectLoader>();
services.AddSingleton<IProjectLoader>(sp => sp.GetRequiredService<MockFwProjectLoader>());
services.AddSingleton<FieldWorksProjectList, MockFwProjectList>();
if (mockProjectLoader)
{
services.AddSingleton<MockFwProjectLoader>();
services.AddSingleton<IProjectLoader>(sp => sp.GetRequiredService<MockFwProjectLoader>());
services.AddSingleton<FieldWorksProjectList, MockFwProjectList>();
}
return services;
}
}
36 changes: 18 additions & 18 deletions backend/FwLite/FwLiteProjectSync.Tests/Fixtures/Sena3SyncFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ namespace FwLiteProjectSync.Tests.Fixtures;

public class Sena3Fixture : IAsyncLifetime
{
private readonly SyncFixture syncFixture;
private readonly AsyncServiceScope _services;

public CrdtFwdataProjectSyncService SyncService =>
Services.GetRequiredService<CrdtFwdataProjectSyncService>();
public IServiceProvider Services => syncFixture.Services;
_services.ServiceProvider.GetRequiredService<CrdtFwdataProjectSyncService>();

public IServiceProvider Services => _services.ServiceProvider;
private readonly LexboxConfig lexboxConfig;
private readonly HttpClient http;
public CrdtMiniLcmApi CrdtApi { get; set; } = null!;
Expand All @@ -30,20 +32,20 @@ public class Sena3Fixture : IAsyncLifetime

public Sena3Fixture()
{
syncFixture = SyncFixture.Create(services =>
{
services.AddOptions<LexboxConfig>()
.BindConfiguration("LexboxConfig")
.ValidateDataAnnotations()
.ValidateOnStart();
// TODO: How do I set default values if and only if they're not already set (e.g., via environment variables)?
services.Configure<LexboxConfig>(c =>
var services = new ServiceCollection()
.AddSyncServices(nameof(Sena3Fixture), false);
services.AddOptions<LexboxConfig>()
.BindConfiguration("LexboxConfig")
.Configure(c =>
{
// TODO: How do I set default values if and only if they're not already set (e.g., via environment variables)?
c.LexboxUrl = "http://localhost/";
c.LexboxUsername = "admin";
c.LexboxPassword = "pass";
});
}, nameof(Sena3Fixture)); // TODO: Or create the project name in the constructor rather than in InitializeAsync, then pass it in here
})
.ValidateDataAnnotations()
.ValidateOnStart();
_services = services.BuildServiceProvider().CreateAsyncScope();
lexboxConfig = Services.GetRequiredService<IOptions<LexboxConfig>>().Value;
var factory = Services.GetRequiredService<IHttpClientFactory>();
http = factory.CreateClient(nameof(Sena3Fixture));
Expand All @@ -63,8 +65,7 @@ public async Task InitializeAsync()
DirectoryHelper.Copy(sena3MasterCopy, fwDataProjectPath);
File.Move(Path.Combine(fwDataProjectPath, "sena-3.fwdata"), fwDataProject.FilePath);

Services.GetRequiredService<IProjectLoader>().LoadCache(fwDataProject);
FwDataApi = Services.GetRequiredService<FwDataFactory>().GetFwDataMiniLcmApi(projectName, false);
FwDataApi = Services.GetRequiredService<FwDataFactory>().GetFwDataMiniLcmApi(fwDataProject, false);

var crdtProjectsFolder =
Services.GetRequiredService<IOptions<LcmCrdtConfig>>().Value.ProjectPath;
Expand All @@ -77,7 +78,7 @@ public async Task InitializeAsync()

public async Task DisposeAsync()
{
await syncFixture.DisposeAsync();
await _services.DisposeAsync();
}

public async Task<Stream> DownloadProjectBackupStream(string code)
Expand All @@ -96,14 +97,13 @@ public async Task LoginAs(string lexboxUsername, string lexboxPassword)

public async Task<string> DownloadSena3()
{

var tempFolder = Path.Combine(Path.GetTempPath(), nameof(Sena3Fixture));
var sena3MasterCopy = Path.Combine(tempFolder, "sena-3");
if (!Directory.Exists(sena3MasterCopy) || !File.Exists(Path.Combine(sena3MasterCopy, "sena-3.fwdata")))
{
await LoginAs(lexboxConfig.LexboxUsername, lexboxConfig.LexboxPassword);
Directory.CreateDirectory(sena3MasterCopy);
var zipStream = await DownloadProjectBackupStream("sena-3");
await using var zipStream = await DownloadProjectBackupStream("sena-3");
ZipFile.ExtractToDirectory(zipStream, sena3MasterCopy);
MercurialTestHelper.HgUpdate(sena3MasterCopy, "tip");
LfMergeBridge.LfMergeBridge.ReassembleFwdataFile(new NullProgress(), false, Path.Combine(sena3MasterCopy, "sena-3.fwdata"));
Expand Down
19 changes: 2 additions & 17 deletions backend/FwLite/FwLiteProjectSync.Tests/Fixtures/SyncFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,14 @@ public class SyncFixture : IAsyncLifetime
_services.ServiceProvider.GetRequiredService<CrdtFwdataProjectSyncService>();
public IServiceProvider Services => _services.ServiceProvider;
private readonly string _projectName;
private readonly MockProjectContext _projectContext = new(null);

public static SyncFixture Create([CallerMemberName] string projectName = "") => new(projectName);

public static SyncFixture Create(Action<IServiceCollection> extraServiceConfiguration, [CallerMemberName] string projectName = "") => new(projectName, extraServiceConfiguration);

private SyncFixture(string projectName, Action<IServiceCollection>? extraServiceConfiguration = null)
private SyncFixture(string projectName)
{
_projectName = projectName;
var crdtServices = new ServiceCollection()
.AddLcmCrdtClient()
.AddSingleton<ProjectContext>(_projectContext)
.AddTestFwDataBridge()
.AddFwLiteProjectSync()
.Configure<FwDataBridgeConfig>(c => c.ProjectsFolder = Path.Combine(".", _projectName, "FwData"))
.Configure<LcmCrdtConfig>(c => c.ProjectPath = Path.Combine(".", _projectName, "LcmCrdt"))
.AddLogging(builder => builder.AddDebug());
if (extraServiceConfiguration is not null) extraServiceConfiguration(crdtServices);
.AddSyncServices(_projectName);
_services = crdtServices.BuildServiceProvider().CreateAsyncScope();
}

Expand Down Expand Up @@ -73,8 +63,3 @@ public async Task DisposeAsync()
public CrdtMiniLcmApi CrdtApi { get; set; } = null!;
public FwDataMiniLcmApi FwDataApi { get; set; } = null!;
}

public class MockProjectContext(CrdtProject? project) : ProjectContext
{
public override CrdtProject? Project { get; set; } = project;
}
27 changes: 27 additions & 0 deletions backend/FwLite/FwLiteProjectSync.Tests/Fixtures/TestingKernel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using FwDataMiniLcmBridge;
using FwDataMiniLcmBridge.Tests.Fixtures;
using LcmCrdt;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace FwLiteProjectSync.Tests.Fixtures;

public static class TestingKernel
{
public static IServiceCollection AddSyncServices(this IServiceCollection services, string projectName, bool mockFwProjectLoader = true)
{
return services.AddLcmCrdtClient()
.AddTestFwDataBridge(mockFwProjectLoader)
.AddFwLiteProjectSync()
.AddSingleton<ProjectContext>(new MockProjectContext(null))
.Configure<FwDataBridgeConfig>(c => c.ProjectsFolder = Path.Combine(".", projectName, "FwData"))
.Configure<LcmCrdtConfig>(c => c.ProjectPath = Path.Combine(".", projectName, "LcmCrdt"))
.AddLogging(builder => builder.AddDebug());
}

public class MockProjectContext(CrdtProject? project) : ProjectContext
{
public override CrdtProject? Project { get; set; } = project;
}

}
45 changes: 0 additions & 45 deletions backend/FwLite/FwLiteProjectSync.Tests/Sena3SyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,12 @@ public class Sena3SyncTests : IClassFixture<Sena3Fixture>, IAsyncLifetime
private readonly Sena3Fixture _fixture;
private readonly CrdtFwdataProjectSyncService _syncService;

private readonly Guid _complexEntryId = Guid.NewGuid();
private Entry _testEntry = new Entry
{
Id = Guid.NewGuid(),
LexemeForm = { Values = { { "en", "Apple" } } },
Note = { Values = { { "en", "this is a test note" } } },
Senses =
[
new Sense
{
Gloss = { Values = { { "en", "Apple" } } },
Definition = { Values = { { "en", "a round fruit with a hard, crisp skin" } } },
ExampleSentences =
[
new ExampleSentence { Sentence = { Values = { { "en", "I went to the store to buy an apple." } } } }
]
}
]
};

public async Task InitializeAsync()

Check warning on line 17 in backend/FwLite/FwLiteProjectSync.Tests/Sena3SyncTests.cs

View workflow job for this annotation

GitHub Actions / Build FW Lite and run tests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
await _fixture.FwDataApi.CreateEntry(_testEntry);
await _fixture.FwDataApi.CreateEntry(new Entry()
{
Id = _complexEntryId,
LexemeForm = { { "en", "Pineapple" } },
Components =
[
new ComplexFormComponent()
{
Id = Guid.NewGuid(),
ComplexFormEntryId = _complexEntryId,
ComplexFormHeadword = "Pineapple",
ComponentEntryId = _testEntry.Id,
ComponentHeadword = "Apple"
}
]
});
}

public async Task DisposeAsync()

Check warning on line 21 in backend/FwLite/FwLiteProjectSync.Tests/Sena3SyncTests.cs

View workflow job for this annotation

GitHub Actions / Build FW Lite and run tests

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
await foreach (var entry in _fixture.FwDataApi.GetEntries())
{
await _fixture.FwDataApi.DeleteEntry(entry.Id);
}
foreach (var entry in await _fixture.CrdtApi.GetEntries().ToArrayAsync())
{
await _fixture.CrdtApi.DeleteEntry(entry.Id);
}
}

public Sena3SyncTests(Sena3Fixture fixture)
Expand Down

0 comments on commit 01e27b3

Please sign in to comment.