|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using Microsoft.Azure.WebJobs.Extensions.Sql.Common; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Unit |
| 11 | +{ |
| 12 | + public class IEnumerableExtensionsTests |
| 13 | + { |
| 14 | + public static readonly TheoryData<int[], int> BatchData = new() |
| 15 | + { |
| 16 | + { new int[] { 1, 2, 3, 4, 5 }, 1 }, // One by one |
| 17 | + { new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 3 }, // Bigger non-single batch |
| 18 | + { new int[] { 1, 2, 3, 4, 5 } , 5 }, // All one batch |
| 19 | + { new int[] { 1 }, 2 }, // Batch size greater than array |
| 20 | + }; |
| 21 | + |
| 22 | + [Theory] |
| 23 | + [MemberData(nameof(BatchData))] |
| 24 | + public void Batch(IEnumerable<int> array, int batchSize) |
| 25 | + { |
| 26 | + int totalCount = 0; |
| 27 | + foreach (IEnumerable<int> batch in array.Batch(batchSize)) |
| 28 | + { |
| 29 | + int batchCount = batch.Count(); |
| 30 | + totalCount += batchCount; |
| 31 | + Assert.True(batch.Count() <= batchSize); |
| 32 | + } |
| 33 | + Assert.Equal(totalCount, array.Count()); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void Batch_Invalid() |
| 38 | + { |
| 39 | + // Array must be non-null |
| 40 | + Assert.ThrowsAny<Exception>(() => IEnumerableExtensions.Batch<int>(null, 0).Count()); |
| 41 | + |
| 42 | + // Size must be >= 1 |
| 43 | + Assert.ThrowsAny<Exception>(() => IEnumerableExtensions.Batch(new int[] { 1, 2, 3 }, 0).Count()); |
| 44 | + Assert.ThrowsAny<Exception>(() => IEnumerableExtensions.Batch(new int[] { 1, 2, 3 }, -1).Count()); |
| 45 | + } |
| 46 | + |
| 47 | + public static readonly TheoryData<int[], int, int[]> TakeLastData = new() |
| 48 | + { |
| 49 | + { new int[] { 1, 2, 3, 4, 5 }, 1 , new int[] { 5 } }, // Take only last number |
| 50 | + { new int[] { 1, 2, 3, 4, 5 }, 3 , new int[] { 3, 4, 5 } }, // Take some middle set of numbers |
| 51 | + { new int[] { 1, 2, 3, 4, 5 }, 6 , new int[] { 1, 2, 3, 4, 5 } }, // Take more than exists in array |
| 52 | + { new int[] { 1, 2, 3, 4, 5 }, 0 , Array.Empty<int>() }, // No numbers |
| 53 | + { new int[] { 1, 2, 3, 4, 5 }, 0 , Array.Empty<int>() }, // Negative numbers (returns empty) |
| 54 | + }; |
| 55 | + |
| 56 | + [Theory] |
| 57 | + [MemberData(nameof(TakeLastData))] |
| 58 | + public void TakeLast(IEnumerable<int> array, int takeCount, IEnumerable<int> expectedValues) |
| 59 | + { |
| 60 | + IEnumerable<int> taken = IEnumerableExtensions.TakeLast(array, takeCount); |
| 61 | + Assert.Equal(taken, expectedValues); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void TakeLast_Invalid() |
| 66 | + { |
| 67 | + // IEnumerable must be non-null |
| 68 | + Assert.ThrowsAny<Exception>(() => { IEnumerableExtensions.TakeLast<int>(null, 0); }); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments