Skip to content

Commit

Permalink
Wrote passing views tests for new dropdown endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonzhu09 committed Aug 16, 2024
1 parent 5a44885 commit 17955f0
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 7 deletions.
120 changes: 113 additions & 7 deletions src/evagram/website/backend/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ def setUp(self):
experiment_name="test_experiment",
owner=self.owner
)
self.begin_cycle_time = timezone.now()
self.reader = Readers.objects.get(reader_name="ioda_obs_space")
self.observation = Observations.objects.create(observation_name="amsua_n19")
self.variable = Variables.objects.create(variable_name="brightnessTemperature", channel=1)
self.group = Groups.objects.create(group_name="gsi_hofx_vs_jedi_hofx")
self.plot = Plots.objects.create(
plot_type='scatter',
begin_cycle_time=timezone.now(),
begin_cycle_time=self.begin_cycle_time,
experiment=self.experiment,
reader=self.reader,
observation=self.observation,
Expand All @@ -28,19 +29,53 @@ def setUp(self):
)

def test_initial_load(self):
"""
Test the 'initial_load' view, which is responsible for the initial load
of the page, ensuring that it returns the expected owners and experiments.
The test simulates a GET request to the 'initial_load' view and
verifies that:
- The response status is 200 OK.
- The response data contains the expected 'owners' and 'experiments' keys.
- The 'owners' list is not empty, ensuring that owners were retrieved.
- The 'experiments' list is not empty, ensuring that experiments associated
with the first owner were retrieved.
This test ensures that the initial data needed for rendering the page
is correctly provided by the API.
"""
response = self.client.get("/api/initial-load/")
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn("owners", response.data)
self.assertIn('owners', response.data)
self.assertIn('experiments', response.data)
self.assertGreater(len(response.data['owners']), 0)
self.assertGreater(len(response.data['experiments']), 0)

def test_get_plots_by_field_valid(self):
"""Test the get_plots_by_field view returns plots for valid inputs."""
"""
Test the 'get_plots_by_field' view to ensure that it returns the correct
plot data based on various input parameters.
The test simulates a GET request to the 'get_plots_by_field' view with specific
query parameters and verifies that:
- The response status is 200 OK when valid parameters are provided.
- The response data contains the expected plot information.
- Missing or incorrect parameters result in appropriate error messages and
status codes (e.g., 400 Bad Request, 404 Not Found).
This test ensures that the view handles various scenarios, such as filtering
by owner, experiment, cycle time, reader, observation, variable, group, and plot type.
"""
response = self.client.get('/api/get-plots-by-field/', {
'owner_id': self.owner.owner_id,
'experiment_id': self.experiment.experiment_id,
'cycle_time': self.begin_cycle_time,
'reader_id': self.reader.reader_id,
'observation_id': self.observation.observation_id,
'variable_name': self.variable.variable_name,
'channel': self.variable.channel,
'group_id': self.group.group_id
'group_id': self.group.group_id,
'plot_type': 'scatter'
})
self.assertEqual(status.HTTP_200_OK, response.status_code)
expected_plots = PlotSerializer(
Expand All @@ -52,22 +87,37 @@ def test_get_plots_by_field_invalid_owner(self):
response = self.client.get('/api/get-plots-by-field/', {
'owner_id': 'null',
'experiment_id': self.experiment.experiment_id,
'cycle_time': self.begin_cycle_time,
'reader_id': self.reader.reader_id,
'observation_id': self.observation.observation_id,
'variable_name': self.variable.variable_name,
'channel': self.variable.channel,
'group_id': self.group.group_id
'group_id': self.group.group_id,
'plot_type': 'scatter'
})
self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
self.assertIn("error", response.data)

def test_get_plots_by_field_missing_param(self):
"""Test get_plots_by_field with a missing parameter."""
"""
The test simulates a GET request to the 'get_plots_by_field' view with
various combinations of parameters to ensure:
- The response status is 200 OK when valid parameters are provided.
- The response contains the correct plot data based on the selected parameters.
- Appropriate error messages and status codes are returned for missing or invalid parameters.
This test ensures that the cascading selection logic is correctly enforced,
and that the view behaves as expected for different parameter configurations.
"""
response = self.client.get('/api/get-plots-by-field/', {
'experiment_id': self.experiment.experiment_id,
'observation_id': self.observation.observation_id,
'cycle_time': self.begin_cycle_time,
'reader_id': self.reader.reader_id,
'variable_name': self.variable.variable_name,
'channel': self.variable.channel,
'group_id': self.group.group_id
'group_id': self.group.group_id,
'plot_type': 'scatter'
})
self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
self.assertIn("error", response.data)
Expand All @@ -89,12 +139,31 @@ def test_update_experiment_option(self):
response = self.client.get('/api/update-experiment-option/',
{'experiment_id': self.experiment.experiment_id})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('cycle_times', response.data)

def test_update_cycle_time_option(self):
"""Test the update_cycle_time_option view returns correct data."""
response = self.client.get('/api/update-cycle-time-option/', {
'experiment_id': self.experiment.experiment_id,
'cycle_time': self.begin_cycle_time,})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('readers', response.data)

def test_update_reader_option(self):
"""Test the update_cycle_reader_option view returns correct data."""
response = self.client.get('/api/update-reader-option/', {
'experiment_id': self.experiment.experiment_id,
'cycle_time': self.begin_cycle_time,
'reader_id': self.reader.reader_id})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('observations', response.data)

def test_update_observation_option(self):
"""Test the update_observation_option view returns correct data."""
response = self.client.get('/api/update-observation-option/', {
'experiment_id': self.experiment.experiment_id,
'cycle_time': self.begin_cycle_time,
'reader_id': self.reader.reader_id,
'observation_id': self.observation.observation_id})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('variables', response.data)
Expand All @@ -104,10 +173,47 @@ def test_update_variable_option(self):
"""Test the update_variable_option view returns correct data."""
response = self.client.get('/api/update-variable-option/', {
'experiment_id': self.experiment.experiment_id,
'cycle_time': self.begin_cycle_time,
'reader_id': self.reader.reader_id,
'observation_id': self.observation.observation_id,
'variable_name': self.variable.variable_name,
'channel': self.variable.channel
})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('groups', response.data)
self.assertIn('channel', response.data)

def test_update_group_option(self):
"""Test the update_group_option view returns correct data."""
response = self.client.get('/api/update-group-option/', {
'experiment_id': self.experiment.experiment_id,
'cycle_time': self.begin_cycle_time,
'reader_id': self.reader.reader_id,
'observation_id': self.observation.observation_id,
'variable_name': self.variable.variable_name,
'channel': self.variable.channel,
'group_id': self.group.group_id
})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('plot_types', response.data)

def test_get_reader_aliases(self):
"""
Test the 'get_reader_aliases' view to ensure that it returns
correct alias mappings for the specified reader.
The test creates a reader and an associated alias, then makes a
GET request to the view with the reader's ID. The response is
checked to confirm that it includes the correct alias names for
observation, variable, and group.
The expected keys in the response are:
- 'observation_name': Should match the alias name for observations.
- 'variable_name': Should match the alias name for variables.
- 'group_name': Should match the alias name for groups.
"""
response = self.client.get('/api/get-reader-aliases/', {'reader_id': self.reader.reader_id})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('observation_name', response.data)
self.assertIn('variable_name', response.data)
self.assertIn('group_name', response.data)
11 changes: 11 additions & 0 deletions src/evagram/website/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ def initial_load(request):

@api_view(['GET'])
def get_plots_by_field(request):
"""
The 'get_plots_by_field' view serves as the central endpoint
for the experiment page. This endpoint allows users to query plots
associated with various parameters that work in a cascading order.
The parameters are expected to be selected in a specific sequence:
- A user (owner) must be selected first.
- Once a user is selected, an experiment associated with that user can be selected.
- Following this, other parameters such as cycle time, reader, observation,
variable, group, and plot type can be selected.
"""
try:
owner_id = request.GET["owner_id"]
experiment_id = request.GET["experiment_id"]
Expand Down

0 comments on commit 17955f0

Please sign in to comment.