from rest_framework.test import APITestCase
from rest_framework import status
from apps.core.models.sensor import Sensor, SensorEnum
from apps.core.models.physical_unit import PhysicalUnit
[docs]class SensorTestCase(APITestCase):
[docs] @classmethod
def setUpClass(cls) -> None:
cls.physical_unit_1 = PhysicalUnit.objects.create(unit_name='1test_physical_unit')
cls.physical_unit_2 = PhysicalUnit.objects.create(unit_name='2test_physical_unit')
[docs] def test_create_single_sensor(self) -> None:
data = [{
"physical_unit": self.physical_unit_1.id,
"sensor_type": 'AIR'
}]
response = self.client.post(f"/api/sensors", data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data[0]['physical_unit'], self.physical_unit_1.id)
[docs] def test_create_multiple_sensors(self) -> None:
data = [{
"physical_unit": self.physical_unit_1.id,
"sensor_type": 'AIR'
}, {
"physical_unit": self.physical_unit_2.id,
"sensor_type": 'TEMP'
}]
response = self.client.post(f"/api/sensors", data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data[0]['physical_unit'], self.physical_unit_1.id)
self.assertEqual(response.data[1]['physical_unit'], self.physical_unit_2.id)
[docs] def test_create_empty_list_of_sensors(self) -> None:
data = []
response = self.client.post(f"/api/sensors", data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
[docs] def test_update_sensor(self) -> None:
sensor = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
data = {
"physical_unit": self.physical_unit_1.id,
"sensor_type": 'TEMP'
}
response = self.client.put(f"/api/sensors/{sensor.id}", data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['sensor_type'], 'TEMP')
[docs] def test_update_non_existing_sensor(self) -> None:
sensor_id = '401d8295-c800-4706-8015-401f14d2e767'
data = {
"physical_unit": self.physical_unit_1.id,
"sensor_type": 'TEMP'
}
response = self.client.put(f"/api/sensors/{sensor_id}", data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
[docs] def test_update_sensor_invalid_values(self) -> None:
sensor = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
data = {
"physical_unit": self.physical_unit_1.id,
"fake_type": 'TEMP'
}
response = self.client.put(f"/api/sensors/{sensor.id}", data=data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
[docs] def test_get_sensor(self) -> None:
sensor = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
response = self.client.get(f"/api/sensors/{sensor.id}")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['physical_unit'], self.physical_unit_1.id)
[docs] def test_get_non_existing_sensor(self) -> None:
sensor_id = '401d8295-c800-4706-8015-401f14d2e767'
response = self.client.get(f"/api/sensors/{sensor_id}")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
[docs] def test_delete_sensor(self) -> None:
sensor = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
response = self.client.delete(f"/api/sensors/{sensor.id}")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
[docs] def test_delete_non_existing_sensor(self) -> None:
sensor_id = '401d8295-c800-4706-8015-401f14d2e767'
response = self.client.get(f"/api/sensors/{sensor_id}")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
[docs] def test_get_multiple_sensors_by_type(self) -> None:
sensor_type = 'AIR'
sensor1 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
sensor2 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
response = self.client.get(f"/api/sensors/filter?sensor_type={sensor_type}")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data[0]['physical_unit'], self.physical_unit_1.id)
self.assertEqual(response.data[1]['physical_unit'], self.physical_unit_1.id)
[docs] def test_get_multiple_sensors_by_empty_type(self) -> None:
sensor_type = ''
sensor1 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
sensor2 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
response = self.client.get(f"/api/sensors/filter?sensor_type={sensor_type}")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
[docs] def test_get_non_existing_sensors_by_type(self) -> None:
sensor_type = 'TEMP'
sensor1 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
sensor2 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
response = self.client.get(f"/api/sensors/filter?sensor_type={sensor_type}")
self.assertEqual(response.status_code, status.HTTP_200_OK)
[docs] def test_delete_multiple_sensors_by_type(self) -> None:
sensor_type = 'AIR'
sensor1 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
sensor2 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
response = self.client.delete(f"/api/sensors/filter?sensor_type={sensor_type}")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
[docs] def test_delete_multiple_sensors_by_empty_type(self) -> None:
sensor_type = ''
sensor1 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
sensor2 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
response = self.client.delete(f"/api/sensors/filter?sensor_type={sensor_type}")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_delete_multiple_sensors_by_invalid_type(self) -> None:
sensor_type = 'air'
sensor1 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
sensor2 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
response = self.client.delete(f"/api/sensors/filter?sensor_type={sensor_type}")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
[docs] def test_delete_multiple_sensors_by_invalid_type(self) -> None:
sensor_type = 'TEMP'
sensor1 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
sensor2 = Sensor.objects.create(physical_unit=self.physical_unit_1, sensor_type=SensorEnum.AIR)
response = self.client.delete(f"/api/sensors/filter?sensor_type={sensor_type}")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
[docs] @classmethod
def tearDownClass(cls) -> None:
cls.physical_unit_1.delete()
cls.physical_unit_2.delete()