Permalink
Browse files

Added unit tests for Win8 Curator and tweaked Tag-based Curator

  • Loading branch information...
1 parent aa30aa5 commit 01da1970f7867d50cde2d641b5d743bfb86f38e5 @anurse anurse committed Jun 18, 2012
View
@@ -131,6 +131,7 @@
<Compile Include="Infrastructure\CookieTempDataProviderFacts.cs" />
<Compile Include="Infrastructure\Jobs\UpdateStatisticsJobFacts.cs" />
<Compile Include="Infrastructure\LuceneIndexingServiceFacts.cs" />
+ <Compile Include="PackageCurators\Windows8PackageCuratorFacts.cs" />
<Compile Include="PackageCurators\WebMatrixPackageCuratorFacts.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RequireRemoteHttpsAttributeFacts.cs" />
@@ -0,0 +1,199 @@
+using System;
+using Moq;
+using NuGet;
+using Xunit;
+using Xunit.Extensions;
+
+namespace NuGetGallery.PackageCurators
+{
+ public class Windows8PackageCuratorFacts
+ {
+ public class TheCurateMethod
+ {
+ [Fact]
+ public void WillNotIncludeThePackageWhenTheWindows8CuratedFeedDoesNotExist()
+ {
+ var curator = new TestableWindows8PackageCurator();
+ curator.StubCuratedFeedByNameQry.Setup(stub => stub.Execute(It.IsAny<string>(), It.IsAny<bool>())).Returns((CuratedFeed)null);
+ var package = CreateStubGalleryPackage();
+ package.Tags = "winrt";
+
+ curator.Curate(package, null);
+
+ curator.StubCreatedCuratedPackageCmd.Verify(stub => stub.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<bool>(),
+ It.IsAny<bool>(),
+ It.IsAny<string>()), Times.Never());
+ }
+
+ [Theory]
+ [InlineData("winrt")]
+ [InlineData("win8")]
+ [InlineData("windows8")]
+ [InlineData("winjs")]
+ [InlineData("wInRt")]
+ [InlineData("wIn8")]
+ [InlineData("wInDows8")]
+ [InlineData("wInJs")]
+ public void WillIncludeThePackageWhenItHasAcceptedTag(string tag)
+ {
+ var curator = new TestableWindows8PackageCurator();
+ var stubGalleryPackage = CreateStubGalleryPackage();
+ stubGalleryPackage.Tags = "aTag " + tag + " aThirdTag";
+
+ curator.Curate(stubGalleryPackage, null);
+
+ curator.StubCreatedCuratedPackageCmd.Verify(stub => stub.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<bool>(),
+ It.IsAny<bool>(),
+ It.IsAny<string>()), Times.Once());
+ }
+
+ [Fact]
+ public void WillNotIncludeThePackageWhenNotTagged()
+ {
+ var curator = new TestableWindows8PackageCurator();
+ var stubGalleryPackage = CreateStubGalleryPackage();
+ stubGalleryPackage.Tags = "aTag notforwinrt aThirdTag";
+
+ curator.Curate(stubGalleryPackage, null);
+
+ curator.StubCreatedCuratedPackageCmd.Verify(stub => stub.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<bool>(),
+ It.IsAny<bool>(),
+ It.IsAny<string>()), Times.Never());
+ }
+
+ [Fact]
+ public void WillNotIncludeThePackageWhenTagsIsNull()
+ {
+ var curator = new TestableWindows8PackageCurator();
+ var stubGalleryPackage = CreateStubGalleryPackage();
+ stubGalleryPackage.Tags = null;
+
+ curator.Curate(stubGalleryPackage, null);
+
+ curator.StubCreatedCuratedPackageCmd.Verify(stub => stub.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<bool>(),
+ It.IsAny<bool>(),
+ It.IsAny<string>()), Times.Never());
+ }
+
+ [Fact]
+ public void WillIncludeThePackageUsingTheCuratedFeedKey()
+ {
+ var curator = new TestableWindows8PackageCurator();
+ curator.StubCuratedFeed.Key = 42;
+ var package = CreateStubGalleryPackage();
+ package.Tags = "winrt";
+
+ curator.Curate(package, CreateStubNuGetPackage().Object);
+
+ curator.StubCreatedCuratedPackageCmd.Verify(stub => stub.Execute(
+ 42,
+ It.IsAny<int>(),
+ It.IsAny<bool>(),
+ It.IsAny<bool>(),
+ It.IsAny<string>()));
+ }
+
+ [Fact]
+ public void WillIncludeThePackageUsingThePackageRegistrationKey()
+ {
+ var curator = new TestableWindows8PackageCurator();
+ var stubGalleryPackage = CreateStubGalleryPackage();
+ stubGalleryPackage.PackageRegistration.Key = 42;
+ stubGalleryPackage.Tags = "winrt";
+
+ curator.Curate(stubGalleryPackage, CreateStubNuGetPackage().Object);
+
+ curator.StubCreatedCuratedPackageCmd.Verify(stub => stub.Execute(
+ It.IsAny<int>(),
+ 42,
+ It.IsAny<bool>(),
+ It.IsAny<bool>(),
+ It.IsAny<string>()));
+ }
+
+ [Fact]
+ public void WillSetTheAutomaticBitWhenIncludingThePackage()
+ {
+ var curator = new TestableWindows8PackageCurator();
+ var stubGalleryPackage = CreateStubGalleryPackage();
+ stubGalleryPackage.Tags = "winrt";
+
+ curator.Curate(stubGalleryPackage, CreateStubNuGetPackage().Object);
+
+ curator.StubCreatedCuratedPackageCmd.Verify(stub => stub.Execute(
+ It.IsAny<int>(),
+ It.IsAny<int>(),
+ It.IsAny<bool>(),
+ true,
+ It.IsAny<string>()));
+ }
+
+ static Package CreateStubGalleryPackage()
+ {
+ return new Package
+ {
+ IsLatestStable = true ,
+ PackageRegistration = new PackageRegistration
+ {
+ Key = 0,
+ },
+ };
+ }
+
+ static Mock<IPackage> CreateStubNuGetPackage()
+ {
+ var stubNuGetPackage = new Mock<IPackage>();
+ stubNuGetPackage.Setup(stub => stub.GetFiles()).Returns(new IPackageFile[] {});
+ return stubNuGetPackage;
+ }
+
+ static Mock<IPackageFile> CreateStubNuGetPackageFile(string path)
+ {
+ var stubPackageFile = new Mock<IPackageFile>();
+ stubPackageFile.Setup(stub => stub.Path).Returns(path);
+ return stubPackageFile;
+ }
+ }
+
+ public class TestableWindows8PackageCurator : Windows8PackageCurator
+ {
+ public TestableWindows8PackageCurator()
+ {
+ StubCreatedCuratedPackageCmd = new Mock<ICreateCuratedPackageCommand>();
+ StubCuratedFeed = new CuratedFeed { Key = 0 };
+ StubCuratedFeedByNameQry = new Mock<ICuratedFeedByNameQuery>();
+
+ StubCuratedFeedByNameQry
+ .Setup(stub => stub.Execute(It.IsAny<string>(), It.IsAny<bool>()))
+ .Returns(StubCuratedFeed);
+ }
+
+ public Mock<ICreateCuratedPackageCommand> StubCreatedCuratedPackageCmd { get; set; }
+ public CuratedFeed StubCuratedFeed { get; private set; }
+ public Mock<ICuratedFeedByNameQuery> StubCuratedFeedByNameQry { get; private set; }
+
+ protected override T GetService<T>()
+ {
+ if (typeof(T) == typeof(ICreateCuratedPackageCommand))
+ return (T)StubCreatedCuratedPackageCmd.Object;
+
+ if (typeof(T) == typeof(ICuratedFeedByNameQuery))
+ return (T)StubCuratedFeedByNameQry.Object;
+
+ throw new Exception("Tried to get an unexpected service.");
+ }
+ }
+ }
+}
@@ -20,24 +20,22 @@ public abstract class TagBasedPackageCurator : AutomaticPackageCurator
public override void Curate(Package galleryPackage, IPackage nugetPackage)
{
- // Check if this package should be curated
- if (galleryPackage.Tags != null && RequiredTags.Any(s => galleryPackage.Tags.ToLowerInvariant().Contains(s)))
+ // Make sure the target feed exists
+ CuratedFeed feed = GetService<ICuratedFeedByNameQuery>().Execute(CuratedFeedName, includePackages: false);
+ if (feed != null && galleryPackage.Tags != null)
{
- // It should! Add it to the curated feed
- AddPackageToFeed(galleryPackage);
- }
- }
+ // Break the tags up so we can be sure we don't catch any partial matches (i.e. "foobar" when we're looking for "foo")
+ string[] tags = galleryPackage.Tags.Split();
- protected virtual void AddPackageToFeed(Package galleryPackage)
- {
- CuratedFeed feed = GetTargetFeed();
- GetService<ICreateCuratedPackageCommand>().Execute(
- feed.Key, galleryPackage.PackageRegistration.Key, automaticallyCurated: true);
- }
+ // Check if this package should be curated
+ if (tags.Any(tag => RequiredTags.Contains(tag, StringComparer.OrdinalIgnoreCase)))
+ {
+ // It should! Add it to the curated feed
+ GetService<ICreateCuratedPackageCommand>().Execute(
+ feed.Key, galleryPackage.PackageRegistration.Key, automaticallyCurated: true);
+ }
- protected virtual CuratedFeed GetTargetFeed()
- {
- return GetService<ICuratedFeedByNameQuery>().Execute(CuratedFeedName, includePackages: false);
+ }
}
}
}
@@ -5,21 +5,22 @@
namespace NuGetGallery
{
-public class Windows8PackageCurator : TagBasedPackageCurator
-{
- protected override IEnumerable<string> RequiredTags
+ public class Windows8PackageCurator : TagBasedPackageCurator
{
- get {
- yield return "winrt";
- yield return "win8";
- yield return "windows8";
- yield return "winjs";
+ protected override IEnumerable<string> RequiredTags
+ {
+ get
+ {
+ yield return "winrt";
+ yield return "win8";
+ yield return "windows8";
+ yield return "winjs";
+ }
}
- }
- protected override string CuratedFeedName
- {
- get { return "Win8"; }
+ protected override string CuratedFeedName
+ {
+ get { return "windows8-packages"; }
+ }
}
-}
}

0 comments on commit 01da197

Please sign in to comment.