Skip to content

Commit

Permalink
Added tests for ancestors and descendents
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanforsberg committed Sep 2, 2013
1 parent 7e7dae7 commit 5104896
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 7 deletions.
75 changes: 75 additions & 0 deletions src/WhiteMagic.Tests/ContentRepository/when_getting_ancestors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using System.Linq;
using EPiServer.Core;
using NUnit.Framework;
using Shouldly;
using WhiteMagic.Tests.Pages;

namespace WhiteMagic.Tests.ContentRepository
{
public class when_getting_ancestors : TestBase
{
private PageReference _pageToUseRef;
private IEnumerable<IContent> _ancestors;

public override void Given()
{
base.Given();

var startPageReference = ContentRepository.Publish<StartPage>(ContentReference.RootPage, "StartPage");
var childPage1Reference = ContentRepository.Publish<StartPage>(startPageReference, "ChildPage1");
var childPage1ChildPage1Reference = ContentRepository.Publish<StandardPage>(childPage1Reference, "ChildPage1-ChildPage1");

_pageToUseRef = ContentRepository.Publish<StandardPage>(childPage1ChildPage1Reference, "ChildPage1-ChildPage3-ChildPage1");
}

public override void When()
{
base.When();
_ancestors = ContentRepository.GetAncestors(_pageToUseRef);
}

[Test]
public void it_should_return_the_correct_number_of_pages()
{
_ancestors.Count().ShouldBe(4);
}

[Test]
public void it_should_return_the_pages_in_order_of_distance_to_origin()
{
_ancestors.ElementAt(0).Name.ShouldBe("ChildPage1-ChildPage1");
_ancestors.ElementAt(1).Name.ShouldBe("ChildPage1");
_ancestors.ElementAt(2).Name.ShouldBe("StartPage");
_ancestors.ElementAt(3).Name.ShouldBe("RootPage");
}
}

public class when_getting_ancestors_for_the_root_page : TestBase
{
private IEnumerable<IContent> _ancestors;

//public override void Given()
//{
// base.Given();

// var startPageReference = ContentRepository.Publish<StartPage>(ContentReference.RootPage, "StartPage");
// var childPage1Reference = ContentRepository.Publish<StartPage>(startPageReference, "ChildPage1");
// var childPage1ChildPage1Reference = ContentRepository.Publish<StandardPage>(childPage1Reference, "ChildPage1-ChildPage1");

// _pageToUseRef = ContentRepository.Publish<StandardPage>(childPage1ChildPage1Reference, "ChildPage1-ChildPage3-ChildPage1");
//}

public override void When()
{
base.When();
_ancestors = ContentRepository.GetAncestors(ContentReference.RootPage);
}

[Test]
public void it_should_not_have_any_ancestors()
{
_ancestors.Count().ShouldBe(0);
}
}
}
31 changes: 31 additions & 0 deletions src/WhiteMagic.Tests/ContentRepository/when_getting_children.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,35 @@ public void it_should_throw_a_page_not_found_exception()
Assert.Throws<PageNotFoundException>(() => ContentRepository.GetChildren<PageData>(new ContentReference(1000)));
}
}

public class when_getting_children_of_a_page_that_has_no_children : TestBase
{
private PageReference _startPageReference;
private PageReference _childPage1Reference;
private IEnumerable<StandardPage> _children;

public override void Given()
{
base.Given();

_startPageReference = ContentRepository.Publish<StartPage>(ContentReference.RootPage);

//_childPage1Reference = ContentRepository.Publish<StandardPage>(_startPageReference, "ChildPage1");
//ContentRepository.Publish<StandardPage>(_startPageReference, "ChildPage2");
//ContentRepository.Publish<StandardPage>(_startPageReference, "ChildPage3");
//ContentRepository.Publish<StandardPage>(_childPage1Reference, "ChildPage1-ChildPage1");
}

public override void When()
{
base.When();
_children = ContentRepository.GetChildren<StandardPage>(_startPageReference);
}

[Test]
public void it_should_not_return_any_children()
{
_children.Count().ShouldBe(0);
}
}
}
70 changes: 70 additions & 0 deletions src/WhiteMagic.Tests/ContentRepository/when_getting_descendents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EPiServer.Core;
using NUnit.Framework;
using Shouldly;
using WhiteMagic.Tests.Pages;

namespace WhiteMagic.Tests.ContentRepository
{
public class when_getting_descendents : TestBase
{
protected PageReference StartPageReference;
protected PageReference ChildPage3Reference;
protected PageReference ChildPage3ChildPage3Reference;
protected IEnumerable<ContentReference> Descendants;

public override void Given()
{
base.Given();

StartPageReference = ContentRepository.Publish<StartPage>(ContentReference.RootPage);

ContentRepository.Publish<StartPage>(StartPageReference, "ChildPage1");
ContentRepository.Publish<StartPage>(StartPageReference, "ChildPage2");
ChildPage3Reference = ContentRepository.Publish<StartPage>(StartPageReference, "ChildPage3");

ContentRepository.Publish<StandardPage>(ChildPage3Reference, "ChildPage3-ChildPage1");
ContentRepository.Publish<StandardPage>(ChildPage3Reference, "ChildPage3-ChildPage2");
ChildPage3ChildPage3Reference = ContentRepository.Publish<StandardPage>(ChildPage3Reference, "ChildPage3-ChildPage3");

ContentRepository.Publish<StandardPage>(ChildPage3ChildPage3Reference, "ChildPage3-ChildPage3-ChildPage1");
ContentRepository.Publish<StandardPage>(ChildPage3ChildPage3Reference, "ChildPage3-ChildPage3-ChildPage2");
ContentRepository.Publish<StandardPage>(ChildPage3ChildPage3Reference, "ChildPage3-ChildPage3-ChildPage3");
}
}

public class when_getting_descendents_from_start_page : when_getting_descendents
{
public override void When()
{
base.When();
Descendants = ContentRepository.GetDescendents(ContentReference.RootPage);
}

[Test]
public void it_should_return_the_correct_number_of_descendants_including_the_waste_basket_page()
{
Descendants.Count().ShouldBe(11);
}
}

public class when_getting_descendents_from_page_deeper_in_structure : when_getting_descendents
{

public override void When()
{
base.When();
Descendants = ContentRepository.GetDescendents(ChildPage3Reference);
}

[Test]
public void it_should_return_the_correct_number_of_descendants()
{
Descendants.Count().ShouldBe(6);
}
}
}
38 changes: 31 additions & 7 deletions src/WhiteMagic.Tests/InMemoryContentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,35 @@ public IEnumerable<T> GetChildren<T>(ContentReference contentLink, ILanguageSele

public IEnumerable<ContentReference> GetDescendents(ContentReference contentLink)
{
throw new NotImplementedException();
var descendents = new List<ContentReference>();

foreach (var child in GetChildren<IContent>(contentLink))
{
descendents.Add(child.ContentLink);
descendents.AddRange(GetDescendents(child.ContentLink));
}

return descendents;
}

public IEnumerable<IContent> GetAncestors(ContentReference contentLink)
{
throw new NotImplementedException();
if (contentLink.CompareToIgnoreWorkID(ContentReference.RootPage))
{
yield break;
}

var parent = Get<IContent>(Get<IContent>(contentLink.ToPageReference()).ParentLink);

yield return parent;

if (!parent.ContentLink.CompareToIgnoreWorkID(ContentReference.RootPage))
{
foreach (var ancestor in GetAncestors(parent.ContentLink))
{
yield return ancestor;
}
}
}

public IEnumerable<IContent> GetItems(IEnumerable<ContentReference> contentLinks, ILanguageSelector selector)
Expand Down Expand Up @@ -189,6 +212,11 @@ private PageDataCollection GetChildren(PageReference pageLink, int startIndex, i
{
var children = new List<IContent>();

if (!_pages.ContainsKey(pageLink))
{
throw new PageNotFoundException(pageLink);
}

List<PageReference> childrenRefs;
if (_structure.TryGetValue(pageLink, out childrenRefs))
{
Expand All @@ -207,11 +235,7 @@ private PageDataCollection GetChildren(PageReference pageLink, int startIndex, i
children.Add(GetPage(link));
}
}
else
{
throw new PageNotFoundException(pageLink);
}


return new PageDataCollection(children);
}

Expand Down
2 changes: 2 additions & 0 deletions src/WhiteMagic.Tests/WhiteMagic.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@
<ItemGroup>
<Compile Include="ContentExtensions.cs" />
<Compile Include="ContentRepositoryExtensions.cs" />
<Compile Include="ContentRepository\when_getting_ancestors.cs" />
<Compile Include="ContentRepository\when_getting_a_page.cs" />
<Compile Include="ContentRepository\when_getting_children_using_startindex_and_maxrows.cs" />
<Compile Include="ContentRepository\when_getting_descendents.cs" />
<Compile Include="InMemoryContentRepository.cs" />
<Compile Include="InMemoryPermanentLinkMapper.cs" />
<Compile Include="Pages\StandardPage.cs" />
Expand Down

0 comments on commit 5104896

Please sign in to comment.