22using FluentAssertions ;
33using JetBrains . Annotations ;
44using JsonApiDotNetCore . Configuration ;
5+ using JsonApiDotNetCore . Queries ;
56using JsonApiDotNetCore . Queries . Expressions ;
67using JsonApiDotNetCore . Resources ;
78using JsonApiDotNetCore . Resources . Annotations ;
@@ -179,6 +180,104 @@ public void Expressions_can_accept_visitor(QueryExpression left, QueryExpression
179180 right . Accept ( EmptyQueryExpressionVisitor . Instance , null ) . Should ( ) . BeNull ( ) ;
180181 }
181182
183+ [ Fact ]
184+ public void Can_convert_QueryLayer_to_string ( )
185+ {
186+ // Arrange
187+ QueryLayer queryLayer = TestQueryLayerFactory . Instance . Default ( ) ;
188+
189+ // Act
190+ string text = queryLayer . ToString ( ) ;
191+
192+ // Assert
193+ text . Should ( ) . Be ( """
194+ QueryLayer<DerivedTestResource>
195+ {
196+ Include: parent.children
197+ Filter: and(contains(text,'example'),not(equals(text,'example')))
198+ Sort: -count(children)
199+ Pagination: Page number: 2, size: 5
200+ Selection
201+ {
202+ FieldSelectors<DerivedTestResource>
203+ {
204+ text
205+ parent: QueryLayer<DerivedTestResource>
206+ {
207+ Selection
208+ {
209+ FieldSelectors<DerivedTestResource>
210+ {
211+ text
212+ }
213+ }
214+ }
215+ children: QueryLayer<DerivedTestResource>
216+ {
217+ Selection
218+ {
219+ FieldSelectors<DerivedTestResource>
220+ {
221+ text
222+ }
223+ }
224+ }
225+ }
226+ }
227+ }
228+
229+ """ ) ;
230+ }
231+
232+ [ Fact ]
233+ public void Can_convert_QueryLayer_to_full_string ( )
234+ {
235+ // Arrange
236+ QueryLayer queryLayer = TestQueryLayerFactory . Instance . Default ( ) ;
237+
238+ // Act
239+ string text = queryLayer . ToFullString ( ) ;
240+
241+ // Assert
242+ text . Should ( ) . Be ( """
243+ QueryLayer<DerivedTestResource>
244+ {
245+ Include: baseTestResources:parent.baseTestResources:children
246+ Filter: and(contains(baseTestResources:text,'example'),not(equals(baseTestResources:text,'example')))
247+ Sort: -count(baseTestResources:children)
248+ Pagination: Page number: 2, size: 5
249+ Selection
250+ {
251+ FieldSelectors<DerivedTestResource>
252+ {
253+ derivedTestResources:text
254+ derivedTestResources:parent: QueryLayer<DerivedTestResource>
255+ {
256+ Selection
257+ {
258+ FieldSelectors<DerivedTestResource>
259+ {
260+ derivedTestResources:text
261+ }
262+ }
263+ }
264+ derivedTestResources:children: QueryLayer<DerivedTestResource>
265+ {
266+ Selection
267+ {
268+ FieldSelectors<DerivedTestResource>
269+ {
270+ derivedTestResources:text
271+ }
272+ }
273+ }
274+ }
275+ }
276+ }
277+
278+ """ ) ;
279+ }
280+
182281 [ UsedImplicitly ( ImplicitUseKindFlags . InstantiatedNoFixedConstructorSignature ) ]
183282 private class BaseTestResource : Identifiable < Guid >
184283 {
@@ -202,6 +301,7 @@ private sealed class TestExpressionFactory
202301 private readonly AttrAttribute _textAttribute ;
203302 private readonly RelationshipAttribute _parentRelationship ;
204303 private readonly RelationshipAttribute _childrenRelationship ;
304+
205305 public static TestExpressionFactory Instance { get ; } = new ( ) ;
206306
207307 private TestExpressionFactory ( )
@@ -262,7 +362,7 @@ public LiteralConstantExpression LiteralConstant()
262362
263363 public LogicalExpression Logical ( )
264364 {
265- return new LogicalExpression ( LogicalOperator . Or , Comparison ( ) , MatchText ( ) ) ;
365+ return new LogicalExpression ( LogicalOperator . And , MatchText ( ) , Not ( ) ) ;
266366 }
267367
268368 public MatchTextExpression MatchText ( )
@@ -364,4 +464,63 @@ private EmptyQueryExpressionVisitor()
364464 {
365465 }
366466 }
467+
468+ private sealed class TestQueryLayerFactory
469+ {
470+ public static TestQueryLayerFactory Instance { get ; } = new ( ) ;
471+
472+ private TestQueryLayerFactory ( )
473+ {
474+ }
475+
476+ public QueryLayer Default ( )
477+ {
478+ var options = new JsonApiOptions ( ) ;
479+
480+ var builder = new ResourceGraphBuilder ( options , NullLoggerFactory . Instance ) ;
481+ builder . Add < BaseTestResource , Guid > ( ) ;
482+ builder . Add < DerivedTestResource , Guid > ( ) ;
483+ IResourceGraph resourceGraph = builder . Build ( ) ;
484+
485+ ResourceType resourceType = resourceGraph . GetResourceType < DerivedTestResource > ( ) ;
486+ AttrAttribute textAttribute = resourceType . GetAttributeByPropertyName ( nameof ( DerivedTestResource . Text ) ) ;
487+ RelationshipAttribute parentRelationship = resourceType . GetRelationshipByPropertyName ( nameof ( DerivedTestResource . Parent ) ) ;
488+ RelationshipAttribute childrenRelationship = resourceType . GetRelationshipByPropertyName ( nameof ( DerivedTestResource . Children ) ) ;
489+
490+ return new QueryLayer ( resourceType )
491+ {
492+ Include = TestExpressionFactory . Instance . Include ( ) ,
493+ Filter = TestExpressionFactory . Instance . Logical ( ) ,
494+ Sort = TestExpressionFactory . Instance . Sort ( ) ,
495+ Pagination = TestExpressionFactory . Instance . Pagination ( ) ,
496+ Selection = new FieldSelection
497+ {
498+ [ resourceType ] = new FieldSelectors
499+ {
500+ [ textAttribute ] = null ,
501+ [ parentRelationship ] = new QueryLayer ( resourceType )
502+ {
503+ Selection = new FieldSelection
504+ {
505+ [ resourceType ] = new FieldSelectors
506+ {
507+ [ textAttribute ] = null
508+ }
509+ }
510+ } ,
511+ [ childrenRelationship ] = new QueryLayer ( resourceType )
512+ {
513+ Selection = new FieldSelection
514+ {
515+ [ resourceType ] = new FieldSelectors
516+ {
517+ [ textAttribute ] = null
518+ }
519+ }
520+ }
521+ }
522+ }
523+ } ;
524+ }
525+ }
367526}
0 commit comments