Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #69 from AnthonySteele/test-the-api-package-lookup
Browse files Browse the repository at this point in the history
Test the api package lookup
  • Loading branch information
Dan Cohen authored Jul 24, 2017
2 parents 4018d22 + 5bab41d commit 5396d72
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
45 changes: 45 additions & 0 deletions NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Threading.Tasks;
using NuKeeper.NuGet.Api;
using NUnit.Framework;

namespace NuKeeper.Tests.Nuget.Api
{
[TestFixture]
public class ApiPackageLookupTests
{
[Test]
public async Task UnknownPackageName_ShouldNotReturnResult()
{
IApiPackageLookup lookup = new ApiPackageLookup();

var package = await lookup.LookupLatest(Guid.NewGuid().ToString());

Assert.That(package, Is.Null);
}

[Test]
public async Task WellKnownPackageName_ShouldReturnResult()
{
IApiPackageLookup lookup = new ApiPackageLookup();

var package = await lookup.LookupLatest("Newtonsoft.Json");

Assert.That(package, Is.Not.Null);
Assert.That(package.Identity, Is.Not.Null);
Assert.That(package.Identity.Id, Is.EqualTo("Newtonsoft.Json"));
}

[Test]
public async Task AmbigousPackageName_ShouldReturnCorrectResult()
{
IApiPackageLookup lookup = new ApiPackageLookup();

var package = await lookup.LookupLatest("AWSSDK");

Assert.That(package, Is.Not.Null);
Assert.That(package.Identity, Is.Not.Null);
Assert.That(package.Identity.Id, Is.EqualTo("AWSSDK"));
}
}
}
26 changes: 13 additions & 13 deletions NuKeeper/NuGet/Api/ApiPackageLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
Expand All @@ -11,18 +12,21 @@ namespace NuKeeper.NuGet.Api
{
public class ApiPackageLookup : IApiPackageLookup
{
private readonly ILogger _logger = new ConsoleLogger();

public async Task<IPackageSearchMetadata> LookupLatest(string packageName)
{
var versions = await Lookup(packageName);
return versions.FirstOrDefault();
return versions
.OrderByDescending(p => p?.Identity?.Version)
.FirstOrDefault();
}

private async Task<IEnumerable<IPackageSearchMetadata>> Lookup(string packageName)
{
var sourceRepository = BuildSourceRepository();
var searchResource = await sourceRepository.GetResourceAsync<PackageSearchResource>();

return await SearchForPackages(searchResource, packageName);
var metadataResource = await sourceRepository.GetResourceAsync<PackageMetadataResource>();
return await FindPackage(metadataResource, packageName);
}

private static SourceRepository BuildSourceRepository()
Expand All @@ -35,15 +39,11 @@ private static SourceRepository BuildSourceRepository()
return new SourceRepository(packageSource, providers);
}

private static async Task<IEnumerable<IPackageSearchMetadata>> SearchForPackages(PackageSearchResource searchResource, string packageName)
{
var logger = new ConsoleLogger();
var filter = new SearchFilter(false);

var packages = await searchResource
.SearchAsync(packageName, filter, 0, 10, logger, CancellationToken.None);

return packages;
private async Task<IEnumerable<IPackageSearchMetadata>> FindPackage(
PackageMetadataResource metadataResource, string packageName)
{
return await metadataResource
.GetMetadataAsync(packageName, false, false, _logger, CancellationToken.None);
}
}
}

0 comments on commit 5396d72

Please sign in to comment.