From 9c2aff53d75813701ce30706cc6852113ad4d19b Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 31 Jan 2020 13:27:31 +0100 Subject: [PATCH] add tests for mixed operators expression; test precedence --- spec/query_generator_spec.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/spec/query_generator_spec.rb b/spec/query_generator_spec.rb index df1fb1c..05fb720 100644 --- a/spec/query_generator_spec.rb +++ b/spec/query_generator_spec.rb @@ -35,19 +35,19 @@ class SqlGeneratorTester it 'tests search without operators' do @query = TextToSqlQuery.new('123 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') - expect(@query.where_clause).to eq ['players.device_id LIKE ? OR players.device_id LIKE ?', '%123%', '%456%'] + expect(@query.where_clause).to eq ['(players.device_id LIKE ? OR players.device_id LIKE ?)', '%123%', '%456%'] end it 'tests search with OR operator' do @query = TextToSqlQuery.new('123 or 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') - expect(@query.where_clause).to eq ['players.device_id LIKE ? OR players.device_id LIKE ?', '%123%', '%456%'] + expect(@query.where_clause).to eq ['(players.device_id LIKE ? OR players.device_id LIKE ?)', '%123%', '%456%'] end it 'tests search with AND operator' do @query = TextToSqlQuery.new('123 and 456', [:'players.title', :'players.tag', :'players.device_id'], :'players.device_id') - expect(@query.where_clause).to eq ['players.device_id LIKE ? AND players.device_id LIKE ?', '%123%', '%456%'] + expect(@query.where_clause).to eq ['(players.device_id LIKE ? AND players.device_id LIKE ?)', '%123%', '%456%'] end it 'tests search with NOT operator on default column' do @@ -65,13 +65,25 @@ class SqlGeneratorTester 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') - expect(@query.where_clause).to eq ['players.title LIKE ? AND NOT players.tag LIKE ?', '%ab%', '%hf-1%'] + expect(@query.where_clause).to eq ['(players.title LIKE ? AND NOT players.tag LIKE ?)', '%ab%', '%hf-1%'] 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') - expect(@query.where_clause).to eq ['players.title LIKE ? AND players.title LIKE ? OR players.title LIKE ?', '%a%', '%b%', '%c%'] + expect(@query.where_clause).to eq ['((players.title LIKE ? AND players.title LIKE ?) OR players.title LIKE ?)', '%a%', '%b%', '%c%'] + 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') + + expect(@query.where_clause).to eq ['(players.title LIKE ? AND (players.title LIKE ? OR players.title LIKE ?))', '%a%', '%b%', '%c%'] + 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') + + expect(@query.where_clause).to eq ['(players.title LIKE ? AND NOT (players.title LIKE ? OR players.title LIKE ?))', '%a%', '%b%', '%c%'] end end end \ No newline at end of file