11namespace QueryKit . UnitTests ;
22
33using Bogus ;
4+ using Configuration ;
5+ using Exceptions ;
46using FluentAssertions ;
57using Operators ;
68using WebApiTestProject . Entities ;
9+ using WebApiTestProject . Entities . Recipes ;
710
811public class CustomFilterPropertyTests
912{
@@ -16,7 +19,7 @@ public void can_have_child_prop_name_ownsone()
1619 var filterExpression = FilterParser . ParseFilter < TestingPerson > ( input ) ;
1720 filterExpression . ToString ( ) . Should ( ) . Be ( $ """ x => (x.PhysicalAddress.State == "{ value } ")""" ) ;
1821 }
19-
22+
2023 [ Fact ]
2124 public void can_have_custom_child_prop_name_ownsone ( )
2225 {
@@ -72,6 +75,36 @@ public void can_have_custom_prop_name_for_string()
7275 filterExpression . ToString ( ) . Should ( ) . Be ( $ """ x => (x.Title == "{ value } ")""" ) ;
7376 }
7477
78+ [ Fact ]
79+ public void can_handle_alias_in_value ( )
80+ {
81+ var faker = new Faker ( ) ;
82+ var value = faker . Lorem . Word ( ) ;
83+ var input = $ """ special_title == "{ value } with special_value" """ ;
84+
85+ var config = new QueryKitConfiguration ( config =>
86+ {
87+ config . Property < TestingPerson > ( x => x . Title ) . HasQueryName ( "special_title" ) ;
88+ } ) ;
89+ var filterExpression = FilterParser . ParseFilter < TestingPerson > ( input , config ) ;
90+ filterExpression . ToString ( ) . Should ( ) . Be ( $ """ x => (x.Title == "{ value } with special_value")""" ) ;
91+ }
92+
93+ [ Fact ]
94+ public void can_handle_alias_in_value_with_operator_after_it ( )
95+ {
96+ var faker = new Faker ( ) ;
97+ var value = faker . Lorem . Word ( ) ;
98+ var input = $ """ special_title == "{ value } with special_value @=* a thing" """ ;
99+
100+ var config = new QueryKitConfiguration ( config =>
101+ {
102+ config . Property < TestingPerson > ( x => x . Title ) . HasQueryName ( "special_title" ) ;
103+ } ) ;
104+ var filterExpression = FilterParser . ParseFilter < TestingPerson > ( input , config ) ;
105+ filterExpression . ToString ( ) . Should ( ) . Be ( $ """ x => (x.Title == "{ value } with special_value @=* a thing")""" ) ;
106+ }
107+
75108 [ Fact ]
76109 public void can_have_custom_prop_name_for_multiple_props ( )
77110 {
@@ -169,4 +202,35 @@ public void filter_prevented_props_always_have_true_equals_true_regardless_of_co
169202 var filterExpression = FilterParser . ParseFilter < TestingPerson > ( input , config ) ;
170203 filterExpression . ToString ( ) . Should ( ) . Be ( $ """ x => (True == True)""" ) ;
171204 }
205+
206+ [ Fact ]
207+ public void can_throw_error_when_property_has_space ( )
208+ {
209+ var faker = new Faker ( ) ;
210+ var propertyName = faker . Lorem . Sentence ( ) ;
211+ var firstWord = propertyName . Split ( ' ' ) . First ( ) ;
212+ var input = $ """ { propertyName } == 25""" ;
213+
214+ var config = new QueryKitConfiguration ( config =>
215+ {
216+ config . Property < TestingPerson > ( x => x . Id ) . PreventFilter ( ) ;
217+ } ) ;
218+ var act = ( ) => FilterParser . ParseFilter < TestingPerson > ( input , config ) ;
219+ act . Should ( ) . Throw < UnknownFilterPropertyException > ( )
220+ . WithMessage ( $ "The filter property '{ firstWord } ' was not recognized.") ;
221+ }
222+
223+ [ Fact ]
224+ public void can_handle_nonexistent_property ( )
225+ {
226+ var faker = new Faker ( ) ;
227+ var input = $ """ { faker . Lorem . Word ( ) } == 25""" ;
228+
229+ var config = new QueryKitConfiguration ( config =>
230+ {
231+ config . AllowUnknownProperties = true ;
232+ } ) ;
233+ var filterExpression = FilterParser . ParseFilter < TestingPerson > ( input , config ) ;
234+ filterExpression . ToString ( ) . Should ( ) . Be ( "x => (True == True)" ) ;
235+ }
172236}
0 commit comments