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

Commit dfc9f18

Browse files
author
Ihar Yakimush
committed
add expand attributes
1 parent 0834501 commit dfc9f18

File tree

13 files changed

+444
-48
lines changed

13 files changed

+444
-48
lines changed

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

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,25 @@
1010
public class ExpandTests
1111
{
1212
[Fact]
13-
public void DefaultExpand()
13+
public void EmptyExpand()
1414
{
1515
ISelectExpandWrapper[] result = ClassWithLink.CreateQuery().OData().SelectExpand().ToArray();
1616

1717
IDictionary<string, object> metadata = result[0].ToDictionary();
1818

1919
// Not expanded by default except auto expand attribute
20-
Assert.Equal(3, metadata.Count);
21-
Assert.Null(metadata["Link3"]);
20+
Assert.Equal(2, metadata.Count);
21+
}
22+
23+
[Fact]
24+
public void EmptyExpandSelectAll()
25+
{
26+
ISelectExpandWrapper[] result = ClassWithLink.CreateQuery().OData().SelectExpand("*").ToArray();
27+
28+
IDictionary<string, object> metadata = result[0].ToDictionary();
29+
30+
// Not expanded by default except auto expand attribute
31+
Assert.Equal(2, metadata.Count);
2232
}
2333

2434
[Fact]
@@ -29,7 +39,7 @@ public void ExpandLink()
2939
IDictionary<string, object> metadata = result[0].ToDictionary();
3040

3141
// Not expanded by default
32-
Assert.Equal(4, metadata.Count);
42+
Assert.Equal(3, metadata.Count);
3343

3444
Assert.Equal(6, (metadata["Link1"] as ISelectExpandWrapper).ToDictionary().Count);
3545
}
@@ -42,7 +52,7 @@ public void ExpandSelect()
4252
IDictionary<string, object> metadata = result[0].ToDictionary();
4353

4454
// Not expanded by default
45-
Assert.Equal(3, metadata.Count);
55+
Assert.Equal(2, metadata.Count);
4656
Assert.Equal(6, (metadata["Link1"] as ISelectExpandWrapper).ToDictionary().Count);
4757
}
4858

@@ -54,7 +64,7 @@ public void ExpandLinkSelect()
5464
IDictionary<string, object> metadata = result[0].ToDictionary();
5565

5666
// Not expanded by default
57-
Assert.Equal(3, metadata.Count);
67+
Assert.Equal(2, metadata.Count);
5868
Assert.Equal(1, (metadata["Link1"] as ISelectExpandWrapper).ToDictionary().Count);
5969
}
6070

@@ -127,5 +137,59 @@ public void ExpandCollectionWithFilterAndSelect()
127137

128138
Assert.Equal(1, collection.Single().ToDictionary().Count);
129139
}
140+
141+
[Fact]
142+
public void ExpandCollectionWithNotExpandable()
143+
{
144+
Assert.Throws<ODataException>(
145+
() => SampleWithCustomKey.CreateQuery().OData().SelectExpand("Id", "NotExpandableLink"));
146+
}
147+
148+
[Fact]
149+
public void ExpandWithAttributes()
150+
{
151+
ISelectExpandWrapper[] result = SampleWithCustomKey.CreateQuery().OData().SelectExpand().ToArray();
152+
153+
IDictionary<string, object> metadata = result[0].ToDictionary();
154+
155+
// Not expanded by default
156+
Assert.Equal(4, metadata.Count);
157+
158+
Assert.Equal(6, (metadata["AutoExpandLink"] as ISelectExpandWrapper).ToDictionary().Count);
159+
Assert.Equal(6, (metadata["AutoExpandAndSelectLink"] as ISelectExpandWrapper).ToDictionary().Count);
160+
}
161+
162+
[Fact]
163+
public void ExpandWithAttributesAndExplicit()
164+
{
165+
ISelectExpandWrapper[] result = SampleWithCustomKey.CreateQuery().OData().SelectExpand("*", "AutoExpandAndSelectLink").ToArray();
166+
167+
IDictionary<string, object> metadata = result[0].ToDictionary();
168+
169+
// Not expanded by default
170+
Assert.Equal(3, metadata.Count);
171+
172+
Assert.Equal(6, (metadata["AutoExpandAndSelectLink"] as ISelectExpandWrapper).ToDictionary().Count);
173+
}
174+
175+
[Fact]
176+
public void ExpandMaxDeepNotExceed()
177+
{
178+
ISelectExpandWrapper[] result = SampleWithCustomKey.CreateQuery().OData().SelectExpand(null, "RecursiveLink($expand=RecursiveLink)").ToArray();
179+
180+
IDictionary<string, object> metadata = result[0].ToDictionary();
181+
182+
// Not expanded by default
183+
Assert.Equal(5, metadata.Count);
184+
185+
Assert.NotNull(metadata["RecursiveLink"]);
186+
}
187+
188+
[Fact]
189+
public void ExpandMaxDeepExceed()
190+
{
191+
Assert.Throws<ODataException>(
192+
() => SampleWithCustomKey.CreateQuery().OData().SelectExpand(null, "RecursiveLink($expand=RecursiveLink($expand=RecursiveLink))"));
193+
}
130194
}
131195
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ public class JsonTests
1414
{
1515
[Fact]
1616
public void SerializeSelectExpand()
17-
{
18-
string result = ClassWithCollection.CreateQuery().OData().SelectExpandJsonString("Name", "Link2($filter=Id eq 311;$select=Name)");
19-
20-
Assert.NotNull(result);
21-
17+
{
2218
JToken token = ClassWithCollection.CreateQuery().OData().SelectExpandJsonToken("Name", "Link2($filter=Id eq 311;$select=Name)");
2319
Assert.NotNull(token);
2420

25-
Assert.Equal(result, token.ToString(Formatting.None));
21+
Assert.DoesNotContain("ModelID", token.ToString(Formatting.None));
2622
}
2723
}
2824
}

Community.Data.OData.Linq.xTests/SampleData/ClassWithLink.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ public static IQueryable<ClassWithLink> CreateQuery()
2626
[NotNavigable]
2727
public virtual SimpleClass Link2 { get; set; }
2828

29-
[AutoExpand]
3029
[Select(SelectType = OData.Query.SelectExpandType.Automatic)]
3130
public virtual SimpleClass Link3 { get; set; }
31+
32+
[Select(SelectType = OData.Query.SelectExpandType.Disabled)]
33+
public virtual SimpleClass Link4 { get; set; }
3234
}
3335
}

Community.Data.OData.Linq.xTests/SampleData/SampleWithCustomKey.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
namespace Community.OData.Linq.xTests.SampleData
22
{
3+
using Community.OData.Linq.Annotations;
4+
using Community.OData.Linq.OData.Query;
35
using System;
6+
using System.Collections.Generic;
47
using System.ComponentModel.DataAnnotations;
58
using System.Linq;
69

710
public class SampleWithCustomKey
811
{
912
private static readonly SampleWithCustomKey[] items =
1013
{
11-
new SampleWithCustomKey { Name = "n1", DateTime = new DateTime(2018, 1, 26)},
12-
new SampleWithCustomKey { Name = "n2", DateTime = new DateTime(2001, 1, 26)}
14+
new SampleWithCustomKey {
15+
Name = "n1", DateTime = new DateTime(2018, 1, 26),
16+
ExpandableLink = new SimpleClass() { Id=1 },
17+
SelectableLink = new SimpleClass() { Id=2 },
18+
AutoExpandLink = new SimpleClass() { Id=3 },
19+
AutoExpandAndSelectLink = new SimpleClass() { Id=4 },
20+
ExpandAndSelectLink = new SimpleClass() { Id=5 },
21+
RecursiveLink = new SampleWithCustomKey(){RecursiveLink=new SampleWithCustomKey{RecursiveLink=new SampleWithCustomKey{Name="qwe"}} }
22+
},
23+
new SampleWithCustomKey {
24+
Name = "n2", DateTime = new DateTime(2001, 1, 26),
25+
ExpandableLink = new SimpleClass() { Id=2 },
26+
SelectableLink = new SimpleClass() { Id=2 },
27+
AutoExpandLink = new SimpleClass() { Id=3 },
28+
AutoExpandAndSelectLink = new SimpleClass() { Id=4 },
29+
ExpandAndSelectLink = new SimpleClass() { Id=5 },
30+
RecursiveLink = new SampleWithCustomKey(){RecursiveLink=new SampleWithCustomKey{RecursiveLink=new SampleWithCustomKey{Name="qwe"}} }
31+
},
1332
};
1433

1534
public static IQueryable<SampleWithCustomKey> CreateQuery()
@@ -21,5 +40,28 @@ public static IQueryable<SampleWithCustomKey> CreateQuery()
2140
public string Name { get; set; }
2241

2342
public DateTime DateTime { get; set; }
43+
44+
[NotExpandable]
45+
public ICollection<SimpleClass> NotExpandableLink { get; set; }
46+
47+
[Expand(ExpandType = SelectExpandType.Automatic, MaxDepth = 2)]
48+
public SimpleClass ExpandableLink { get; set; }
49+
50+
[Select(SelectType = SelectExpandType.Automatic)]
51+
public SimpleClass SelectableLink { get; set; }
52+
53+
[AutoExpand(DisableWhenSelectPresent = true)]
54+
public SimpleClass AutoExpandLink { get; set; }
55+
56+
[AutoExpand(DisableWhenSelectPresent = true)]
57+
[Select(SelectType = SelectExpandType.Automatic)]
58+
public SimpleClass AutoExpandAndSelectLink { get; set; }
59+
60+
[Expand(ExpandType = SelectExpandType.Automatic, MaxDepth = 2)]
61+
[Select(SelectType = SelectExpandType.Automatic)]
62+
public SimpleClass ExpandAndSelectLink { get; set; }
63+
64+
[Expand(ExpandType = SelectExpandType.Automatic, MaxDepth = 2)]
65+
public SampleWithCustomKey RecursiveLink { get; set; }
2466
}
2567
}

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public void SelectDefault()
2323
Assert.Equal(6, metadata.Count);
2424
}
2525

26+
[Fact]
27+
public void SelectAllt()
28+
{
29+
ISelectExpandWrapper[] result = SimpleClass.CreateQuery().OData().SelectExpand("*").ToArray();
30+
31+
IDictionary<string, object> metadata = result[0].ToDictionary();
32+
33+
Assert.Equal(6, metadata.Count);
34+
}
35+
2636
[Fact]
2737
public void SelectName()
2838
{
@@ -98,17 +108,24 @@ public void SelectNameToIgnore()
98108
() => SimpleClass.CreateQuery().OData().SelectExpand("NameToIgnore"));
99109
}
100110

111+
[Fact]
112+
public void SelectDisabled()
113+
{
114+
ISelectExpandWrapper[] result = ClassWithLink.CreateQuery().OData().SelectExpand("Link4").ToArray();
115+
116+
IDictionary<string, object> metadata = result[0].ToDictionary();
117+
118+
Assert.Equal(0, metadata.Count);
119+
}
120+
101121
[Fact]
102122
public void SelectLinkWithoutExpandNotWorking()
103123
{
104124
ISelectExpandWrapper[] result = ClassWithLink.CreateQuery().OData().SelectExpand("Link1").ToArray();
105125

106126
IDictionary<string, object> metadata = result[0].ToDictionary();
107127

108-
Assert.Equal(1, metadata.Count);
109-
Assert.Null(metadata["Link3"]);
128+
Assert.Equal(0, metadata.Count);
110129
}
111-
112-
113130
}
114131
}

0 commit comments

Comments
 (0)