Skip to content

Commit 61c288a

Browse files
authored
[Client SDK] Add first round of tests (loic-sharma#338)
Part of loic-sharma#309
1 parent 269496c commit 61c288a

22 files changed

+1942
-103
lines changed

src/BaGet.Azure/Search/AzureSearchService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task IndexAsync(Package package, CancellationToken cancellationToke
4242
}
4343

4444
public async Task<SearchResponse> SearchAsync(
45-
string query,
45+
string query = null,
4646
int skip = 0,
4747
int take = 20,
4848
bool includePrerelease = true,
@@ -61,7 +61,7 @@ public async Task<SearchResponse> SearchAsync(
6161
}
6262

6363
public async Task<SearchResponse> SearchAsync(
64-
string query,
64+
string query = null,
6565
int skip = 0,
6666
int take = 20,
6767
bool includePrerelease = true,
@@ -134,7 +134,7 @@ public async Task<SearchResponse> SearchAsync(
134134
}
135135

136136
public async Task<AutocompleteResponse> AutocompleteAsync(
137-
string query,
137+
string query = null,
138138
AutocompleteType type = AutocompleteType.PackageIds,
139139
int skip = 0,
140140
int take = 20,

src/BaGet.Core/Mirror/MirrorService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private Package ToPackage(PackageMetadata metadata)
113113
Downloads = 0,
114114
HasReadme = false,
115115
Language = metadata.Language,
116-
Listed = metadata.Listed,
116+
Listed = metadata.IsListed(),
117117
MinClientVersion = metadata.MinClientVersion,
118118
Published = metadata.Published.UtcDateTime,
119119
RequireLicenseAcceptance = metadata.RequireLicenseAcceptance,

src/BaGet.Core/Search/DatabaseSearchService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public Task IndexAsync(Package package, CancellationToken cancellationToken)
2929
}
3030

3131
public async Task<SearchResponse> SearchAsync(
32-
string query,
32+
string query = null,
3333
int skip = 0,
3434
int take = 20,
3535
bool includePrerelease = true,
@@ -48,7 +48,7 @@ public async Task<SearchResponse> SearchAsync(
4848
}
4949

5050
public async Task<SearchResponse> SearchAsync(
51-
string query,
51+
string query = null,
5252
int skip = 0,
5353
int take = 20,
5454
bool includePrerelease = true,
@@ -107,7 +107,7 @@ public async Task<SearchResponse> SearchAsync(
107107
}
108108

109109
public async Task<AutocompleteResponse> AutocompleteAsync(
110-
string query,
110+
string query = null,
111111
AutocompleteType type = AutocompleteType.PackageIds,
112112
int skip = 0,
113113
int take = 20,

src/BaGet.Core/Search/IBaGetSearchResource.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public interface IBaGetSearchResource : ISearchResource
3434
/// <param name="cancellationToken">A token to cancel the task.</param>
3535
/// <returns>The search response.</returns>
3636
Task<SearchResponse> SearchAsync(
37-
string query,
37+
string query = null,
3838
int skip = 0,
3939
int take = 20,
4040
bool includePrerelease = true,

src/BaGet.Core/Search/NullSearchService.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class NullSearchService : IBaGetSearchResource
3636
});
3737

3838
public Task<AutocompleteResponse> AutocompleteAsync(
39-
string query,
39+
string query = null,
4040
AutocompleteType type = AutocompleteType.PackageIds,
4141
int skip = 0,
4242
int take = 20,
@@ -62,7 +62,7 @@ public Task IndexAsync(Package package, CancellationToken cancellationToken = de
6262
}
6363

6464
public Task<SearchResponse> SearchAsync(
65-
string query,
65+
string query = null,
6666
int skip = 0,
6767
int take = 20,
6868
bool includePrerelease = true,
@@ -75,7 +75,7 @@ public Task<SearchResponse> SearchAsync(
7575
}
7676

7777
public Task<SearchResponse> SearchAsync(
78-
string query,
78+
string query = null,
7979
int skip = 0,
8080
int take = 20,
8181
bool includePrerelease = true,

src/BaGet.Protocol/Extensions/PackageMetadataModelExtensions.cs

+17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ public static NuGetVersion ParseVersion(this PackageMetadata package)
1919
return NuGetVersion.Parse(package.Version);
2020
}
2121

22+
/// <summary>
23+
/// Determines if the provided package metadata represents a listed package.
24+
/// </summary>
25+
/// <param name="package">The package metadata.</param>
26+
/// <returns>True if the package is listed.</returns>
27+
public static bool IsListed(this PackageMetadata package)
28+
{
29+
if (package.Listed.HasValue)
30+
{
31+
return package.Listed.Value;
32+
}
33+
34+
// A published year of 1900 indicates that this package is unlisted, when the listed property itself is
35+
// not present (legacy behavior).
36+
return package.Published.Year != 1900;
37+
}
38+
2239
/// <summary>
2340
/// Parse the registration page's lower version as a <see cref="NuGetVersion" />.
2441
/// </summary>

src/BaGet.Protocol/Models/PackageMetadata.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public class PackageMetadata
6161

6262
/// <summary>
6363
/// Whether the package is listed in search results.
64+
/// If <see langword="null"/>, the package should be considered as listed.
6465
/// </summary>
6566
[JsonProperty("listed")]
66-
public bool Listed { get; set; }
67+
public bool? Listed { get; set; }
6768

6869
/// <summary>
6970
/// The minimum NuGet client version needed to use this package.

src/BaGet.Protocol/NuGetClient.cs

+21-9
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public virtual async Task<IReadOnlyList<NuGetVersion>> ListPackageVersionsAsync(
111111
var packages = await GetPackageMetadataAsync(packageId, cancellationToken);
112112

113113
return packages
114-
.Where(p => p.Listed)
114+
.Where(p => p.IsListed())
115115
.Select(p => p.ParseVersion())
116116
.ToList();
117117
}
@@ -249,10 +249,12 @@ public virtual async Task<PackageMetadata> GetPackageMetadataAsync(string packag
249249
/// <summary>
250250
/// Search for packages. Includes prerelease packages.
251251
/// </summary>
252-
/// <param name="query">The search query.</param>
252+
/// <param name="query">
253+
/// The search query. If <see langword="null"/>, gets default search results.
254+
/// </param>
253255
/// <param name="cancellationToken">A token to cancel the task.</param>
254256
/// <returns>The search results, including prerelease packages.</returns>
255-
public virtual async Task<SearchResponse> SearchAsync(string query, CancellationToken cancellationToken = default)
257+
public virtual async Task<SearchResponse> SearchAsync(string query = null, CancellationToken cancellationToken = default)
256258
{
257259
var client = await _clientFactory.CreateSearchClientAsync(cancellationToken);
258260

@@ -262,7 +264,9 @@ public virtual async Task<SearchResponse> SearchAsync(string query, Cancellation
262264
/// <summary>
263265
/// Search for packages.
264266
/// </summary>
265-
/// <param name="query">The search query.</param>
267+
/// <param name="query">
268+
/// The search query. If <see langword="null"/>, gets default search results.
269+
/// </param>
266270
/// <param name="includePrerelease">Whether to include prerelease packages.</param>
267271
/// <param name="cancellationToken">A token to cancel the task.</param>
268272
/// <returns>The search results.</returns>
@@ -279,7 +283,9 @@ public virtual async Task<SearchResponse> SearchAsync(string query, bool include
279283
/// <summary>
280284
/// Search for packages. Includes prerelease packages.
281285
/// </summary>
282-
/// <param name="query">The search query.</param>
286+
/// <param name="query">
287+
/// The search query. If <see langword="null"/>, gets default search results.
288+
/// </param>
283289
/// <param name="skip">The number of results to skip.</param>
284290
/// <param name="take">The number of results to include.</param>
285291
/// <param name="cancellationToken">A token to cancel the task.</param>
@@ -294,7 +300,9 @@ public virtual async Task<SearchResponse> SearchAsync(string query, int skip, in
294300
/// <summary>
295301
/// Search for packages.
296302
/// </summary>
297-
/// <param name="query">The search query.</param>
303+
/// <param name="query">
304+
/// The search query. If <see langword="null"/>, gets default search results.
305+
/// </param>
298306
/// <param name="includePrerelease">Whether to include prerelease packages.</param>
299307
/// <param name="skip">The number of results to skip.</param>
300308
/// <param name="take">The number of results to include.</param>
@@ -310,10 +318,12 @@ public virtual async Task<SearchResponse> SearchAsync(string query, bool include
310318
/// <summary>
311319
/// Search for package IDs.
312320
/// </summary>
313-
/// <param name="query">The search query.</param>
321+
/// <param name="query">
322+
/// The search query. If <see langword="null"/>, gets default autocomplete results.
323+
/// </param>
314324
/// <param name="cancellationToken">A token to cancel the task.</param>
315325
/// <returns>The autocomplete results.</returns>
316-
public virtual async Task<AutocompleteResponse> AutocompleteAsync(string query, CancellationToken cancellationToken = default)
326+
public virtual async Task<AutocompleteResponse> AutocompleteAsync(string query = null, CancellationToken cancellationToken = default)
317327
{
318328
var client = await _clientFactory.CreateSearchClientAsync(cancellationToken);
319329

@@ -323,7 +333,9 @@ public virtual async Task<AutocompleteResponse> AutocompleteAsync(string query,
323333
/// <summary>
324334
/// Search for package IDs.
325335
/// </summary>
326-
/// <param name="query">The search query.</param>
336+
/// <param name="query">
337+
/// The search query. If <see langword="null"/>, gets default autocomplete results.
338+
/// </param>
327339
/// <param name="skip">The number of results to skip.</param>
328340
/// <param name="take">The number of results to include.</param>
329341
/// <param name="cancellationToken">A token to cancel the task.</param>

src/BaGet.Protocol/Search/ISearchResource.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public interface ISearchResource
2222
/// <param name="cancellationToken">A token to cancel the task.</param>
2323
/// <returns>The search response.</returns>
2424
Task<SearchResponse> SearchAsync(
25-
string query,
25+
string query = null,
2626
int skip = 0,
2727
int take = 20,
2828
bool includePrerelease = true,
@@ -42,7 +42,7 @@ Task<SearchResponse> SearchAsync(
4242
/// <param name="cancellationToken">A token to cancel the task.</param>
4343
/// <returns>The autocomplete response.</returns>
4444
Task<AutocompleteResponse> AutocompleteAsync(
45-
string query,
45+
string query = null,
4646
AutocompleteType type = AutocompleteType.PackageIds,
4747
int skip = 0,
4848
int take = 20,

src/BaGet.Protocol/Search/SearchClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public SearchClient(HttpClient httpClient, string searchUrl, string autocomplete
3333

3434
/// <inheritdoc />
3535
public async Task<AutocompleteResponse> AutocompleteAsync(
36-
string query,
36+
string query = null,
3737
AutocompleteType type = AutocompleteType.PackageIds,
3838
int skip = 0,
3939
int take = 20,
@@ -51,7 +51,7 @@ public async Task<AutocompleteResponse> AutocompleteAsync(
5151

5252
/// <inheritdoc />
5353
public async Task<SearchResponse> SearchAsync(
54-
string query,
54+
string query = null,
5555
int skip = 0,
5656
int take = 20,
5757
bool includePrerelease = true,

tests/BaGet.Protocol.Tests/BaGet.Protocol.Tests.csproj

+15
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,19 @@
2222
<ProjectReference Include="..\..\src\BaGet.Protocol\BaGet.Protocol.csproj" />
2323
</ItemGroup>
2424

25+
<ItemGroup>
26+
<Compile Update="TestData.Designer.cs">
27+
<DependentUpon>TestData.resx</DependentUpon>
28+
<DesignTime>True</DesignTime>
29+
<AutoGen>True</AutoGen>
30+
</Compile>
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<EmbeddedResource Update="TestData.resx">
35+
<LastGenOutput>TestData.Designer.cs</LastGenOutput>
36+
<Generator>ResXFileCodeGenerator</Generator>
37+
</EmbeddedResource>
38+
</ItemGroup>
39+
2540
</Project>

tests/BaGet.Protocol.Tests/CatalogClientTests.cs

+38-21
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,57 @@ public CatalogClientTests(ProtocolFixture fixture)
1717
}
1818

1919
[Fact]
20-
public async Task GetsCatalogIndex()
20+
public async Task GetCatalogIndex()
2121
{
2222
var result = await _target.GetIndexAsync();
2323

2424
Assert.NotNull(result);
25-
Assert.True(result.Count > 0);
26-
Assert.NotEmpty(result.Items);
25+
Assert.Equal(2, result.Count);
26+
Assert.Equal(2, result.Items.Count);
27+
Assert.Equal(TestData.CatalogPageUrl, result.Items.Select(i => i.CatalogPageUrl).First());
2728
}
2829

2930
[Fact]
30-
public async Task GetsCatalogLeaf()
31+
public async Task GetCatalogPage()
3132
{
32-
var index = await _target.GetIndexAsync();
33-
var pageItem = index.Items.First();
33+
var page = await _target.GetPageAsync(TestData.CatalogPageUrl);
3434

35-
var page = await _target.GetPageAsync(pageItem.CatalogPageUrl);
36-
var leafItem = page.Items.First();
35+
Assert.Equal(2, page.Count);
36+
Assert.Equal(2, page.Items.Count);
37+
Assert.Equal(TestData.CatalogIndexUrl, page.CatalogIndexUrl);
38+
Assert.Equal(TestData.PackageDetailsCatalogLeafUrl, page.Items[0].CatalogLeafUrl);
39+
Assert.Equal(TestData.PackageDeleteCatalogLeafUrl, page.Items[1].CatalogLeafUrl);
40+
}
41+
42+
[Fact]
43+
public async Task GetPackageDetailsLeaf()
44+
{
45+
var leaf = await _target.GetPackageDetailsLeafAsync(TestData.PackageDetailsCatalogLeafUrl);
46+
47+
Assert.Equal(TestData.PackageDetailsCatalogLeafUrl, leaf.CatalogLeafUrl);
48+
Assert.Equal(CatalogLeafType.PackageDetails, leaf.Type);
49+
50+
Assert.Equal("Test.Package", leaf.PackageId);
51+
Assert.Equal("1.0.0", leaf.PackageVersion);
52+
}
3753

38-
CatalogLeaf result;
39-
switch (leafItem.Type)
40-
{
41-
case CatalogLeafType.PackageDelete:
42-
result = await _target.GetPackageDeleteLeafAsync(leafItem.CatalogLeafUrl);
43-
break;
54+
[Fact]
55+
public async Task GetPackageDeleteLeaf()
56+
{
57+
var leaf = await _target.GetPackageDeleteLeafAsync(TestData.PackageDeleteCatalogLeafUrl);
4458

45-
case CatalogLeafType.PackageDetails:
46-
result = await _target.GetPackageDetailsLeafAsync(leafItem.CatalogLeafUrl);
47-
break;
59+
Assert.Equal(TestData.PackageDeleteCatalogLeafUrl, leaf.CatalogLeafUrl);
60+
Assert.Equal(CatalogLeafType.PackageDelete, leaf.Type);
4861

49-
default:
50-
throw new NotSupportedException($"Unknown leaf type '{leafItem.Type}'");
51-
}
62+
Assert.Equal("Deleted.Package", leaf.PackageId);
63+
Assert.Equal("1.0.0", leaf.PackageVersion);
64+
}
5265

53-
Assert.NotNull(result.PackageId);
66+
[Fact]
67+
public async Task ThrowsOnTypeMismatch()
68+
{
69+
await Assert.ThrowsAsync<ArgumentException>(() => _target.GetPackageDetailsLeafAsync(TestData.PackageDeleteCatalogLeafUrl));
70+
await Assert.ThrowsAsync<ArgumentException>(() => _target.GetPackageDeleteLeafAsync(TestData.PackageDetailsCatalogLeafUrl));
5471
}
5572
}
5673
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Xunit;
2+
3+
namespace BaGet.Protocol.Tests
4+
{
5+
public class NuGetClientFactoryTests : IClassFixture<ProtocolFixture>
6+
{
7+
private readonly NuGetClientFactory _target;
8+
9+
public NuGetClientFactoryTests(ProtocolFixture fixture)
10+
{
11+
_target = fixture.NuGetClientFactory;
12+
}
13+
14+
// TODO: Test package download
15+
// TODO: Test package manifest download
16+
// TODO: List package versions
17+
// TODO: List package versions #2
18+
// TODO: List all package versions
19+
// TODO: Get package metadata inlined
20+
// TODO: Get package metadata paged
21+
// TODO: Get single package metadata inlined
22+
// TODO: Get single package metadata paged
23+
// TODO: Search
24+
// TODO: Autocomplete
25+
}
26+
}

0 commit comments

Comments
 (0)