forked from matteobortolazzo/couchdb-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Min/Max support using OrderBy + Take(1).
- Loading branch information
1 parent
8ba8738
commit 243c5e0
Showing
4 changed files
with
147 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/CouchDB.Driver/ExpressionVisitors/QueryPretranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace CouchDB.Driver.ExpressionVisitors | ||
{ | ||
public class QueryPretranslator : ExpressionVisitor | ||
{ | ||
protected override Expression VisitMethodCall(MethodCallExpression node) | ||
{ | ||
if (node.Method.Name == nameof(Queryable.Min)) | ||
{ | ||
Type[] genericArgs = node.Method.GetGenericArguments(); | ||
MethodCallExpression orderByDescExpression = Expression.Call(typeof(Queryable), nameof(Queryable.OrderBy), genericArgs, node.Arguments[0], node.Arguments[1]); | ||
MethodCallExpression takeExpression = Expression.Call(typeof(Queryable), nameof(Queryable.Take), genericArgs.Take(1).ToArray(), orderByDescExpression, Expression.Constant(1)); | ||
return takeExpression; | ||
} | ||
if (node.Method.Name == nameof(Queryable.Max)) | ||
{ | ||
Type[] genericArgs = node.Method.GetGenericArguments(); | ||
MethodCallExpression orderByDescExpression = Expression.Call(typeof(Queryable), nameof(Queryable.OrderByDescending), genericArgs, node.Arguments[0], node.Arguments[1]); | ||
MethodCallExpression takeExpression = Expression.Call(typeof(Queryable), nameof(Queryable.Take), genericArgs.Take(1).ToArray(), orderByDescExpression, Expression.Constant(1)); | ||
return takeExpression; | ||
} | ||
return base.VisitMethodCall(node); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/CouchDB.Driver.UnitTests/SupportByCombination_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using CouchDB.Driver.UnitTests.Models; | ||
using Flurl.Http.Testing; | ||
using Xunit; | ||
|
||
namespace CouchDB.Driver.UnitTests | ||
{ | ||
public class SupportByCombination_Tests | ||
{ | ||
private readonly CouchDatabase<Rebel> _rebels; | ||
private readonly Rebel _mainRebel; | ||
private readonly List<Rebel> _rebelsList; | ||
private object _response; | ||
|
||
public SupportByCombination_Tests() | ||
{ | ||
var client = new CouchClient("http://localhost"); | ||
_rebels = client.GetDatabase<Rebel>(); | ||
_mainRebel = new Rebel | ||
{ | ||
Id = Guid.NewGuid().ToString(), | ||
Name = "Luke", | ||
Age = 19, | ||
Skills = new List<string> { "Force" } | ||
}; | ||
_rebelsList = new List<Rebel> | ||
{ | ||
_mainRebel | ||
}; | ||
_response = new | ||
{ | ||
Docs = _rebelsList | ||
}; | ||
} | ||
|
||
[Fact] | ||
public async Task Max() | ||
{ | ||
using (var httpTest = new HttpTest()) | ||
{ | ||
httpTest.RespondWithJson(_response); | ||
var result = _rebels.AsQueryable().Max(r => r.Age); | ||
Assert.Equal(_mainRebel.Age, result); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Min() | ||
{ | ||
using (var httpTest = new HttpTest()) | ||
{ | ||
httpTest.RespondWithJson(_response); | ||
var result = _rebels.AsQueryable().Min(r => r.Age); | ||
Assert.Equal(_mainRebel.Age, result); | ||
} | ||
} | ||
} | ||
} |