test_s7_schema.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import unittest
  2. from pydantic import ValidationError
  3. from app.schemas.s7 import S7ConnectScanRequest, S7PointReadRequest, S7RawReadRequest
  4. class S7SchemaTest(unittest.TestCase):
  5. def test_device_type_port_and_connection_type_defaults(self):
  6. request = S7RawReadRequest(
  7. ip="127.0.0.1",
  8. rock=0,
  9. slot=1,
  10. read={"area": "db", "db": 1, "start": 0, "size": 4},
  11. )
  12. self.assertEqual("S7TCP", request.device_type)
  13. self.assertEqual(102, request.port)
  14. self.assertEqual("PG", request.tsap_conn_type)
  15. self.assertEqual("DB", request.read.area)
  16. def test_tsap_conn_type_is_case_insensitive(self):
  17. request = S7RawReadRequest(
  18. ip="127.0.0.1",
  19. rock=0,
  20. slot=1,
  21. tsap_conn_type="basic",
  22. read={"area": "m", "start": 0, "size": 1},
  23. )
  24. self.assertEqual("BASIC", request.tsap_conn_type)
  25. self.assertEqual("M", request.read.area)
  26. def test_db_area_requires_positive_db_number(self):
  27. with self.assertRaises(ValidationError) as context:
  28. S7RawReadRequest(
  29. ip="127.0.0.1",
  30. rock=0,
  31. slot=1,
  32. read={"area": "DB", "db": 0, "start": 0, "size": 1},
  33. )
  34. self.assertIn("db must be greater than 0", str(context.exception))
  35. def test_bit_is_only_allowed_for_bool_points(self):
  36. with self.assertRaises(ValidationError) as context:
  37. S7PointReadRequest(
  38. ip="127.0.0.1",
  39. rock=0,
  40. slot=1,
  41. points=[{"area": "DB", "db": 1, "start": 0, "type": "int16", "bit": 0}],
  42. )
  43. self.assertIn("bit is only supported for bool points", str(context.exception))
  44. def test_connect_scan_request_only_requires_ip(self):
  45. request = S7ConnectScanRequest(ip="127.0.0.1")
  46. self.assertEqual("127.0.0.1", request.ip)
  47. if __name__ == "__main__":
  48. unittest.main()