Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mocking/testing support into the Future Extension Methods #198

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Source/EntityFramework.Extended/Extensions/FutureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ public static FutureQuery<TEntity> Future<TEntity>(this IQueryable<TEntity> sour

ObjectQuery<TEntity> sourceQuery = source.ToObjectQuery();
if (sourceQuery == null)
{
// Early return for test scenarios, here so it only slows down the error path
if (source is IFutureTestQueryable<TEntity>)
return new FutureQuery<TEntity>(source, null);

throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");
}

var futureContext = GetFutureContext(sourceQuery);
var future = new FutureQuery<TEntity>(sourceQuery, futureContext.ExecuteFutureQueries);
Expand All @@ -54,7 +60,13 @@ public static FutureCount FutureCount<TEntity>(this IQueryable<TEntity> source)

ObjectQuery sourceQuery = source.ToObjectQuery();
if (sourceQuery == null)
{
// Early return for test scenarios, here so it only slows down the error path
if (source is IFutureTestQueryable)
return new FutureCount(source, null);

throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");
}

// create count expression
var expression = Expression.Call(
Expand All @@ -65,8 +77,6 @@ public static FutureCount FutureCount<TEntity>(this IQueryable<TEntity> source)

// create query from expression using internal ObjectQueryProvider
ObjectQuery countQuery = sourceQuery.CreateQuery(expression, typeof(int));
if (countQuery == null)
throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");

var futureContext = GetFutureContext(sourceQuery);
var future = new FutureCount(countQuery, futureContext.ExecuteFutureQueries);
Expand All @@ -90,7 +100,13 @@ public static FutureValue<TResult> FutureValue<TEntity, TResult>(this IQueryable

var sourceQuery = source.ToObjectQuery();
if (sourceQuery == null)
{
// Early return for test scenarios, here so it only slows down the error path
if (source is IFutureTestQueryable<TEntity>)
return new FutureValue<TResult>(source, null);

throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");
}

var methodExpr = selector.Body as MethodCallExpression;
if (methodExpr == null || methodExpr.Arguments.Count == 0)
Expand Down Expand Up @@ -131,14 +147,18 @@ public static FutureValue<TEntity> FutureFirstOrDefault<TEntity>(this IQueryable

ObjectQuery sourceQuery = source.ToObjectQuery();
if (sourceQuery == null)
{
// Early return for test scenarios, here so it only slows down the error path
if (source is IFutureTestQueryable<TEntity>)
return new FutureValue<TEntity>(source, null);

throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");
}

// make sure to only get the first value
IQueryable<TEntity> firstQuery = source.Take(1);

ObjectQuery<TEntity> objectQuery = firstQuery.ToObjectQuery();
if (objectQuery == null)
throw new ArgumentException("The source query must be of type ObjectQuery or DbQuery.", "source");

var futureContext = GetFutureContext(sourceQuery);
var future = new FutureValue<TEntity>(objectQuery, futureContext.ExecuteFutureQueries);
Expand Down
4 changes: 2 additions & 2 deletions Source/EntityFramework.Extended/Future/FutureValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace EntityFramework.Future
/// <typeparam name="T">The type for the future query.</typeparam>
/// <example>The following is an example of how to use FutureValue.
/// <code><![CDATA[
/// var db = new TrackeContext;
/// var db = new TrackedContext();
/// // build up queries
/// var q1 = db.User.ByEmailAddress("one@test.com").FutureValue();
/// var q2 = db.Task.Where(t => t.Summary == "Test").Future();
Expand All @@ -20,7 +20,7 @@ namespace EntityFramework.Future
/// ]]>
/// </code>
/// </example>
[DebuggerDisplay("IsLoaded={IsLoaded}, Value={UnderlingValue}")]
[DebuggerDisplay("IsLoaded={IsLoaded}, Value={UnderlyingValue}")]
public class FutureValue<T> : FutureQueryBase<T>
{
private bool _hasValue;
Expand Down
15 changes: 15 additions & 0 deletions Source/EntityFramework.Extended/Future/IFutureTestQueryable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Linq;

namespace EntityFramework.Future
{
/// <summary>
/// Allows mocking and testing of the Future() ExtensionMethods.
/// </summary>
public interface IFutureTestQueryable : IQueryable { }

/// <summary>
/// Allows mocking and testing of the Future() ExtensionMethods.
/// </summary>
/// <typeparam name="T">Return type</typeparam>
public interface IFutureTestQueryable<T> : IQueryable<T>, IFutureTestQueryable { }
}