public void TestEnumeratorFetchesCorrectBatchItemsInReverseOrder()
        {
            var en = new BatchEnumerator <int, int>(
                Items.AsQueryable(),
                q => q,
                qq => qq.OrderByDescending(q => q),
                null,
                5
                );

            Assert.AreEqual(0, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 21, 20, 19, 18, 17 }, en.Current);
            Assert.AreEqual(1, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 16, 15, 14, 13, 12 }, en.Current);
            Assert.AreEqual(2, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 11, 10, 9, 8, 7 }, en.Current);
            Assert.AreEqual(3, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 6, 5, 4, 3, 2 }, en.Current);
            Assert.AreEqual(4, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 1 }, en.Current);
            Assert.AreEqual(5, en.BatchNumber);
            Assert.False(en.MoveNext());
        }
Ejemplo n.º 2
0
        public static void EnumerateBatches <T>(this IEnumerable <T> enumerable, int batchSize, Action <IEnumerator <T> > batchAction)
        {
            if (enumerable is null)
            {
                throw new ArgumentNullException(nameof(enumerable));
            }

            if (batchSize <= 0)
            {
                throw new ArgumentException("Batch size should be greater zero.", nameof(batchSize));
            }

            if (batchAction is null)
            {
                throw new ArgumentNullException(nameof(batchAction));
            }

            using (var enumerator = enumerable.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    using (var batchEnumerator = new BatchEnumerator <T>(enumerator, batchSize))
                    {
                        batchAction(batchEnumerator);
                    }
                }
            }
        }
        public void TestEnumeratorFetchesCorrectBatchItems()
        {
            var en = new BatchEnumerator <int, int>(
                Items.AsQueryable(),
                q => q,
                null,
                null,
                5
                );

            Assert.AreEqual(0, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 1, 2, 3, 4, 5 }, en.Current);
            Assert.AreEqual(1, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 6, 7, 8, 9, 10 }, en.Current);
            Assert.AreEqual(2, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 11, 12, 13, 14, 15 }, en.Current);
            Assert.AreEqual(3, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 16, 17, 18, 19, 20 }, en.Current);
            Assert.AreEqual(4, en.BatchNumber);

            Assert.True(en.MoveNext());
            Assert.AreEqual(new[] { 21 }, en.Current);
            Assert.AreEqual(5, en.BatchNumber);
            Assert.False(en.MoveNext());
        }
 public void TestEnumeratorThrowsExceptionWithNullResponseArgument()
 {
     Assert.Throws <ArgumentNullException>(() => {
         var en = new BatchEnumerator <int, int>(
             Items.AsQueryable(),
             null,
             null,
             null
             );
     });
 }
        public void TestBatchSizeWithoutInitializationHasValueGreaterThan0()
        {
            var en = new BatchEnumerator <int, int>(
                Items.AsQueryable(),
                q => q,
                null,
                null
                );

            Assert.AreEqual(10, en.BatchSize);
        }
 public void TestEnumeratorThrowsExceptionWithNullItems()
 {
     Assert.Throws <ArgumentNullException>(() => {
         var en = new BatchEnumerator <int, int>(
             null,
             q => q,
             null,
             null,
             5
             );
     });
 }
        public void TestBatchSizeWithInitializationHasCorrectValue()
        {
            var en = new BatchEnumerator <int, int>(
                Items.AsQueryable(),
                q => q,
                null,
                null,
                5
                );

            Assert.AreEqual(5, en.BatchSize);
        }
        public void TestSetPaddingWithNoIteration()
        {
            var en = new BatchEnumerator <int, int>(
                Items.AsQueryable(),
                q => q,
                null,
                null,
                5
                );

            en.Padding = 6;
            Assert.AreEqual(6, en.Padding);
        }
 public void TestSetPaddingInsideIterationThrowsException()
 {
     Assert.Throws <ArgumentException>(() => {
         var en = new BatchEnumerator <int, int>(
             Items.AsQueryable(),
             q => q,
             null,
             null,
             5
             );
         en.MoveNext();
         en.Padding = 6;
     });
 }
Ejemplo n.º 10
0
        public void TestEnumeratorFiltersEnumeration()
        {
            var en = new BatchEnumerator <int, int>(
                Items.AsQueryable(),
                q => q,
                null,
                q => q < 4,
                5
                );

            Assert.AreEqual(0, en.BatchNumber);
            Assert.True(en.MoveNext());
            Assert.AreEqual(3, en.Current.Count());
            Assert.AreEqual(new[] { 1, 2, 3 }, en.Current);
            Assert.False(en.MoveNext());
        }
Ejemplo n.º 11
0
        public void TestResetResetsPage()
        {
            var en = new BatchEnumerator <int, int>(
                Items.AsQueryable(),
                q => q,
                null,
                null,
                5
                );

            Assert.True(en.MoveNext());
            Assert.True(en.MoveNext());
            Assert.AreEqual(2, en.BatchNumber);
            en.Reset();
            Assert.AreEqual(0, en.BatchNumber);
        }
Ejemplo n.º 12
0
        public void TestBaseEnumeratorCurrentProperty()
        {
            var en = new BatchEnumerator <int, int>(
                Items.AsQueryable(),
                q => q,
                null,
                null,
                5
                );
            var baseEnumerator = (IEnumerator)en;

            baseEnumerator.MoveNext();
            Assert.IsInstanceOf <IEnumerable <int> >(baseEnumerator.Current);
            var value = (IEnumerable <int>)baseEnumerator.Current;

            Assert.AreEqual(5, value.Count());
        }
Ejemplo n.º 13
0
        public void TestDispose(string propertyName)
        {
            var en = new BatchEnumerator <int, int>(
                Items.AsQueryable(),
                q => q,
                qq => qq.OrderBy(q => q),
                null,
                5
                );

            en.Dispose();

            var property = en.GetType().GetField(
                propertyName,
                BindingFlags.NonPublic | BindingFlags.Instance
                );
            var propertyValue = property.GetValue(en);

            Assert.AreEqual(null, propertyValue);
        }