def test_should_intercept_ignore_paths(self): SilkyConfig().SILKY_IGNORE_PATHS = ['/ignorethis'] request = Request() request.path = '/ignorethis' should_intercept = _should_intercept(request) self.assertFalse(should_intercept)
def test_should_intercept_ignore_paths(self): SilkyConfig().SILKY_IGNORE_PATHS = [ '/ignorethis' ] request = Request() request.path = '/ignorethis' should_intercept = SilkyMiddleware()._should_intercept(request) self.assertFalse(should_intercept)
def test_method_as_str(self): # noinspection PyShadowingNames def foo(_): pass # noinspection PyUnresolvedReferences with patch.object(MyClass, 'foo', foo): profile_function_or_method('silk.tests.test_dynamic_profiling', 'MyClass.foo', 'test') mock_data_collector = Mock() mock_data_collector.queries = [] mock_data_collector.request = Request() with patch('silk.profiling.profiler.DataCollector', return_value=mock_data_collector) as mock_DataCollector: MyClass().foo() self.assertEqual( mock_DataCollector.return_value.register_profile. call_count, 1) call_args = mock_DataCollector.return_value.register_profile.call_args[ 0][0] self.assertDictContainsSubset( { 'func_name': foo.__name__, 'dynamic': True, 'file_path': source_file_name(), 'name': 'test', 'line_num': six.get_function_code(foo).co_firstlineno }, call_args)
def mock_data_collector(): mock = Mock() mock.queries = [] mock.local = Mock() r = Request() mock.local.request = r mock.request = r return mock
def test_analyze_queries(self): DataCollector().configure(Request(reverse('example_app:index'))) client = Client() with silk_profile(name='test_profile'): resp = client.get(reverse('example_app:index')) DataCollector().profiles.values() assert len(resp.context['blinds']) == 5
def test_dynamic_decorator(self): middleware = SilkyMiddleware() SilkyConfig().SILKY_DYNAMIC_PROFILING = [{ 'module': 'silk.tests.data.dynamic', 'function': 'foo' }] middleware._apply_dynamic_mappings() from silk.tests.data.dynamic import foo mock = Mock() mock.queries = [] mock.request = Request() with patch('silk.profiling.profiler.DataCollector', return_value=mock) as mock_DataCollector: foo() # Should be wrapped in a decorator self.assertTrue( mock_DataCollector.return_value.register_profile.call_count)
def test_func_as_str(self): name = foo.__name__ line_num = six.get_function_code(foo).co_firstlineno profile_function_or_method('tests.tests.test_dynamic_profiling', 'foo', 'test') mock_data_collector = Mock() mock_data_collector.queries = [] mock_data_collector.request = Request() with patch('silk.profiling.profiler.DataCollector', return_value=mock_data_collector) as mock_DataCollector: foo() self.assertEqual(mock_DataCollector.return_value.register_profile.call_count, 1) call_args = mock_DataCollector.return_value.register_profile.call_args[0][0] self.assertDictContainsSubset({ 'func_name': name, 'dynamic': True, 'file_path': source_file_name(), 'name': 'test', 'line_num': line_num }, call_args)
def test_dynamic_context_manager(self): middleware = SilkyMiddleware() SilkyConfig().SILKY_DYNAMIC_PROFILING = [{ 'module': 'tests.tests.data.dynamic', 'function': 'foo', 'start_line': 1, 'end_line': 2, }] middleware._apply_dynamic_mappings() from .data.dynamic import foo mock = Mock() mock.queries = [] mock.request = Request() with patch('silk.profiling.profiler.DataCollector', return_value=mock) as mock_DataCollector: foo() self.assertTrue( mock_DataCollector.return_value.register_profile.call_count)
def test_should_intercept_silk_request(self): request = Request() request.path = reverse('silk:summary') should_intercept = _should_intercept(request) self.assertFalse(should_intercept)
def test_should_intercept_non_silk_request(self): request = Request() request.path = '/myapp/foo' should_intercept = _should_intercept(request) self.assertTrue(should_intercept)
def test_should_intercept_without_silk_urls(self): request = Request() request.path = '/login' _should_intercept(request) # Just checking no crash
def setUpClass(cls): super().setUpClass() call_execute_sql(cls, Request())
def setUpClass(cls): super(TestCallRequest, cls).setUpClass() call_execute_sql(cls, Request())
def test_should_intercept_non_silk_request(self): request = Request() request.path = '/myapp/foo' should_intercept = SilkyMiddleware()._should_intercept(request) self.assertTrue(should_intercept)
def test_should_intercept_silk_request(self): request = Request() request.path = reverse('silk:summary') should_intercept = SilkyMiddleware()._should_intercept(request) self.assertFalse(should_intercept)