1+ namespace Community . OData . Linq . xTests
2+ {
3+ using System . Collections . Generic ;
4+ using System . Linq ;
5+
6+ using Community . OData . Linq . xTests . SampleData ;
7+ using Microsoft . OData ;
8+ using Xunit ;
9+
10+ public class ExpandTests
11+ {
12+ [ Fact ]
13+ public void EmptyExpand ( )
14+ {
15+ ISelectExpandWrapper [ ] result = ClassWithLink . CreateQuery ( ) . OData ( ) . SelectExpand ( ) . ToArray ( ) ;
16+
17+ IDictionary < string , object > metadata = result [ 0 ] . ToDictionary ( ) ;
18+
19+ // Not expanded by default except auto expand attribute
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 ) ;
32+ }
33+
34+ [ Fact ]
35+ public void ExpandLink ( )
36+ {
37+ ISelectExpandWrapper [ ] result = ClassWithLink . CreateQuery ( ) . OData ( ) . SelectExpand ( null , "Link1" ) . ToArray ( ) ;
38+
39+ IDictionary < string , object > metadata = result [ 0 ] . ToDictionary ( ) ;
40+
41+ // Not expanded by default
42+ Assert . Equal ( 3 , metadata . Count ) ;
43+
44+ Assert . Equal ( 6 , ( metadata [ "Link1" ] as ISelectExpandWrapper ) . ToDictionary ( ) . Count ) ;
45+ }
46+
47+ [ Fact ]
48+ public void ExpandSelect ( )
49+ {
50+ ISelectExpandWrapper [ ] result = ClassWithLink . CreateQuery ( ) . OData ( ) . SelectExpand ( "Name,Link1" , "Link1" ) . ToArray ( ) ;
51+
52+ IDictionary < string , object > metadata = result [ 0 ] . ToDictionary ( ) ;
53+
54+ // Not expanded by default
55+ Assert . Equal ( 2 , metadata . Count ) ;
56+ Assert . Equal ( 6 , ( metadata [ "Link1" ] as ISelectExpandWrapper ) . ToDictionary ( ) . Count ) ;
57+ }
58+
59+ [ Fact ]
60+ public void ExpandLinkSelect ( )
61+ {
62+ ISelectExpandWrapper [ ] result = ClassWithLink . CreateQuery ( ) . OData ( ) . SelectExpand ( "Name" , "Link1($select=Name)" ) . ToArray ( ) ;
63+
64+ IDictionary < string , object > metadata = result [ 0 ] . ToDictionary ( ) ;
65+
66+ // Not expanded by default
67+ Assert . Equal ( 2 , metadata . Count ) ;
68+ Assert . Equal ( 1 , ( metadata [ "Link1" ] as ISelectExpandWrapper ) . ToDictionary ( ) . Count ) ;
69+ }
70+
71+ [ Fact ]
72+ public void ExpandCollection ( )
73+ {
74+ ISelectExpandWrapper [ ] result = ClassWithCollection . CreateQuery ( ) . OData ( ) . SelectExpand ( "Name" , "Link2" ) . ToArray ( ) ;
75+
76+ IDictionary < string , object > metadata = result [ 0 ] . ToDictionary ( ) ;
77+
78+ // Not expanded by default
79+ Assert . Equal ( 2 , metadata . Count ) ;
80+ Assert . Equal ( 2 , ( metadata [ "Link2" ] as IEnumerable < ISelectExpandWrapper > ) . Count ( ) ) ;
81+ }
82+
83+ [ Fact ]
84+ public void ExpandCollectionWithTop ( )
85+ {
86+ ISelectExpandWrapper [ ] result = ClassWithCollection . CreateQuery ( ) . OData ( ) . SelectExpand ( "Name" , "Link2($top=1)" ) . ToArray ( ) ;
87+
88+ IDictionary < string , object > metadata = result [ 0 ] . ToDictionary ( ) ;
89+
90+ // Not expanded by default
91+ Assert . Equal ( 2 , metadata . Count ) ;
92+ Assert . Single ( ( metadata [ "Link2" ] as IEnumerable < ISelectExpandWrapper > ) ) ;
93+ }
94+
95+ [ Fact ]
96+ public void ExpandCollectionWithTopDefaultPageSize ( )
97+ {
98+ ISelectExpandWrapper [ ] result = ClassWithCollection . CreateQuery ( ) . OData ( s => s . QuerySettings . PageSize = 1 ) . SelectExpand ( "Name" , "Link2" ) . ToArray ( ) ;
99+
100+ IDictionary < string , object > metadata = result [ 0 ] . ToDictionary ( ) ;
101+
102+ // Not expanded by default
103+ Assert . Equal ( 2 , metadata . Count ) ;
104+ Assert . Single ( ( metadata [ "Link2" ] as IEnumerable < ISelectExpandWrapper > ) ) ;
105+ }
106+
107+ [ Fact ]
108+ public void ExpandCollectionWithTop21 ( )
109+ {
110+ ISelectExpandWrapper [ ] result = ClassWithCollection . CreateQuery ( ) . OData ( ) . SelectExpand ( "Name" , "Link2($top=21)" ) . ToArray ( ) ;
111+
112+ IDictionary < string , object > metadata = result [ 0 ] . ToDictionary ( ) ;
113+
114+ // Not expanded by default
115+ Assert . Equal ( 2 , metadata . Count ) ;
116+ Assert . Equal ( 2 , ( metadata [ "Link2" ] as IEnumerable < ISelectExpandWrapper > ) . Count ( ) ) ;
117+ }
118+
119+ [ Fact ]
120+ public void ExpandCollectionWithTopExceedLimit ( )
121+ {
122+ Assert . Throws < ODataException > (
123+ ( ) => ClassWithCollection . CreateQuery ( ) . OData ( ) . SelectExpand ( "Name" , "Link2($top=101)" ) ) ;
124+ }
125+
126+ [ Fact ]
127+ public void ExpandCollectionWithFilterAndSelect ( )
128+ {
129+ ISelectExpandWrapper [ ] result = ClassWithCollection . CreateQuery ( ) . OData ( ) . SelectExpand ( "Name" , "Link2($filter=Id eq 311;$select=Name)" ) . ToArray ( ) ;
130+
131+ IDictionary < string , object > metadata = result [ 0 ] . ToDictionary ( ) ;
132+
133+ // Not expanded by default
134+ Assert . Equal ( 2 , metadata . Count ) ;
135+ IEnumerable < ISelectExpandWrapper > collection = metadata [ "Link2" ] as IEnumerable < ISelectExpandWrapper > ;
136+ Assert . Single ( collection ) ;
137+
138+ Assert . Equal ( 1 , collection . Single ( ) . ToDictionary ( ) . Count ) ;
139+ }
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+ }
194+ }
195+ }
0 commit comments