<s> """<STR_LIT>""" <EOL> from django . contrib . postgres . search import ( <EOL> SearchQuery , SearchRank , SearchVector , <EOL> ) <EOL> from django . db . models import F <EOL> from django . test import modify_settings <EOL> from . import PostgreSQLTestCase <EOL> from . models import Character , Line , Scene <EOL> class GrailTestData ( object ) : <EOL> @ classmethod <EOL> def setUpTestData ( cls ) : <EOL> cls . robin = Scene . objects . create ( scene = '<STR_LIT>' , setting = '<STR_LIT>' ) <EOL> cls . minstrel = Character . objects . create ( name = '<STR_LIT>' ) <EOL> verses = [ <EOL> ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> ] <EOL> cls . verses = [ Line . objects . create ( <EOL> scene = cls . robin , <EOL> character = cls . minstrel , <EOL> dialogue = verse , <EOL> ) for verse in verses ] <EOL> cls . verse0 , cls . verse1 , cls . verse2 = cls . verses <EOL> cls . witch_scene = Scene . objects . create ( scene = '<STR_LIT>' , setting = "<STR_LIT>" ) <EOL> bedemir = Character . objects . create ( name = '<STR_LIT>' ) <EOL> crowd = Character . objects . create ( name = '<STR_LIT>' ) <EOL> witch = Character . objects . create ( name = '<STR_LIT>' ) <EOL> duck = Character . objects . create ( name = '<STR_LIT>' ) <EOL> cls . bedemir0 = Line . objects . create ( <EOL> scene = cls . witch_scene , <EOL> character = bedemir , <EOL> dialogue = '<STR_LIT>' , <EOL> dialogue_config = '<STR_LIT>' , <EOL> ) <EOL> cls . bedemir1 = Line . objects . create ( <EOL> scene = cls . witch_scene , <EOL> character = bedemir , <EOL> dialogue = '<STR_LIT>' , <EOL> dialogue_config = '<STR_LIT>' , <EOL> ) <EOL> cls . duck = Line . objects . create ( scene = cls . witch_scene , character = duck , dialogue = None ) <EOL> cls . crowd = Line . objects . create ( scene = cls . witch_scene , character = crowd , dialogue = '<STR_LIT>' ) <EOL> cls . witch = Line . objects . create ( scene = cls . witch_scene , character = witch , dialogue = "<STR_LIT>" ) <EOL> trojan_rabbit = Scene . objects . create ( scene = '<STR_LIT>' , setting = "<STR_LIT>" ) <EOL> guards = Character . objects . create ( name = '<STR_LIT>' ) <EOL> cls . french = Line . objects . create ( <EOL> scene = trojan_rabbit , <EOL> character = guards , <EOL> dialogue = '<STR_LIT>' , <EOL> dialogue_config = '<STR_LIT>' , <EOL> ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> class SimpleSearchTest ( GrailTestData , PostgreSQLTestCase ) : <EOL> def test_simple ( self ) : <EOL> searched = Line . objects . filter ( dialogue__search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . verse1 ] ) <EOL> def test_non_exact_match ( self ) : <EOL> searched = Line . objects . filter ( dialogue__search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . verse2 ] ) <EOL> def test_search_two_terms ( self ) : <EOL> searched = Line . objects . filter ( dialogue__search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . verse2 ] ) <EOL> def test_search_two_terms_with_partial_match ( self ) : <EOL> searched = Line . objects . filter ( dialogue__search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . verse0 ] ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> class SearchVectorFieldTest ( GrailTestData , PostgreSQLTestCase ) : <EOL> def test_existing_vector ( self ) : <EOL> Line . objects . update ( dialogue_search_vector = SearchVector ( '<STR_LIT>' ) ) <EOL> searched = Line . objects . filter ( dialogue_search_vector = SearchQuery ( '<STR_LIT>' ) ) <EOL> self . assertSequenceEqual ( searched , [ self . verse0 ] ) <EOL> def test_existing_vector_config_explicit ( self ) : <EOL> Line . objects . update ( dialogue_search_vector = SearchVector ( '<STR_LIT>' ) ) <EOL> searched = Line . objects . filter ( dialogue_search_vector = SearchQuery ( '<STR_LIT>' , config = '<STR_LIT>' ) ) <EOL> self . assertSequenceEqual ( searched , [ self . french ] ) <EOL> class MultipleFieldsTest ( GrailTestData , PostgreSQLTestCase ) : <EOL> def test_simple_on_dialogue ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . verse1 ] ) <EOL> def test_simple_on_scene ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , self . verses ) <EOL> def test_non_exact_match ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . verse2 ] ) <EOL> def test_search_two_terms ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . verse2 ] ) <EOL> def test_terms_adjacent ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , self . verses ) <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ ] ) <EOL> def test_search_with_null ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertEqual ( set ( searched ) , { self . bedemir0 , self . bedemir1 , self . crowd , self . witch , self . duck } ) <EOL> def test_config_query_explicit ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' , config = '<STR_LIT>' ) , <EOL> ) . filter ( search = SearchQuery ( '<STR_LIT>' , config = '<STR_LIT>' ) ) <EOL> self . assertSequenceEqual ( searched , [ self . french ] ) <EOL> def test_config_query_implicit ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' , config = '<STR_LIT>' ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . french ] ) <EOL> def test_config_from_field_explicit ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' , config = F ( '<STR_LIT>' ) ) , <EOL> ) . filter ( search = SearchQuery ( '<STR_LIT>' , config = F ( '<STR_LIT>' ) ) ) <EOL> self . assertSequenceEqual ( searched , [ self . french ] ) <EOL> def test_config_from_field_implicit ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' , config = F ( '<STR_LIT>' ) ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . french ] ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> class TestCombinations ( GrailTestData , PostgreSQLTestCase ) : <EOL> def test_vector_add ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' ) + SearchVector ( '<STR_LIT>' ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertEqual ( set ( searched ) , { self . bedemir0 , self . bedemir1 , self . crowd , self . witch , self . duck } ) <EOL> def test_vector_add_multi ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = ( <EOL> SearchVector ( '<STR_LIT>' ) + <EOL> SearchVector ( '<STR_LIT>' ) + <EOL> SearchVector ( '<STR_LIT>' ) <EOL> ) , <EOL> ) . filter ( search = '<STR_LIT>' ) <EOL> self . assertEqual ( set ( searched ) , { self . bedemir0 , self . bedemir1 , self . crowd , self . witch , self . duck } ) <EOL> def test_query_and ( self ) : <EOL> searched = Line . objects . annotate ( <EOL> search = SearchVector ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) . filter ( search = SearchQuery ( '<STR_LIT>' ) & SearchQuery ( '<STR_LIT>' ) ) <EOL> self . assertSequenceEqual ( searched , [ self . bedemir0 ] ) <EOL> def test_query_or ( self ) : <EOL> searched = Line . objects . filter ( dialogue__search = SearchQuery ( '<STR_LIT>' ) | SearchQuery ( '<STR_LIT>' ) ) <EOL> self . assertSequenceEqual ( set ( searched ) , { self . verse1 , self . verse2 } ) <EOL> def test_query_invert ( self ) : <EOL> searched = Line . objects . filter ( character = self . minstrel , dialogue__search = ~ SearchQuery ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( set ( searched ) , { self . verse0 , self . verse2 } ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> class TestRankingAndWeights ( GrailTestData , PostgreSQLTestCase ) : <EOL> def test_ranking ( self ) : <EOL> searched = Line . objects . filter ( character = self . minstrel ) . annotate ( <EOL> rank = SearchRank ( SearchVector ( '<STR_LIT>' ) , SearchQuery ( '<STR_LIT>' ) ) , <EOL> ) . order_by ( '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . verse2 , self . verse1 , self . verse0 ] ) <EOL> def test_rank_passing_untyped_args ( self ) : <EOL> searched = Line . objects . filter ( character = self . minstrel ) . annotate ( <EOL> rank = SearchRank ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) . order_by ( '<STR_LIT>' ) <EOL> self . assertSequenceEqual ( searched , [ self . verse2 , self . verse1 , self . verse0 ] ) <EOL> def test_weights_in_vector ( self ) : <EOL> vector = SearchVector ( '<STR_LIT>' , weight = '<STR_LIT:A>' ) + SearchVector ( '<STR_LIT>' , weight = '<STR_LIT:D>' ) <EOL> searched = Line . objects . filter ( scene = self . witch_scene ) . annotate ( <EOL> rank = SearchRank ( vector , SearchQuery ( '<STR_LIT>' ) ) , <EOL> ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:2> ] <EOL> self . assertSequenceEqual ( searched , [ self . crowd , self . witch ] ) <EOL> vector = SearchVector ( '<STR_LIT>' , weight = '<STR_LIT:D>' ) + SearchVector ( '<STR_LIT>' , weight = '<STR_LIT:A>' ) <EOL> searched = Line . objects . filter ( scene = self . witch_scene ) . annotate ( <EOL> rank = SearchRank ( vector , SearchQuery ( '<STR_LIT>' ) ) , <EOL> ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:2> ] <EOL> self . assertSequenceEqual ( searched , [ self . witch , self . crowd ] ) <EOL> def test_ranked_custom_weights ( self ) : <EOL> vector = SearchVector ( '<STR_LIT>' , weight = '<STR_LIT:D>' ) + SearchVector ( '<STR_LIT>' , weight = '<STR_LIT:A>' ) <EOL> searched = Line . objects . filter ( scene = self . witch_scene ) . annotate ( <EOL> rank = SearchRank ( vector , SearchQuery ( '<STR_LIT>' ) , weights = [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0.5> ] ) , <EOL> ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:2> ] <EOL> self . assertSequenceEqual ( searched , [ self . crowd , self . witch ] ) <EOL> def test_ranking_chaining ( self ) : <EOL> searched = Line . objects . filter ( character = self . minstrel ) . annotate ( <EOL> rank = SearchRank ( SearchVector ( '<STR_LIT>' ) , SearchQuery ( '<STR_LIT>' ) ) , <EOL> ) . filter ( rank__gt = <NUM_LIT> ) <EOL> self . assertSequenceEqual ( searched , [ self . verse0 ] ) </s>
