Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor package mirroring #711

Merged
merged 4 commits into from
Dec 12, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update tests
  • Loading branch information
loic-sharma committed Dec 12, 2021
commit 008429b06a6d02a0901be7ec2225a54685a11127
100 changes: 83 additions & 17 deletions tests/BaGet.Core.Tests/Services/PackageServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void Setup(
localPackages = localPackages ?? new List<Package>();
upstreamPackages = upstreamPackages ?? new List<NuGetVersion>();

_packages
_db
.Setup(p => p.FindAsync(
"MyPackage",
/*includeUnlisted: */ true,
Expand Down Expand Up @@ -187,7 +187,7 @@ private void Setup(
localPackages = localPackages ?? new List<Package>();
upstreamPackages = upstreamPackages ?? new List<Package>();

_packages
_db
.Setup(p => p.FindAsync(
"MyPackage",
/*includeUnlisted: */ true,
Expand All @@ -202,19 +202,85 @@ private void Setup(
}
}

public class MirrorAsync : FactsBase
public class FindPackageOrNullAsync : MirrorAsync
{
private readonly string _id = "MyPackage";
private readonly NuGetVersion _version = new NuGetVersion("1.0.0");
protected override async Task TargetAsync()
=> await _target.FindPackageOrNullAsync(_id, _version, _cancellationToken);

[Fact]
public async Task ExistsInDatabase()
{
var expected = new Package();

_db
.Setup(p => p.ExistsAsync(_id, _version, _cancellationToken))
.ReturnsAsync(true);
_db
.Setup(p => p.FindOrNullAsync(_id, _version, /*includeUnlisted:*/ true, _cancellationToken))
.ReturnsAsync(expected);

var result = await _target.FindPackageOrNullAsync(_id, _version, _cancellationToken);

Assert.Same(expected, result);
}

[Fact]
public async Task DoesNotExistInDatabase()
{
_db
.Setup(p => p.FindOrNullAsync(_id, _version, /*includeUnlisted:*/ true, _cancellationToken))
.ReturnsAsync((Package)null);

var result = await _target.FindPackageOrNullAsync(_id, _version, _cancellationToken);

Assert.Null(result);
}
}

public class ExistsAsync : MirrorAsync
{
protected override async Task TargetAsync() => await _target.ExistsAsync(_id, _version, _cancellationToken);

[Fact]
public async Task ExistsInDatabase()
{
_db
.Setup(p => p.ExistsAsync(_id, _version, _cancellationToken))
.ReturnsAsync(true);

var result = await _target.ExistsAsync(_id, _version, _cancellationToken);

Assert.True(result);
}

[Fact]
public async Task DoesNotExistInDatabase()
{
_db
.Setup(p => p.ExistsAsync(_id, _version, _cancellationToken))
.ReturnsAsync(false);

var result = await _target.ExistsAsync(_id, _version, _cancellationToken);

Assert.False(result);
}
}

public abstract class MirrorAsync : FactsBase
{
protected readonly string _id = "MyPackage";
protected readonly NuGetVersion _version = new NuGetVersion("1.0.0");

protected abstract Task TargetAsync();

[Fact]
public async Task SkipsIfAlreadyMirrored()
{
_packages
_db
.Setup(p => p.ExistsAsync(_id, _version, _cancellationToken))
.ReturnsAsync(true);

await _target.MirrorAsync(_id, _version, _cancellationToken);
await TargetAsync();

_indexer.Verify(
i => i.IndexAsync(It.IsAny<Stream>(), _cancellationToken),
Expand All @@ -224,15 +290,15 @@ public async Task SkipsIfAlreadyMirrored()
[Fact]
public async Task SkipsIfUpstreamDoesntHavePackage()
{
_packages
_db
.Setup(p => p.ExistsAsync(_id, _version, _cancellationToken))
.ReturnsAsync(false);

_upstream
.Setup(u => u.DownloadPackageOrNullAsync(_id, _version, _cancellationToken))
.ReturnsAsync((Stream)null);

await _target.MirrorAsync(_id, _version, _cancellationToken);
await TargetAsync();

_indexer.Verify(
i => i.IndexAsync(It.IsAny<Stream>(), _cancellationToken),
Expand All @@ -242,15 +308,15 @@ public async Task SkipsIfUpstreamDoesntHavePackage()
[Fact]
public async Task SkipsIfUpstreamThrows()
{
_packages
_db
.Setup(p => p.ExistsAsync(_id, _version, _cancellationToken))
.ReturnsAsync(false);

_upstream
.Setup(u => u.DownloadPackageOrNullAsync(_id, _version, _cancellationToken))
.ThrowsAsync(new InvalidOperationException("Hello world"));

await _target.MirrorAsync(_id, _version, _cancellationToken);
await TargetAsync();

_indexer.Verify(
i => i.IndexAsync(It.IsAny<Stream>(), _cancellationToken),
Expand All @@ -260,7 +326,7 @@ public async Task SkipsIfUpstreamThrows()
[Fact]
public async Task MirrorsPackage()
{
_packages
_db
.Setup(p => p.ExistsAsync(_id, _version, _cancellationToken))
.ReturnsAsync(false);

Expand All @@ -270,7 +336,7 @@ public async Task MirrorsPackage()
.Setup(u => u.DownloadPackageOrNullAsync(_id, _version, _cancellationToken))
.ReturnsAsync(downloadStream);

await _target.MirrorAsync(_id, _version, _cancellationToken);
await TargetAsync();

_indexer.Verify(
i => i.IndexAsync(It.IsAny<Stream>(), _cancellationToken),
Expand All @@ -281,21 +347,21 @@ public async Task MirrorsPackage()

public class FactsBase
{
protected readonly Mock<IPackageDatabase> _packages;
protected readonly Mock<IPackageDatabase> _db;
protected readonly Mock<IUpstreamClient> _upstream;
protected readonly Mock<IPackageIndexingService> _indexer;

protected readonly CancellationToken _cancellationToken = CancellationToken.None;
protected readonly PackageService _target;

public FactsBase()
protected FactsBase()
{
_packages = new Mock<IPackageDatabase>();
_db = new Mock<IPackageDatabase>();
_upstream = new Mock<IUpstreamClient>();
_indexer = new Mock<IPackageIndexingService>();

_target = new PackageService(
_packages.Object,
_db.Object,
_upstream.Object,
_indexer.Object,
Mock.Of<ILogger<PackageService>>());
Expand Down