|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data.Entity; |
| 4 | +using System.Data.Entity.Infrastructure; |
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Http; |
| 8 | +using System.Net.Http.Formatting; |
| 9 | +using System.Text; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using System.Web.Http.Controllers; |
| 13 | +using System.Web.Http.Filters; |
| 14 | +using FluentAssertions; |
| 15 | +using JSONAPI.EntityFramework.ActionFilters; |
| 16 | +using JSONAPI.EntityFramework.Tests.Helpers; |
| 17 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 18 | +using Moq; |
| 19 | + |
| 20 | +namespace JSONAPI.EntityFramework.Tests.ActionFilters |
| 21 | +{ |
| 22 | + [TestClass] |
| 23 | + public class EnumerateQueryableAsyncAttributeTests |
| 24 | + { |
| 25 | + public class Dummy |
| 26 | + { |
| 27 | + public string Id { get; set; } |
| 28 | + |
| 29 | + public string Name { get; set; } |
| 30 | + } |
| 31 | + |
| 32 | + private IQueryable<Dummy> _fixtures; |
| 33 | + |
| 34 | + [TestInitialize] |
| 35 | + public void SetupFixtures() |
| 36 | + { |
| 37 | + _fixtures = new List<Dummy>() |
| 38 | + { |
| 39 | + new Dummy |
| 40 | + { |
| 41 | + Id = "1", |
| 42 | + Name = "Blue" |
| 43 | + }, |
| 44 | + new Dummy |
| 45 | + { |
| 46 | + Id = "2", |
| 47 | + Name = "Red" |
| 48 | + }, |
| 49 | + new Dummy |
| 50 | + { |
| 51 | + Id = "3", |
| 52 | + Name = "Green" |
| 53 | + } |
| 54 | + }.AsQueryable(); |
| 55 | + } |
| 56 | + |
| 57 | + private HttpActionExecutedContext CreateActionExecutedContext(IDbAsyncEnumerator<Dummy> asyncEnumerator) |
| 58 | + { |
| 59 | + var mockSet = new Mock<DbSet<Dummy>>(); |
| 60 | + mockSet.As<IDbAsyncEnumerable<Dummy>>() |
| 61 | + .Setup(m => m.GetAsyncEnumerator()) |
| 62 | + .Returns(asyncEnumerator); |
| 63 | + |
| 64 | + mockSet.As<IQueryable<Dummy>>() |
| 65 | + .Setup(m => m.Provider) |
| 66 | + .Returns(new TestDbAsyncQueryProvider<Dummy>(_fixtures.Provider)); |
| 67 | + |
| 68 | + mockSet.As<IQueryable<Dummy>>().Setup(m => m.Expression).Returns(_fixtures.Expression); |
| 69 | + mockSet.As<IQueryable<Dummy>>().Setup(m => m.ElementType).Returns(_fixtures.ElementType); |
| 70 | + mockSet.As<IQueryable<Dummy>>().Setup(m => m.GetEnumerator()).Returns(_fixtures.GetEnumerator()); |
| 71 | + |
| 72 | + var formatter = new JsonMediaTypeFormatter(); |
| 73 | + |
| 74 | + var httpContent = new ObjectContent(typeof(IQueryable<Dummy>), mockSet.Object, formatter); |
| 75 | + |
| 76 | + return new HttpActionExecutedContext |
| 77 | + { |
| 78 | + ActionContext = new HttpActionContext |
| 79 | + { |
| 80 | + ControllerContext = new HttpControllerContext |
| 81 | + { |
| 82 | + Request = new HttpRequestMessage(HttpMethod.Get, "http://api.example.com/dummies") |
| 83 | + } |
| 84 | + }, |
| 85 | + Response = new HttpResponseMessage(HttpStatusCode.OK) |
| 86 | + { |
| 87 | + Content = httpContent |
| 88 | + } |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public async Task ResolvesQueryable() |
| 94 | + { |
| 95 | + var actionFilter = new EnumerateQueryableAsyncAttribute(); |
| 96 | + |
| 97 | + var context = CreateActionExecutedContext(new TestDbAsyncEnumerator<Dummy>(_fixtures.GetEnumerator())); |
| 98 | + |
| 99 | + await actionFilter.OnActionExecutedAsync(context, new CancellationToken()); |
| 100 | + |
| 101 | + var objectContent = context.Response.Content as ObjectContent; |
| 102 | + objectContent.Should().NotBeNull(); |
| 103 | + |
| 104 | + var array = objectContent.Value as Dummy[]; |
| 105 | + array.Should().NotBeNull(); |
| 106 | + array.Length.Should().Be(3); |
| 107 | + array[0].Id.Should().Be("1"); |
| 108 | + array[1].Id.Should().Be("2"); |
| 109 | + array[2].Id.Should().Be("3"); |
| 110 | + } |
| 111 | + |
| 112 | + [TestMethod] |
| 113 | + public void CancelsProperly() |
| 114 | + { |
| 115 | + var actionFilter = new EnumerateQueryableAsyncAttribute(); |
| 116 | + |
| 117 | + var context = CreateActionExecutedContext(new WaitsUntilCancellationDbAsyncEnumerator<Dummy>(1000, _fixtures.GetEnumerator())); |
| 118 | + |
| 119 | + var cts = new CancellationTokenSource(); |
| 120 | + cts.CancelAfter(300); |
| 121 | + |
| 122 | + Func<Task> action = async () => |
| 123 | + { |
| 124 | + await actionFilter.OnActionExecutedAsync(context, cts.Token); |
| 125 | + }; |
| 126 | + action.ShouldThrow<TaskCanceledException>(); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments