Permalink
Browse files

Add parsed value to SkipQueryOption and TopQueryOption for convenience

  • Loading branch information...
1 parent cb838d3 commit 6b5687093715570f9f17d4325d81345a374ae056 youssefm committed Nov 1, 2012
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Web.Http.OData.Properties;
@@ -10,6 +11,8 @@ namespace System.Web.Http.OData.Query
{
public class SkipQueryOption
{
+ private int? _value;
+
/// <summary>
/// Initialize a new instance of <see cref="OrderByQueryOption"/> based on the raw $skip value and
/// an EdmModel from <see cref="ODataQueryContext"/>.
@@ -33,16 +36,39 @@ public SkipQueryOption(string rawValue, ODataQueryContext context)
}
/// <summary>
- /// Gets the given <see cref="ODataQueryContext"/>.
+ /// Gets the given <see cref="ODataQueryContext"/>.
/// </summary>
public ODataQueryContext Context { get; private set; }
/// <summary>
- /// Gets the raw $skip value.
+ /// Gets the raw $skip value.
/// </summary>
public string RawValue { get; private set; }
/// <summary>
+ /// Gets the value of the $skip as a parsed integer.
+ /// </summary>
+ public int Value
+ {
+ get
+ {
+ if (_value == null)
+ {
+ try
+ {
+ _value = Convert.ToInt32(RawValue, CultureInfo.InvariantCulture);
+ }
+ catch (FormatException exception)
+ {
+ throw new ODataException(Error.Format(SRResources.CanNotParseInteger, RawValue), exception);
+ }
+ }
+ Contract.Assert(_value.HasValue);
+ return _value.Value;
+ }
+ }
+
+ /// <summary>
/// Apply the $skip query to the given IQueryable.
/// </summary>
/// <param name="query">The IQueryable that we are applying skip query against.</param>
@@ -64,14 +90,7 @@ public IQueryable ApplyTo(IQueryable query)
private IQueryable ApplyToCore(IQueryable query)
{
- try
- {
- return ExpressionHelpers.Skip(query, Convert.ToInt32(RawValue, CultureInfo.InvariantCulture), Context.EntityClrType);
- }
- catch (FormatException exception)
- {
- throw new ODataException(Error.Format(SRResources.CanNotParseInteger, RawValue), exception);
- }
+ return ExpressionHelpers.Skip(query, Value, Context.EntityClrType);
}
}
}
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
+using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Web.Http.OData.Properties;
@@ -10,6 +11,8 @@ namespace System.Web.Http.OData.Query
{
public class TopQueryOption
{
+ private int? _value;
+
/// <summary>
/// Initialize a new instance of <see cref="OrderByQueryOption"/> based on the raw $top value and
/// an EdmModel from <see cref="ODataQueryContext"/>.
@@ -29,20 +32,43 @@ public TopQueryOption(string rawValue, ODataQueryContext context)
}
Context = context;
- RawValue = rawValue;
+ RawValue = rawValue;
}
/// <summary>
- /// Gets the given <see cref="ODataQueryContext"/>.
+ /// Gets the given <see cref="ODataQueryContext"/>.
/// </summary>
public ODataQueryContext Context { get; private set; }
/// <summary>
- /// Gets the raw $top value.
+ /// Gets the raw $top value.
/// </summary>
public string RawValue { get; private set; }
/// <summary>
+ /// Gets the value of the $top as a parsed integer.
+ /// </summary>
+ public int Value
+ {
+ get
+ {
+ if (_value == null)
+ {
+ try
+ {
+ _value = Convert.ToInt32(RawValue, CultureInfo.InvariantCulture);
+ }
+ catch (FormatException exception)
+ {
+ throw new ODataException(Error.Format(SRResources.CanNotParseInteger, RawValue), exception);
+ }
+ }
+ Contract.Assert(_value.HasValue);
+ return _value.Value;
+ }
+ }
+
+ /// <summary>
/// Apply the $top query to the given IQueryable.
/// </summary>
/// <param name="query">The IQueryable that we are applying top query against.</param>
@@ -64,14 +90,7 @@ public IQueryable ApplyTo(IQueryable query)
private IQueryable ApplyToCore(IQueryable query)
{
- try
- {
- return ExpressionHelpers.Take(query, Convert.ToInt32(RawValue, CultureInfo.InvariantCulture), Context.EntityClrType);
- }
- catch (FormatException exception)
- {
- throw new ODataException(Error.Format(SRResources.CanNotParseInteger, RawValue), exception);
- }
+ return ExpressionHelpers.Take(query, Value, Context.EntityClrType);
}
}
}
@@ -65,6 +65,32 @@ public void ApplyInValidSkipQueryThrows(string skipValue)
skip.ApplyTo(ODataQueryOptionTest.Customers));
}
+ [Theory]
+ [InlineData("-1", -1)]
+ [InlineData("0", 0)]
+ [InlineData("100", 100)]
+ public void Value_Returns_ParsedSkipValue(string skipValue, int expectedValue)
+ {
+ var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
+ var context = new ODataQueryContext(model, typeof(Customer), "Customers");
+ var skip = new SkipQueryOption(skipValue, context);
+
+ Assert.Equal(expectedValue, skip.Value);
+ }
+
+ [Theory]
+ [InlineData("NotANumber")]
+ [InlineData("''")]
+ [InlineData(" ")]
+ public void Value_ThrowsODataException_ForInvalidValues(string skipValue)
+ {
+ var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
+ var context = new ODataQueryContext(model, typeof(Customer), "Customers");
+ var skip = new SkipQueryOption(skipValue, context);
+
+ Assert.Throws<ODataException>(() => skip.Value);
+ }
+
[Fact]
public void CanApplySkip()
{
@@ -65,6 +65,32 @@ public void ApplyInvalidSkipQueryThrows(string topValue)
top.ApplyTo(ODataQueryOptionTest.Customers));
}
+ [Theory]
+ [InlineData("-1", -1)]
+ [InlineData("0", 0)]
+ [InlineData("100", 100)]
+ public void Value_Returns_ParsedTopValue(string topValue, int expectedValue)
+ {
+ var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
+ var context = new ODataQueryContext(model, typeof(Customer), "Customers");
+ var top = new TopQueryOption(topValue, context);
+
+ Assert.Equal(expectedValue, top.Value);
+ }
+
+ [Theory]
+ [InlineData("NotANumber")]
+ [InlineData("''")]
+ [InlineData(" ")]
+ public void Value_ThrowsODataException_ForInvalidValues(string skipValue)
+ {
+ var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
+ var context = new ODataQueryContext(model, typeof(Customer), "Customers");
+ var top = new TopQueryOption(skipValue, context);
+
+ Assert.Throws<ODataException>(() => top.Value);
+ }
+
[Fact]
public void CanApplyTop()
{

0 comments on commit 6b56870

Please sign in to comment.