def test_share_view(self): """Test the share view""" profiler_patcher = mock.patch('flaskext.gae_mini_profiler.profiler', spec=True) request_patcher = mock.patch('flaskext.gae_mini_profiler.request', spec=True) profiler = profiler_patcher.start() request = request_patcher.start() try: from flaskext.gae_mini_profiler import GAEMiniProfiler request_id = mock.Sentinel() request.args = {'request_id': request_id} app = mock.Mock() ext = GAEMiniProfiler(app) with mock.patch.object(ext, '_render') as render: ext._share_view() profiler.RequestStats.get.assert_called_once_with(request_id) self.assertTrue(1, render.call_count) self.assertEquals(request_id, render.call_args[0][1]['request_id']) finally: request_patcher.stop() profiler_patcher.stop()
def test_request_view(self): """Test the request stats view""" request_patcher = mock.patch('flaskext.gae_mini_profiler.request', spec=True) profiler_patcher = mock.patch('flaskext.gae_mini_profiler.profiler', spec=True) jsonify_patcher = mock.patch('flaskext.gae_mini_profiler.jsonify', spec=True) request = request_patcher.start() profiler = profiler_patcher.start() jsonify = jsonify_patcher.start() try: from flaskext.gae_mini_profiler import GAEMiniProfiler properties = ['a', 'b', 'c'] request_ids = ['1', '2', '3'] request.args = {'request_ids': ','.join(request_ids)} stats = profiler.RequestStats.get.return_value stats.__getattribute__ = lambda x: x stats.disabled = False stats.temporary_redirect = False profiler.RequestStats.serialized_properties = properties app = mock.Mock() ext = GAEMiniProfiler(app) ext._request_view() self.assertEquals(len(request_ids), profiler.RequestStats.get.call_count) expected_result = [] for id in request_ids: expected_result.append(dict(zip(properties, properties))) jsonify.assert_called_once_with(stats=expected_result) finally: request_patcher.stop() profiler_patcher.stop() jsonify_patcher.stop()
def test_response_processing(self, *mocks): from flaskext.gae_mini_profiler import GAEMiniProfiler app = mock.Mock() rendering = "GAEMiniProfiler" class Response(object): """Mock response""" status_code = 200 is_sequence = True charset = "utf-8" data = u"<body>Hello World!</body>" with mock.patch_object(GAEMiniProfiler, '_render') as render_mock: render_mock.return_value = rendering ext = GAEMiniProfiler(app) new_response = ext._process_response(Response()) self.assertEquals([u"<body>Hello World!GAEMiniProfiler</body>"], new_response.response)
""" Initialize Flask app """ from flask import Flask from flaskext.gae_mini_profiler import GAEMiniProfiler app = Flask('application') app.config.from_object('application.settings') # Enable profiler (enabled in non-production environ only) GAEMiniProfiler(app) # Pull in URL dispatch routes import urls