Skip to content

Commit

Permalink
Add tests for ScoopSearchIndexerTests
Browse files Browse the repository at this point in the history
  • Loading branch information
gpailler committed May 30, 2023
1 parent 3a794cd commit df4e9d4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 60 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@
<ItemGroup>
<ProjectReference Include="..\ScoopSearch.Indexer\ScoopSearch.Indexer.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Processor\FetchBucketsProcessorTests.cs" />
<None Include="Processor\FetchBucketsProcessorTests.cs" />
</ItemGroup>
</Project>
70 changes: 70 additions & 0 deletions src/ScoopSearch.Indexer.Tests/ScoopSearchIndexerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Text.RegularExpressions;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using ScoopSearch.Indexer.Buckets;
using ScoopSearch.Indexer.Buckets.Sources;
using ScoopSearch.Indexer.Configuration;
using ScoopSearch.Indexer.Data;
using ScoopSearch.Indexer.Processor;
using ScoopSearch.Indexer.Tests.Helpers;
using Xunit.Abstractions;

namespace ScoopSearch.Indexer.Tests;

public class ScoopSearchIndexerTests : IClassFixture<HostFixture>
{
private readonly Mock<IIndexingProcessor> _indexingProcessorMock;
private readonly XUnitLogger<ScoopSearchIndexer> _logger;
private readonly ScoopSearchIndexer _sut;

public ScoopSearchIndexerTests(HostFixture hostFixture, ITestOutputHelper testOutputHelper)
{
hostFixture.Configure(testOutputHelper);

_indexingProcessorMock = new Mock<IIndexingProcessor>();

var fetchManifestsProcessorMock = new Mock<IFetchManifestsProcessor>();
fetchManifestsProcessorMock
.Setup(x => x.FetchManifestsAsync(It.IsAny<Bucket>(), It.IsAny<CancellationToken>()))
.Returns(AsyncEnumerable.Empty<ManifestInfo>());

_logger = new XUnitLogger<ScoopSearchIndexer>(testOutputHelper);
_sut = new ScoopSearchIndexer(
hostFixture.Instance.Services.GetRequiredService<IEnumerable<IBucketsSource>>(),
hostFixture.Instance.Services.GetRequiredService<IOfficialBucketsSource>(),
fetchManifestsProcessorMock.Object,
_indexingProcessorMock.Object,
hostFixture.Instance.Services.GetRequiredService<IOptions<BucketsOptions>>(),
_logger);
}

[Fact]
public async void ExecuteAsync_ReturnsBuckets_Succeeds()
{
// Arrange
const int expectedAtLeastBucketsCount = 1500;
var cancellationToken = new CancellationToken();
Uri[]? actualBucketsUris = null;
_indexingProcessorMock
.Setup(x => x.CleanIndexFromNonExistentBucketsAsync(It.IsAny<Uri[]>(), cancellationToken))
.Returns(Task.CompletedTask)
.Callback<Uri[], CancellationToken>((uris, _) => actualBucketsUris = uris)
.Verifiable();

// Act
await _sut.ExecuteAsync(cancellationToken);

// Assert
_indexingProcessorMock.Verify();
actualBucketsUris.Should().HaveCountGreaterThan(expectedAtLeastBucketsCount);
actualBucketsUris.Should().OnlyHaveUniqueItems(_ => _.AbsoluteUri.ToLowerInvariant());

_logger.Should().Log(LogLevel.Information, _ => Regex.IsMatch(_, @"Found \d+ buckets for a total of \d+ manifests."));
_logger.Should().Log(LogLevel.Information, _ => _.StartsWith("Processed bucket "), Times.AtLeast(expectedAtLeastBucketsCount));
_logger.Should().Log(LogLevel.Information, _ => _.StartsWith("Processed bucket https://github.com/ScoopInstaller/Main"));
_logger.Should().Log(LogLevel.Information, _ => _.StartsWith("Processed bucket https://github.com/ScoopInstaller/Extras"));
}
}

0 comments on commit df4e9d4

Please sign in to comment.