Skip to content

Commit

Permalink
Add $count support to FindPackagesById() OData endpoints (#4790)
Browse files Browse the repository at this point in the history
Fix #4760
This bug occurred in commit 3f92c55.
  • Loading branch information
joelverhagen committed Oct 3, 2017
1 parent a3a6a70 commit abfea8d
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/NuGetGallery/Controllers/ODataV1FeedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public async Task<IHttpActionResult> FindPackagesById(ODataQueryOptions<V1FeedPa
return await GetCore(options, id, version: null, return404NotFoundWhenNoResults: false);
}

// /api/v1/FindPackagesById()/$count?id=
[HttpGet]
[CacheOutput(ServerTimeSpan = NuGetODataConfig.GetByIdAndVersionCacheTimeInSeconds, Private = true, ClientTimeSpan = NuGetODataConfig.GetByIdAndVersionCacheTimeInSeconds)]
public async Task<IHttpActionResult> FindPackagesByIdCount(ODataQueryOptions<V1FeedPackage> options, [FromODataUri]string id)
{
var result = await FindPackagesById(options, id);
return result.FormattedAsCountResult<V1FeedPackage>();
}

private async Task<IHttpActionResult> GetCore(ODataQueryOptions<V1FeedPackage> options, string id, string version, bool return404NotFoundWhenNoResults)
{
var packages = _packagesRepository.GetAll()
Expand Down
13 changes: 13 additions & 0 deletions src/NuGetGallery/Controllers/ODataV2CuratedFeedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ public async Task<IHttpActionResult> FindPackagesById(
return await GetCore(options, curatedFeedName, id, normalizedVersion: null, return404NotFoundWhenNoResults: false, semVerLevel: semVerLevel);
}

// /api/v2/curated-feed/curatedFeedName/FindPackagesById()/$count?id=&semVerLevel=
[HttpGet]
[CacheOutput(NoCache = true)]
public async Task<IHttpActionResult> FindPackagesByIdCount(
ODataQueryOptions<V2FeedPackage> options,
string curatedFeedName,
[FromODataUri] string id,
[FromUri] string semVerLevel = null)
{
var result = await FindPackagesById(options, curatedFeedName, id, semVerLevel);
return result.FormattedAsCountResult<V2FeedPackage>();
}

private async Task<IHttpActionResult> GetCore(
ODataQueryOptions<V2FeedPackage> options,
string curatedFeedName,
Expand Down
11 changes: 11 additions & 0 deletions src/NuGetGallery/Controllers/ODataV2FeedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ public async Task<IHttpActionResult> FindPackagesById(
return404NotFoundWhenNoResults: false);
}

// /api/v2/FindPackagesById()/$count?semVerLevel=
[HttpGet]
[CacheOutput(NoCache = true)]
public async Task<IHttpActionResult> FindPackagesByIdCount(
ODataQueryOptions<V2FeedPackage> options,
[FromODataUri]string id,
[FromUri]string semVerLevel = null)
{
return (await FindPackagesById(options, id, semVerLevel)).FormattedAsCountResult<V2FeedPackage>();
}

private async Task<IHttpActionResult> GetCore(
ODataQueryOptions<V2FeedPackage> options,
string id,
Expand Down
12 changes: 12 additions & 0 deletions tests/NuGetGallery.Facts/Controllers/ODataV1FeedControllerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public async Task FindPackagesById_FiltersSemVerV2PackageVersions()
Assert.Equal(NonSemVer2Packages.Where(p => !p.IsPrerelease).Count(), resultSet.Count);
}

[Fact]
public async Task FindPackagesByIdCount_FiltersSemVerV2PackageVersions()
{
// Act
var count = await GetInt<V1FeedPackage>(
async (controller, options) => await controller.FindPackagesByIdCount(options, TestPackageId),
$"/api/v1/FindPackagesById/$count?id='{TestPackageId}'");

// Assert
Assert.Equal(NonSemVer2Packages.Where(p => !p.IsPrerelease).Count(), count);
}

[Fact]
public async Task Search_FiltersSemVerV2PackageVersions()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,41 @@ public async Task FindPackagesById_IncludesSemVerV2PackageVersionsWhenSemVerLeve
// Act
var resultSet = await GetCollection<V2FeedPackage>(
async (controller, options) => await controller.FindPackagesById(options, _curatedFeedName, id: TestPackageId, semVerLevel: semVerLevel),
$"/api/v2/curated-feed/{_curatedFeedName}/FindPackagesById?id='{TestPackageId}'?semVerLevel={semVerLevel}");
$"/api/v2/curated-feed/{_curatedFeedName}/FindPackagesById?id='{TestPackageId}'&semVerLevel={semVerLevel}");

// Assert
AssertSemVer2PackagesIncludedInResult(resultSet, includePrerelease: true);
Assert.Equal(AvailablePackages.Count, resultSet.Count);
}

[Fact]
public async Task FindPackagesByIdCount_FiltersSemVerV2PackageVersions()
{
// Act
var count = await GetInt<V2FeedPackage>(
async (controller, options) => await controller.FindPackagesByIdCount(options, _curatedFeedName, TestPackageId),
$"/api/v2/curated-feed/{_curatedFeedName}/FindPackagesById/$count?id='{TestPackageId}'");

// Assert
Assert.Equal(NonSemVer2Packages.Count, count);
}

[Theory]
[InlineData("2.0.0")]
[InlineData("2.0.1")]
[InlineData("3.0.0-alpha")]
[InlineData("3.0.0")]
public async Task FindPackagesByIdCount_IncludesSemVerV2PackageVersionsWhenSemVerLevel2OrHigher(string semVerLevel)
{
// Act
var count = await GetInt<V2FeedPackage>(
async (controller, options) => await controller.FindPackagesByIdCount(options, _curatedFeedName, id: TestPackageId, semVerLevel: semVerLevel),
$"/api/v2/curated-feed/{_curatedFeedName}/FindPackagesById/$count?id='{TestPackageId}'&semVerLevel={semVerLevel}");

// Assert
Assert.Equal(AvailablePackages.Count, count);
}

[Fact]
public async Task Search_FiltersSemVerV2PackageVersions_ExcludePrerelease()
{
Expand Down Expand Up @@ -183,7 +211,7 @@ public async Task Search_IncludesSemVerV2PackageVersionsWhenSemVerLevel2OrHigher
searchTerm: TestPackageId,
includePrerelease: true,
semVerLevel: semVerLevel),
$"/api/v2/curated-feed/{_curatedFeedName}/Search?searchTerm='{TestPackageId}'?semVerLevel={semVerLevel}&includePrerelease=true");
$"/api/v2/curated-feed/{_curatedFeedName}/Search?searchTerm='{TestPackageId}'&semVerLevel={semVerLevel}&includePrerelease=true");

// Assert
AssertSemVer2PackagesIncludedInResult(resultSet, includePrerelease: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,41 @@ public async Task FindPackagesById_IncludesSemVerV2PackageVersionsWhenSemVerLeve
// Act
var resultSet = await GetCollection<V2FeedPackage>(
(controller, options) => controller.FindPackagesById(options, id: TestPackageId, semVerLevel: semVerLevel),
$"/api/v2/FindPackagesById?id='{TestPackageId}'?semVerLevel={semVerLevel}");
$"/api/v2/FindPackagesById?id='{TestPackageId}'&semVerLevel={semVerLevel}");

// Assert
AssertSemVer2PackagesIncludedInResult(resultSet);
Assert.Equal(AvailablePackages.Count, resultSet.Count);
}

[Fact]
public async Task FindPackagesByIdCount_FiltersSemVerV2PackageVersionsByDefault()
{
// Act
var count = await GetInt<V2FeedPackage>(
(controller, options) => controller.FindPackagesByIdCount(options, id: TestPackageId),
$"/api/v2/FindPackagesById/$count?id='{TestPackageId}'");

// Assert
Assert.Equal(NonSemVer2Packages.Count, count);
}

[Theory]
[InlineData("2.0.0")]
[InlineData("2.0.1")]
[InlineData("3.0.0-alpha")]
[InlineData("3.0.0")]
public async Task FindPackagesByIdCount_IncludesSemVerV2PackageVersionsWhenSemVerLevel2OrHigher(string semVerLevel)
{
// Act
var count = await GetInt<V2FeedPackage>(
(controller, options) => controller.FindPackagesByIdCount(options, id: TestPackageId, semVerLevel: semVerLevel),
$"/api/v2/FindPackagesById/$count?id='{TestPackageId}'&semVerLevel={semVerLevel}");

// Assert
Assert.Equal(AvailablePackages.Count, count);
}

[Fact]
public async Task Search_FiltersSemVerV2PackageVersionsByDefault_IncludePrerelease()
{
Expand Down

0 comments on commit abfea8d

Please sign in to comment.