def test_for_write(self): cr = loading.ConnectionRouter() self.assertEqual(cr.for_write(), ['default']) cr = loading.ConnectionRouter(routers_list=[ 'test_haystack.mocks.MockMasterSlaveRouter', 'haystack.routers.DefaultRouter' ]) self.assertEqual(cr.for_write(), ['master', 'default']) # Demonstrate pass-through. cr = loading.ConnectionRouter(routers_list=[ 'test_haystack.mocks.MockPassthroughRouter', 'test_haystack.mocks.MockMasterSlaveRouter', 'haystack.routers.DefaultRouter' ]) self.assertEqual(cr.for_write(), ['master', 'default']) # Demonstrate that hinting can change routing. cr = loading.ConnectionRouter(routers_list=[ 'test_haystack.mocks.MockPassthroughRouter', 'test_haystack.mocks.MockMasterSlaveRouter', 'haystack.routers.DefaultRouter' ]) self.assertEqual(cr.for_write(pass_through=False), ['pass', 'master', 'default'])
def test_for_read(self): cr = loading.ConnectionRouter() self.assertEqual(cr.for_read(), 'default') cr = loading.ConnectionRouter(routers_list=[ 'core.tests.mocks.MockMasterSlaveRouter', 'haystack.routers.DefaultRouter' ]) self.assertEqual(cr.for_read(), 'slave') # Demonstrate pass-through. cr = loading.ConnectionRouter(routers_list=[ 'core.tests.mocks.MockPassthroughRouter', 'core.tests.mocks.MockMasterSlaveRouter', 'haystack.routers.DefaultRouter' ]) self.assertEqual(cr.for_read(), 'slave') # Demonstrate that hinting can change routing. cr = loading.ConnectionRouter(routers_list=[ 'core.tests.mocks.MockPassthroughRouter', 'core.tests.mocks.MockMasterSlaveRouter', 'haystack.routers.DefaultRouter' ]) self.assertEqual(cr.for_read(pass_through=False), 'pass')
def test_init(self): cr = loading.ConnectionRouter() self.assertEqual(cr.routers_list, ['haystack.routers.DefaultRouter']) self.assertEqual([str(route.__class__) for route in cr.routers], ["<class 'haystack.routers.DefaultRouter'>"]) cr = loading.ConnectionRouter(routers_list=['haystack.routers.DefaultRouter']) self.assertEqual(cr.routers_list, ['haystack.routers.DefaultRouter']) self.assertEqual([str(route.__class__) for route in cr.routers], ["<class 'haystack.routers.DefaultRouter'>"]) cr = loading.ConnectionRouter(routers_list=['core.tests.mocks.MockMasterSlaveRouter', 'haystack.routers.DefaultRouter']) self.assertEqual(cr.routers_list, ['core.tests.mocks.MockMasterSlaveRouter', 'haystack.routers.DefaultRouter']) self.assertEqual([str(route.__class__) for route in cr.routers], ["<class 'core.tests.mocks.MockMasterSlaveRouter'>", "<class 'haystack.routers.DefaultRouter'>"])
def test_init(self): del settings.HAYSTACK_ROUTERS cr = loading.ConnectionRouter() self.assertEqual( [str(route.__class__) for route in cr.routers], ["<class 'haystack.routers.DefaultRouter'>"], )
def test_actions3(self): cr = loading.ConnectionRouter() # Demonstrate pass-through self.assertEqual(cr.for_read(), ['slave', 'default']) self.assertEqual(cr.for_write(), ['master', 'default']) # Demonstrate that hinting can change routing. self.assertEqual(cr.for_read(pass_through=False), ['pass', 'slave', 'default']) self.assertEqual(cr.for_write(pass_through=False), ['pass', 'master', 'default'])
def test_router_override3(self): cr = loading.ConnectionRouter() self.assertEqual( [str(route.__class__) for route in cr.routers], [ "<class 'test_haystack.mocks.MockMasterSlaveRouter'>", "<class 'haystack.routers.DefaultRouter'>", ], )
def test_actions3(self): cr = loading.ConnectionRouter() # Demonstrate pass-through self.assertEqual(cr.for_read(), "slave") self.assertEqual(cr.for_write(), ["master", "default"]) # Demonstrate that hinting can change routing. self.assertEqual(cr.for_read(pass_through=False), "pass") self.assertEqual(cr.for_write(pass_through=False), ["pass", "master", "default"])
def test_actions4(self): cr = loading.ConnectionRouter() # Demonstrate that a router can return multiple backends in the "for_write" method self.assertEqual(cr.for_read(), "default") self.assertEqual(cr.for_write(), ["multi1", "multi2", "default"])
def test_actions2(self): cr = loading.ConnectionRouter() self.assertEqual(cr.for_read(), "slave") self.assertEqual(cr.for_write(), ["master", "default"])
def test_actions1(self): del settings.HAYSTACK_ROUTERS cr = loading.ConnectionRouter() self.assertEqual(cr.for_read(), "default") self.assertEqual(cr.for_write(), ["default"])
def test_router_override2(self): cr = loading.ConnectionRouter() self.assertEqual( [str(route.__class__) for route in cr.routers], ["<class 'haystack.routers.DefaultRouter'>"], )
'The HAYSTACK_INCLUDE_SPELLING setting is now a per-backend setting & belongs in HAYSTACK_CONNECTIONS.' ) # Check the 2.X+ bits. if not hasattr(settings, 'HAYSTACK_CONNECTIONS'): raise ImproperlyConfigured('The HAYSTACK_CONNECTIONS setting is required.') if DEFAULT_ALIAS not in settings.HAYSTACK_CONNECTIONS: raise ImproperlyConfigured( "The default alias '%s' must be included in the HAYSTACK_CONNECTIONS setting." % DEFAULT_ALIAS) # Load the connections. connections = loading.ConnectionHandler(settings.HAYSTACK_CONNECTIONS) # Load the router(s). connection_router = loading.ConnectionRouter() if hasattr(settings, 'HAYSTACK_ROUTERS'): if not isinstance(settings.HAYSTACK_ROUTERS, (list, tuple)): raise ImproperlyConfigured( "The HAYSTACK_ROUTERS setting must be either a list or tuple.") connection_router = loading.ConnectionRouter(settings.HAYSTACK_ROUTERS) # Setup the signal processor. signal_processor_path = getattr(settings, 'HAYSTACK_SIGNAL_PROCESSOR', 'haystack.signals.BaseSignalProcessor') signal_processor_class = loading.import_class(signal_processor_path) signal_processor = signal_processor_class(connections, connection_router)
if DEFAULT_ALIAS not in settings.HAYSTACK_CONNECTIONS: raise ImproperlyConfigured( "The default alias '%s' must be included in the HAYSTACK_CONNECTIONS setting." % DEFAULT_ALIAS) # Load the connections. connections = loading.ConnectionHandler(settings.HAYSTACK_CONNECTIONS) # Just check HAYSTACK_ROUTERS setting validity, routers will be loaded lazily if hasattr(settings, "HAYSTACK_ROUTERS"): if not isinstance(settings.HAYSTACK_ROUTERS, (list, tuple)): raise ImproperlyConfigured( "The HAYSTACK_ROUTERS setting must be either a list or tuple.") # Load the router(s). connection_router = loading.ConnectionRouter() # Per-request, reset the ghetto query log. # Probably not extraordinarily thread-safe but should only matter when # DEBUG = True. def reset_search_queries(**kwargs): for conn in connections.all(): if conn: conn.reset_queries() if settings.DEBUG: from django.core import signals as django_signals django_signals.request_started.connect(reset_search_queries)
def test_actions2(self): cr = loading.ConnectionRouter() self.assertEqual(cr.for_read(), 'slave') self.assertEqual(cr.for_write(), ['master', 'default'])