def test_failed_compare_in_process_view(self, mock_set_custom_metric): """ Tests that the process_view gets the same function. This is to conservatively test that the resolve method results in all the same metrics. """ class MockWithMockModule(): """ Added inside function to enable quick clean-up when this is removed. """ def __init__(self, mocked_module_name): self._mocked_module_name = mocked_module_name def __getattribute__(self, name): if name == '__module__': return self._mocked_module_name return super().__getattribute__(name) with patch( 'lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): request_path = '/test/' expected_owner = 'team-red' request = RequestFactory().get(request_path) self.middleware(request) expected_view_func_module = self._REQUEST_PATH_TO_MODULE_PATH[ request_path] mock_view_func = MockWithMockModule('different.module') self.middleware.process_view(None, mock_view_func, None, None) self._assert_code_owner_custom_metrics( expected_view_func_module, mock_set_custom_metric, expected_code_owner=expected_owner, process_view_func=mock_view_func, )
def test_no_resolver_for_request_path(self, mock_set_custom_metric): with patch( 'lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): request = RequestFactory().get('/bad/path/') self.middleware(request) self._assert_code_owner_custom_metrics(None, mock_set_custom_metric, has_error=True)
def test_load_config_with_invalid_dict(self, mock_set_custom_metric): with patch( 'lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): mock_view_func = MockWithMockModule('xblock') self.middleware.process_view(None, mock_view_func, None, None) self._assert_code_owner_custom_metrics('xblock', mock_set_custom_metric, has_error=True)
def test_load_config_with_invalid_dict(self, mock_set_custom_metric): with patch( 'lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): request = RequestFactory().get('/test/') self.middleware(request) expected_view_func_module = self._REQUEST_PATH_TO_MODULE_PATH[ '/test/'] self._assert_code_owner_custom_metrics(expected_view_func_module, mock_set_custom_metric, has_error=True)
def test_code_owner_mapping_hits_and_misses(self, view_func_module, expected_owner, mock_set_custom_metric): with patch( 'lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): mock_view_func = MockWithMockModule(view_func_module) self.middleware.process_view(None, mock_view_func, None, None) self._assert_code_owner_custom_metrics( view_func_module, mock_set_custom_metric, expected_code_owner=expected_owner)
def test_code_owner_mapping_hits_and_misses(self, request_path, expected_owner, mock_set_custom_metric): with patch( 'lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): request = RequestFactory().get(request_path) self.middleware(request) view_func, _, _ = resolve(request_path) expected_view_func_module = self._REQUEST_PATH_TO_MODULE_PATH[ request_path] self._assert_code_owner_custom_metrics( expected_view_func_module, mock_set_custom_metric, expected_code_owner=expected_owner)
def test_mapping_performance(self): code_owner_mappings = { 'team-red': [] } # create a long list of mappings that are nearly identical for n in range(1, 200): path = 'openedx.core.djangoapps.{}'.format(n) code_owner_mappings['team-red'].append(path) with override_settings(CODE_OWNER_MAPPINGS=code_owner_mappings): with patch( 'lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings() ): call_iterations = 100 time = timeit.timeit( # test a module name that matches nearly to the end, but doesn't actually match lambda: get_code_owner_from_module('openedx.core.djangoapps.XXX.views'), number=call_iterations ) average_time = time / call_iterations self.assertTrue(average_time < 0.0005, 'Mapping takes {}s which is too slow.'.format(average_time))
def test_load_config_with_invalid_dict(self): with patch('lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): self.assertTrue(is_code_owner_mappings_configured(), "Although invalid, mappings should be configured.") with self.assertRaises(AssertionError): get_code_owner_from_module('xblock')
def test_code_owner_mapping_hits_and_misses(self, module, expected_owner): with patch('lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): actual_owner = get_code_owner_from_module(module) self.assertEqual(expected_owner, actual_owner)
def test_is_config_loaded_with_invalid_dict(self): with patch('lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): self.assertTrue(is_code_owner_mappings_configured(), "Although invalid, mappings should be configured.")
def test_is_config_loaded_with_no_config(self): with patch('lms.djangoapps.monitoring.utils._PATH_TO_CODE_OWNER_MAPPINGS', _process_code_owner_mappings()): self.assertFalse(is_code_owner_mappings_configured(), "Mappings should not be configured.")