-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated passing views tests with docstrings
- Loading branch information
1 parent
91145cf
commit e8366f0
Showing
4 changed files
with
123 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters