Files
Umbraco-CMS/src/Umbraco.Tests/EnumerableExtensionsTests.cs
Stephan 365a01a476 From PetaPoco to NPoco (#1207)
* NPoco - 2.x (builds)

* NPoco - v3.1 (does not build)

* NPoco - builds

* NPoco - configure database factory (tests fail)

* Pick fix from 7.4

* NPoco - stock v3.1 - sort-of working

* NPoco - fix merge

* Fix Newtonsoft.Json in web.Template.Debug.config

* NPoco - fix SELECT *

* NPoco - fixing repositories

* NPoco - fix EntityRepository

* NPoco - fix EntityRepository

* NPoco - cosmetic

* NPoco - use 3.1.0-u001 from github/zpqrtbnk/NPoco

* Fixes build, NPoco needed to be referenced in the cms and UmbracoExamine projects

* Fixes lots of tests

* fixes more tests

* NPoco - bugfixing

* Bugfix CacheHelper in tests

* Bugfix connection mocking in tests

* NPoco - inject database in Sql.Select<>

* NPoco - discovery retry policy only once

* Enable C# 6 for Umbraco.Core

* NPoco - introduce UmbracoSql, cleanup

* NPoco - more cleanup and fixing

* NPoco - fix UserRepository

* Optimize InGroupsOf

* Implement UmbracoDatabase.FetchByGroups

* NPoco - fix Select

* NPoco - simplify GetPagedResultsByQuery

* Cherry-pick DisableBrowserCacheAttribute fix from 7.4

* Upgrade NPoco to use Sql<TContext>

* U4-8257 - cleanup relators

* 4-8257 - cleanup more relators

* Upgrade NPoco with more OOTB version

* fixes a couple tests, changes double check lock to Lazy<T>
2016-04-12 15:11:07 +02:00

196 lines
7.9 KiB
C#

using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Tests;
using umbraco.BusinessLogic;
namespace Umbraco.Tests
{
[TestFixture]
public class EnumerableExtensionsTests
{
[Test]
public void Unsorted_Sequence_Equal()
{
var list1 = new[] { 1, 2, 3, 4, 5, 6 };
var list2 = new[] { 6, 5, 3, 2, 1, 4 };
var list3 = new[] { 6, 5, 4, 3, 2, 2 };
Assert.IsTrue(list1.UnsortedSequenceEqual(list2));
Assert.IsTrue(list2.UnsortedSequenceEqual(list1));
Assert.IsFalse(list1.UnsortedSequenceEqual(list3));
Assert.IsTrue(((IEnumerable<object>)null).UnsortedSequenceEqual(null));
Assert.IsFalse(((IEnumerable<int>)null).UnsortedSequenceEqual(list1));
Assert.IsFalse(list1.UnsortedSequenceEqual(null));
}
[Test]
public void Contains_All()
{
var list1 = new[] {1, 2, 3, 4, 5, 6};
var list2 = new[] {6, 5, 3, 2, 1, 4};
var list3 = new[] {6, 5, 4, 3};
Assert.IsTrue(list1.ContainsAll(list2));
Assert.IsTrue(list2.ContainsAll(list1));
Assert.IsTrue(list1.ContainsAll(list3));
Assert.IsFalse(list3.ContainsAll(list1));
}
[Test]
public void Flatten_List_2()
{
var hierarchy = new TestItem()
{
Children = new List<TestItem>()
{
new TestItem(),
new TestItem(),
new TestItem()
}
};
var flattened = hierarchy.Children.FlattenList(x => x.Children);
var selectRecursive = hierarchy.Children.SelectRecursive(x => x.Children);
Assert.AreEqual(3, flattened.Count());
Assert.AreEqual(3, selectRecursive.Count());
Assert.IsTrue(flattened.SequenceEqual(selectRecursive));
}
[Test]
public void Flatten_List()
{
var hierarchy = new TestItem()
{
Children = new List<TestItem>()
{
new TestItem()
{
Children = new List<TestItem>()
{
new TestItem()
{
Children = new List<TestItem>()
{
new TestItem()
{
Children = new List<TestItem>()
{
new TestItem(),
new TestItem()
}
}
}
}
}
},
new TestItem()
{
Children = new List<TestItem>()
{
new TestItem()
{
Children = new List<TestItem>()
{
new TestItem()
{
Children = new List<TestItem>()
}
}
},
new TestItem()
{
Children = new List<TestItem>()
{
new TestItem()
{
Children = new List<TestItem>()
}
}
}
}
},
}
};
var flattened = hierarchy.Children.FlattenList(x => x.Children);
var selectRecursive = hierarchy.Children.FlattenList(x => x.Children);
Assert.AreEqual(10, flattened.Count());
Assert.AreEqual(10, selectRecursive.Count());
Assert.IsTrue(flattened.SequenceEqual(selectRecursive));
}
private class TestItem
{
public TestItem()
{
Children = Enumerable.Empty<TestItem>();
}
public IEnumerable<TestItem> Children { get; set; }
}
[Test]
public void InGroupsOf_ReturnsAllElements()
{
var integers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
var groupsOfTwo = integers.InGroupsOf(2).ToArray();
var flattened = groupsOfTwo.SelectMany(x => x).ToArray();
Assert.That(groupsOfTwo.Length, Is.EqualTo(5));
Assert.That(flattened.Length, Is.EqualTo(integers.Length));
CollectionAssert.AreEquivalent(integers, flattened);
var groupsOfMassive = integers.InGroupsOf(100).ToArray();
Assert.That(groupsOfMassive.Length, Is.EqualTo(1));
flattened = groupsOfMassive.SelectMany(x => x).ToArray();
Assert.That(flattened.Length, Is.EqualTo(integers.Length));
CollectionAssert.AreEquivalent(integers, flattened);
}
[Test]
public void InGroupsOf_CanRepeat()
{
var integers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
var inGroupsOf = integers.InGroupsOf(2);
Assert.AreEqual(5, inGroupsOf.Count());
Assert.AreEqual(5, inGroupsOf.Count()); // again
}
[TestCase]
public void DistinctBy_ReturnsDistinctElements_AndResetsIteratorCorrectly()
{
// Arrange
var tuple1 = new System.Tuple<string, string>("fruit", "apple");
var tuple2 = new System.Tuple<string, string>("fruit", "orange");
var tuple3 = new System.Tuple<string, string>("fruit", "banana");
var tuple4 = new System.Tuple<string, string>("fruit", "banana"); // Should be filtered out
var list = new List<System.Tuple<string, string>>()
{
tuple1,
tuple2,
tuple3,
tuple4
};
// Act
var iteratorSource = list.DistinctBy(x => x.Item2);
// Assert
// First check distinction
Assert.AreEqual(3, iteratorSource.Count());
// Check for iterator block mistakes - reset to original query first
iteratorSource = list.DistinctBy(x => x.Item2);
Assert.AreEqual(iteratorSource.Count(), iteratorSource.ToList().Count());
}
}
}