Commit ba26835d authored by Stevan Radakovic's avatar Stevan Radakovic
Browse files

Add unit tests for filtering in REST API

Fixes #359
parent 90869b05
......@@ -161,7 +161,8 @@ class WorkerFilter(filters.FilterSet):
value = Worker.HEALTH_REVERSE[value]
except KeyError:
raise ValidationError(
"Select a valid choice. %s is not one of the available choices." % value
"Select a valid choice. %s is not one of the available choices: %s"
% (value, list(zip(*Worker.HEALTH_CHOICES))[1])
)
return queryset.filter(health=value)
......@@ -170,7 +171,8 @@ class WorkerFilter(filters.FilterSet):
value = Worker.STATE_REVERSE[value]
except KeyError:
raise ValidationError(
"Select a valid choice. %s is not one of the available choices." % value
"Select a valid choice. %s is not one of the available choices: %s"
% (value, list(zip(*Worker.STATE_CHOICES))[1])
)
return queryset.filter(state=value)
......@@ -194,8 +196,8 @@ class WorkerFilter(filters.FilterSet):
"endswith",
],
"last_ping": ["exact", "lt", "gt"],
"state": ["exact", "in"],
"health": ["exact", "in"],
"state": ["exact"],
"health": ["exact"],
}
......@@ -216,7 +218,8 @@ class DeviceTypeFilter(filters.FilterSet):
value = DeviceType.HEALTH_DENOMINATOR_REVERSE[value]
except KeyError:
raise ValidationError(
"Select a valid choice. %s is not one of the available choices." % value
"Select a valid choice. %s is not one of the available choices: %s"
% (value, list(zip(*DeviceType.HEALTH_DENOMINATOR))[1])
)
return queryset.filter(health_denominator=value)
......@@ -242,7 +245,7 @@ class DeviceTypeFilter(filters.FilterSet):
],
"health_frequency": ["exact", "in"],
"disable_health_check": ["exact", "in"],
"health_denominator": ["exact", "in"],
"health_denominator": ["exact"],
"display": ["exact", "in"],
"core_count": ["exact", "in"],
}
......@@ -272,10 +275,12 @@ class DeviceFilter(filters.FilterSet):
def filter_health(self, queryset, name, value):
try:
value = Device.HEALTH_REVERSE[value]
# Need upper() here because HEALTH_REVERSE has inconsistant keys.
value = Device.HEALTH_REVERSE[value.upper()]
except KeyError:
raise ValidationError(
"Select a valid choice. %s is not one of the available choices." % value
"Select a valid choice. %s is not one of the available choices: %s"
% (value, list(zip(*Device.HEALTH_CHOICES))[1])
)
return queryset.filter(health=value)
......@@ -284,7 +289,8 @@ class DeviceFilter(filters.FilterSet):
value = Device.STATE_REVERSE[value]
except KeyError:
raise ValidationError(
"Select a valid choice. %s is not one of the available choices." % value
"Select a valid choice. %s is not one of the available choices: %s"
% (value, list(zip(*Device.STATE_CHOICES))[1])
)
return queryset.filter(state=value)
......@@ -315,8 +321,8 @@ class DeviceFilter(filters.FilterSet):
"startswith",
"endswith",
],
"state": ["exact", "in"],
"health": ["exact", "in"],
"state": ["exact"],
"health": ["exact"],
}
......@@ -345,7 +351,8 @@ class TestJobFilter(filters.FilterSet):
value = TestJob.HEALTH_REVERSE[value]
except KeyError:
raise ValidationError(
"Select a valid choice. %s is not one of the available choices." % value
"Select a valid choice. %s is not one of the available choices: %s"
% (value, list(zip(*TestJob.HEALTH_CHOICES))[1])
)
return queryset.filter(health=value)
......@@ -354,7 +361,8 @@ class TestJobFilter(filters.FilterSet):
value = TestJob.STATE_REVERSE[value]
except KeyError:
raise ValidationError(
"Select a valid choice. %s is not one of the available choices." % value
"Select a valid choice. %s is not one of the available choices: %s"
% (value, list(zip(*TestJob.STATE_CHOICES))[1])
)
return queryset.filter(state=value)
......@@ -366,8 +374,8 @@ class TestJobFilter(filters.FilterSet):
"end_time": ["exact", "lt", "gt"],
"health_check": ["exact"],
"target_group": ["exact", "in", "contains", "icontains", "startswith"],
"state": ["exact", "in"],
"health": ["exact", "in"],
"state": ["exact"],
"health": ["exact"],
"priority": ["exact", "in", "lt", "lte", "gt", "gte"],
"definition": [
"exact",
......
......@@ -24,9 +24,11 @@ import pathlib
import pytest
import tap
from datetime import timedelta
from django.conf import settings
from django.contrib.auth.models import User, Group
from django.urls import reverse
from django.utils import timezone
from rest_framework.test import APIClient
from lava_common.compat import yaml_load, yaml_safe_dump
......@@ -152,6 +154,7 @@ class TestRestApi:
hostname="public02",
device_type=self.public_device_type1,
worker_host=self.worker1,
state=Device.STATE_RUNNING,
)
self.retired_device1 = Device.objects.create(
hostname="retired01",
......@@ -176,12 +179,17 @@ class TestRestApi:
definition=EXAMPLE_WORKING_JOB,
submitter=self.user,
requested_device_type=self.public_device_type1,
health=TestJob.HEALTH_INCOMPLETE,
)
self.private_testjob1 = TestJob.objects.create(
definition=EXAMPLE_JOB,
submitter=self.admin,
requested_device_type=self.public_device_type1,
health=TestJob.HEALTH_COMPLETE,
)
self.private_testjob1.submit_time = timezone.now() - timedelta(days=7)
self.private_testjob1.save()
# create logs
# create results for testjobs
......@@ -495,6 +503,35 @@ ok 2 bar
msg = json.loads(response.content)
assert msg["message"] == "Job valid."
def test_testjobs_filters(self):
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "jobs/?health=Incomplete",
)
assert len(data["results"]) == 1 # nosec - unit test support
job1_submit_time = timezone.make_naive(self.public_testjob1.submit_time)
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version])
+ "jobs/?submit_time=%s" % job1_submit_time,
)
assert len(data["results"]) == 1 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version])
+ "jobs/?submit_time__lt=%s" % job1_submit_time,
)
assert len(data["results"]) == 1 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version])
+ "jobs/?definition__contains=public_device",
)
assert len(data["results"]) == 1 # nosec - unit test support
def test_devices_list(self):
data = self.hit(
self.userclient,
......@@ -628,6 +665,33 @@ ok 2 bar
)
assert response.status_code == 400 # nosec
def test_devices_filters(self):
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "devices/?state=Idle",
)
assert len(data["results"]) == 2 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "devices/?health=Retired",
)
assert len(data["results"]) == 0 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version])
+ "devices/?all=True&health=Retired",
)
assert len(data["results"]) == 1 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version])
+ "devices/?hostname__contains=public",
)
assert len(data["results"]) == 2 # nosec - unit test support
def test_devicetypes(self):
data = self.hit(
self.userclient, reverse("api-root", args=[self.version]) + "devicetypes/"
......@@ -806,6 +870,34 @@ ok 2 bar
)
assert response.status_code == 400 # nosec
def test_devicetype_filters(self):
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "devicetypes/?display=True",
)
assert len(data["results"]) == 2 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version])
+ "devicetypes/?display__in=True,False",
)
assert len(data["results"]) == 3 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version])
+ "devicetypes/?name__contains=public",
)
assert len(data["results"]) == 1 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version])
+ "devicetypes/?name__in=public_device_type1,restricted_device_type1",
)
assert len(data["results"]) == 2 # nosec - unit test support
def test_workers_list(self):
data = self.hit(
self.userclient,
......@@ -1050,6 +1142,25 @@ ok 2 bar
)
assert response.status_code == 400 # nosec
def test_workers_filters(self):
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "workers/?health=Active",
)
assert len(data["results"]) == 2 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "workers/?state=Online",
)
assert len(data["results"]) == 1 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "workers/?hostname=worker1",
)
assert len(data["results"]) == 1 # nosec - unit test support
def test_aliases_list(self):
data = self.hit(
self.userclient,
......@@ -1093,6 +1204,25 @@ ok 2 bar
)
assert response.status_code == 204 # nosec - unit test support
def test_aliases_filters(self):
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "aliases/?name=test2",
)
assert len(data["results"]) == 1 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "aliases/?name__in=test1,test2",
)
assert len(data["results"]) == 2 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "aliases/?name__contains=2",
)
assert len(data["results"]) == 1 # nosec - unit test support
def test_submit_unauthorized(self):
response = self.userclient.post(
reverse("api-root", args=[self.version]) + "jobs/",
......@@ -1298,6 +1428,25 @@ ok 2 bar
)
assert response.status_code == 204 # nosec - unit test support
def test_tags_filters(self):
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "tags/?name=tag1",
)
assert len(data["results"]) == 1 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "tags/?name__in=tag1,tag2",
)
assert len(data["results"]) == 2 # nosec - unit test support
data = self.hit(
self.adminclient,
reverse("api-root", args=[self.version]) + "tags/?name__contains=2",
)
assert len(data["results"]) == 1 # nosec - unit test support
def test_view_root(client):
ret = client.get(reverse("api-root", args=[versions.versions[-1]]) + "?format=api")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment