|
| 1 | +using FluentAssertions; |
| 2 | +using Microsoft.AspNet.OData.Query; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | +using NSubstitute; |
| 5 | +using NUnit.Framework; |
| 6 | +using System.Linq; |
| 7 | +using Models = SubSonic.Extensions.Test.Models; |
| 8 | + |
| 9 | +namespace SubSonic.Tests.DAL.OData |
| 10 | +{ |
| 11 | + using Infrastructure; |
| 12 | + using Infrastructure.Logging; |
| 13 | + using Linq.Expressions; |
| 14 | + using System; |
| 15 | + |
| 16 | + [TestFixture] |
| 17 | + public class ODataCompatibilityTests |
| 18 | + : SUT.BaseTestFixture |
| 19 | + { |
| 20 | + IODataQueryOptions<Models.Person> personQueryOptions = null; |
| 21 | + |
| 22 | + public override void SetupTestFixture() |
| 23 | + { |
| 24 | + base.SetupTestFixture(); |
| 25 | + |
| 26 | + personQueryOptions = Substitute.For<IODataQueryOptions<Models.Person>>(); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// simulate an $filter on more than two fields |
| 31 | + /// </summary> |
| 32 | + /// <param name="direction"></param> |
| 33 | + [Test] |
| 34 | + [TestCase(nameof(string.StartsWith))] |
| 35 | + [TestCase(nameof(string.EndsWith))] |
| 36 | + [TestCase(nameof(string.Contains))] |
| 37 | + [TestCase(nameof(string.Equals))] |
| 38 | + public void ShouldBeAbleToApplyWhereUsingOData(string @operator) |
| 39 | + { |
| 40 | + personQueryOptions.ApplyTo(Arg.Any<IQueryable>()) |
| 41 | + .Returns(call => |
| 42 | + { |
| 43 | + if (call.Arg<IQueryable>() is IQueryable<Models.Person> people) |
| 44 | + { |
| 45 | + switch (@operator) |
| 46 | + { |
| 47 | + case nameof(string.StartsWith): |
| 48 | + return people |
| 49 | + .Where(x => |
| 50 | + x.FamilyName.StartsWith("Car")); |
| 51 | + case nameof(string.EndsWith): |
| 52 | + return people |
| 53 | + .Where(x => |
| 54 | + x.FamilyName.EndsWith("Car")); |
| 55 | + case nameof(string.Contains): |
| 56 | + return people |
| 57 | + .Where(x => |
| 58 | + x.FamilyName.Contains("Car")); |
| 59 | + case nameof(string.Equals): |
| 60 | + return people |
| 61 | + .Where(x => |
| 62 | + x.FamilyName == "Carter"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + throw new NotImplementedException(); |
| 67 | + }); |
| 68 | + |
| 69 | + IQueryable select = personQueryOptions.ApplyTo(Context.People); |
| 70 | + |
| 71 | + IDbQuery query = null; |
| 72 | + |
| 73 | + var logging = Context.Instance.GetService<ISubSonicLogger<DbSelectExpression>>(); |
| 74 | + |
| 75 | + using (var perf = logging.Start("SQL Query Writer")) |
| 76 | + { |
| 77 | + FluentActions.Invoking(() => |
| 78 | + { |
| 79 | + ISubSonicQueryProvider<Models.Person> builder = Context.Instance.GetService<ISubSonicQueryProvider<Models.Person>>(); |
| 80 | + |
| 81 | + query = builder.ToQuery(select.Expression); |
| 82 | + }).Should().NotThrow(); |
| 83 | + } |
| 84 | + |
| 85 | + logging.LogInformation("\n" + query.Sql + "\n"); |
| 86 | + |
| 87 | + query.Sql.Should().NotBeNullOrEmpty(); |
| 88 | + query.Sql.Should().Contain("WHERE"); |
| 89 | + switch (@operator) |
| 90 | + { |
| 91 | + case nameof(string.StartsWith): |
| 92 | + case nameof(string.EndsWith): |
| 93 | + case nameof(string.Contains): |
| 94 | + query.Sql.Should().Contain("LIKE"); |
| 95 | + break; |
| 96 | + case nameof(string.Equals): |
| 97 | + query.Sql.Should().Contain(" = @"); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// simulate an $orderby descending and ascending |
| 104 | + /// </summary> |
| 105 | + /// <param name="direction"></param> |
| 106 | + [Test] |
| 107 | + [TestCase("ascending")] |
| 108 | + [TestCase("descending")] |
| 109 | + public void ShouldBeAbleToApplyOrderByUsingOData(string direction) |
| 110 | + { |
| 111 | + personQueryOptions.ApplyTo(Arg.Any<IQueryable>()) |
| 112 | + .Returns(call => |
| 113 | + { |
| 114 | + if (call.Arg<IQueryable>() is IQueryable<Models.Person> people) |
| 115 | + { |
| 116 | + if (direction.Equals("ascending")) |
| 117 | + { |
| 118 | + return people.OrderBy(x => x.FamilyName); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + return people.OrderByDescending(x => x.FamilyName); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + throw new NotImplementedException(); |
| 127 | + }); |
| 128 | + |
| 129 | + IQueryable select = personQueryOptions.ApplyTo(Context.People); |
| 130 | + |
| 131 | + IDbQuery query = null; |
| 132 | + |
| 133 | + var logging = Context.Instance.GetService<ISubSonicLogger<DbSelectExpression>>(); |
| 134 | + |
| 135 | + using (var perf = logging.Start("SQL Query Writer")) |
| 136 | + { |
| 137 | + FluentActions.Invoking(() => |
| 138 | + { |
| 139 | + ISubSonicQueryProvider<Models.Person> builder = Context.Instance.GetService<ISubSonicQueryProvider<Models.Person>>(); |
| 140 | + |
| 141 | + query = builder.ToQuery(select.Expression); |
| 142 | + }).Should().NotThrow(); |
| 143 | + } |
| 144 | + |
| 145 | + logging.LogInformation("\n" + query.Sql + "\n"); |
| 146 | + |
| 147 | + query.Sql.Should().NotBeNullOrEmpty(); |
| 148 | + query.Sql.Should().Contain("ORDER BY"); |
| 149 | + |
| 150 | + if (direction.Equals("descending")) |
| 151 | + { |
| 152 | + query.Sql.Should().Contain("DESC"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// simulate an $orderby on more than two fields |
| 158 | + /// </summary> |
| 159 | + /// <param name="direction"></param> |
| 160 | + [Test] |
| 161 | + [TestCase("ascending")] |
| 162 | + [TestCase("descending")] |
| 163 | + public void ShouldBeAbleToApplyOrderByThenByUsingOData(string direction) |
| 164 | + { |
| 165 | + personQueryOptions.ApplyTo(Arg.Any<IQueryable>()) |
| 166 | + .Returns(call => |
| 167 | + { |
| 168 | + if (call.Arg<IQueryable>() is IQueryable<Models.Person> people) |
| 169 | + { |
| 170 | + if (direction.Equals("ascending")) |
| 171 | + { |
| 172 | + return people |
| 173 | + .OrderBy(x => x.FamilyName) |
| 174 | + .ThenByDescending(x => x.FirstName); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + return people |
| 179 | + .OrderByDescending(x => x.FamilyName) |
| 180 | + .ThenBy(x => x.FirstName); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + throw new NotImplementedException(); |
| 185 | + }); |
| 186 | + |
| 187 | + IQueryable select = personQueryOptions.ApplyTo(Context.People); |
| 188 | + |
| 189 | + IDbQuery query = null; |
| 190 | + |
| 191 | + var logging = Context.Instance.GetService<ISubSonicLogger<DbSelectExpression>>(); |
| 192 | + |
| 193 | + using (var perf = logging.Start("SQL Query Writer")) |
| 194 | + { |
| 195 | + FluentActions.Invoking(() => |
| 196 | + { |
| 197 | + ISubSonicQueryProvider<Models.Person> builder = Context.Instance.GetService<ISubSonicQueryProvider<Models.Person>>(); |
| 198 | + |
| 199 | + query = builder.ToQuery(select.Expression); |
| 200 | + }).Should().NotThrow(); |
| 201 | + } |
| 202 | + |
| 203 | + logging.LogInformation("\n" + query.Sql + "\n"); |
| 204 | + |
| 205 | + query.Sql.Should().NotBeNullOrEmpty(); |
| 206 | + query.Sql.Should().Contain("ORDER BY"); |
| 207 | + |
| 208 | + if (direction.Equals("descending")) |
| 209 | + { |
| 210 | + query.Sql.Should().Contain("DESC"); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments