Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add unit tests.

  • Loading branch information...
commit 07eb3fc8c1495f0d7641d54e19a874d9694e6a8e 1 parent fd304b6
dotnetjunky authored
Showing with 208 additions and 20 deletions.
  1. +196 −11 Facts/Services/FeedServiceFacts.cs
  2. +12 −9 Website/DataServices/V2Feed.svc.cs
View
207 Facts/Services/FeedServiceFacts.cs
@@ -188,7 +188,7 @@ public void V2FeedGetUpdatesReturnsEmptyResultsIfInputIsMalformed(string id, str
var v2Service = new TestableV2Feed(repo, configuration, null);
// Act
- var result = v2Service.GetUpdates(id, version, includePrerelease: false, includeAllVersions: true, targetFrameworks: null)
+ var result = v2Service.GetUpdates(id, version, includePrerelease: false, includeAllVersions: true, targetFrameworks: null, versionConstraints: null)
.ToList();
// Assert
@@ -216,7 +216,7 @@ public void V2FeedGetUpdatesIgnoresItemsWithMalformedVersions()
var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
// Act
- var result = v2Service.GetUpdates("Foo|Qux", "1.0.0|abcd", includePrerelease: false, includeAllVersions: false, targetFrameworks: null)
+ var result = v2Service.GetUpdates("Foo|Qux", "1.0.0|abcd", includePrerelease: false, includeAllVersions: false, targetFrameworks: null, versionConstraints: null)
.ToList();
// Assert
@@ -245,7 +245,7 @@ public void V2FeedGetUpdatesReturnsVersionsNewerThanListedVersion()
var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
// Act
- var result = v2Service.GetUpdates("Foo|Qux", "1.0.0|0.9", includePrerelease: false, includeAllVersions: true, targetFrameworks: null)
+ var result = v2Service.GetUpdates("Foo|Qux", "1.0.0|0.9", includePrerelease: false, includeAllVersions: true, targetFrameworks: null, versionConstraints: null)
.ToList();
// Assert
@@ -255,6 +255,193 @@ public void V2FeedGetUpdatesReturnsVersionsNewerThanListedVersion()
AssertPackage(new { Id = "Qux", Version = "2.0" }, result[2]);
}
+ [Theory]
+ [InlineData("2.3|3.5|(1.0,2.3)")]
+ [InlineData("2.3")]
+ [InlineData("1.0||2.0")]
+ [InlineData("||")]
+ [InlineData("|1.0|")]
+ public void V2FeedGetUpdatesReturnsEmptyIfVersionConstraintsContainWrongNumberOfElements(string constraintString)
+ {
+ // Arrange
+ var packageRegistrationA = new PackageRegistration { Id = "Foo" };
+ var packageRegistrationB = new PackageRegistration { Id = "Qux" };
+ var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
+ repo.Setup(r => r.GetAll()).Returns(
+ new[]
+ {
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.0.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.1.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0-alpha", IsPrerelease = true, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "3.0", IsPrerelease = false, Listed = true },
+ }.AsQueryable());
+ var configuration = new Mock<IConfiguration>(MockBehavior.Strict);
+ configuration.Setup(c => c.GetSiteRoot(false)).Returns("https://localhost:8081/");
+ var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
+
+ // Act
+ var result = v2Service.GetUpdates(
+ "Foo|Qux",
+ "1.0.0|0.9",
+ includePrerelease: false,
+ includeAllVersions: true,
+ targetFrameworks: null,
+ versionConstraints: constraintString)
+ .ToList();
+
+ // Assert
+ Assert.Equal(0, result.Count);
+ }
+
+ [Fact]
+ public void V2FeedGetUpdatesReturnsVersionsConformingToConstraints()
+ {
+ // Arrange
+ var packageRegistrationA = new PackageRegistration { Id = "Foo" };
+ var packageRegistrationB = new PackageRegistration { Id = "Qux" };
+ var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
+ repo.Setup(r => r.GetAll()).Returns(
+ new[]
+ {
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.0.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.1.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0-alpha", IsPrerelease = true, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "3.0", IsPrerelease = false, Listed = true },
+ }.AsQueryable());
+ var configuration = new Mock<IConfiguration>(MockBehavior.Strict);
+ configuration.Setup(c => c.GetSiteRoot(false)).Returns("https://localhost:8081/");
+ var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
+
+ // Act
+ var result = v2Service.GetUpdates(
+ "Foo|Qux",
+ "1.0.0|0.9",
+ includePrerelease: false,
+ includeAllVersions: true,
+ targetFrameworks: null,
+ versionConstraints: "(,1.2.0)|[2.0,2.3]")
+ .ToList();
+
+ // Assert
+ Assert.Equal(2, result.Count);
+ AssertPackage(new { Id = "Foo", Version = "1.1.0" }, result[0]);
+ AssertPackage(new { Id = "Qux", Version = "2.0" }, result[1]);
+ }
+
+ [Fact]
+ public void V2FeedGetUpdatesIgnoreInvalidVersionConstraints()
+ {
+ // Arrange
+ var packageRegistrationA = new PackageRegistration { Id = "Foo" };
+ var packageRegistrationB = new PackageRegistration { Id = "Qux" };
+ var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
+ repo.Setup(r => r.GetAll()).Returns(
+ new[]
+ {
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.0.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.1.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0-alpha", IsPrerelease = true, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "3.0", IsPrerelease = false, Listed = true },
+ }.AsQueryable());
+ var configuration = new Mock<IConfiguration>(MockBehavior.Strict);
+ configuration.Setup(c => c.GetSiteRoot(false)).Returns("https://localhost:8081/");
+ var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
+
+ // Act
+ var result = v2Service.GetUpdates(
+ "Foo|Qux",
+ "1.0.0|0.9",
+ includePrerelease: false,
+ includeAllVersions: true,
+ targetFrameworks: null,
+ versionConstraints: "(,1.2.0)|abdfsdf")
+ .ToList();
+
+ // Assert
+ Assert.Equal(3, result.Count);
+ AssertPackage(new { Id = "Foo", Version = "1.1.0" }, result[0]);
+ AssertPackage(new { Id = "Qux", Version = "2.0" }, result[1]);
+ AssertPackage(new { Id = "Qux", Version = "3.0" }, result[2]);
+ }
+
+ [Fact]
+ public void V2FeedGetUpdatesReturnsVersionsConformingToConstraintsWithMissingConstraintsForSomePackges()
+ {
+ // Arrange
+ var packageRegistrationA = new PackageRegistration { Id = "Foo" };
+ var packageRegistrationB = new PackageRegistration { Id = "Qux" };
+ var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
+ repo.Setup(r => r.GetAll()).Returns(
+ new[]
+ {
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.0.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.1.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0-alpha", IsPrerelease = true, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "3.0", IsPrerelease = false, Listed = true },
+ }.AsQueryable());
+ var configuration = new Mock<IConfiguration>(MockBehavior.Strict);
+ configuration.Setup(c => c.GetSiteRoot(false)).Returns("https://localhost:8081/");
+ var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
+
+ // Act
+ var result = v2Service.GetUpdates(
+ "Foo|Qux",
+ "1.0.0|0.9",
+ includePrerelease: false,
+ includeAllVersions: false,
+ targetFrameworks: null,
+ versionConstraints: "|(1.2,2.8)")
+ .ToList();
+
+ // Assert
+ Assert.Equal(2, result.Count);
+ AssertPackage(new { Id = "Foo", Version = "1.2.0" }, result[0]);
+ AssertPackage(new { Id = "Qux", Version = "2.0" }, result[1]);
+ }
+
+ [Fact]
+ public void V2FeedGetUpdatesReturnsEmptyPackagesIfNoPackageSatisfiesConstraints()
+ {
+ // Arrange
+ var packageRegistrationA = new PackageRegistration { Id = "Foo" };
+ var packageRegistrationB = new PackageRegistration { Id = "Qux" };
+ var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
+ repo.Setup(r => r.GetAll()).Returns(
+ new[]
+ {
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.0.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.1.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0-alpha", IsPrerelease = true, Listed = true },
+ new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "2.0", IsPrerelease = false, Listed = true },
+ new Package { PackageRegistration = packageRegistrationB, Version = "3.0", IsPrerelease = false, Listed = true },
+ }.AsQueryable());
+ var configuration = new Mock<IConfiguration>(MockBehavior.Strict);
+ configuration.Setup(c => c.GetSiteRoot(false)).Returns("https://localhost:8081/");
+ var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
+
+ // Act
+ var result = v2Service.GetUpdates(
+ "Foo|Qux",
+ "1.0.0|0.9",
+ includePrerelease: false,
+ includeAllVersions: false,
+ targetFrameworks: null,
+ versionConstraints: "3.4|4.0")
+ .ToList();
+
+ // Assert
+ Assert.Equal(0, result.Count);
+ }
+
[Fact]
public void V2FeedGetUpdatesReturnsPrereleasePackages()
{
@@ -276,7 +463,7 @@ public void V2FeedGetUpdatesReturnsPrereleasePackages()
var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
// Act
- var result = v2Service.GetUpdates("Foo|Qux", "1.1.0|0.9", includePrerelease: true, includeAllVersions: true, targetFrameworks: null)
+ var result = v2Service.GetUpdates("Foo|Qux", "1.1.0|0.9", includePrerelease: true, includeAllVersions: true, targetFrameworks: null, versionConstraints: null)
.ToList();
// Assert
@@ -287,7 +474,7 @@ public void V2FeedGetUpdatesReturnsPrereleasePackages()
}
[Fact]
- public void V2FeedGetUpdatesIgnoresDuplicatesInPackageList()
+ public void V2FeedGetUpdatesReturnsEmptyResultsIfDuplicatesInPackageList()
{
// Arrange
var packageRegistrationA = new PackageRegistration { Id = "Foo" };
@@ -308,13 +495,11 @@ public void V2FeedGetUpdatesIgnoresDuplicatesInPackageList()
// Act
var result =
- v2Service.GetUpdates("Foo|Qux|Foo", "0.9|1.5|1.1.2", includePrerelease: false, includeAllVersions: true, targetFrameworks: null)
+ v2Service.GetUpdates("Foo|Qux|Foo", "0.9|1.5|1.1.2", includePrerelease: false, includeAllVersions: true, targetFrameworks: null, versionConstraints: null)
.ToList();
// Assert
- Assert.Equal(2, result.Count);
- AssertPackage(new { Id = "Foo", Version = "1.2.0" }, result[0]);
- AssertPackage(new { Id = "Qux", Version = "2.0" }, result[1]);
+ Assert.Equal(0, result.Count);
}
[Fact]
@@ -364,7 +549,7 @@ public void V2FeedGetUpdatesFiltersByTargetFramework()
var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
// Act
- var result = v2Service.GetUpdates("Foo|Qux", "1.0|1.5", includePrerelease: false, includeAllVersions: true, targetFrameworks: "net40")
+ var result = v2Service.GetUpdates("Foo|Qux", "1.0|1.5", includePrerelease: false, includeAllVersions: true, targetFrameworks: "net40", versionConstraints: null)
.ToList();
// Assert
@@ -430,7 +615,7 @@ public void V2FeedGetUpdatesFiltersIncludesHighestPrereleasePackage()
var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
// Act
- var result = v2Service.GetUpdates("Foo|Qux", "1.0|1.5", includePrerelease: true, includeAllVersions: false, targetFrameworks: "net40")
+ var result = v2Service.GetUpdates("Foo|Qux", "1.0|1.5", includePrerelease: true, includeAllVersions: false, targetFrameworks: "net40", versionConstraints: null)
.ToList();
// Assert
View
21 Website/DataServices/V2Feed.svc.cs
@@ -97,20 +97,23 @@ public IQueryable<V2FeedPackage> FindPackagesById(string id)
if (versionLookup.ContainsKey(id))
{
- // REVIEW: Should we throw here or return empty value?
- throw new ArgumentException("The parameter 'packageIds' contain duplicated id: " + id);
+ // Exit early if the request contains duplicate ids
+ return Enumerable.Empty<V2FeedPackage>().AsQueryable();
}
SemanticVersion currentVersion = null;
- IVersionSpec versionConstraint = null;
-
- SemanticVersion.TryParse(versionValues[i], out currentVersion);
-
- if (versionConstraintValues[i] != null)
+ if (SemanticVersion.TryParse(versionValues[i], out currentVersion))
{
- VersionUtility.TryParseVersionSpec(versionConstraintValues[i], out versionConstraint);
+ IVersionSpec versionConstraint = null;
+ if (versionConstraintValues[i] != null)
+ {
+ if (!VersionUtility.TryParseVersionSpec(versionConstraintValues[i], out versionConstraint))
+ {
+ versionConstraint = null;
+ }
+ }
+ versionLookup.Add(id, Tuple.Create(currentVersion, versionConstraint));
}
- versionLookup.Add(id, Tuple.Create(currentVersion, versionConstraint));
}
var packages = PackageRepository.GetAll()
Please sign in to comment.
Something went wrong with that request. Please try again.