@@ -490,32 +490,32 @@ def validate_where_clause(where_clause, expected_tokens):
490490 assert len (where_clause .tokens ) == len (expected_tokens )
491491 for where_token , expected_token in zip (where_clause , expected_tokens ):
492492 expected_ttype , expected_value = expected_token
493- assert where_token .match (expected_ttype , expected_value )
494-
495- [p1 ] = sqlparse .parse ("select * from mytable where column LIKE 'expr%' limit 5;" )
493+ if where_token .ttype is not None :
494+ assert where_token .match (expected_ttype , expected_value , regex = True )
495+ else :
496+ # Certain tokens, such as comparison tokens, do not define a ttype that can be
497+ # matched against. For these tokens, we ensure that the token instance is of
498+ # the expected type and has a value conforming to specified regular expression
499+ import re
500+ assert (isinstance (where_token , expected_ttype )
501+ and re .match (expected_value , where_token .value ))
502+
503+ [p1 ] = sqlparse .parse ("select * from mytable where mytable.mycolumn LIKE 'expr%' limit 5;" )
496504 [p1_where ] = [token for token in p1 if isinstance (token , sql .Where )]
497505 validate_where_clause (p1_where , [
498506 (T .Keyword , "where" ),
499507 (T .Whitespace , None ),
500- (T .Keyword , "column" ),
501- (T .Whitespace , None ),
502- (T .Comparison , "LIKE" ),
503- (T .Whitespace , None ),
504- (T .String .Single , "'expr%'" ),
508+ (sql .Comparison , r"mytable.mycolumn LIKE.*" ),
505509 (T .Whitespace , None ),
506510 ])
507511
508512 [p2 ] = sqlparse .parse (
509- "select * from mytable where mycol NOT ILIKE '-expr' group by mytable. othercolumn" )
513+ "select * from mytable where mycolumn NOT ILIKE '-expr' group by othercolumn; " )
510514 [p2_where ] = [token for token in p2 if isinstance (token , sql .Where )]
511515 validate_where_clause (p2_where , [
512516 (T .Keyword , "where" ),
513517 (T .Whitespace , None ),
514- (T .Keyword , "mycol" ),
515- (T .Whitespace , None ),
516- (T .Comparison , "NOT ILIKE" ),
517- (T .Whitespace , None ),
518- (T .String .Single , "'-expr'" ),
518+ (sql .Comparison , r"mycolumn NOT ILIKE.*" ),
519519 (T .Whitespace , None ),
520520 ])
521521
0 commit comments