<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> import unittest <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . test import TestCase , override_settings <EOL> from test_haystack . core . models import AnotherMockModel , MockModel <EOL> from haystack import indexes <EOL> from haystack . exceptions import NotHandled , SearchFieldError <EOL> from haystack . utils import loading <EOL> try : <EOL> import pysolr <EOL> except ImportError : <EOL> pysolr = False <EOL> class ConnectionHandlerTestCase ( TestCase ) : <EOL> def test_init ( self ) : <EOL> ch = loading . ConnectionHandler ( { } ) <EOL> self . assertEqual ( ch . connections_info , { } ) <EOL> ch = loading . ConnectionHandler ( { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } ) <EOL> self . assertEqual ( ch . connections_info , { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } ) <EOL> @ unittest . skipIf ( pysolr is False , "<STR_LIT>" ) <EOL> def test_get_item ( self ) : <EOL> ch = loading . ConnectionHandler ( { } ) <EOL> try : <EOL> empty_engine = ch [ '<STR_LIT:default>' ] <EOL> self . fail ( ) <EOL> except ImproperlyConfigured : <EOL> pass <EOL> ch = loading . ConnectionHandler ( { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } ) <EOL> solr_engine = ch [ '<STR_LIT:default>' ] <EOL> backend_path , memory_address = repr ( solr_engine ) . strip ( '<STR_LIT>' ) . split ( '<STR_LIT>' ) <EOL> self . assertEqual ( backend_path , '<STR_LIT>' ) <EOL> solr_engine_2 = ch [ '<STR_LIT:default>' ] <EOL> backend_path_2 , memory_address_2 = repr ( solr_engine_2 ) . strip ( '<STR_LIT>' ) . split ( '<STR_LIT>' ) <EOL> self . assertEqual ( backend_path_2 , '<STR_LIT>' ) <EOL> self . assertEqual ( memory_address_2 , memory_address ) <EOL> try : <EOL> empty_engine = ch [ '<STR_LIT>' ] <EOL> self . fail ( ) <EOL> except ImproperlyConfigured : <EOL> pass <EOL> def test_get_unified_index ( self ) : <EOL> ch = loading . ConnectionHandler ( { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> } ) <EOL> ui = ch [ '<STR_LIT:default>' ] . get_unified_index ( ) <EOL> klass , address = repr ( ui ) . strip ( '<STR_LIT>' ) . split ( '<STR_LIT>' ) <EOL> self . assertEqual ( str ( klass ) , '<STR_LIT>' ) <EOL> ui_2 = ch [ '<STR_LIT:default>' ] . get_unified_index ( ) <EOL> klass_2 , address_2 = repr ( ui_2 ) . strip ( '<STR_LIT>' ) . split ( '<STR_LIT>' ) <EOL> self . assertEqual ( str ( klass_2 ) , '<STR_LIT>' ) <EOL> self . assertEqual ( address_2 , address ) <EOL> class ConnectionRouterTestCase ( TestCase ) : <EOL> @ override_settings ( ) <EOL> def test_init ( self ) : <EOL> del settings . HAYSTACK_ROUTERS <EOL> cr = loading . ConnectionRouter ( ) <EOL> self . assertEqual ( [ str ( route . __class__ ) for route in cr . routers ] , [ "<STR_LIT>" ] ) <EOL> @ override_settings ( HAYSTACK_ROUTERS = [ '<STR_LIT>' ] ) <EOL> def test_router_override1 ( self ) : <EOL> cr = loading . ConnectionRouter ( ) <EOL> self . assertEqual ( [ str ( route . __class__ ) for route in cr . routers ] , [ "<STR_LIT>" ] ) <EOL> @ override_settings ( HAYSTACK_ROUTERS = [ ] ) <EOL> def test_router_override2 ( self ) : <EOL> cr = loading . ConnectionRouter ( ) <EOL> self . assertEqual ( [ str ( route . __class__ ) for route in cr . routers ] , [ "<STR_LIT>" ] ) <EOL> @ override_settings ( HAYSTACK_ROUTERS = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_router_override3 ( self ) : <EOL> cr = loading . ConnectionRouter ( ) <EOL> self . assertEqual ( [ str ( route . __class__ ) for route in cr . routers ] , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> @ override_settings ( ) <EOL> def test_actions1 ( self ) : <EOL> del settings . HAYSTACK_ROUTERS <EOL> cr = loading . ConnectionRouter ( ) <EOL> self . assertEqual ( cr . for_read ( ) , '<STR_LIT:default>' ) <EOL> self . assertEqual ( cr . for_write ( ) , [ '<STR_LIT:default>' ] ) <EOL> @ override_settings ( HAYSTACK_ROUTERS = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_actions2 ( self ) : <EOL> cr = loading . ConnectionRouter ( ) <EOL> self . assertEqual ( cr . for_read ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( cr . for_write ( ) , [ '<STR_LIT>' , '<STR_LIT:default>' ] ) <EOL> @ override_settings ( HAYSTACK_ROUTERS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_actions3 ( self ) : <EOL> cr = loading . ConnectionRouter ( ) <EOL> self . assertEqual ( cr . for_read ( ) , '<STR_LIT>' ) <EOL> self . assertEqual ( cr . for_write ( ) , [ '<STR_LIT>' , '<STR_LIT:default>' ] ) <EOL> self . assertEqual ( cr . for_read ( pass_through = False ) , '<STR_LIT>' ) <EOL> self . assertEqual ( cr . for_write ( pass_through = False ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:default>' ] ) <EOL> @ override_settings ( HAYSTACK_ROUTERS = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_actions4 ( self ) : <EOL> cr = loading . ConnectionRouter ( ) <EOL> self . assertEqual ( cr . for_read ( ) , '<STR_LIT:default>' ) <EOL> self . assertEqual ( cr . for_write ( ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:default>' ] ) <EOL> class MockNotAModel ( object ) : <EOL> pass <EOL> class FakeSearchIndex ( indexes . BasicSearchIndex , indexes . Indexable ) : <EOL> def update_object ( self , instance , ** kwargs ) : <EOL> return True <EOL> def remove_object ( self , instance , ** kwargs ) : <EOL> return True <EOL> def get_model ( self ) : <EOL> return MockModel <EOL> class InvalidSearchIndex ( indexes . SearchIndex , indexes . Indexable ) : <EOL> document = indexes . CharField ( document = True ) <EOL> def get_model ( self ) : <EOL> return MockModel <EOL> class BasicMockModelSearchIndex ( indexes . BasicSearchIndex , indexes . Indexable ) : <EOL> def get_model ( self ) : <EOL> return MockModel <EOL> class BasicAnotherMockModelSearchIndex ( indexes . BasicSearchIndex , indexes . Indexable ) : <EOL> def get_model ( self ) : <EOL> return AnotherMockModel <EOL> class ValidSearchIndex ( indexes . SearchIndex , indexes . Indexable ) : <EOL> text = indexes . CharField ( document = True ) <EOL> author = indexes . CharField ( index_fieldname = '<STR_LIT:name>' ) <EOL> title = indexes . CharField ( indexed = False ) <EOL> def get_model ( self ) : <EOL> return MockModel <EOL> class AlternateValidSearchIndex ( indexes . SearchIndex , indexes . Indexable ) : <EOL> text = indexes . CharField ( document = True ) <EOL> author = indexes . CharField ( faceted = True ) <EOL> title = indexes . CharField ( faceted = True ) <EOL> def get_model ( self ) : <EOL> return AnotherMockModel <EOL> class ExplicitFacetSearchIndex ( indexes . SearchIndex , indexes . Indexable ) : <EOL> text = indexes . CharField ( document = True ) <EOL> author = indexes . CharField ( faceted = True ) <EOL> title = indexes . CharField ( ) <EOL> title_facet = indexes . FacetCharField ( facet_for = '<STR_LIT:title>' ) <EOL> bare_facet = indexes . FacetCharField ( ) <EOL> def get_model ( self ) : <EOL> return MockModel <EOL> class MultiValueValidSearchIndex ( indexes . SearchIndex , indexes . Indexable ) : <EOL> text = indexes . CharField ( document = True ) <EOL> author = indexes . MultiValueField ( stored = False ) <EOL> title = indexes . CharField ( indexed = False ) <EOL> def get_model ( self ) : <EOL> return MockModel <EOL> class UnifiedIndexTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( UnifiedIndexTestCase , self ) . setUp ( ) <EOL> self . ui = loading . UnifiedIndex ( ) <EOL> self . ui . build ( [ ] ) <EOL> def test_get_index ( self ) : <EOL> self . assertRaises ( NotHandled , self . ui . get_index , MockModel ) <EOL> try : <EOL> self . ui . get_index ( MockModel ) <EOL> except NotHandled as e : <EOL> self . assertTrue ( MockModel . __name__ in str ( e ) ) <EOL> self . ui . build ( indexes = [ BasicMockModelSearchIndex ( ) ] ) <EOL> self . assertTrue ( isinstance ( self . ui . get_index ( MockModel ) , indexes . BasicSearchIndex ) ) <EOL> def test_get_indexed_models ( self ) : <EOL> self . assertEqual ( self . ui . get_indexed_models ( ) , [ ] ) <EOL> self . ui . build ( indexes = [ ValidSearchIndex ( ) ] ) <EOL> indexed_models = self . ui . get_indexed_models ( ) <EOL> self . assertEqual ( len ( indexed_models ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( MockModel in indexed_models ) <EOL> def test_get_indexes ( self ) : <EOL> self . assertEqual ( self . ui . get_indexes ( ) , { } ) <EOL> index = ValidSearchIndex ( ) <EOL> self . ui . build ( indexes = [ index ] ) <EOL> results = self . ui . get_indexes ( ) <EOL> self . assertEqual ( len ( results ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( MockModel in results ) <EOL> self . assertEqual ( results [ MockModel ] , index ) <EOL> def test_all_searchfields ( self ) : <EOL> self . ui . build ( indexes = [ BasicMockModelSearchIndex ( ) ] ) <EOL> fields = self . ui . all_searchfields ( ) <EOL> self . assertEqual ( len ( fields ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( '<STR_LIT:text>' in fields ) <EOL> self . assertTrue ( isinstance ( fields [ '<STR_LIT:text>' ] , indexes . CharField ) ) <EOL> self . assertEqual ( fields [ '<STR_LIT:text>' ] . document , True ) <EOL> self . assertEqual ( fields [ '<STR_LIT:text>' ] . use_template , True ) <EOL> self . ui . build ( indexes = [ BasicMockModelSearchIndex ( ) , AlternateValidSearchIndex ( ) ] ) <EOL> fields = self . ui . all_searchfields ( ) <EOL> self . assertEqual ( len ( fields ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( sorted ( fields . keys ( ) ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:text>' , '<STR_LIT:title>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( '<STR_LIT:text>' in fields ) <EOL> self . assertTrue ( isinstance ( fields [ '<STR_LIT:text>' ] , indexes . CharField ) ) <EOL> self . assertEqual ( fields [ '<STR_LIT:text>' ] . document , True ) <EOL> self . assertEqual ( fields [ '<STR_LIT:text>' ] . use_template , True ) <EOL> self . assertTrue ( '<STR_LIT:title>' in fields ) <EOL> self . assertTrue ( isinstance ( fields [ '<STR_LIT:title>' ] , indexes . CharField ) ) <EOL> self . assertEqual ( fields [ '<STR_LIT:title>' ] . document , False ) <EOL> self . assertEqual ( fields [ '<STR_LIT:title>' ] . use_template , False ) <EOL> self . assertEqual ( fields [ '<STR_LIT:title>' ] . faceted , True ) <EOL> self . assertEqual ( fields [ '<STR_LIT:title>' ] . indexed , True ) <EOL> self . assertTrue ( '<STR_LIT>' in fields ) <EOL> self . assertTrue ( isinstance ( fields [ '<STR_LIT>' ] , indexes . CharField ) ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . document , False ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . use_template , False ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . faceted , True ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . stored , True ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . index_fieldname , '<STR_LIT>' ) <EOL> self . ui . build ( indexes = [ AlternateValidSearchIndex ( ) , MultiValueValidSearchIndex ( ) ] ) <EOL> fields = self . ui . all_searchfields ( ) <EOL> self . assertEqual ( len ( fields ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( sorted ( fields . keys ( ) ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:text>' , '<STR_LIT:title>' , '<STR_LIT>' ] ) <EOL> self . assertTrue ( '<STR_LIT:text>' in fields ) <EOL> self . assertTrue ( isinstance ( fields [ '<STR_LIT:text>' ] , indexes . CharField ) ) <EOL> self . assertEqual ( fields [ '<STR_LIT:text>' ] . document , True ) <EOL> self . assertEqual ( fields [ '<STR_LIT:text>' ] . use_template , False ) <EOL> self . assertTrue ( '<STR_LIT:title>' in fields ) <EOL> self . assertTrue ( isinstance ( fields [ '<STR_LIT:title>' ] , indexes . CharField ) ) <EOL> self . assertEqual ( fields [ '<STR_LIT:title>' ] . document , False ) <EOL> self . assertEqual ( fields [ '<STR_LIT:title>' ] . use_template , False ) <EOL> self . assertEqual ( fields [ '<STR_LIT:title>' ] . faceted , True ) <EOL> self . assertEqual ( fields [ '<STR_LIT:title>' ] . indexed , True ) <EOL> self . assertTrue ( '<STR_LIT>' in fields ) <EOL> self . assertTrue ( isinstance ( fields [ '<STR_LIT>' ] , indexes . MultiValueField ) ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . document , False ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . use_template , False ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . stored , True ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . faceted , True ) <EOL> self . assertEqual ( fields [ '<STR_LIT>' ] . index_fieldname , '<STR_LIT>' ) <EOL> try : <EOL> self . ui . build ( indexes = [ AlternateValidSearchIndex ( ) , InvalidSearchIndex ( ) ] ) <EOL> self . fail ( ) <EOL> except SearchFieldError : <EOL> pass <EOL> def test_get_index_fieldname ( self ) : <EOL> self . assertEqual ( self . ui . _fieldnames , { } ) <EOL> self . ui . build ( indexes = [ ValidSearchIndex ( ) , BasicAnotherMockModelSearchIndex ( ) ] ) <EOL> self . ui . get_index_fieldname ( '<STR_LIT:text>' ) <EOL> self . assertEqual ( self . ui . _fieldnames , { '<STR_LIT:text>' : '<STR_LIT:text>' , '<STR_LIT:title>' : '<STR_LIT:title>' , '<STR_LIT>' : '<STR_LIT:name>' } ) <EOL> self . assertEqual ( self . ui . get_index_fieldname ( '<STR_LIT:text>' ) , '<STR_LIT:text>' ) <EOL> self . assertEqual ( self . ui . get_index_fieldname ( '<STR_LIT>' ) , '<STR_LIT:name>' ) <EOL> self . assertEqual ( self . ui . get_index_fieldname ( '<STR_LIT:title>' ) , '<STR_LIT:title>' ) <EOL> self . ui . reset ( ) <EOL> self . assertEqual ( self . ui . _fieldnames , { } ) <EOL> try : <EOL> self . ui . build ( indexes = [ ValidSearchIndex ( ) , AlternateValidSearchIndex ( ) ] ) <EOL> self . fail ( ) <EOL> except SearchFieldError : <EOL> pass <EOL> def test_basic_get_facet_field_name ( self ) : <EOL> self . assertEqual ( self . ui . _facet_fieldnames , { } ) <EOL> self . ui . build ( indexes = [ BasicMockModelSearchIndex ( ) , AlternateValidSearchIndex ( ) ] ) <EOL> self . ui . get_facet_fieldname ( '<STR_LIT:text>' ) <EOL> self . assertEqual ( self . ui . _facet_fieldnames , { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( self . ui . get_index_fieldname ( '<STR_LIT:text>' ) , '<STR_LIT:text>' ) <EOL> self . assertEqual ( self . ui . get_index_fieldname ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( self . ui . get_index_fieldname ( '<STR_LIT:title>' ) , '<STR_LIT:title>' ) <EOL> self . assertEqual ( self . ui . get_facet_fieldname ( '<STR_LIT:text>' ) , '<STR_LIT:text>' ) <EOL> self . assertEqual ( self . ui . get_facet_fieldname ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( self . ui . get_facet_fieldname ( '<STR_LIT:title>' ) , '<STR_LIT>' ) <EOL> def test_more_advanced_get_facet_field_name ( self ) : <EOL> self . assertEqual ( self . ui . _facet_fieldnames , { } ) <EOL> self . ui . build ( indexes = [ BasicAnotherMockModelSearchIndex ( ) , ExplicitFacetSearchIndex ( ) ] ) <EOL> self . ui . get_facet_fieldname ( '<STR_LIT:text>' ) <EOL> self . assertEqual ( self . ui . _facet_fieldnames , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( self . ui . get_facet_fieldname ( '<STR_LIT:title>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( self . ui . get_facet_fieldname ( '<STR_LIT>' ) , '<STR_LIT>' ) </s>
