def test_put_request(self): middleware = DBRoutingMiddleware() request = Mock(spec=HttpRequest, method='PUT') request.session = {} output = middleware.process_request(request) self.assertIsNone(output) self.assertTrue(is_thread_readwrite())
def test_get_request(self): # Get request uses replica DB if not pinned middleware = DBRoutingMiddleware() request = Mock(spec=HttpRequest, method='GET') request.session = {} output = middleware.process_request(request) self.assertIsNone(output) self.assertFalse(is_thread_readwrite())
def test_get_request_pinned(self): # If session is pinned, use master DB middleware = DBRoutingMiddleware() request = Mock(spec=HttpRequest, method='GET') request.session = { PINNING_KEY: datetime.now() + timedelta(seconds=30) } output = middleware.process_request(request) self.assertIsNone(output) self.assertTrue(is_thread_readwrite())
def test_post_request_without_write(self): middleware = DBRoutingMiddleware() request = Mock(spec=HttpRequest, method='POST') request.session = {} output = middleware.process_request(request) self.assertIsNone(output) self.assertTrue(is_thread_readwrite()) response = Mock() output = middleware.process_response(request, response) self.assertEqual(output, response) # No write - did not pin self.assertNotIn(PINNING_KEY, request.session)
def test_post_request_with_write(self): middleware = DBRoutingMiddleware() request = Mock(spec=HttpRequest, method='POST') request.session = {} output = middleware.process_request(request) self.assertIsNone(output) self.assertTrue(is_thread_readwrite()) set_db_written_flag() response = Mock() output = middleware.process_response(request, response) self.assertEqual(output, response) # Db was written, so pin self.assertTrue(request.session[PINNING_KEY] > datetime.now())