test_bacnet_schema.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import unittest
  2. from pydantic import ValidationError
  3. from app.schemas.bacnet import BacnetBBMDWhoIsRequest, BacnetPointReadRequest, BacnetPointSearchRequest, bac0_object_type
  4. class BacnetSchemaTest(unittest.TestCase):
  5. def test_port_defaults_to_bacnet_ip_port(self):
  6. request = BacnetPointSearchRequest(ip="192.168.75.240", bacnet_device_id=12345)
  7. self.assertEqual(47808, request.port)
  8. def test_search_request_only_requires_device_fields(self):
  9. request = BacnetPointSearchRequest(ip="192.168.75.240", bacnet_device_id=12345)
  10. self.assertEqual("192.168.75.240", request.ip)
  11. self.assertEqual(12345, request.bacnet_device_id)
  12. def test_ip_must_be_valid_address(self):
  13. with self.assertRaises(ValidationError) as context:
  14. BacnetPointSearchRequest(ip="not-an-ip", bacnet_device_id=12345)
  15. self.assertIn("ip must be a valid IP address", str(context.exception))
  16. def test_device_id_range_is_validated(self):
  17. with self.assertRaises(ValidationError) as context:
  18. BacnetPointSearchRequest(ip="192.168.75.240", bacnet_device_id=4_194_304)
  19. self.assertIn("bacnet_device_id", str(context.exception))
  20. def test_points_must_not_be_empty(self):
  21. with self.assertRaises(ValidationError) as context:
  22. BacnetPointReadRequest(ip="192.168.75.240", bacnet_device_id=12345, points=[])
  23. self.assertIn("points", str(context.exception))
  24. def test_object_type_is_normalized(self):
  25. request = BacnetPointReadRequest(
  26. ip="192.168.75.240",
  27. bacnet_device_id=12345,
  28. points=[{"object_type": "analog-input", "object_id": 1}],
  29. )
  30. self.assertEqual("AnalogInput", request.points[0].object_type)
  31. self.assertEqual("analogInput", bac0_object_type(request.points[0].object_type))
  32. def test_bbmd_whois_defaults_to_full_device_range(self):
  33. request = BacnetBBMDWhoIsRequest(bbmd_ip="192.168.1.1")
  34. self.assertEqual(47808, request.bbmd_port)
  35. self.assertEqual(60, request.ttl)
  36. self.assertEqual(0, request.low_limit)
  37. self.assertEqual(4_194_303, request.high_limit)
  38. def test_bbmd_whois_range_must_be_ordered(self):
  39. with self.assertRaises(ValidationError) as context:
  40. BacnetBBMDWhoIsRequest(bbmd_ip="192.168.1.1", low_limit=10, high_limit=1)
  41. self.assertIn("low_limit must be less than or equal to high_limit", str(context.exception))
  42. if __name__ == "__main__":
  43. unittest.main()