@@ -14,7 +14,7 @@ public void WhenQueryKeywordWithWhereAndContains_ThenCorrectArticles()
1414 {
1515 List < Article > articles = _fixture . blogDbContext ! . Articles . Where ( x => x . Title . Contains ( "n" ) ) . ToList ( ) ;
1616
17- Assert . True ( articles . Count == 3 ) ;
17+ Assert . Equal ( 3 , articles . Count ) ;
1818 Assert . Contains ( _fixture . startsWithN , articles . Select ( x => x . Title ) ) ;
1919 Assert . Contains ( _fixture . containsN , articles . Select ( x => x . Title ) ) ;
2020 Assert . Contains ( _fixture . endsWithN , articles . Select ( x => x . Title ) ) ;
@@ -42,7 +42,7 @@ public void WhenQueryKeywordWithWhereAndStartsWith_ThenCorrectArticles()
4242 {
4343 List < Article > articles = _fixture . blogDbContext ! . Articles . Where ( x => x . Title . StartsWith ( "n" ) ) . ToList ( ) ;
4444
45- Assert . True ( articles . Count == 1 ) ;
45+ Assert . Single ( articles ) ;
4646 Assert . Contains ( _fixture . startsWithN , articles . Select ( x => x . Title ) ) ;
4747 Assert . DoesNotContain ( _fixture . containsN , articles . Select ( x => x . Title ) ) ;
4848 Assert . DoesNotContain ( _fixture . endsWithN , articles . Select ( x => x . Title ) ) ;
@@ -54,7 +54,7 @@ public void WhenQueryKeywordWithWhereAndEndsWith_ThenCorrectArticles()
5454 {
5555 List < Article > articles = _fixture . blogDbContext ! . Articles . Where ( x => x . Title . EndsWith ( "n" ) ) . ToList ( ) ;
5656
57- Assert . True ( articles . Count == 1 ) ;
57+ Assert . Single ( articles ) ;
5858 Assert . DoesNotContain ( _fixture . startsWithN , articles . Select ( x => x . Title ) ) ;
5959 Assert . DoesNotContain ( _fixture . containsN , articles . Select ( x => x . Title ) ) ;
6060 Assert . Contains ( _fixture . endsWithN , articles . Select ( x => x . Title ) ) ;
@@ -66,7 +66,7 @@ public void WhenUsingWildcardsWithWhereAndStartsWith_ThenWildcardDoesNotWork()
6666 {
6767 List < Article > articles = _fixture . blogDbContext ! . Articles . Where ( x => x . Title . StartsWith ( "_n" ) ) . ToList ( ) ;
6868
69- Assert . True ( articles . Count == 0 ) ;
69+ Assert . Empty ( articles ) ;
7070 Assert . DoesNotContain ( _fixture . startsWithN , articles . Select ( x => x . Title ) ) ;
7171 Assert . DoesNotContain ( _fixture . containsN , articles . Select ( x => x . Title ) ) ;
7272 Assert . DoesNotContain ( _fixture . endsWithN , articles . Select ( x => x . Title ) ) ;
@@ -78,10 +78,47 @@ public void WhenUsingWildcardsWithWhereAndEFFunctionLike_ThenCorrectArticles()
7878 {
7979 List < Article > articles = _fixture . blogDbContext ! . Articles . Where ( x => EF . Functions . Like ( x . Title , "_n%" ) ) . ToList ( ) ;
8080
81- Assert . True ( articles . Count == 1 ) ;
81+ Assert . Single ( articles ) ;
8282 Assert . DoesNotContain ( _fixture . startsWithN , articles . Select ( x => x . Title ) ) ;
8383 Assert . Contains ( _fixture . containsN , articles . Select ( x => x . Title ) ) ;
8484 Assert . DoesNotContain ( _fixture . endsWithN , articles . Select ( x => x . Title ) ) ;
8585 Assert . DoesNotContain ( _fixture . notContainsN , articles . Select ( x => x . Title ) ) ;
8686 }
87+
88+ [ Fact ]
89+ public void WhenSearchTermHasWildcardAndContains_ThenWildcardIsLiteral ( )
90+ {
91+ string searchTerm = "50%" ;
92+ List < Article > articles = _fixture . blogDbContext ! . Articles . Where ( x => x . Title . Contains ( searchTerm ) ) . ToList ( ) ;
93+
94+ Assert . Single ( articles ) ;
95+ Assert . Contains ( _fixture . percentTitle , articles . Select ( x => x . Title ) ) ;
96+ }
97+
98+ [ Fact ]
99+ public void WhenSearchTermHasWildcardAndEFFunctionLike_ThenWildcardMatchesEverything ( )
100+ {
101+ string pattern = "50%%" ;
102+ List < Article > articles = _fixture . blogDbContext ! . Articles . Where ( x => EF . Functions . Like ( x . Title , pattern ) ) . ToList ( ) ;
103+
104+ Assert . Equal ( 2 , articles . Count ) ;
105+ }
106+
107+ [ Fact ]
108+ public void WhenSearchTermIsEscapedAndEFFunctionLike_ThenWildcardIsLiteral ( )
109+ {
110+ string safePattern = $ "{ LikePattern . Escape ( "50%" ) } %";
111+ List < Article > articles = _fixture . blogDbContext ! . Articles . Where ( x => EF . Functions . Like ( x . Title , safePattern , @"\" ) ) . ToList ( ) ;
112+
113+ Assert . Single ( articles ) ;
114+ Assert . Contains ( _fixture . percentTitle , articles . Select ( x => x . Title ) ) ;
115+ }
116+
117+ [ Fact ]
118+ public void WhenEFFunctionLikeRunsOnAList_ThenItThrows ( )
119+ {
120+ List < string > titles = [ "nop" , "mnp" ] ;
121+
122+ Assert . Throws < InvalidOperationException > ( ( ) => titles . Where ( t => EF . Functions . Like ( t , "_n%" ) ) . ToList ( ) ) ;
123+ }
87124}
0 commit comments