Skip to content

Commit

Permalink
Updated passing views tests with docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonzhu09 committed Aug 1, 2024
1 parent 91145cf commit e8366f0
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 182 deletions.
8 changes: 4 additions & 4 deletions src/evagram/website/backend/api/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def setUp(self):
self.variable = Variables.objects.create(variable_name="brightnessTemperature", channel=1)
# Create a group
self.group = Groups.objects.create(group_name="gsi_hofx_vs_jedi_hofx")

def test_plot_creation(self):
"""Test that a plot can be created successfully."""
plot = Plots.objects.create(
Expand All @@ -35,12 +35,12 @@ def test_plot_creation(self):
plot_count = Plots.objects.count()
self.assertEqual(plot_count, 1)
self.assertEqual(plot.plot_type, PlotType.SCATTER)

def test_experiment_unique_together_constraint(self):
"""Test the unique together constraint for Experiments."""
with self.assertRaises(Exception):
Experiments.objects.create(experiment_name="test_experiment", owner=self.owner)

def test_variable_unique_together_constraint(self):
"""Test the unique together constraint for Variables."""
with self.assertRaises(Exception):
Expand Down Expand Up @@ -79,4 +79,4 @@ def test_plot_type_constraint(self):
observation=self.observation,
variable=self.variable,
group=self.group
)
)
271 changes: 106 additions & 165 deletions src/evagram/website/backend/api/test_views.py
Original file line number Diff line number Diff line change
@@ -1,172 +1,113 @@
from django.test import TestCase
from api.models import Owners, Plots
from api.models import *
from api.serializers import PlotSerializer
from rest_framework import status


class TestAPIView(TestCase):

def setUp(self):
# Set up initial data for testing
self.owner = Owners.objects.create(first_name="John", last_name="Doe", username="johndoe")
self.experiment = Experiments.objects.create(
experiment_name="test_experiment",
owner=self.owner
)
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(),
experiment=self.experiment,
reader=self.reader,
observation=self.observation,
variable=self.variable,
group=self.group
)

def test_initial_load(self):
response = self.client.get("/api/initial-load/")
self.assertEqual(200, response.status_code)
self.assertTrue("owners" in response.json())

def test_plot_insufficient_params(self):
response = self.client.get("/api/get-plots-by-field/?owner_id=1")
self.assertEqual("owner_id=1", response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)

def test_plot_invalid_username(self):
response = self.client.get(
"/api/get-plots-by-field/?owner_id=null&experiment_id=12&observation_id=1"
"&variable_name=brightnessTemperature&channel=4&group_id=1")
self.assertEqual("owner_id=null&experiment_id=12&observation_id=1"
"&variable_name=brightnessTemperature&channel=4&group_id=1",
response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)

def test_plot_username_not_found(self):
response = self.client.get(
"/api/get-plots-by-field/?owner_id=-1&experiment_id=12&observation_id=1"
"&variable_name=brightnessTemperature&channel=4&group_id=1")
self.assertEqual("owner_id=-1&experiment_id=12&observation_id=1"
"&variable_name=brightnessTemperature&channel=4&group_id=1",
response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)

def test_plot_value_error(self):
response = self.client.get(
"/api/get-plots-by-field/?owner_id=1&experiment_id=xyz&observation_id=1"
"&variable_name=brightnessTemperature&channel=4&group_id=1")
self.assertEqual("owner_id=1&experiment_id=xyz&observation_id=1"
"&variable_name=brightnessTemperature&channel=4&group_id=1",
response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)

def test_plot_input_cascade(self):
# Tests if the experiment_id is handled before the observation_id
response = self.client.get(
"/api/get-plots-by-field/?owner_id=1&experiment_id=null&observation_id=1"
"&variable_name=null&channel=null&group_id=null")
self.assertEqual("owner_id=1&experiment_id=null&observation_id=1"
"&variable_name=null&channel=null&group_id=null",
response.request['QUERY_STRING'])
# If diagnostics were queried out of order by observation_id,
# the queryset will be empty because experiment_id=null
self.assertNotEqual(0, len(response.data))

def test_plot_channel_error(self):
# Tests if channel was not provided and variable requires it
response = self.client.get(
"/api/get-plots-by-field/?owner_id=1&experiment_id=12&observation_id=1"
"&variable_name=brightnessTemperature&channel=null&group_id=null")
self.assertEqual("owner_id=1&experiment_id=12&observation_id=1"
"&variable_name=brightnessTemperature&channel=null&group_id=null",
response.request['QUERY_STRING'])
self.assertEqual(404, response.status_code)

def test_plot_channel_is_optional(self):
# Tests if channel was not provided but variable takes optional channel value
response = self.client.get(
"/api/get-plots-by-field/?owner_id=1&experiment_id=12&observation_id=1"
"&variable_name=windEastward&channel=null&group_id=null")
self.assertEqual("owner_id=1&experiment_id=12&observation_id=1"
"&variable_name=windEastward&channel=null&group_id=null",
response.request['QUERY_STRING'])
self.assertEqual(200, response.status_code)

def test_update_user_option_valid_params(self):
response = self.client.get("/api/update-user-option/?owner_id=1")
self.assertEqual("owner_id=1", response.request['QUERY_STRING'])
self.assertEqual(200, response.status_code)
self.assertTrue("experiments" in response.json())
self.assertTrue("observations" in response.json())
self.assertTrue("variables" in response.json())
self.assertTrue("groups" in response.json())

def test_update_user_option_object_not_found(self):
response = self.client.get("/api/update-user-option/?owner_id=-1")
self.assertEqual("owner_id=-1", response.request['QUERY_STRING'])
self.assertEqual(404, response.status_code)

def test_update_user_option_value_error(self):
response = self.client.get("/api/update-user-option/?owner_id=owner")
self.assertEqual("owner_id=owner", response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)

def test_update_user_option_insufficient_params(self):
response = self.client.get("/api/update-user-option/")
self.assertEqual("", response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)
self.assertEqual("Missing request parameter detected: 'owner_id'", response.json()['error'])

def test_update_experiment_option_valid_params(self):
response = self.client.get("/api/update-experiment-option/?experiment_id=1")
self.assertEqual("experiment_id=1", response.request['QUERY_STRING'])
self.assertEqual(200, response.status_code)
self.assertTrue("observations" in response.json())
self.assertTrue("variables" in response.json())
self.assertTrue("groups" in response.json())

def test_update_experiment_option_object_not_found(self):
response = self.client.get("/api/update-experiment-option/?experiment_id=-1")
self.assertEqual("experiment_id=-1", response.request['QUERY_STRING'])
self.assertEqual(404, response.status_code)

def test_update_experiment_option_value_error(self):
response = self.client.get("/api/update-experiment-option/?experiment_id=!")
self.assertEqual("experiment_id=!", response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)

def test_update_experiment_option_insufficient_params(self):
response = self.client.get("/api/update-experiment-option/?key1=value1")
self.assertEqual("key1=value1", response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)

def test_update_observation_option_valid_params(self):
response = self.client.get("/api/update-observation-option/"
"?experiment_id=12&observation_id=1")
self.assertEqual("experiment_id=12&observation_id=1", response.request['QUERY_STRING'])
self.assertEqual(200, response.status_code)
self.assertTrue("groups" in response.json())
self.assertTrue("variables" in response.json())
self.assertTrue("variablesMap" in response.json())

def test_update_observation_option_object_not_found(self):
response = self.client.get("/api/update-observation-option/"
"?experiment_id=1&observation_id=-1")
self.assertEqual("experiment_id=1&observation_id=-1", response.request['QUERY_STRING'])
self.assertEqual(404, response.status_code)

def test_update_observation_option_value_error(self):
response = self.client.get("/api/update-observation-option/?observation_id=~")
self.assertEqual("observation_id=~", response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)

def test_update_observation_option_insufficient_params(self):
response = self.client.get("/api/update-observation-option/")
self.assertEqual("", response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)

def test_update_variable_option_valid_params(self):
response = self.client.get(
"/api/update-variable-option/?experiment_id=12&observation_id=1&"
"variable_name=brightnessTemperature&channel=4")
self.assertEqual("experiment_id=12&observation_id=1"
"&variable_name=brightnessTemperature&channel=4",
response.request['QUERY_STRING'])
self.assertEqual(200, response.status_code)
self.assertTrue("groups" in response.json())
self.assertTrue("channel" in response.json())

def test_update_variable_option_object_not_found(self):
response = self.client.get(
"/api/update-variable-option/?experiment_id=12&observation_id=1&"
"variable_name=variable&channel=null")
self.assertEqual("experiment_id=12&observation_id=1&"
"variable_name=variable&channel=null",
response.request['QUERY_STRING'])
self.assertEqual(404, response.status_code)

def test_update_variable_option_insufficient_params(self):
response = self.client.get("/api/update-variable-option/")
self.assertEqual("", response.request['QUERY_STRING'])
self.assertEqual(400, response.status_code)
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn("owners", response.data)

def test_get_plots_by_field_valid(self):
"""Test the get_plots_by_field view returns plots for valid inputs."""
response = self.client.get('/api/get-plots-by-field/', {
'owner_id': self.owner.owner_id,
'experiment_id': self.experiment.experiment_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)
expected_plots = PlotSerializer(
Plots.objects.filter(experiment=self.experiment), many=True).data
self.assertEqual(expected_plots, response.data)

def test_get_plots_by_field_invalid_owner(self):
"""Test get_plots_by_field with invalid owner_id."""
response = self.client.get('/api/get-plots-by-field/', {
'owner_id': 'null',
'experiment_id': self.experiment.experiment_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_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."""
response = self.client.get('/api/get-plots-by-field/', {
'experiment_id': self.experiment.experiment_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_400_BAD_REQUEST, response.status_code)
self.assertIn("error", response.data)

def test_update_user_option(self):
"""Test the update_user_option view returns correct data."""
response = self.client.get('/api/update-user-option/', {'owner_id': self.owner.owner_id})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('experiments', response.data)

def test_update_user_option_invalid_owner(self):
"""Test update_user_option with an invalid owner_id."""
response = self.client.get('/api/update-user-option/', {'owner_id': 999})
self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code)
self.assertIn("error", response.data)

def test_update_experiment_option(self):
"""Test the update_experiment_option view returns correct data."""
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('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,
'observation_id': self.observation.observation_id})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('variables', response.data)
self.assertIn('variablesMap', response.data)

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,
'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)
10 changes: 2 additions & 8 deletions src/evagram/website/backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ def update_user_option(request):
owner_id = request.GET["owner_id"]
Owners.objects.get(pk=owner_id)
data = {
"experiments": [],
"observations": [],
"variables": [],
"groups": []
"experiments": []
}
data["experiments"] = get_experiments_by_owner(owner_id)
return Response(data)
Expand All @@ -126,9 +123,7 @@ def update_experiment_option(request):
experiment_id = request.GET["experiment_id"]
Experiments.objects.get(pk=experiment_id)
data = {
"observations": [],
"variables": [],
"groups": []
"observations": []
}
data["observations"] = get_observations_by_experiment(experiment_id)
return Response(data)
Expand All @@ -152,7 +147,6 @@ def update_observation_option(request):
Observations.objects.get(pk=observation_id)
data = {
"variables": [],
"groups": [],
"variablesMap": {}
}
data["variables"] = get_variables_by_observation(experiment_id, observation_id)
Expand Down
16 changes: 11 additions & 5 deletions src/evagram/website/backend/input_app/test_input_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@ def test_ExperimentPathNotFound(self):

def test_RollbackOnException(self):
with self.assertRaises(Exception):
input_data(owner="postgres", experiment="bad_experiment", eva_directory="tests/invalid_reader")
input_data(owner="postgres",
experiment="bad_experiment",
eva_directory="tests/invalid_reader")

owner = Owners.objects.get(username="postgres")
experiments = Experiments.objects.filter(experiment_name="bad_experiment", owner=owner)
self.assertEqual(0, len(experiments))

def test_InvalidCycleTime(self):
with self.assertRaises(ValueError):
input_data(owner="postgres", experiment="invalid_cycle_time", eva_directory="tests/invalid_cycle_time")

input_data(owner="postgres",
experiment="invalid_cycle_time",
eva_directory="tests/invalid_cycle_time")

def test_InvalidPlotType(self):
with self.assertRaises(Exception):
input_data(owner="postgres", experiment="invalid_plot_type", eva_directory="tests/invalid_plot_type")
input_data(owner="postgres",
experiment="invalid_plot_type",
eva_directory="tests/invalid_plot_type")

0 comments on commit e8366f0

Please sign in to comment.