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
Support PackageType in Catalog2AzureSearch (#716)
Browse files Browse the repository at this point in the history
* Support PackageType in Catalog2AzureSearch
* update tests.
* Dump unneeded properties from serialization of packagetype.
* fix nit. better array comparison
  • Loading branch information
ryuyu authored Dec 17, 2019
1 parent 3902f6b commit 7d9c316
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class PackageDetailsCatalogLeaf : CatalogLeaf
[JsonProperty("packageSize")]
public long PackageSize { get; set; }

[JsonProperty("packageTypes")]
public List<PackageType> PackageTypes { get; set; }

[JsonProperty("projectUrl")]
public string ProjectUrl { get; set; }

Expand Down
20 changes: 20 additions & 0 deletions src/NuGet.Protocol.Catalog/Models/PackageType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Newtonsoft.Json;

namespace NuGet.Protocol.Catalog
{
/// <summary>
/// Source: https://docs.microsoft.com/en-us/nuget/api/catalog-resource#catalog-leaf
/// </summary>
public class PackageType
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("version")]
public string Version { get; set; }
}
}
1 change: 1 addition & 0 deletions src/NuGet.Protocol.Catalog/NuGet.Protocol.Catalog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="Models\PackageDependency.cs" />
<Compile Include="Models\PackageDependencyGroup.cs" />
<Compile Include="Models\PackageDetailsCatalogLeaf.cs" />
<Compile Include="Models\PackageType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\AssemblyInfo.*.cs" />
<Compile Include="ResponseAndResult.cs" />
Expand Down
8 changes: 7 additions & 1 deletion src/NuGet.Services.AzureSearch/SearchDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ public SearchDocument.UpdateLatest UpdateLatestFromCatalog(
{
var document = new SearchDocument.UpdateLatest();

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

PopulateUpdateLatest(
document,
leaf.PackageId,
Expand All @@ -180,7 +186,7 @@ public SearchDocument.UpdateLatest UpdateLatestFromCatalog(
isLatest: isLatest,
fullVersion: fullVersion,
owners: owners,
packageTypes: null);
packageTypes: packageTypes);
_baseDocumentBuilder.PopulateMetadata(document, normalizedVersion, leaf);

return document;
Expand Down
115 changes: 92 additions & 23 deletions tests/NuGet.Services.AzureSearch.Tests/SearchDocumentBuilderFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,29 @@ public async Task SetsExpectedProperties(SearchFilters searchFilters, string exp
}", json);
}

[Theory]
[MemberData(nameof(CatalogPackageTypesData))]
public void SetsExpectedPackageTypes(List<NuGet.Protocol.Catalog.PackageType> packageTypes, string[] expectedFilterable, string[] expectedDisplay)
{
var leaf = Data.Leaf;
leaf.PackageTypes = packageTypes;

var document = _target.UpdateLatestFromCatalog(
SearchFilters.Default,
Data.Versions,
isLatestStable: false,
isLatest: true,
normalizedVersion: Data.NormalizedVersion,
fullVersion: Data.FullVersion,
leaf: leaf,
owners: Data.Owners);

SetDocumentLastUpdated(document);
Assert.Equal(document.FilterablePackageTypes.Length, document.PackageTypes.Length);
Assert.Equal(expectedFilterable, document.FilterablePackageTypes);
Assert.Equal(expectedDisplay, document.PackageTypes);
}

[Fact]
public void LeavesNullRequiresLicenseAcceptanceAsNull()
{
Expand Down Expand Up @@ -747,8 +770,8 @@ public async Task SetsExpectedProperties(SearchFilters searchFilters, string exp
}

[Theory]
[MemberData(nameof(PackageTypesData))]
public async Task SetsExpectedPackageTypes(List<PackageType> packageTypes, string expectedFilterable, string expectedDisplay)
[MemberData(nameof(DBPackageTypesData))]
public void SetsExpectedPackageTypes(List<PackageType> packageTypes, string[] expectedFilterable, string[] expectedDisplay)
{
var package = Data.PackageEntity;
package.PackageTypes = packageTypes;
Expand All @@ -766,16 +789,9 @@ public async Task SetsExpectedPackageTypes(List<PackageType> packageTypes, strin
isExcludedByDefault: false);

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

Assert.Contains(@"
""packageTypes"": [
" + expectedDisplay + @"
],", json);
Assert.Equal(document.FilterablePackageTypes.Length, document.PackageTypes.Length);
Assert.Equal(expectedFilterable, document.FilterablePackageTypes);
Assert.Equal(expectedDisplay, document.PackageTypes);
}

[Fact]
Expand Down Expand Up @@ -868,7 +884,60 @@ public abstract class BaseFacts
new object[] { SearchFilters.IncludePrereleaseAndSemVer2, "IncludePrereleaseAndSemVer2" },
};

public static IEnumerable<object[]> PackageTypesData => new[]
public static IEnumerable<object[]> CatalogPackageTypesData => new[]
{
new object[] {
new List<NuGet.Protocol.Catalog.PackageType> {
new NuGet.Protocol.Catalog.PackageType
{
Name = "DotNetCliTool"
}
},
new string[] { "dotnetclitool" },
new string[] { "DotNetCliTool" }
},

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

new object[] {
new List<NuGet.Protocol.Catalog.PackageType>(),
new string[] { "dependency" },
new string[] { "Dependency" }
},

new object[] {
new List<NuGet.Protocol.Catalog.PackageType> {
new NuGet.Protocol.Catalog.PackageType
{
Name = "DotNetCliTool"
},
new NuGet.Protocol.Catalog.PackageType
{
Name = "Dependency"
}
},
new string[] { "dotnetclitool", "dependency" },
new string[] { "DotNetCliTool", "Dependency" },
},

new object[] {
new List<NuGet.Protocol.Catalog.PackageType> {
new NuGet.Protocol.Catalog.PackageType
{
Name = "DotNetCliTool",
Version = "1.0.0"
}
},
new string[] { "dotnetclitool" },
new string[] { "DotNetCliTool" }
},
};

public static IEnumerable<object[]> DBPackageTypesData => new[]
{
new object[] {
new List<PackageType> {
Expand All @@ -877,20 +946,20 @@ public abstract class BaseFacts
Name = "DotNetCliTool"
}
},
@"""dotnetclitool""",
@"""DotNetCliTool"""
new string[] { "dotnetclitool" },
new string[] { "DotNetCliTool" }
},

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

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

new object[] {
Expand All @@ -904,8 +973,8 @@ public abstract class BaseFacts
Name = "Dependency"
}
},
"\"dotnetclitool\",\r\n \"dependency\"",
"\"DotNetCliTool\",\r\n \"Dependency\"",
new string[] { "dotnetclitool", "dependency" },
new string[] { "DotNetCliTool", "Dependency" },
},

new object[] {
Expand All @@ -916,8 +985,8 @@ public abstract class BaseFacts
Version = "1.0.0"
}
},
@"""dotnetclitool""",
@"""DotNetCliTool"""
new string[] { "dotnetclitool" },
new string[] { "DotNetCliTool" }
},
};

Expand Down

0 comments on commit 7d9c316

Please sign in to comment.