Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
/ NuGet.Jobs Public archive

Commit

Permalink
[PackageType]Support PackageType in db2azuresearch (#715)
Browse files Browse the repository at this point in the history
* Support PackageType in db2azuresearch
* Update to assert limited in test for readability.
  • Loading branch information
ryuyu authored Dec 17, 2019
1 parent fe74d98 commit 25f2e4e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ private async Task<IReadOnlyList<Package>> GetPackagesAsync(PackageRegistrationR
var query = context
.Set<Package>()
.Include(x => x.PackageRegistration)
.Include(x => x.PackageTypes)
.Where(p => p.PackageStatusKey == PackageStatus.Available)
.Where(p => p.PackageRegistrationKey >= minKey);

Expand Down
33 changes: 30 additions & 3 deletions src/NuGet.Services.AzureSearch/SearchDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using NuGet.Protocol.Catalog;
using NuGet.Services.Entities;

namespace NuGet.Services.AzureSearch
{
public class SearchDocumentBuilder : ISearchDocumentBuilder
{
private readonly string[] ImpliedDependency = new string[] { "Dependency" };
private readonly string[] FilterableImpliedDependency = new string[] { "dependency" };

private readonly IBaseDocumentBuilder _baseDocumentBuilder;

public SearchDocumentBuilder(IBaseDocumentBuilder baseDocumentBuilder)
Expand Down Expand Up @@ -175,7 +179,8 @@ public SearchDocument.UpdateLatest UpdateLatestFromCatalog(
isLatestStable: isLatestStable,
isLatest: isLatest,
fullVersion: fullVersion,
owners: owners);
owners: owners,
packageTypes: null);
_baseDocumentBuilder.PopulateMetadata(document, normalizedVersion, leaf);

return document;
Expand All @@ -195,6 +200,12 @@ public SearchDocument.Full FullFromDb(
{
var document = new SearchDocument.Full();

// Determine if we have packageTypes to forward.
// Otherwise, we need to let the system know that there were no explicit package types
var packageTypes = package.PackageTypes != null && package.PackageTypes.Count > 0 ?
package.PackageTypes.Select(pt => pt.Name).ToArray() :
null;

PopulateUpdateLatest(
document,
packageId,
Expand All @@ -206,7 +217,8 @@ public SearchDocument.Full FullFromDb(
isLatestStable: isLatestStable,
isLatest: isLatest,
fullVersion: fullVersion,
owners: owners);
owners: owners,
packageTypes: packageTypes);
_baseDocumentBuilder.PopulateMetadata(document, packageId, package);
PopulateDownloadCount(document, totalDownloadCount);
PopulateIsExcludedByDefault(document, isExcludedByDefault);
Expand Down Expand Up @@ -252,7 +264,8 @@ private void PopulateUpdateLatest(
bool isLatestStable,
bool isLatest,
string fullVersion,
string[] owners)
string[] owners,
string[] packageTypes)
{
PopulateVersions(
document,
Expand All @@ -266,6 +279,20 @@ private void PopulateUpdateLatest(
isLatest);
document.SearchFilters = DocumentUtilities.GetSearchFilterString(searchFilters);
document.FullVersion = fullVersion;

// If the package has explicit types, we will set them here.
// Otherwise, we will treat the package as a "Depedency" type and fill in the explicit type.
if (packageTypes != null && packageTypes.Length > 0)
{
document.PackageTypes = packageTypes;
document.FilterablePackageTypes = packageTypes.Select(pt => pt.ToLowerInvariant()).ToArray();
}
else
{
document.PackageTypes = ImpliedDependency;
document.FilterablePackageTypes = FilterableImpliedDependency;
}

PopulateOwners(
document,
owners);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,19 @@ public async Task SetsExpectedProperties(SearchFilters searchFilters, string exp
""azure-sdk""
],
""searchFilters"": """ + expected + @""",
""filterablePackageTypes"": [
""dependency""
],
""fullVersion"": ""7.1.2-alpha+git"",
""versions"": [
""1.0.0"",
""2.0.0+git"",
""3.0.0-alpha.1"",
""7.1.2-alpha+git""
],
""packageTypes"": [
""Dependency""
],
""isLatestStable"": false,
""isLatest"": true,
""semVerLevel"": 2,
Expand Down Expand Up @@ -677,13 +683,19 @@ public async Task SetsExpectedProperties(SearchFilters searchFilters, string exp
""azure-sdk""
],
""searchFilters"": """ + expected + @""",
""filterablePackageTypes"": [
""dependency""
],
""fullVersion"": ""7.1.2-alpha+git"",
""versions"": [
""1.0.0"",
""2.0.0+git"",
""3.0.0-alpha.1"",
""7.1.2-alpha+git""
],
""packageTypes"": [
""Dependency""
],
""isLatestStable"": false,
""isLatest"": true,
""semVerLevel"": 2,
Expand Down Expand Up @@ -734,6 +746,38 @@ public async Task SetsExpectedProperties(SearchFilters searchFilters, string exp
}", json);
}

[Theory]
[MemberData(nameof(PackageTypesData))]
public async Task SetsExpectedPackageTypes(List<PackageType> packageTypes, string expectedFilterable, string expectedDisplay)
{
var package = Data.PackageEntity;
package.PackageTypes = packageTypes;

var document = _target.FullFromDb(
Data.PackageId,
SearchFilters.Default,
Data.Versions,
isLatestStable: false,
isLatest: true,
fullVersion: Data.FullVersion,
package: package,
owners: Data.Owners,
totalDownloadCount: Data.TotalDownloadCount,
isExcludedByDefault: false);

SetDocumentLastUpdated(document);
var json = await SerializationUtilities.SerializeToJsonAsync(document);
Assert.Contains(@"
""filterablePackageTypes"": [
" + expectedFilterable + @"
],", json);

Assert.Contains(@"
""packageTypes"": [
" + expectedDisplay + @"
],", json);
}

[Fact]
public void SplitsTags()
{
Expand Down Expand Up @@ -824,6 +868,59 @@ public abstract class BaseFacts
new object[] { SearchFilters.IncludePrereleaseAndSemVer2, "IncludePrereleaseAndSemVer2" },
};

public static IEnumerable<object[]> PackageTypesData => new[]
{
new object[] {
new List<PackageType> {
new PackageType
{
Name = "DotNetCliTool"
}
},
@"""dotnetclitool""",
@"""DotNetCliTool"""
},

new object[] {
null,
@"""dependency""",
@"""Dependency"""
},

new object[] {
new List<PackageType>(),
@"""dependency""",
@"""Dependency"""
},

new object[] {
new List<PackageType> {
new PackageType
{
Name = "DotNetCliTool"
},
new PackageType
{
Name = "Dependency"
}
},
"\"dotnetclitool\",\r\n \"dependency\"",
"\"DotNetCliTool\",\r\n \"Dependency\"",
},

new object[] {
new List<PackageType> {
new PackageType
{
Name = "DotNetCliTool",
Version = "1.0.0"
}
},
@"""dotnetclitool""",
@"""DotNetCliTool"""
},
};

[Fact]
public void AllSearchFiltersAreCovered()
{
Expand Down

0 comments on commit 25f2e4e

Please sign in to comment.