Skip to content

Commit

Permalink
💎implement suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
PitButtchereit committed May 23, 2024
1 parent b76301d commit 0f74a91
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 60 deletions.
56 changes: 28 additions & 28 deletions tracex_project/extraction/tests/test_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ 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"

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()
Expand All @@ -57,37 +57,37 @@ 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()

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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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"

Expand Down
61 changes: 29 additions & 32 deletions tracex_project/extraction/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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')
Expand All @@ -65,26 +66,26 @@ 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)

self.assertEqual(response.status_code, 200)
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)
Expand Down Expand Up @@ -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)

Expand All @@ -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')

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -193,22 +194,22 @@ 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)

self.assertEqual(response.status_code, 200)
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')
Expand All @@ -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)

Expand All @@ -247,35 +248,30 @@ 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.')
self.url = reverse('journey_filter')
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)

self.assertEqual(response.status_code, 200)
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)
Expand Down Expand Up @@ -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')
Expand All @@ -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.')
Expand All @@ -342,27 +339,27 @@ 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)

self.assertEqual(response.status_code, 200)
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)
Expand All @@ -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)
Expand Down

0 comments on commit 0f74a91

Please sign in to comment.