-
Notifications
You must be signed in to change notification settings - Fork 644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tilovell/403 latest stable should be default package version seen #1626
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
using NuGet; | ||
using NuGetGallery.Packaging; | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace NuGetGallery | ||
{ | ||
|
@@ -1046,108 +1047,116 @@ public void WillThrowIfThePackageDoesNotExist() | |
public class TheFindPackageByIdAndVersionMethod | ||
{ | ||
[Fact] | ||
public void WillGetTheLatestVersionWhenTheVersionArgumentIsNull() | ||
{ | ||
var packageRegistration = new PackageRegistration { Id = "theId" }; | ||
var packages = new[] | ||
{ | ||
new Package { Version = "1.0", PackageRegistration = packageRegistration }, | ||
new Package | ||
{ Version = "2.0", PackageRegistration = packageRegistration, IsLatestStable = true, IsLatest = true } | ||
}.AsQueryable(); | ||
var packageRepository = new Mock<IEntityRepository<Package>>(); | ||
packageRepository.Setup(r => r.GetAll()).Returns(packages); | ||
var service = CreateService(packageRepository: packageRepository); | ||
|
||
var package = service.FindPackageByIdAndVersion("theId", null); | ||
|
||
Assert.Equal("2.0", package.Version); | ||
} | ||
|
||
[Fact] | ||
public void WillGetSpecifiedVersionWhenTheVersionArgumentIsNotNull() | ||
public void ReturnsTheRequestedPackageVersion() | ||
{ | ||
var service = CreateService(setup: | ||
mockPackageService => | ||
{ | ||
mockPackageService.Setup(x => x.FindPackageRegistrationById(It.IsAny<string>())).Throws( | ||
new Exception("This should not be called when the version is specified.")); | ||
}); | ||
mockPackageService => | ||
{ | ||
mockPackageService.Setup(x => x.FindPackageRegistrationById(It.IsAny<string>())).Throws( | ||
new Exception("This should not be called when the version is specified.")); | ||
}); | ||
|
||
Assert.DoesNotThrow(() => service.FindPackageByIdAndVersion("theId", "1.0.42")); | ||
|
||
// Nothing to assert because it's too damn complicated to test the actual LINQ expression. | ||
// What we're testing via the throw above is that it didn't load the registration and get the latest version. | ||
} | ||
|
||
[Fact] | ||
public void WillThrowIfIdIsNull() | ||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void WillThrowIfIdIsNull(string id) | ||
{ | ||
var service = CreateService(); | ||
var ex = Assert.Throws<ArgumentNullException>(() => service.FindPackageByIdAndVersion(id, "1.0.42")); | ||
Assert.Equal("id", ex.ParamName); | ||
} | ||
|
||
var ex = Assert.Throws<ArgumentNullException>(() => service.FindPackageByIdAndVersion(null, "1.0.42")); | ||
[Fact] | ||
public void ReturnsTheLatestStableVersionIfAvailable() | ||
{ | ||
// Arrange | ||
var repository = new Mock<IEntityRepository<Package>>(MockBehavior.Strict); | ||
var packageRegistration = new PackageRegistration { Id = "theId" }; | ||
var package1 = new Package { Version = "1.0", PackageRegistration = packageRegistration, Listed = true, IsLatestStable = true }; | ||
var package2 = new Package { Version = "1.0.0a", PackageRegistration = packageRegistration, IsPrerelease = true, Listed = true, IsLatest = true }; | ||
|
||
Assert.Equal("id", ex.ParamName); | ||
repository | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't know if it's in dev-start yet, but I have a helper in TestUtils\MockExtensions.cs called "HasData" which lets you rewrite these as: repository.HasData(new[] {...}). It does the Setup, Returns and AsQueryable for you :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't look like it's there yet. CanHasData = false. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I hadn't pulled dev-start looks like. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CanHasData = true! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or not. Still NoCanHasData. Unless there's another dev-start coming up? |
||
.Setup(repo => repo.GetAll()) | ||
.Returns(new[] { package1, package2 }.AsQueryable()); | ||
var service = CreateService(packageRepository: repository); | ||
|
||
// Act | ||
var result = service.FindPackageByIdAndVersion("theId", version: null); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Equal("1.0", result.Version); | ||
} | ||
|
||
[Fact] | ||
public void FindPackageReturnsTheLatestVersionIfAvailable() | ||
public void ReturnsTheLatestVersionIfNoLatestStableVersionIsAvailable() | ||
{ | ||
// Arrange | ||
var repository = new Mock<IEntityRepository<Package>>(MockBehavior.Strict); | ||
var package = CreatePackage("Foo", "1.0.0"); | ||
package.IsLatest = true; | ||
package.IsLatestStable = true; | ||
var packageA = CreatePackage("Foo", "1.0.0a"); | ||
var packageRegistration = new PackageRegistration { Id = "theId" }; | ||
var package1 = new Package { Version = "1.0.0b", PackageRegistration = packageRegistration, IsPrerelease = true, Listed = true, IsLatest = true }; | ||
var package2 = new Package { Version = "1.0.0a", PackageRegistration = packageRegistration, IsPrerelease = true, Listed = true }; | ||
|
||
repository.Setup(repo => repo.GetAll()) | ||
.Returns(new[] { package, packageA }.AsQueryable()); | ||
repository | ||
.Setup(repo => repo.GetAll()) | ||
.Returns(new[] { package1, package2 }.AsQueryable()); | ||
var service = CreateService(packageRepository: repository); | ||
|
||
// Act | ||
var result = service.FindPackageByIdAndVersion("Foo", version: null); | ||
var result = service.FindPackageByIdAndVersion("theId", version: null); | ||
|
||
// Assert | ||
Assert.Equal(package, result); | ||
Assert.NotNull(result); | ||
Assert.Equal("1.0.0b", result.Version); | ||
} | ||
|
||
[Fact] | ||
public void FindPackageReturnsTheLatestVersionIfNoLatestStableVersionIsAvailable() | ||
public void ReturnsNullIfNoLatestStableVersionIsAvailableAndPrereleaseIsDisallowed() | ||
{ | ||
// Arrange | ||
var repository = new Mock<IEntityRepository<Package>>(MockBehavior.Strict); | ||
var package = CreatePackage("Foo", "1.0.0b"); | ||
package.IsLatest = true; | ||
var packageA = CreatePackage("Foo", "1.0.0a"); | ||
var packageRegistration = new PackageRegistration { Id = "theId" }; | ||
var package1 = new Package { Version = "1.0.0b", PackageRegistration = packageRegistration, IsPrerelease = true, Listed = true, IsLatest = true }; | ||
var package2 = new Package { Version = "1.0.0a", PackageRegistration = packageRegistration, IsPrerelease = true, Listed = true }; | ||
|
||
repository.Setup(repo => repo.GetAll()) | ||
.Returns(new[] { package, packageA }.AsQueryable()); | ||
repository | ||
.Setup(repo => repo.GetAll()) | ||
.Returns(new[] { package1, package2 }.AsQueryable()); | ||
var service = CreateService(packageRepository: repository); | ||
|
||
// Act | ||
var result = service.FindPackageByIdAndVersion("Foo", null); | ||
var result = service.FindPackageByIdAndVersion("theId", version: null, allowPrerelease: false); | ||
|
||
// Assert | ||
Assert.Equal(package, result); | ||
Assert.Null(result); | ||
} | ||
|
||
[Fact] | ||
public void FindPackageReturnsTheLatestVersionIfNoLatestVersionIsAvailable() | ||
public void ReturnsTheMostRecentVersionIfNoLatestVersionIsAvailable() | ||
{ | ||
// Arrange | ||
var repository = new Mock<IEntityRepository<Package>>(MockBehavior.Strict); | ||
var package = CreatePackage("Foo", "1.0.0b"); | ||
var packageA = CreatePackage("Foo", "1.0.0a"); | ||
var packageRegistration = new PackageRegistration { Id = "theId" }; | ||
var package1 = new Package { Version = "1.0.0b", PackageRegistration = packageRegistration, IsPrerelease = true, Listed = false }; | ||
var package2 = new Package { Version = "1.0.0a", PackageRegistration = packageRegistration, IsPrerelease = true, Listed = false }; | ||
|
||
repository.Setup(repo => repo.GetAll()) | ||
.Returns(new[] { package, packageA }.AsQueryable()); | ||
repository | ||
.Setup(repo => repo.GetAll()) | ||
.Returns(new[] { package1, package2 }.AsQueryable()); | ||
var service = CreateService(packageRepository: repository); | ||
|
||
// Act | ||
var result = service.FindPackageByIdAndVersion("Foo", null); | ||
var result = service.FindPackageByIdAndVersion("theId", null); | ||
|
||
// Assert | ||
Assert.Equal(package, result); | ||
Assert.NotNull(result); | ||
Assert.Equal("1.0.0b", result.Version); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<picking-nits-like-a-bawse>WillThrowIfIdIsNullOrEmpty</> :)