testSearchKeys.py 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
#                     Ivan Tyagov <ivan@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import unittest
from DateTime import DateTime
31
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
32 33 34 35 36 37 38 39 40
from Products.ZSQLCatalog.SearchKey.DefaultKey import DefaultKey
from Products.ZSQLCatalog.SearchKey.RawKey import RawKey
from Products.ZSQLCatalog.SearchKey.KeyWordKey import KeyWordKey
from Products.ZSQLCatalog.SearchKey.DateTimeKey import DateTimeKey
from Products.ZSQLCatalog.SearchKey.FullTextKey import FullTextKey
from Products.ZSQLCatalog.SearchKey.FloatKey import FloatKey
from Products.ZSQLCatalog.SQLCatalog import getSearchKeyInstance
from Products.ZSQLCatalog.SearchKey.ScriptableKey import ScriptableKey, KeyMappingKey

41
class TestSearchKeyLexer(ERP5TypeTestCase):
42 43 44 45
  """Test search keys
  """
  run_all_test = 1
  quiet = 0
46
  
47 48 49
  def compare(self, search_key_class, search_value, expected_token_types):
    """ """
    key = getSearchKeyInstance(search_key_class)
50
    tokens = key.tokenize(search_value)  
51
    token_types = [x.type for x in tokens]
52
    self.assertSameSet(token_types, expected_token_types)  
53

54
  
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  def test_01ProperPoolInitialization(self, quiet=quiet, run=run_all_test):
    """ Check that search key pool is properly initialized """
    if not run: return
    for search_key_class in (DefaultKey, RawKey, KeyWordKey, DateTimeKey, 
                         FullTextKey, FloatKey, ScriptableKey, KeyMappingKey):
      self.assertTrue(isinstance(getSearchKeyInstance(search_key_class), search_key_class))

  def test_02DefaultKey(self, quiet=quiet, run=run_all_test):
    """ Check lexer for DefaultKey."""
    if not run: return
    self.compare(DefaultKey, 'John', ('WORD',))
    self.compare(DefaultKey, 'group/nexedi', ('WORD',))
    self.compare(DefaultKey, 'John OR Petar', 
                             ('WORD', 'OR', 'WORD',))    
    self.compare(DefaultKey, 'John AND Petar', 
                            ('WORD', 'AND', 'WORD',))   
    self.compare(DefaultKey, 'John AND Petar OR Petar', 
                             ('WORD', 'AND', 'WORD', 'OR', 'WORD'))
    # at end operator is considered as WORD
    self.compare(DefaultKey, 'John OR', 
                             ('WORD', 'WORD'))  
    self.compare(DefaultKey, '=John OR "WORD SET"', 
                             ('WORD', 'OR', 'WORDSET')) 
    # multiple lines are considered as one WORD
    multi_lines = """colour/apparel_model_module/1/1
morphology/apparel_model_module/1/5
size/Child/34"""                         
    self.compare(DefaultKey, multi_lines, ('WORD',))  
    # non ASCII chars support
    self.compare(DefaultKey, 
                'S\xc3\xa9bastien or !="Doe John1" and Doe', 
                ('WORD', 'OR', 'NOT', 'WORDSET', 'AND', 'WORD',))  
                             
  def test_03KeyWordKey(self, quiet=quiet, run=run_all_test):
    """ Check lexer for KeyWordKey."""
    if not run: return
    self.compare(KeyWordKey, 'John', ('WORD',))
    self.compare(KeyWordKey, '%John', ('KEYWORD',))   
    self.compare(KeyWordKey, '%John%', ('KEYWORD',))  
    self.compare(KeyWordKey, 'John%', ('KEYWORD',))  
    self.compare(KeyWordKey, 'John% OR JOHN', 
                            ('KEYWORD', 'OR', 'WORD',))
    self.compare(KeyWordKey, 'John% and "JOHN John"', 
                            ('KEYWORD', 'AND', 'WORDSET',))
    self.compare(KeyWordKey, '<John% and >"JOHN John"', 
                            ('LESSTHAN', 'KEYWORD', 'AND', 
                             'GREATERTHAN', 'WORDSET',)) 
    self.compare(KeyWordKey, '<=John% and >="JOHN John"', 
                            ('LESSTHANEQUAL', 'KEYWORD', 'AND', 
                             'GREATERTHANEQUAL', 'WORDSET',))                             
105 106
    self.compare(KeyWordKey, '=John% and >="JOHN John"', 
                            ('EXPLICITEQUALLITYWORD', 'KEYWORD', 'AND', 
107 108 109 110 111
                             'GREATERTHANEQUAL', 'WORDSET',))
                             
  def test_04DateTimeKey(self, quiet=quiet, run=run_all_test):
    """ Check lexer for DateTimeKey."""
    if not run: return
112 113
    self.compare(DateTimeKey, '2007.12.23', ('DATE',))     
    self.compare(DateTimeKey, 
114 115
                 '=2007.12.23 22:00:00 Universal or =23/12/2007 10:10 and !=2009-12-12',
                 ('EQUAL', 'DATE', 'OR', 'EQUAL', 'DATE', 'AND', 'NOT', 'DATE',))   
116
    self.compare(DateTimeKey, 
117 118 119
                 '>=2007.12.23 22:00:00 GMT+02 or <=23/12/2007 and >2009/12/12 and <2009-11-11',
                 ('GREATERTHANEQUAL', 'DATE', 'OR', 'LESSTHANEQUAL', 'DATE', 
                   'AND', 'GREATERTHAN', 'DATE', 'AND', 'LESSTHAN', 'DATE'))
120
                     
121
  def test_05FullTextKey(self, quiet=quiet, run=run_all_test):
122
    """ Check lexer for FullTextKey."""  
123
    if not run: return
124 125 126 127
    self.compare(FullTextKey, 'John Doe', 
                 ('WORD', 'WORD',)) 
    self.compare(FullTextKey, '+John -Doe', 
                 ('PLUS', 'WORD', 'MINUS', 'WORD',)) 
128 129 130 131 132 133 134 135 136 137 138

  def test_06ScriptableKey(self, quiet=quiet, run=run_all_test):
    """ Check lexer for ScriptableKey."""  
    if not run: return
    self.compare(ScriptableKey, 
                'John Doe AND portal_type:Person OR creation_date>=2005/12/12',
                ('WORD', 'WORD', 'AND', 'KEYMAPPING', 'OR', 'KEYMAPPING',))
    self.compare(ScriptableKey, 
                'John Doe OR creation_date>=2005/12/12',
                ('WORD', 'WORD', 'OR', 'KEYMAPPING',))                
                   
139
class TestSearchKeyQuery(ERP5TypeTestCase):
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  """Test search keys query generation
  """
  run_all_test = 1
  quiet = 0

  def compare(self, search_key_class, 
              key, value, expected_where_expression, expected_select_expression, format=None):
    """ compare generated SQL (as string)"""
    search_key_instance = getSearchKeyInstance(search_key_class)
    where_expression, select_expressions = \
       search_key_instance.buildSQLExpression(key, value, format)
    self.assertEqual(expected_where_expression, where_expression)
    self.assertEqual(expected_select_expression, select_expressions)

  def test_01DefaultKey(self, quiet=quiet, run=run_all_test):
    """ Check DefaultKey query generation"""
    if not run: return    
    self.compare(DefaultKey,
                'title',
                'John Doe', 
                "((((title = 'John Doe'))))",
                [])
    self.compare(DefaultKey,
                'title',
                '<John Doe', 
                "((((title < 'John Doe'))))",
                []) 
    self.compare(DefaultKey,
                'title',
                '>John Doe', 
                "((((title > 'John Doe'))))",
                [])
    self.compare(DefaultKey,
                'title',
                '>=John Doe or <=Doe John', 
                "((((title >= 'John Doe'))) OR (((title <= 'Doe John'))))",
                [])
    self.compare(DefaultKey,
                'title',
                '=John Doe and Doe John', 
                "((((title = 'John Doe') AND (title = 'Doe John'))))",
                [])
    self.compare(DefaultKey,
                'title',
                '!=John Doe or !=Doe John', 
                "((((title != 'John Doe'))) OR (((title != 'Doe John'))))",
                [])                  
    self.compare(DefaultKey,
                'title',
                '"John Doe" or "Doe John"', 
                "((((title = 'John Doe'))) OR (((title = 'Doe John'))))",
                []) 
    self.compare(DefaultKey,
                'title',
                '!="John Doe" or !="Doe John"', 
                "((((title != 'John Doe'))) OR (((title != 'Doe John'))))",
                [])
    self.compare(DefaultKey,
                'title',
                '%John and !=Doe%', 
                "((((title = '%John') AND (title != 'Doe%'))))",
                [])
                
  def test_02KeyWordKey(self, quiet=quiet, run=run_all_test):
    """ Check DefaultKey query generation"""
    if not run: return    
    self.compare(KeyWordKey,
                'title',
                'John Doe', 
                "((((title LIKE '%John Doe%'))))",
                [])
    self.compare(KeyWordKey,
                'title',
                '!=John Doe', 
                "((((title NOT LIKE 'John Doe'))))",
                [])       
    self.compare(KeyWordKey,
                'title',
                '%John Doe', 
                "((((title LIKE '%John Doe'))))",
                [])
    self.compare(KeyWordKey,
                'title',
                '%John Doe%', 
                "((((title LIKE '%John Doe%'))))",
                [])
    self.compare(KeyWordKey,
                'title',
                '%John Doe% or =Doe John', 
                "((((title LIKE '%John Doe%'))) OR (((title = 'Doe John'))))",
                [])       
                
  def test_03DateTimeKey(self, quiet=quiet, run=run_all_test):
    """ Check DefaultKey query generation"""
    if not run: return 
    now = DateTime()
    sql_quoted = now.toZone('UTC').ISO()
    self.compare(DateTimeKey,
                'delivery.start_date',
                '<%s' %now, 
                "((((delivery.start_date < '%s'))))" %sql_quoted,
                [])
    self.compare(DateTimeKey,
                'delivery.start_date',
                '>=%s' %now, 
                "((((delivery.start_date >= '%s'))))" %sql_quoted,
                [])
    self.compare(DateTimeKey,
                'delivery.start_date',
                '>=%s and <=%s' %(now, now), 
                "((((delivery.start_date >= '%s') AND (delivery.start_date <= '%s'))))"  %(sql_quoted, sql_quoted),
                [])
    # if only date is given assume range within the same day (24 hours)
    self.compare(DateTimeKey,
                'delivery.start_date',
                '%s' %now, 
                "((((delivery.start_date >= '%s' AND delivery.start_date < '%s'))))"  \
                  %(now.toZone('UTC').ISO(), (now +1).toZone('UTC').ISO()),
                [])   
    # %d/%m/%Y
    date_value = '11/01/2008'
    self.compare(DateTimeKey,
                'delivery.start_date',
                '<%s' %date_value, 
                "((((delivery.start_date < '%s'))))" %DateTime(date_value, datefmt="international").toZone('UTC').ISO(),
                [],
                '%d/%m/%Y')
    # %Y/%m/%d
    date_value = '2008/01/11'
    self.compare(DateTimeKey,
                'delivery.start_date',
                '>%s' %date_value, 
                "((((delivery.start_date > '%s'))))" %DateTime(date_value, datefmt="international").toZone('UTC').ISO(),
                [],
                '%Y/%m/%d')
    # %m/%d/%Y (US style)
    date_value = '01/11/2008'
    self.compare(DateTimeKey,
                'delivery.start_date',
                '>=%s' %date_value, 
                "((((delivery.start_date >= '%s'))))" %DateTime(date_value).toZone('UTC').ISO(),
                [],
                '%m/%d/%Y')
    # give only year value, should generate a range catching all dates
    # for respective year (converted to UTC)
    date_value = '2008'
    start_utc = DateTime('2008/01/01')
    self.compare(DateTimeKey,
                'delivery.start_date',
                '%s' %date_value, 
                "((((delivery.start_date >= '%s' AND delivery.start_date < '%s'))))" \
                  %(start_utc.toZone('UTC').ISO(), (start_utc + 366).toZone('UTC').ISO()),
                [])
    # same as above use case but adding '%' like operator
    date_value = '2118%'
    start_utc = DateTime('2118/01/01')
    self.compare(DateTimeKey,
                'delivery.start_date',
                '%s' %date_value, 
                "((((delivery.start_date >= '%s' AND delivery.start_date < '%s'))))" \
                  %(start_utc.toZone('UTC').ISO(), (start_utc + 366).toZone('UTC').ISO()),
                [])
                       
  
  def test_04FloatKey(self, quiet=quiet, run=run_all_test):
    """ Check FloatKey query generation"""
    if not run: return  
    self.compare(FloatKey, 
                'delivery.stock',
                '>10.123456', 
                "((((delivery.stock > '10.123456'))))",
                [])
    self.compare(FloatKey, 
                'delivery.stock',
                '>=10.123456', 
                "((((delivery.stock >= '10.123456'))))",
                [],) 
    self.compare(FloatKey, 
                'delivery.stock',
                '<10.123456', 
                "((((delivery.stock < '10.123456'))))",
                [])   
    self.compare(FloatKey, 
                'delivery.stock',
                '<=10.123456', 
                "((((delivery.stock <= '10.123456'))))",
                [])   
    self.compare(FloatKey, 
                'delivery.stock',
                '>=7.10 and <=10.123456', 
                "((((delivery.stock >= '7.10') AND (delivery.stock <= '10.123456'))))",
                [])
    self.compare(FloatKey, 
                'delivery.stock',
                '>7.111 or >=10.66744', 
                "((((delivery.stock > '7.111'))) OR (((delivery.stock >= '10.66744'))))",
                [])
    self.compare(FloatKey, 
                'delivery.stock',
                '>7.111 or >=10.66744', 
                "((((TRUNCATE(delivery.stock,4) > '7.111'))) OR (((TRUNCATE(delivery.stock,4) >= '10.66744'))))",
                [],
                '11.1111')                 
                
  def test_05RawKey(self, quiet=quiet, run=run_all_test):
    """ Check FullTextKey query generation"""
    if not run: return  
    self.compare(RawKey, 
                'delivery.stock',
                '1ab521ty', 
                "delivery.stock = '1ab521ty'",
                [])    
352
                
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  def test_06FullTextKey(self, quiet=quiet, run=run_all_test):
    """ Check FullTextKey query generation"""
    if not run: return
    self.compare(FullTextKey, 
                'full_text.SearchableText',
                'john', 
                "MATCH full_text.SearchableText AGAINST ('john' )",
                ["MATCH full_text.SearchableText AGAINST ('john' ) AS full_text_SearchableText_relevance", 
                "MATCH full_text.SearchableText AGAINST ('john' ) AS SearchableText_relevance"])    
    
  def test_07ScriptableKey(self, quiet=quiet, run=run_all_test):
    """ Check ScriptableKey query generation"""
    if not run: return    
    self.compare(ScriptableKey, 
                '',
                'John Doe', 
                "((((((((((SearchableText = 'John Doe'))))) OR (((((reference = 'John Doe'))))) OR (((((title = 'John Doe'))))))))))",
                [])
    
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestSearchKeyLexer))
  suite.addTest(unittest.makeSuite(TestSearchKeyQuery))  
  return suite