Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit b79cf95

Browse files
author
Ihar Yakimush
committed
expand collection unit tests
1 parent 2306455 commit b79cf95

File tree

4 files changed

+85
-21
lines changed

4 files changed

+85
-21
lines changed

Community.Data.OData.Linq.xTests/ExpandTests.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Linq;
55

66
using Community.OData.Linq.xTests.SampleData;
7-
7+
using Microsoft.OData;
88
using Xunit;
99

1010
public class ExpandTests
@@ -57,5 +57,60 @@ public void ExpandLinkSelect()
5757
Assert.Equal(3, metadata.Count);
5858
Assert.Equal(1, (metadata["Link1"] as ISelectExpandWrapper).ToDictionary().Count);
5959
}
60+
61+
[Fact]
62+
public void ExpandCollection()
63+
{
64+
ISelectExpandWrapper[] result = ClassWithCollection.CreateQuery().OData().SelectExpand("Name", "Link2").ToArray();
65+
66+
IDictionary<string, object> metadata = result[0].ToDictionary();
67+
68+
// Not expanded by default
69+
Assert.Equal(2, metadata.Count);
70+
Assert.Equal(2, (metadata["Link2"] as IEnumerable<ISelectExpandWrapper>).Count());
71+
}
72+
73+
[Fact]
74+
public void ExpandCollectionWithTop()
75+
{
76+
ISelectExpandWrapper[] result = ClassWithCollection.CreateQuery().OData().SelectExpand("Name", "Link2($top=1)").ToArray();
77+
78+
IDictionary<string, object> metadata = result[0].ToDictionary();
79+
80+
// Not expanded by default
81+
Assert.Equal(2, metadata.Count);
82+
Assert.Single((metadata["Link2"] as IEnumerable<ISelectExpandWrapper>));
83+
}
84+
85+
[Fact]
86+
public void ExpandCollectionWithTopDefaultPageSize()
87+
{
88+
ISelectExpandWrapper[] result = ClassWithCollection.CreateQuery().OData(s => s.QuerySettings.PageSize = 1).SelectExpand("Name", "Link2").ToArray();
89+
90+
IDictionary<string, object> metadata = result[0].ToDictionary();
91+
92+
// Not expanded by default
93+
Assert.Equal(2, metadata.Count);
94+
Assert.Single((metadata["Link2"] as IEnumerable<ISelectExpandWrapper>));
95+
}
96+
97+
[Fact]
98+
public void ExpandCollectionWithTop21()
99+
{
100+
ISelectExpandWrapper[] result = ClassWithCollection.CreateQuery().OData().SelectExpand("Name", "Link2($top=21)").ToArray();
101+
102+
IDictionary<string, object> metadata = result[0].ToDictionary();
103+
104+
// Not expanded by default
105+
Assert.Equal(2, metadata.Count);
106+
Assert.Equal(2, (metadata["Link2"] as IEnumerable<ISelectExpandWrapper>).Count());
107+
}
108+
109+
[Fact]
110+
public void ExpandCollectionWithTopExceedLimit()
111+
{
112+
Assert.Throws<ODataException>(
113+
() => ClassWithCollection.CreateQuery().OData().SelectExpand("Name", "Link2($top=101)"));
114+
}
60115
}
61116
}

Community.Data.OData.Linq.xTests/SelectTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ public void SelectNameToIgnore()
9696
{
9797
Assert.Throws<ODataException>(
9898
() => SimpleClass.CreateQuery().OData().SelectExpand("NameToIgnore"));
99-
}
99+
}
100+
101+
[Fact]
102+
public void SelectLinkWithoutExpandNotWorking()
103+
{
104+
ISelectExpandWrapper[] result = ClassWithLink.CreateQuery().OData().SelectExpand("Link1").ToArray();
105+
106+
IDictionary<string, object> metadata = result[0].ToDictionary();
107+
108+
Assert.Equal(1, metadata.Count);
109+
Assert.Null(metadata["Link3"]);
110+
}
111+
112+
100113
}
101114
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
namespace Community.OData.Linq
22
{
3-
using System.ComponentModel.Design;
4-
53
using Community.OData.Linq.OData.Query;
64

75
using Microsoft.OData.UriParser;
@@ -10,7 +8,7 @@ public class ODataSettings
108
{
119
internal static ODataUriResolver DefaultResolver = new StringAsEnumResolver { EnableCaseInsensitive = true };
1210

13-
public ODataQuerySettings QuerySettings { get; } = new ODataQuerySettings();
11+
public ODataQuerySettings QuerySettings { get; } = new ODataQuerySettings() { PageSize = 20 };
1412

1513
public ODataValidationSettings ValidationSettings { get; } = new ODataValidationSettings();
1614

@@ -19,5 +17,15 @@ public class ODataSettings
1917
public ODataUriResolver Resolver { get; set; } = DefaultResolver;
2018

2119
public bool EnableCaseInsensitive { get; set; } = true;
20+
21+
public DefaultQuerySettings DefaultQuerySettings { get; } =
22+
new DefaultQuerySettings
23+
{
24+
EnableFilter = true,
25+
EnableOrderBy = true,
26+
EnableExpand = true,
27+
EnableSelect = true,
28+
MaxTop = 100
29+
};
2230
}
2331
}

Community.Data.OData.Linq/OdataLinqExtensions.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@ public static class ODataLinqExtensions
2525
/// </summary>
2626
private static readonly ODataSimplifiedOptions SimplifiedOptions = new ODataSimplifiedOptions();
2727

28-
/// <summary>
29-
/// The query settings default.
30-
/// </summary>
31-
private static readonly DefaultQuerySettings QuerySettingsDefault =
32-
new DefaultQuerySettings
33-
{
34-
EnableFilter = true,
35-
EnableOrderBy = true,
36-
EnableExpand = true,
37-
EnableSelect = true
38-
};
39-
4028
/// <summary>
4129
/// Enable applying OData specific functions to query
4230
/// </summary>
@@ -86,8 +74,8 @@ public static ODataQuery<T> OData<T>(this IQueryable<T> query, Action<ODataSetti
8674
container.AddService(typeof(ODataUriResolver), settings.Resolver ?? ODataSettings.DefaultResolver);
8775
container.AddService(typeof(ODataSimplifiedOptions), SimplifiedOptions);
8876
container.AddService(typeof(ODataSettings), settings);
89-
container.AddService(typeof(DefaultQuerySettings), QuerySettingsDefault);
90-
container.AddService(typeof(SelectExpandQueryValidator), new SelectExpandQueryValidator(QuerySettingsDefault));
77+
container.AddService(typeof(DefaultQuerySettings), settings.DefaultQuerySettings);
78+
container.AddService(typeof(SelectExpandQueryValidator), new SelectExpandQueryValidator(settings.DefaultQuerySettings));
9179

9280
ODataQuery<T> dataQuery = new ODataQuery<T>(query, container);
9381

@@ -133,7 +121,7 @@ public static ODataQuery<T> Filter<T>(this ODataQuery<T> query, string filterTex
133121
filterClause = new FilterClause(filterExpression, filterClause.RangeVariable);
134122
Contract.Assert(filterClause != null);
135123

136-
var validator = new FilterQueryValidator(QuerySettingsDefault);
124+
var validator = new FilterQueryValidator(settings.DefaultQuerySettings);
137125
validator.Validate(filterClause, settings.ValidationSettings, edmModel);
138126

139127
Expression filter = FilterBinder.Bind(query, filterClause, typeof(T), query.ServiceProvider);
@@ -160,7 +148,7 @@ public static IOrderedQueryable<T> OrderBy<T>(this ODataQuery<T> query, string o
160148

161149
ICollection<OrderByNode> nodes = OrderByNode.CreateCollection(orderByClause);
162150

163-
var validator = new OrderByQueryValidator(QuerySettingsDefault);
151+
var validator = new OrderByQueryValidator(settings.DefaultQuerySettings);
164152
validator.Validate(nodes, settings.ValidationSettings, edmModel);
165153

166154
IOrderedQueryable<T> result = (IOrderedQueryable<T>)OrderByBinder.OrderApplyToCore(query, settings.QuerySettings, nodes, edmModel);

0 commit comments

Comments
 (0)