From 0f74a91d23fcfb57b19f5d8a60ece42c237eba38 Mon Sep 17 00:00:00 2001 From: Pit Buttchereit <116184086+PitButtchereit@users.noreply.github.com> Date: Thu, 23 May 2024 18:40:49 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=8Eimplement=20suggestions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extraction/tests/test_orchestrator.py | 56 ++++++++--------- tracex_project/extraction/tests/test_views.py | 61 +++++++++---------- 2 files changed, 57 insertions(+), 60 deletions(-) diff --git a/tracex_project/extraction/tests/test_orchestrator.py b/tracex_project/extraction/tests/test_orchestrator.py index 569e2f20..2b44081c 100644 --- a/tracex_project/extraction/tests/test_orchestrator.py +++ b/tracex_project/extraction/tests/test_orchestrator.py @@ -32,14 +32,14 @@ def tearDown(self): # pylint: disable=invalid-name Orchestrator.reset_instance() def test_single_instance_creation(self): - """Tests if two initialized orchestrators are the same instance.""" + """Test if two initialized orchestrators are the same instance.""" orchestrator1 = Orchestrator() orchestrator2 = Orchestrator() self.assertIs(orchestrator1, orchestrator2) def test_consistent_object_state(self): - """Tests if the state of the orchestrator instance is the same for two instances.""" + """Test if the state of the orchestrator instance is the same for two instances.""" orchestrator1 = Orchestrator() orchestrator2 = Orchestrator() orchestrator1.data = "test_data" @@ -47,7 +47,7 @@ def test_consistent_object_state(self): self.assertEqual(orchestrator1.data, orchestrator2.data) def test_get_instance_method(self): - """Tests if the get_instance method returns the same instance and if a new instance is the same instance.""" + """Test if the get_instance method returns the same instance and if a new instance is the same instance.""" Orchestrator() orchestrator1 = Orchestrator.get_instance() orchestrator2 = Orchestrator.get_instance() @@ -57,7 +57,7 @@ def test_get_instance_method(self): self.assertIs(orchestrator1, orchestrator3) def test_reset_instance(self): - """Tests if reset_instance resets the instance for all objects.""" + """Test if reset_instance resets the instance for all objects.""" orchestrator1 = Orchestrator() Orchestrator.reset_instance() orchestrator2 = Orchestrator() @@ -65,29 +65,29 @@ def test_reset_instance(self): self.assertIsNot(orchestrator1, orchestrator2) def test_set_configuration(self): - """Tests if the set_configuration method correctly updates the Orchestrators instance's configuration.""" - config = ExtractionConfiguration() - orchestrator = Orchestrator(config) - new_config = ExtractionConfiguration() - orchestrator.set_configuration(new_config) + """Test if the set_configuration method correctly updates the Orchestrators instance's configuration.""" + configuration = ExtractionConfiguration() + orchestrator = Orchestrator(configuration) + new_configuration = ExtractionConfiguration() + orchestrator.set_configuration(new_configuration) - self.assertIs(orchestrator.configuration, new_config) + self.assertIs(orchestrator.configuration, new_configuration) def test_singleton_with_configuration(self): - """Tests that the Orchestrator's configuration remains unchanged with subsequent instantiations.""" + """Test that the Orchestrator's configuration remains unchanged with subsequent instantiations.""" Orchestrator.reset_instance() - config1 = ExtractionConfiguration() - orchestrator1 = Orchestrator(config1) - config2 = ExtractionConfiguration() - orchestrator2 = Orchestrator(config2) + configuration_1 = ExtractionConfiguration() + orchestrator1 = Orchestrator(configuration_1) + configuration_2 = ExtractionConfiguration() + orchestrator2 = Orchestrator(configuration_2) self.assertIs(orchestrator1.configuration, orchestrator2.configuration) def test_initialize_modules(self): - """Tests if initialize_modules correctly initializes a module.""" + """Test if initialize_modules correctly initializes a module.""" Orchestrator.reset_instance() - config = ExtractionConfiguration() - orchestrator = Orchestrator(configuration=config) + configuration = ExtractionConfiguration() + orchestrator = Orchestrator(configuration=configuration) orchestrator.configuration.update( modules={ "activity_labeling": ActivityLabeler, @@ -99,20 +99,20 @@ def test_initialize_modules(self): self.assertEqual(modules['activity_labeling'].name, "Activity Labeler") def test_run(self): - """Tests if the run method correctly return a dataframe. Execution of ActivityLabeler and Preprocessor is + """Test if the run method correctly returns a dataframe. Execution of ActivityLabeler and Preprocessor is necessary since the run method makes assumptions on how the patient journey looks like.""" Orchestrator.reset_instance() - config = ExtractionConfiguration( - patient_journey="This is a test patient journey. This is some description about how I fell and and " + configuration = ExtractionConfiguration( + patient_journey="This is a test patient journey. This is some description about how I fell ill and " "recovered in the end.", ) - config.update( + configuration.update( modules={ "preprocessing": Preprocessor, "activity_labeling": ActivityLabeler, } ) - orchestrator = Orchestrator(configuration=config) + orchestrator = Orchestrator(configuration=configuration) orchestrator.run() self.assertIsNot(orchestrator.get_data(), None) @@ -145,16 +145,16 @@ def test_get_db_objects_id_key_error(self): self.orchestrator.get_db_objects_id(object_name) def test_update_progress_with_view(self): - """Tests if the progress of the views are updated correctly.""" + """Test if the progress of the views are updated correctly.""" request = self.factory.get("/extraction/filter") - middleware = SessionMiddleware(lambda req: req) + middleware = SessionMiddleware(lambda _request: _request) middleware.process_request(request) request.session.save() class MockView: """Mock View class for testing purposes.""" - def __init__(self, request): - self.request = request + def __init__(self, _request): + self.request = _request view = MockView(request) current_step = 2 @@ -166,7 +166,7 @@ def __init__(self, request): self.assertEqual(request.session["status"], module_name) def test_update_progress_without_view(self): - """Tests if the progress of the views are updated correctly.""" + """Test if the progress of the views are updated correctly.""" current_step = 2 module_name = "Activity Labeler" diff --git a/tracex_project/extraction/tests/test_views.py b/tracex_project/extraction/tests/test_views.py index 74ffae7a..be3eafc0 100644 --- a/tracex_project/extraction/tests/test_views.py +++ b/tracex_project/extraction/tests/test_views.py @@ -32,7 +32,7 @@ def setUp(self): # pylint: disable=invalid-name self.url = reverse('choose_input_method') def test_view_get_request(self): - """Tests that the view URL exists and is accessible by passing a GET request.""" + """Test that the view URL exists and is accessible by passing a GET request.""" response = self.client.get(self.url) resolver = resolve(self.url) @@ -41,15 +41,16 @@ def test_view_get_request(self): def test_view_post_request(self): """ - Test that a POST request to the view returns a method not allowed error since the JourneyInputSelectView - is a simple TemplateView that does not handle POST request. + Test that a POST request to the view returns a method not allowed error. + + The JourneyInputSelectView is a simple TemplateView that does not handle POST request. """ response = self.client.post(self.url) self.assertEqual(response.status_code, 405) def test_view_uses_correct_template(self): - """Tests that the view uses the correct template.""" + """Test that the view uses the correct template.""" response = self.client.get(self.url) self.assertTemplateUsed(response, 'choose_input_method.html') @@ -65,12 +66,12 @@ class JourneyUploadViewTests(TestCase): """Test cases for the JourneyUploadView.""" def setUp(self): # pylint: disable=invalid-name - """Set up method that gets called everytime before tests are executed.""" + """Set up test client and URL.""" self.client = Client() self.url = reverse('journey_upload') def test_view_get_request(self): - """Tests that the view URL exists and is accessible by passing a GET request.""" + """Test that the view URL exists and is accessible by passing a GET request.""" response = self.client.get(self.url) resolver = resolve(self.url) @@ -78,13 +79,13 @@ def test_view_get_request(self): self.assertEqual(resolver.func.view_class, JourneyUploadView) def test_view_uses_correct_template(self): - """Tests that the view uses the correct template.""" + """Test that the view uses the correct template.""" response = self.client.get(self.url) self.assertTemplateUsed(response, 'upload_journey.html') def test_view_uses_correct_form(self): - """Tests that the view uses the correct form.""" + """Test that the view uses the correct form.""" response = self.client.get(self.url) self.assertIsInstance(response.context['form'], JourneyUploadForm) @@ -113,7 +114,7 @@ def test_view_post_valid_form(self): self.assertRedirects(response, reverse('journey_details', kwargs={'pk': mock_journey.id})) def test_view_post_invalid_form(self): - """Tests that posting an invalid form (without a file) returns the same page with a form error.""" + """Test that posting an invalid form (without a file) returns the same page with a form error.""" form_data = {} response = self.client.post(self.url, data=form_data) @@ -131,7 +132,7 @@ class JourneySelectViewTests(TestCase): """Test cases for the JourneySelectView.""" def setUp(self): # pylint: disable=invalid-name - """Set up method that gets called everytime before tests are executed.""" + """Set up test client and URL.""" self.client = Client() self.url = reverse('journey_select') @@ -144,13 +145,13 @@ def test_view_get_request(self): self.assertEqual(resolver.func.view_class, JourneySelectView) def test_view_uses_correct_template(self): - """Tests that the view uses the correct template.""" + """Test that the view uses the correct template.""" response = self.client.get(self.url) self.assertTemplateUsed(response, 'select_journey.html') def test_view_uses_correct_form(self): - """Tests that the view uses the correct form.""" + """Test that the view uses the correct form.""" response = self.client.get(self.url) self.assertIsInstance(response.context['form'], JourneySelectForm) @@ -175,7 +176,7 @@ def test_view_post_valid_form(self): self.assertRedirects(response, reverse('journey_details', kwargs={'pk': mock_journey.id})) def test_view_post_invalid_form(self): - """Tests that posting an invalid form (without selecting a file) returns the same page with a form error.""" + """Test that posting an invalid form (without selecting a file) returns the same page with a form error.""" form_data = {} response = self.client.post(self.url, data=form_data) @@ -193,14 +194,14 @@ class JourneyDetailViewTests(TestCase): """Test cases for the JourneyDetailView.""" def setUp(self): # pylint: disable=invalid-name - """Set up method that gets called everytime before tests are executed.""" + """Set up test client, a mock patient journey and the URL.""" self.client = Client() self.mock_journey = PatientJourney.manager.create(name='Test Journey', patient_journey='This is a test patient journey.') self.url = reverse('journey_details', kwargs={'pk': self.mock_journey.pk}) def test_view_get_request(self): - """Tests that the view URL exists and is accessible by passing a GET request.""" + """Test that the view URL exists and is accessible by passing a GET request.""" response = self.client.get(self.url) resolver = resolve(self.url) @@ -208,7 +209,7 @@ def test_view_get_request(self): self.assertEqual(resolver.func.view_class, JourneyDetailView) def test_uses_correct_template(self): - """Tests that the view uses the correct template.""" + """Test that the view uses the correct template.""" response = self.client.get(self.url) self.assertTemplateUsed(response, 'journey_details.html') @@ -228,7 +229,7 @@ def test_view_without_patient_journey(self): self.assertEqual(response.status_code, 404) def test_post_method_redirect(self): - """Test that a POST request to the view redirects to the same page.""" + """Test that a POST request to the view redirects to the JourneyFilterView.""" # Perform a GET request to set up session data response = self.client.get(self.url) @@ -247,13 +248,8 @@ class JourneyFilterViewTests(TestCase): fixtures = ["tracex_project/extraction/fixtures/prompts_fixture.json"] - @staticmethod - def dummy_get_response(): - """This is a dummy function to satisfy the get_response parameter of the SessionMiddleware.""" - return None - def setUp(self): # pylint: disable=invalid-name - """Set up method that gets called everytime before tests are executed.""" + """Set up test client, a mock patient journey, the URL and a factory that sends requests to the view.""" self.client = Client() self.mock_journey = PatientJourney.manager.create(name='Test Journey', patient_journey='This is a test patient journey.') @@ -261,7 +257,7 @@ def setUp(self): # pylint: disable=invalid-name self.factory = RequestFactory() def test_view_get_request(self): - """Tests that the view URL exists and is accessible by passing a GET request.""" + """Test that the view URL exists and is accessible by passing a GET request.""" response = self.client.get(self.url) resolver = resolve(self.url) @@ -269,13 +265,13 @@ def test_view_get_request(self): self.assertEqual(resolver.func.view_class, JourneyFilterView) def test_uses_correct_template(self): - """Tests that the view uses the correct template.""" + """Test that the view uses the correct template.""" response = self.client.get(self.url) self.assertTemplateUsed(response, 'filter_journey.html') def test_view_uses_correct_form(self): - """Tests that the view uses the correct form.""" + """Test that the view uses the correct form.""" response = self.client.get(self.url) self.assertIsInstance(response.context['form'], FilterForm) @@ -314,6 +310,7 @@ def test_form_valid(self): def test_get_ajax(self): """ Test the `get` method when an AJAX request is made. + Ensure that the correct JSON response is returned with the progress and status information. """ request = self.factory.get(self.url, HTTP_X_REQUESTED_WITH='XMLHttpRequest') @@ -332,7 +329,7 @@ class ResultViewTests(TestCase): fixtures = ["tracex_project/tracex/fixtures/dataframe_fixtures.json"] def setUp(self): # pylint: disable=invalid-name - """Set up method that gets called everytime before tests are executed.""" + """Set up test client, a mock patient journey, session data and the URL.""" self.client = Client() self.mock_journey = PatientJourney.manager.create(name='Test Journey', patient_journey='This is a test patient journey.') @@ -342,7 +339,7 @@ def setUp(self): # pylint: disable=invalid-name self.url = reverse('result') def test_view_get_request(self): - """Tests that the view URL exists and is accessible by passing a GET request.""" + """Test that the view URL exists and is accessible by passing a GET request.""" response = self.client.get(self.url) resolver = resolve(self.url) @@ -350,19 +347,19 @@ def test_view_get_request(self): self.assertEqual(resolver.func.view_class, ResultView) def test_uses_correct_template(self): - """Tests that the view uses the correct template.""" + """Test that the view uses the correct template.""" response = self.client.get(self.url) self.assertTemplateUsed(response, 'result.html') def test_uses_correct_form(self): - """Tests that the view uses the correct form.""" + """Test that the view uses the correct form.""" response = self.client.get(self.url) self.assertIsInstance(response.context['form'], ResultForm) def test_get_form_kwargs(self): - """Tests that correct form kwargs are passed to the form.""" + """Test that correct form kwargs are passed to the form.""" response = self.client.get(self.url) self.assertEqual(response.status_code, 200) @@ -373,7 +370,7 @@ def test_get_form_kwargs(self): self.assertEqual((form.initial['selected_modules']), self.session['selected_modules']) def test_get_context_data(self): - """Tests that the view fetches the correct context data.""" + """Test that the view fetches the correct context data.""" response = self.client.get(self.url) self.assertEqual(response.status_code, 200)