|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Web.Http; |
| 7 | +using FluentAssertions; |
| 8 | +using JSONAPI.ActionFilters; |
| 9 | +using JSONAPI.Core; |
| 10 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 11 | + |
| 12 | +namespace JSONAPI.Tests.ActionFilters |
| 13 | +{ |
| 14 | + [TestClass] |
| 15 | + public class DefaultPaginationTransformerTests |
| 16 | + { |
| 17 | + private class Dummy |
| 18 | + { |
| 19 | + // ReSharper disable UnusedAutoPropertyAccessor.Local |
| 20 | + public string Id { get; set; } |
| 21 | + // ReSharper restore UnusedAutoPropertyAccessor.Local |
| 22 | + |
| 23 | + public string FirstName { get; set; } |
| 24 | + |
| 25 | + public string LastName { get; set; } |
| 26 | + } |
| 27 | + |
| 28 | + private IList<Dummy> _fixtures; |
| 29 | + private IQueryable<Dummy> _fixturesQuery; |
| 30 | + |
| 31 | + [TestInitialize] |
| 32 | + public void SetupFixtures() |
| 33 | + { |
| 34 | + _fixtures = new List<Dummy> |
| 35 | + { |
| 36 | + new Dummy { Id = "1", FirstName = "Thomas", LastName = "Paine" }, |
| 37 | + new Dummy { Id = "2", FirstName = "Samuel", LastName = "Adams" }, |
| 38 | + new Dummy { Id = "3", FirstName = "George", LastName = "Washington"}, |
| 39 | + new Dummy { Id = "4", FirstName = "Thomas", LastName = "Jefferson" }, |
| 40 | + new Dummy { Id = "5", FirstName = "Martha", LastName = "Washington"}, |
| 41 | + new Dummy { Id = "6", FirstName = "Abraham", LastName = "Lincoln" }, |
| 42 | + new Dummy { Id = "7", FirstName = "Andrew", LastName = "Jackson" }, |
| 43 | + new Dummy { Id = "8", FirstName = "Andrew", LastName = "Johnson" }, |
| 44 | + new Dummy { Id = "9", FirstName = "William", LastName = "Harrison" } |
| 45 | + }; |
| 46 | + _fixturesQuery = _fixtures.AsQueryable(); |
| 47 | + } |
| 48 | + |
| 49 | + private DefaultPaginationTransformer GetTransformer(int maxPageSize) |
| 50 | + { |
| 51 | + return new DefaultPaginationTransformer("page.number", "page.size", maxPageSize); |
| 52 | + } |
| 53 | + |
| 54 | + private Dummy[] GetArray(string uri, int maxPageSize = 50) |
| 55 | + { |
| 56 | + var request = new HttpRequestMessage(HttpMethod.Get, uri); |
| 57 | + return GetTransformer(maxPageSize).ApplyPagination(_fixturesQuery, request).ToArray(); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void ApplyPagination_has_no_effect_when_no_paging_parameters_are_supplied() |
| 62 | + { |
| 63 | + var array = GetArray("http://api.example.com/dummies"); |
| 64 | + array.Length.Should().Be(9); |
| 65 | + } |
| 66 | + |
| 67 | + [TestMethod] |
| 68 | + public void ApplyPagination_returns_all_results_when_they_are_within_page() |
| 69 | + { |
| 70 | + var array = GetArray("http://api.example.com/dummies?page[number]=0&page[size]=10"); |
| 71 | + array.Length.Should().Be(9); |
| 72 | + } |
| 73 | + |
| 74 | + [TestMethod] |
| 75 | + public void ApplyPagination_returns_no_results_when_page_size_is_zero() |
| 76 | + { |
| 77 | + var array = GetArray("http://api.example.com/dummies?page[number]=0&page[size]=0"); |
| 78 | + array.Length.Should().Be(0); |
| 79 | + } |
| 80 | + |
| 81 | + [TestMethod] |
| 82 | + public void ApplyPagination_returns_first_page_of_data() |
| 83 | + { |
| 84 | + var array = GetArray("http://api.example.com/dummies?page[number]=0&page[size]=4"); |
| 85 | + array.Should().BeEquivalentTo(_fixtures[0], _fixtures[1], _fixtures[2], _fixtures[3]); |
| 86 | + } |
| 87 | + |
| 88 | + [TestMethod] |
| 89 | + public void ApplyPagination_returns_second_page_of_data() |
| 90 | + { |
| 91 | + var array = GetArray("http://api.example.com/dummies?page[number]=1&page[size]=4"); |
| 92 | + array.Should().BeEquivalentTo(_fixtures[4], _fixtures[5], _fixtures[6], _fixtures[7]); |
| 93 | + } |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void ApplyPagination_returns_page_at_end() |
| 97 | + { |
| 98 | + var array = GetArray("http://api.example.com/dummies?page[number]=2&page[size]=4"); |
| 99 | + array.Should().BeEquivalentTo(_fixtures[8]); |
| 100 | + } |
| 101 | + |
| 102 | + [TestMethod] |
| 103 | + public void ApplyPagination_returns_nothing_for_page_after_end() |
| 104 | + { |
| 105 | + var array = GetArray("http://api.example.com/dummies?page[number]=3&page[size]=4"); |
| 106 | + array.Length.Should().Be(0); |
| 107 | + } |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public void ApplyPagination_uses_max_page_size_when_requested_page_size_is_higher() |
| 111 | + { |
| 112 | + var array = GetArray("http://api.example.com/dummies?page[number]=1&page[size]=8", 3); |
| 113 | + array.Should().BeEquivalentTo(_fixtures[3], _fixtures[4], _fixtures[5]); |
| 114 | + } |
| 115 | + |
| 116 | + [TestMethod] |
| 117 | + public void ApplyPagination_returns_400_if_page_number_is_negative() |
| 118 | + { |
| 119 | + Action action = () => |
| 120 | + { |
| 121 | + GetArray("http://api.example.com/dummies?page[number]=-4&page[size]=4"); |
| 122 | + }; |
| 123 | + action.ShouldThrow<HttpResponseException>().And.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 124 | + } |
| 125 | + |
| 126 | + [TestMethod] |
| 127 | + public void ApplyPagination_returns_400_if_page_size_is_negative() |
| 128 | + { |
| 129 | + Action action = () => |
| 130 | + { |
| 131 | + GetArray("http://api.example.com/dummies?page[number]=0&page[size]=-4"); |
| 132 | + }; |
| 133 | + action.ShouldThrow<HttpResponseException>().And.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 134 | + } |
| 135 | + |
| 136 | + [TestMethod] |
| 137 | + public void ApplyPagination_returns_400_if_page_number_specified_but_not_size() |
| 138 | + { |
| 139 | + Action action = () => |
| 140 | + { |
| 141 | + GetArray("http://api.example.com/dummies?page[number]=0"); |
| 142 | + }; |
| 143 | + action.ShouldThrow<HttpResponseException>().And.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 144 | + } |
| 145 | + |
| 146 | + [TestMethod] |
| 147 | + public void ApplyPagination_returns_400_if_page_size_specified_but_not_number() |
| 148 | + { |
| 149 | + Action action = () => |
| 150 | + { |
| 151 | + GetArray("http://api.example.com/dummies?page[size]=0"); |
| 152 | + }; |
| 153 | + action.ShouldThrow<HttpResponseException>().And.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 154 | + } |
| 155 | + |
| 156 | + [TestMethod] |
| 157 | + public void DefaultPaginationTransformer_cannot_be_instantiated_if_max_page_size_is_zero() |
| 158 | + { |
| 159 | + Action action = () => |
| 160 | + { |
| 161 | + GetTransformer(0); |
| 162 | + }; |
| 163 | + action.ShouldThrow<ArgumentOutOfRangeException>(); |
| 164 | + } |
| 165 | + |
| 166 | + [TestMethod] |
| 167 | + public void DefaultPaginationTransformer_cannot_be_instantiated_if_max_page_size_is_negative() |
| 168 | + { |
| 169 | + Action action = () => |
| 170 | + { |
| 171 | + GetTransformer(-4); |
| 172 | + }; |
| 173 | + action.ShouldThrow<ArgumentOutOfRangeException>(); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments