Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
For #15
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
TrevorPilley committed Jun 1, 2020
1 parent 1a8af10 commit 6912f1c
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

namespace Net.Http.OData.Tests.Linq
{
public class ODataQueryOptionsExtensionsTests
public class QueryableExtensionsTests
{
private readonly IList<Category> _categories;
private readonly IList<Customer> _customers;
private readonly IList<Employee> _employees;
private readonly IList<Manager> _managers;
private readonly IList<Product> _products;

public ODataQueryOptionsExtensionsTests()
public QueryableExtensionsTests()
{
_categories = new[]
{
Expand Down Expand Up @@ -61,6 +61,23 @@ public ODataQueryOptionsExtensionsTests()
};
}

[Fact]
public void Apply_Throws_ArgumentNullException_For_Null_Queryable()
{
TestHelper.EnsureEDM();

var queryOptions = new ODataQueryOptions(
"?$count=true",
EntityDataModel.Current.EntitySets["Customers"],
Mock.Of<IODataQueryOptionsValidator>());

Assert.Throws<ArgumentNullException>(() => QueryableExtensions.Apply(null, queryOptions));
}

[Fact]
public void Apply_Throws_ArgumentNullException_For_Null_QueryOptions()
=> Assert.Throws<ArgumentNullException>(() => QueryableExtensions.Apply(_categories.AsQueryable(), null));

[Fact]
public void ApplyTo_OrderBy_NotDeclared()
{
Expand All @@ -71,7 +88,7 @@ public void ApplyTo_OrderBy_NotDeclared()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);

Expand All @@ -89,7 +106,7 @@ public void ApplyTo_OrderBy_Properties()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);

Expand All @@ -109,7 +126,7 @@ public void ApplyTo_OrderBy_Properties_IncludingPropertyPath()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);

Expand All @@ -129,7 +146,7 @@ public void ApplyTo_OrderBy_SingleProperty_Ascending()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);
Assert.Equal(_products.Min(x => x.Rating), ((dynamic)results[0]).Rating);
Expand All @@ -146,7 +163,7 @@ public void ApplyTo_OrderBy_SingleProperty_Descending()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);
Assert.Equal(_products.Max(x => x.Rating), ((dynamic)results[0]).Rating);
Expand All @@ -163,7 +180,7 @@ public void ApplyTo_Select_Expand()
EntityDataModel.Current.EntitySets["Customers"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_customers.AsQueryable()).ToList();
IList<ExpandoObject> results = _customers.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_customers.Count, results.Count);

Expand Down Expand Up @@ -205,7 +222,7 @@ public void ApplyTo_Select_NotDeclared()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);

Expand Down Expand Up @@ -233,7 +250,7 @@ public void ApplyTo_Select_Properties()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);

Expand All @@ -256,7 +273,7 @@ public void ApplyTo_Select_Properties_IncludingPropertyPath()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);

Expand All @@ -283,7 +300,7 @@ public void ApplyTo_Select_SingleProperty()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);

Expand All @@ -304,7 +321,7 @@ public void ApplyTo_Select_Star()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_products.Count, results.Count);

Expand Down Expand Up @@ -332,7 +349,7 @@ public void ApplyTo_Skip()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

IEnumerable<Product> skippedProducts = _products.Skip(4);

Expand All @@ -341,23 +358,6 @@ public void ApplyTo_Skip()
Assert.Equal(skippedProducts.Last().ProductId, ((dynamic)results[results.Count - 1]).ProductId);
}

[Fact]
public void ApplyTo_Throws_ArgumentNullException_For_Null_Queryable()
{
TestHelper.EnsureEDM();

var queryOptions = new ODataQueryOptions(
"?$count=true",
EntityDataModel.Current.EntitySets["Customers"],
Mock.Of<IODataQueryOptionsValidator>());

Assert.Throws<ArgumentNullException>(() => ODataQueryOptionsExtensions.ApplyTo(queryOptions, null));
}

[Fact]
public void ApplyTo_Throws_ArgumentNullException_For_Null_QueryOptions()
=> Assert.Throws<ArgumentNullException>(() => ODataQueryOptionsExtensions.ApplyTo(null, _categories.AsQueryable()));

[Fact]
public void ApplyTo_Throws_InvalidOperationException_For_Incorrect_QueryType()
{
Expand All @@ -368,7 +368,7 @@ public void ApplyTo_Throws_InvalidOperationException_For_Incorrect_QueryType()
EntityDataModel.Current.EntitySets["Customers"],
Mock.Of<IODataQueryOptionsValidator>());

Assert.Throws<InvalidOperationException>(() => queryOptions.ApplyTo(_categories.AsQueryable()));
Assert.Throws<InvalidOperationException>(() => _categories.AsQueryable().Apply(queryOptions));
}

[Fact]
Expand All @@ -381,7 +381,7 @@ public void ApplyTo_Top()
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = queryOptions.ApplyTo(_products.AsQueryable()).ToList();
IList<ExpandoObject> results = _products.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(4, results.Count);
Assert.Equal(_products[0].ProductId, ((dynamic)results[0]).ProductId);
Expand Down
Loading

0 comments on commit 6912f1c

Please sign in to comment.