Skip to content

Commit

Permalink
test: Test against multiple versions and configurations of Paperless
Browse files Browse the repository at this point in the history
  • Loading branch information
VMelnalksnis committed Feb 3, 2024
1 parent 47b1f7d commit 8b27671
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 96 deletions.
4 changes: 3 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
<PackageVersion Include="NodaTime.Serialization.SystemTextJson" Version="1.1.2"/>
<PackageVersion Include="NodaTime.Testing" Version="3.1.10"/>
<PackageVersion Include="Nullable" Version="1.3.1"/>
<PackageVersion Include="NUnit" Version="3.14.0"/>
<PackageVersion Include="NUnit.Analyzers" Version="4.0.1"/>
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageVersion Include="Serilog" Version="3.1.1"/>
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.1"/>
<PackageVersion Include="Serilog.Sinks.XUnit" Version="3.0.5"/>
<PackageVersion Include="System.Linq.Async" Version="6.0.1"/>
<PackageVersion Include="System.Net.Http" Version="4.3.4"/>
<PackageVersion Include="System.Net.Http.Json" Version="8.0.0"/>
Expand Down
6 changes: 0 additions & 6 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@
<PackageReference Include="Newtonsoft.Json"/>
<PackageReference Include="System.Net.Http"/>
<PackageReference Include="System.Text.RegularExpressions"/>
<PackageReference Include="xunit"/>
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Using Include="FluentAssertions"/>
<Using Include="FluentAssertions.Execution"/>
<Using Include="FluentAssertions.Equivalency"/>
<Using Include="Xunit"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\source\VMelnalksnis.PaperlessDotNet.DependencyInjection\VMelnalksnis.PaperlessDotNet.DependencyInjection.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="xunit"/>
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\source\VMelnalksnis.PaperlessDotNet.DependencyInjection\VMelnalksnis.PaperlessDotNet.DependencyInjection.csproj" />
<Using Include="Xunit"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@

using VMelnalksnis.PaperlessDotNet.Correspondents;

using Xunit.Abstractions;

namespace VMelnalksnis.PaperlessDotNet.Tests.Integration.Correspondents;

[Collection("Paperless")]
public sealed class CorrespondentClientTests
public sealed class CorrespondentClientTests(PaperlessFixture paperlessFixture) : PaperlessTests(paperlessFixture)
{
private readonly IPaperlessClient _paperlessClient;

public CorrespondentClientTests(ITestOutputHelper testOutputHelper, PaperlessFixture paperlessFixture)
{
_paperlessClient = paperlessFixture.GetPaperlessClient(testOutputHelper);
}

[Fact]
[Test]
public async Task Create_ShouldCreateExpected()
{
var creation = new CorrespondentCreation("Acme Company")
Expand All @@ -34,46 +24,46 @@ public async Task Create_ShouldCreateExpected()
MatchingAlgorithm = MatchingAlgorithm.ExactMatch,
};

var correspondent = await _paperlessClient.Correspondents.Create(creation);
var correspondent = await Client.Correspondents.Create(creation);

using (new AssertionScope())
{
correspondent.LastCorrespondence.Should().BeNull();
correspondent.DocumentCount.Should().Be(0);
correspondent.IsInsensitive.Should().Be(creation.IsInsensitive.Value);
correspondent.MatchingAlgorithm.Should().Be(creation.MatchingAlgorithm);
(await _paperlessClient.Correspondents.Get(correspondent.Id))
(await Client.Correspondents.Get(correspondent.Id))
.Should()
.BeEquivalentTo(correspondent);

var correspondents = await _paperlessClient.Correspondents.GetAll().ToListAsync();
var correspondents = await Client.Correspondents.GetAll().ToListAsync();
correspondents.Should().ContainSingle().Which.Should().BeEquivalentTo(correspondent);
}

await _paperlessClient.Correspondents.Delete(correspondent.Id);
await Client.Correspondents.Delete(correspondent.Id);

(await _paperlessClient.Correspondents.GetAll().ToListAsync()).Should().BeEmpty();
(await Client.Correspondents.GetAll().ToListAsync()).Should().BeEmpty();
}

[Fact]
[Test]
public async Task GetAll_PageSizeShouldNotChangeResult()
{
var correspondents = new List<Correspondent>();
for (var i = 0; i < 5; i++)
{
var creation = new CorrespondentCreation(Guid.NewGuid().ToString("N"));
correspondents.Add(await _paperlessClient.Correspondents.Create(creation));
correspondents.Add(await Client.Correspondents.Create(creation));
}

using (new AssertionScope())
{
(await _paperlessClient.Correspondents.GetAll().ToListAsync()).Should().BeEquivalentTo(correspondents);
(await _paperlessClient.Correspondents.GetAll(1).ToListAsync()).Should().BeEquivalentTo(correspondents);
(await Client.Correspondents.GetAll().ToListAsync()).Should().BeEquivalentTo(correspondents);
(await Client.Correspondents.GetAll(1).ToListAsync()).Should().BeEquivalentTo(correspondents);
}

foreach (var correspondent in correspondents)
{
await _paperlessClient.Correspondents.Delete(correspondent.Id);
await Client.Correspondents.Delete(correspondent.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,53 @@

using VMelnalksnis.PaperlessDotNet.Documents;

using Xunit.Abstractions;

namespace VMelnalksnis.PaperlessDotNet.Tests.Integration.Documents;

[Collection("Paperless")]
public sealed class DocumentClientTests
public sealed class DocumentClientTests(PaperlessFixture paperlessFixture) : PaperlessTests(paperlessFixture)
{
private readonly IPaperlessClient _paperlessClient;
private readonly IClock _clock;

public DocumentClientTests(ITestOutputHelper testOutputHelper, PaperlessFixture paperlessFixture)
{
_paperlessClient = paperlessFixture.GetPaperlessClient(testOutputHelper);
_clock = paperlessFixture.Clock;
}

[Fact]
[Test]
[Order(1)]
public async Task GetAll_ShouldReturnExpected()
{
var documents = await _paperlessClient.Documents.GetAll().ToListAsync();
var documents = await Client.Documents.GetAll().ToListAsync();

documents.Should().BeEmpty();
}

[Fact]
[Test]
public async Task GetAll_PageSizeShouldNotChangeResult()
{
var documents = await _paperlessClient.Documents.GetAll().ToListAsync();
var pageSizeDocuments = await _paperlessClient.Documents.GetAll(1).ToListAsync();
var documents = await Client.Documents.GetAll().ToListAsync();
var pageSizeDocuments = await Client.Documents.GetAll(1).ToListAsync();

documents.Should().BeEquivalentTo(pageSizeDocuments);
}

[Fact]
[Test]
public async Task Create()
{
const string documentName = "Lorem Ipsum.txt";

var correspondent = await _paperlessClient.Correspondents.Create(new("Foo"));
var correspondent = await Client.Correspondents.Create(new("Foo"));
await using var documentStream = typeof(DocumentClientTests).GetResource(documentName);
var documentCreation = new DocumentCreation(documentStream, documentName)
{
Created = _clock.GetCurrentInstant(),
Created = Clock.GetCurrentInstant(),
Title = "Lorem Ipsum",
CorrespondentId = correspondent.Id,
ArchiveSerialNumber = 1,
};

var result = await _paperlessClient.Documents.Create(documentCreation);
var result = await Client.Documents.Create(documentCreation);

if (Fixture.Name.StartsWith("1.9.2"))
{
result.Should().BeOfType<ImportStarted>();
return;
}

var id = result.Should().BeOfType<DocumentCreated>().Subject.Id;
var document = (await _paperlessClient.Documents.Get(id))!;
var document = (await Client.Documents.Get(id))!;

using var scope = new AssertionScope();

Expand All @@ -84,6 +79,6 @@ public async Task Create()
document.Content.Replace("\n", Environment.NewLine).Replace("\r\n", Environment.NewLine).Should().Be(content);
#endif

await _paperlessClient.Correspondents.Delete(correspondent.Id);
await Client.Correspondents.Delete(correspondent.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

namespace VMelnalksnis.PaperlessDotNet.Tests.Integration;

[Collection("Paperless")]
public sealed class MinimalExampleTests
public sealed class MinimalExampleTests : PaperlessTests
{
private readonly IPaperlessClient _paperlessClient;

public MinimalExampleTests(PaperlessFixture paperlessFixture)
: base(paperlessFixture)
{
var options = paperlessFixture.Options;

Expand All @@ -37,7 +37,7 @@ public MinimalExampleTests(PaperlessFixture paperlessFixture)
_paperlessClient = new PaperlessClient(correspondentClient, documentClient);
}

[Fact]
[Test]
public async Task ShouldNotThrow()
{
await FluentActions
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Networks;

using JetBrains.Annotations;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -23,23 +22,25 @@
using VMelnalksnis.PaperlessDotNet.DependencyInjection;
using VMelnalksnis.Testcontainers.Paperless;

using Xunit.Abstractions;

[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]

namespace VMelnalksnis.PaperlessDotNet.Tests.Integration;

[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public sealed class PaperlessFixture : IAsyncLifetime
public sealed class PaperlessFixture : IAsyncDisposable
{
private readonly INetwork _network;
private readonly RedisContainer _redis;
private readonly PaperlessContainer _paperless;

public PaperlessFixture()
public PaperlessFixture(string paperlessVersion)
: this(paperlessVersion, paperlessVersion, builder => builder)
{
}

public PaperlessFixture(string paperlessVersion, string name, Func<PaperlessBuilder, PaperlessBuilder> config)
{
const string redis = "redis";

Name = name;

_network = new NetworkBuilder().Build();

_redis = new RedisBuilder()
Expand All @@ -48,20 +49,32 @@ public PaperlessFixture()
.WithNetworkAliases(redis)
.Build();

_paperless = new PaperlessBuilder()
.WithImage($"{PaperlessBuilder.PaperlessImage}:2.3.3")
var builder = new PaperlessBuilder()
.WithImage($"{PaperlessBuilder.PaperlessImage}:{paperlessVersion}")
.WithNetwork(_network)
.DependsOn(_redis)
.WithRedis($"redis://{redis}:{RedisBuilder.RedisPort}")
.Build();
.WithRedis($"redis://{redis}:{RedisBuilder.RedisPort}");

builder = config(builder);

_paperless = builder.Build();
}

internal string Name { get; }

internal PaperlessOptions Options { get; private set; } = null!;

internal IClock Clock { get; } = new FakeClock(Instant.FromUtc(2024, 01, 17, 18, 8, 23), Duration.Zero);

/// <inheritdoc />
public async Task InitializeAsync()
public async ValueTask DisposeAsync()
{
await _paperless.DisposeAsync();
await _redis.DisposeAsync();
await _network.DisposeAsync();
}

internal async Task InitializeAsync()
{
await _paperless.StartAsync();

Expand All @@ -70,15 +83,7 @@ public async Task InitializeAsync()
Options = new() { BaseAddress = baseAddress, Token = token };
}

/// <inheritdoc />
public async Task DisposeAsync()
{
await _paperless.DisposeAsync();
await _redis.DisposeAsync();
await _network.DisposeAsync();
}

internal IPaperlessClient GetPaperlessClient(ITestOutputHelper testOutputHelper)
internal IPaperlessClient GetPaperlessClient()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new List<KeyValuePair<string, string?>>
Expand All @@ -91,16 +96,14 @@ internal IPaperlessClient GetPaperlessClient(ITestOutputHelper testOutputHelper)
var serviceCollection = new ServiceCollection();
serviceCollection
.AddSingleton(DateTimeZoneProviders.Tzdb)
.AddPaperlessDotNet(configuration);
.AddPaperlessDotNet(configuration)
.ConfigureHttpClient(client => client.Timeout = TimeSpan.FromSeconds(5));

serviceCollection.AddLogging(builder =>
{
var logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.TestOutput(
testOutputHelper,
outputTemplate:
"{Timestamp:HH:mm:ss} [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}")
.WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}")
.Enrich.FromLogContext()
.CreateLogger();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2022 Valters Melnalksnis
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.

using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace VMelnalksnis.PaperlessDotNet.Tests.Integration;

public sealed class PaperlessFixtureSource : IEnumerable<TestFixtureData>
{
/// <inheritdoc />
public IEnumerator<TestFixtureData> GetEnumerator() => PaperlessSetup
.Fixtures
.Select(fixture => new TestFixtureData(fixture).SetArgDisplayNames(fixture.Name))
.GetEnumerator();

/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Loading

0 comments on commit 8b27671

Please sign in to comment.