2020-01-31 11:45:59 +01:00
require './text_to_sql_query'
class SqlGeneratorTester
describe 'Testing query generator' do
it 'tests simple search term without column name and without quotes' do
@query = TextToSqlQuery . new ( '-123' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%-123%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests simple search term with column name and without quotes' do
@query = TextToSqlQuery . new ( 'title:ab' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ 'players.title ILIKE ?' , '%ab%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests simple search term with unknown column name and without quotes' do
@query = TextToSqlQuery . new ( 'unknown:ab' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 17:44:12 +01:00
expect { @query . where_clause } . to raise_error ( RuntimeError , " Unknown field 'unknown' " )
2020-01-31 11:45:59 +01:00
end
it 'tests simple search term without column name and with quotes' do
2020-02-10 14:12:21 +01:00
@query = TextToSqlQuery . new ( '"ab"' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 11:45:59 +01:00
2020-01-31 16:11:12 +01:00
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%ab%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests simple search term with column name and with quotes' do
@query = TextToSqlQuery . new ( 'tag:"ab"' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 16:11:12 +01:00
expect ( @query . where_clause ) . to eq [ 'players.tag ILIKE ?' , '%ab%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests search without operators' do
@query = TextToSqlQuery . new ( '123 456' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ '(players.device_id ILIKE ? OR players.device_id ILIKE ?)' , '%123%' , '%456%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests search with OR operator' do
@query = TextToSqlQuery . new ( '123 or 456' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ '(players.device_id ILIKE ? OR players.device_id ILIKE ?)' , '%123%' , '%456%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests search with AND operator' do
@query = TextToSqlQuery . new ( '123 and 456' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ '(players.device_id ILIKE ? AND players.device_id ILIKE ?)' , '%123%' , '%456%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests search with NOT operator on default column' do
@query = TextToSqlQuery . new ( 'not 23' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ 'NOT players.device_id ILIKE ?' , '%23%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests search with NOT operator on non-default column' do
@query = TextToSqlQuery . new ( 'not tag:23' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ 'NOT players.tag ILIKE ?' , '%23%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests search with mixed logical operators' do
@query = TextToSqlQuery . new ( 'title:ab and not tag:hf-1' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ '(players.title ILIKE ? AND NOT players.tag ILIKE ?)' , '%ab%' , '%hf-1%' ]
2020-01-31 11:45:59 +01:00
end
it 'tests search with mixed logical operators without NOT' do
@query = TextToSqlQuery . new ( 'title:a and title:b or title:c' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ '((players.title ILIKE ? AND players.title ILIKE ?) OR players.title ILIKE ?)' , '%a%' , '%b%' , '%c%' ]
2020-01-31 13:27:31 +01:00
end
it 'tests search with brackets in expression' do
@query = TextToSqlQuery . new ( 'title:a and (title:b or title:c)' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ '(players.title ILIKE ? AND (players.title ILIKE ? OR players.title ILIKE ?))' , '%a%' , '%b%' , '%c%' ]
2020-01-31 13:27:31 +01:00
end
it 'tests search with brackets in expression and with NOT operator' do
@query = TextToSqlQuery . new ( 'title:a and not (title:b or title:c)' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 15:10:48 +01:00
expect ( @query . where_clause ) . to eq [ '(players.title ILIKE ? AND NOT (players.title ILIKE ? OR players.title ILIKE ?))' , '%a%' , '%b%' , '%c%' ]
2020-01-31 11:45:59 +01:00
end
2020-01-31 13:58:09 +01:00
it 'tests search with special characters in search term' do
@query = TextToSqlQuery . new ( 'title:"%a_\"' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
2020-01-31 16:11:12 +01:00
expect ( @query . where_clause ) . to eq [ 'players.title ILIKE ?' , '%\%a\_\\%' ]
end
it 'tests search with field mappings' do
@query = TextToSqlQuery . new ( 'tags:h1-r' , [ :'players.title' , :'players.name' , :'players.device_id' ] , :'players.device_id' , { tags : " tags.name " } )
expect ( @query . where_clause ) . to eq [ 'tags.name ILIKE ?' , '%h1-r%' ]
2020-01-31 13:58:09 +01:00
end
2020-01-31 17:44:12 +01:00
2020-01-31 19:23:10 +01:00
it 'tests search with field mappings when fields array has same mapping' do
@query = TextToSqlQuery . new ( 'tags:hs1-r' , [ :'players.title' , :'players.tags' , :'players.device_id' ] , :'players.device_id' , { tags : " tags.name " } )
expect ( @query . where_clause ) . to eq [ 'tags.name ILIKE ?' , '%hs1-r%' ]
end
2020-01-31 17:44:12 +01:00
it 'tests complex query' do
text = '(device_id:"with space" tags:mta no-quotes-id-123)' \
'or "id with quotes-5" and ( ("id with q 10" or "id with q 20")' \
'and ("id with Q 30" "id with Q 40") and not id-without-Q-50)'
fields = [ :'players.title' , :'players.name' , :'players.device_id' ]
default_field = :'players.device_id'
fields_mappings = { tags : 'tags.name' }
@query = TextToSqlQuery . new ( text , fields , default_field , fields_mappings )
result_expression = '((players.device_id ILIKE ? OR (tags.name ILIKE ? OR players.device_id ILIKE ?)) OR (players.device_id ILIKE ? AND (((players.device_id ILIKE ? OR players.device_id ILIKE ?) AND (players.device_id ILIKE ? OR players.device_id ILIKE ?)) AND NOT players.device_id ILIKE ?)))'
result_params = [ '%with space%' ,
'%mta%' ,
'%no-quotes-id-123%' ,
'%id with quotes-5%' ,
'%id with q 10%' ,
'%id with q 20%' ,
'%id with Q 30%' ,
'%id with Q 40%' ,
'%id-without-Q-50%' ]
expect ( @query . where_clause ) . to eq [ result_expression , result_params ] . flatten
end
2020-02-10 14:12:21 +01:00
it 'tests query without column name containing AND word inside without quotes' do
@query = TextToSqlQuery . new ( 'land' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%land%' ]
end
it 'tests query without column name containing AND word inside with quotes' do
@query = TextToSqlQuery . new ( '"land"' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%land%' ]
end
it 'tests query without column name and with word starting with AND without quotes' do
@query = TextToSqlQuery . new ( 'andromeda' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%andromeda%' ]
end
it 'tests query without column name and with word starting with AND with quotes' do
@query = TextToSqlQuery . new ( '"andromeda"' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%andromeda%' ]
end
it 'tests query with column name containing AND word inside' do
@query = TextToSqlQuery . new ( 'land:some-search-term' , [ :'players.title' , :'players.tag' , :'players.device_id' , :'players.land' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.land ILIKE ?' , '%some-search-term%' ]
end
it 'tests query with column name starting with AND' do
@query = TextToSqlQuery . new ( 'andromeda:some-search-term' , [ :'players.title' , :'players.tag' , :'players.device_id' , :'players.andromeda' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.andromeda ILIKE ?' , '%some-search-term%' ]
end
it 'tests query with search term without quotes containing AND word inside' do
@query = TextToSqlQuery . new ( 'title:land' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.title ILIKE ?' , '%land%' ]
end
it 'tests query with search term with quotes containing AND word inside' do
@query = TextToSqlQuery . new ( 'title:"land"' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.title ILIKE ?' , '%land%' ]
end
it 'tests query with search term without quotes and search term starting with AND without quotes' do
@query = TextToSqlQuery . new ( 'title:andromeda' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.title ILIKE ?' , '%andromeda%' ]
end
it 'tests query with search term without quotes and search term starting with AND with quotes' do
@query = TextToSqlQuery . new ( 'title:"andrew"' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.title ILIKE ?' , '%andrew%' ]
end
it 'tests query with search term in brackets with AND inside word' do
@query = TextToSqlQuery . new ( '(land)' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%land%' ]
end
it 'tests query with search term in brackets starting with AND' do
@query = TextToSqlQuery . new ( '(andromeda)' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%andromeda%' ]
end
it 'tests query with multiple search terms with mixed and-or-not after dash and underscore' do
@query = TextToSqlQuery . new ( '123-and-456 -or-2 -not_not_1' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ '(players.device_id ILIKE ? OR (players.device_id ILIKE ? OR players.device_id ILIKE ?))' , '%123-and-456%' , '%-or-2%' , '%-not\_not\_1%' ]
end
it 'tests query with search term in brackets with AND inside word with quotes' do
@query = TextToSqlQuery . new ( '("land")' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%land%' ]
end
it 'tests query with search term in brackets starting with AND with quotes' do
@query = TextToSqlQuery . new ( '("andromeda")' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ 'players.device_id ILIKE ?' , '%andromeda%' ]
end
it 'tests query with multiple search terms starting with AND' do
@query = TextToSqlQuery . new ( 'land andrew and andromeda or orlando' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ '(players.device_id ILIKE ? OR ((players.device_id ILIKE ? AND players.device_id ILIKE ?) OR players.device_id ILIKE ?))' , '%land%' , '%andrew%' , '%andromeda%' , '%orlando%' ]
end
it 'tests query with multiple search terms with dash and underscore chars' do
@query = TextToSqlQuery . new ( 'land 123-andrew or -andrew- and not _ornela_ or ornela' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ '(players.device_id ILIKE ? OR ((players.device_id ILIKE ? OR (players.device_id ILIKE ? AND NOT players.device_id ILIKE ?)) OR players.device_id ILIKE ?))' , '%land%' , '%123-andrew%' , '%-andrew-%' , '%\_ornela\_%' , '%ornela%' ]
end
it 'tests query with multiple search terms with dash and underscore chars under quotes' do
@query = TextToSqlQuery . new ( '"land" "123-andrew" or "-andrew-"' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ '(players.device_id ILIKE ? OR (players.device_id ILIKE ? OR players.device_id ILIKE ?))' , '%land%' , '%123-andrew%' , '%-andrew-%' ]
end
it 'tests query with multiple search terms with mixed and-or-not on start' do
@query = TextToSqlQuery . new ( 'sindy andora ornoty notary' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ '(players.device_id ILIKE ? OR (players.device_id ILIKE ? OR (players.device_id ILIKE ? OR players.device_id ILIKE ?)))' , '%sindy%' , '%andora%' , '%ornoty%' , '%notary%' ]
end
it 'tests query with multiple search terms with mixed and-or-not on start and with logical operators between' do
@query = TextToSqlQuery . new ( 'sindy and andora-notary or not ornoty notary' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ '((players.device_id ILIKE ? AND players.device_id ILIKE ?) OR NOT (players.device_id ILIKE ? OR players.device_id ILIKE ?))' , '%sindy%' , '%andora-notary%' , '%ornoty%' , '%notary%' ]
end
it 'tests query with multiple search terms with mixed and-or-not on start and with logical operators between' do
@query = TextToSqlQuery . new ( '(sindy or andora-notary)and not(ornoty notary)' , [ :'players.title' , :'players.tag' , :'players.device_id' ] , :'players.device_id' )
expect ( @query . where_clause ) . to eq [ '((players.device_id ILIKE ? OR players.device_id ILIKE ?) AND NOT (players.device_id ILIKE ? OR players.device_id ILIKE ?))' , '%sindy%' , '%andora-notary%' , '%ornoty%' , '%notary%' ]
end
2020-01-31 11:45:59 +01:00
end
end