Skip to content

Commit

Permalink
(#420) Create unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jibedoubleve committed Dec 29, 2023
1 parent f3294d3 commit 58ea47c
Showing 1 changed file with 168 additions and 139 deletions.
307 changes: 168 additions & 139 deletions src/Tests/Lanceur.Tests/ViewModels/KeywordViewModelShould.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using Windows.Services.Maps;
using AutoMapper;
using Castle.Core.Logging;
using Dapper;
using FluentAssertions;
using FluentAssertions.Execution;
using Lanceur.Core.Decorators;
using Lanceur.Core.Managers;
using Humanizer;
using Lanceur.Core.Models;
using Lanceur.Core.Repositories;
using Lanceur.Core.Services;
using Lanceur.Infra.SQLite;
using Lanceur.Infra.Win32.Thumbnails;
using Lanceur.SharedKernel.Web;
using Lanceur.Tests.Logging;
using Lanceur.Tests.Mocks;
using Lanceur.Tests.SQL;
Expand Down Expand Up @@ -46,161 +42,194 @@ public KeywordViewModelShould(ITestOutputHelper output)
#region Methods

[Fact]
public void AbleToRemoveSynonym()
public void BeAbleToRemoveSynonym()=> new TestScheduler().With(scheduler =>
{
new TestScheduler().With(scheduler =>
// ARRANGE
const long idAlias = 10;
var sql = new SqlBuilder().AppendAlias(idAlias, "@multi@", "@alias2@@alias3")
.AppendSynonyms(idAlias, "multi1", "multi2", "multi3")
.AppendAlias(20, "alias2", "action1")
.AppendSynonyms(20, "alias2")
.AppendAlias(30, "alias3", "action2")
.AppendSynonyms(30, "alias3")
.ToString();

var connectionMgr = new SQLiteSingleConnectionManager(BuildFreshDb(sql));

var logger = new XUnitLoggerFactory(_output);
var cfg = new MapperConfiguration(cfg =>
{
// ARRANGE
const long idAlias = 10;
var sql = new SqlBuilder().AppendAlias(10, "@multi@", "@alias2@@alias3")
.AppendSynonyms(10, "multi1", "multi2", "multi3")
.AppendAlias(20, "alias2", "action1")
.AppendSynonyms(20, "alias2")
.AppendAlias(30, "alias3", "action2")
.AppendSynonyms(30, "alias3")
.ToString();

var connectionMgr = new SQLiteSingleConnectionManager(BuildFreshDb(sql));

var logger = new XUnitLoggerFactory(_output);
var cfg = new MapperConfiguration(cfg =>
{
cfg.CreateMap<AliasQueryResult, CompositeAliasQueryResult>();
});
var conversionService = new AutoMapperConverter(new Mapper(cfg));
var dbRepository = new SQLiteRepository(connectionMgr, logger, conversionService);

var vm = new KeywordsViewModelBuilder()
.With(scheduler)
.With(logger)
.With(dbRepository)
.Build();

// ACT
vm.Activate(new());
vm.SearchQuery = "multi";
scheduler.AdvanceBy(TimeSpan.FromMilliseconds(20).Ticks);

vm.SelectedAlias.Synonyms = "multi1, multi2";
vm.SaveOrUpdateAlias.Execute(vm.SelectedAlias).Subscribe();
scheduler.Start();

// ASSERT
using (new AssertionScope())
{
// The database should have one less synonym
var countSql = $"select count(*) from alias_name where id_alias = {idAlias}";
connectionMgr.WithinTransaction(tx => (long)tx.Connection.ExecuteScalar(countSql))
.Should().Be(2);
// And the UI also...
vm.Aliases.Should().HaveCount(2);
}
cfg.CreateMap<AliasQueryResult, CompositeAliasQueryResult>();
});
}

[Fact]
public void BeAbleToCreateAliasForPackagedApp() => new TestScheduler().With(scheduler =>
var conversionService = new AutoMapperConverter(new Mapper(cfg));
var dbRepository = new SQLiteRepository(connectionMgr, logger, conversionService);

var vm = new KeywordsViewModelBuilder()
.With(scheduler)
.With(logger)
.With(dbRepository)
.Build();

// ACT
vm.Activate(new());
vm.SearchQuery = "multi";
scheduler.AdvanceBy(200.Milliseconds().Ticks);

vm.SelectedAlias.Synonyms = "multi1, multi2";
vm.SaveOrUpdateAlias.Execute(vm.SelectedAlias).Subscribe();
scheduler.Start();

// ASSERT
using (new AssertionScope())
{
// ARRANGE
var packagedAppSearchService = Substitute.For<IPackagedAppSearchService>();

packagedAppSearchService.GetByInstalledDirectory(Arg.Any<string>())
.Returns(new List<PackagedApp> { new() { AppUserModelId = Guid.NewGuid().ToString() } });

var vm = new KeywordsViewModelBuilder().With(scheduler)
.With(_output)
.With(packagedAppSearchService)
.Build();

// ACT
vm.Activate(new());
vm.CreatingAlias.Execute().Subscribe();
vm.SaveOrUpdateAlias.Execute(vm.SelectedAlias).Subscribe();

// ASSERT
vm.SelectedAlias.FileName.Should().StartWith("package:");
});
// The database should have one less synonym
var countSql = $"select count(*) from alias_name where id_alias = {idAlias}";
connectionMgr.WithinTransaction(tx => (long)tx.Connection.ExecuteScalar(countSql))
.Should().Be(2);
// And the UI also...
vm.Aliases.Should().HaveCount(2);
}
});

[Fact]
public void CreateAliasAndSelectIt()
public void CreateAliasAndSelectIt() => new TestScheduler().With(scheduler =>
{
new TestScheduler().With(scheduler =>
{
// ARRANGE
var dbRepository = Substitute.For<IDbRepository>();
dbRepository.SelectNames(Arg.Any<string[]>())
.Returns(new ExistingNameResponse(Array.Empty<string>()));
// ARRANGE
var dbRepository = Substitute.For<IDbRepository>();
dbRepository.SelectNames(Arg.Any<string[]>())
.Returns(new ExistingNameResponse(Array.Empty<string>()));

var vm = new KeywordsViewModelBuilder()
.With(scheduler)
.With(_output)
.With(dbRepository)
.Build();

var vm = new KeywordsViewModelBuilder()
.With(scheduler)
.With(_output)
.With(dbRepository)
.Build();
var synonyms = Guid.NewGuid().ToString();
var fileName = Guid.NewGuid().ToString();

var synonyms = Guid.NewGuid().ToString();
var fileName = Guid.NewGuid().ToString();
// ACT

// ACT
vm.Activate(new());
scheduler.Start();
vm.CreatingAlias.Execute().Subscribe();
var hash = vm.SelectedAlias.GetHashCode();

vm.Activate(new());
scheduler.Start();
vm.CreatingAlias.Execute().Subscribe();
var hash = vm.SelectedAlias.GetHashCode();
vm.SelectedAlias.Synonyms = synonyms;
vm.SelectedAlias.FileName = fileName;

vm.SelectedAlias.Synonyms = synonyms;
vm.SelectedAlias.FileName = fileName;
vm.SaveOrUpdateAlias.Execute(vm.SelectedAlias).Subscribe();

vm.SaveOrUpdateAlias.Execute(vm.SelectedAlias).Subscribe();
// ASSERT
vm.SelectedAlias.GetHashCode().Should().Be(hash);
});

[Fact]
public void CreateAliasForPackagedApp() => new TestScheduler().With(scheduler =>
{
// ARRANGE
var packagedAppSearchService = Substitute.For<IPackagedAppSearchService>();
packagedAppSearchService.GetByInstalledDirectory(Arg.Any<string>())
.Returns(new List<PackagedApp>
{ new() { AppUserModelId = Guid.NewGuid().ToString() } });

var vm = new KeywordsViewModelBuilder().With(scheduler)
.With(_output)
.With(packagedAppSearchService)
.Build();

// ACT
vm.Activate(new());
vm.CreatingAlias.Execute().Subscribe();
vm.SaveOrUpdateAlias.Execute(vm.SelectedAlias).Subscribe();

// ASSERT
vm.SelectedAlias.FileName.Should().StartWith("package:");
});

// ASSERT
vm.SelectedAlias.GetHashCode().Should().Be(hash);
[Fact]
public void NotBeAbleToCreateEmptyAlias() => new TestScheduler().With(scheduler =>
{
// ARRANGE
var sql = new SqlBuilder().AppendAlias(10, "@multi@", "@alias2@@alias3")
.AppendSynonyms(10, "multi1", "multi2", "multi3")
.AppendAlias(20, "alias2", "action1")
.AppendSynonyms(20, "alias2")
.AppendAlias(30, "alias3", "action2")
.AppendSynonyms(30, "alias3")
.ToString();

var connectionMgr = new SQLiteSingleConnectionManager(BuildFreshDb(sql));

var logger = new XUnitLoggerFactory(_output);
var cfg = new MapperConfiguration(cfg =>
{
cfg.CreateMap<AliasQueryResult, CompositeAliasQueryResult>();
});
}
var conversionService = new AutoMapperConverter(new Mapper(cfg));
var dbRepository = new SQLiteRepository(connectionMgr, logger, conversionService);

var vm = new KeywordsViewModelBuilder()
.With(scheduler)
.With(logger)
.With(dbRepository)
.Build();
// ACT
vm.Activate(new());
vm.SearchQuery = "multi1";
scheduler.Start();

vm.SelectedAlias.Synonyms = "multi1, ";
vm.SaveOrUpdateAlias.Execute(vm.SelectedAlias).Subscribe();

vm.Activate(new());
vm.SearchQuery = "multi1";
scheduler.AdvanceBy(200.Milliseconds().Ticks);

// ASSERT
vm.ValidationAliasExists.IsValid.Should().BeTrue();
});

[Fact]
public void RefreshThumbnailsWithoutRemovingAdditionalParameters()
{
new TestScheduler().With(scheduler =>
// ARRANGE
var sql = new SqlBuilder().AppendAlias(1, "fileName1", "arguments1")
.AppendSynonyms(1, "a1", "a2", "a3")
.AppendArgument(1, "name_0", "argument_0")
.AppendArgument(1, "name_0", "argument_0")
//--
.AppendAlias(110, "fileName2", "arguments2")
.AppendSynonyms(110, "aa1", "ab2", "ab3")
.AppendArgument(110, "name_0", "argument_0")
.AppendArgument(110, "name_0", "argument_0")
//--
.AppendAlias(120, "fileName3", "arguments3")
.AppendSynonyms(120, "ac1", "ac2", "ac3")
.AppendArgument(120, "name_0", "argument_0")
.AppendArgument(120, "name_0", "argument_0")
.ToString();

var connectionMgr = new SQLiteSingleConnectionManager(BuildFreshDb(sql));
var loggerFactory = new XUnitLoggerFactory(_output);

var cfg = new MapperConfiguration(cfg =>
{
// ARRANGE
var sql = new SqlBuilder().AppendAlias(1, "fileName1", "arguments1")
.AppendSynonyms(1, "a1", "a2", "a3")
.AppendArgument(1, "name_0", "argument_0")
.AppendArgument(1, "name_0", "argument_0")
//--
.AppendAlias(110, "fileName2", "arguments2")
.AppendSynonyms(110, "aa1", "ab2", "ab3")
.AppendArgument(110, "name_0", "argument_0")
.AppendArgument(110, "name_0", "argument_0")
//--
.AppendAlias(120, "fileName3", "arguments3")
.AppendSynonyms(120, "ac1", "ac2", "ac3")
.AppendArgument(120, "name_0", "argument_0")
.AppendArgument(120, "name_0", "argument_0")
.ToString();

var connectionMgr = new SQLiteSingleConnectionManager(BuildFreshDb(sql));
var loggerFactory = new XUnitLoggerFactory(_output);

var cfg = new MapperConfiguration(cfg =>
{
cfg.CreateMap<AliasQueryResult, CompositeAliasQueryResult>();
});
var conversionService = new AutoMapperConverter(new Mapper(cfg));
var dbRepository = new SQLiteRepository(connectionMgr, loggerFactory, conversionService);
var thumbnailRefresher = new MockThumbnailRefresher();

var thumbnailManager = new ThumbnailManager(loggerFactory, dbRepository, thumbnailRefresher);
var aliases = dbRepository.Search("a");

// ACT
thumbnailManager.RefreshThumbnails(aliases);

// ASSERT
connectionMgr
.WithinTransaction(tx => (long)tx.Connection.ExecuteScalar("select count(*) from alias_argument"))
.Should().Be(6);
cfg.CreateMap<AliasQueryResult, CompositeAliasQueryResult>();
});
var conversionService = new AutoMapperConverter(new Mapper(cfg));
var dbRepository = new SQLiteRepository(connectionMgr, loggerFactory, conversionService);
var thumbnailRefresher = new MockThumbnailRefresher();

var thumbnailManager = new ThumbnailManager(loggerFactory, dbRepository, thumbnailRefresher);
var aliases = dbRepository.Search("a");

// ACT
thumbnailManager.RefreshThumbnails(aliases);

// ASSERT
connectionMgr.WithinTransaction(tx => (long)tx.Connection.ExecuteScalar("select count(*) from alias_argument"))
.Should().Be(6);
}

#endregion Methods
Expand Down

0 comments on commit 58ea47c

Please sign in to comment.