test_batch.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import annotations
  2. import pytest
  3. from hth_sdk.batch import ActionBatchBuilder
  4. from hth_sdk.exceptions import (
  5. ActionRejected,
  6. ApiVersionMismatch,
  7. DataGap,
  8. DeviceEpochChanged,
  9. )
  10. from datetime import timedelta
  11. from hth_sdk.profiles import prod2u
  12. from hth_sdk.types import ActionBatch
  13. def test_builder_packs_sorted_targets():
  14. batch = (
  15. prod2u.builder("hood.0", allow_unverified=True)
  16. .command_id("cmd-1")
  17. .lease("lease-a", 3)
  18. .based_on(10, 2)
  19. .set_fan(1, 20)
  20. .set_ptc(1, 0)
  21. .set_door_close_percent("x", 0)
  22. .valid_for(timedelta(seconds=15))
  23. .build()
  24. )
  25. packed = batch.as_dict()
  26. assert packed["device_id"] == "hood.0"
  27. assert packed["command_id"] == "cmd-1"
  28. assert packed["lease_id"] == "lease-a"
  29. assert packed["lease_epoch"] == 3
  30. ids = [t["id"] for t in packed["continuous_targets"]]
  31. assert ids == sorted(ids)
  32. doors = [t for t in packed["continuous_targets"] if t["id"] == "door.x.close_percent"]
  33. assert doors[0]["value"] == 0 # FULL OPEN, not STOP
  34. def test_door_zero_is_legal_full_open():
  35. batch = prod2u.builder("hood.0", allow_unverified=True).set_door_close_percent("z", 0).valid_for(timedelta(seconds=15)).build()
  36. assert dict(batch.continuous_targets)["door.z.close_percent"] == 0
  37. def test_rejects_unknown_id():
  38. with pytest.raises(ActionRejected) as ei:
  39. prod2u.builder("hood.0", allow_unverified=True).set("fan.rpm.1", 10)
  40. assert ei.value.reason == "UNKNOWN_ID"
  41. def test_rejects_out_of_range():
  42. with pytest.raises(ActionRejected) as ei:
  43. prod2u.builder("hood.0", allow_unverified=True).set_fan(1, 101)
  44. assert ei.value.reason == "OUT_OF_RANGE"
  45. def test_rejects_door_101():
  46. with pytest.raises(ActionRejected):
  47. prod2u.builder("hood.0", allow_unverified=True).set_door_close_percent("x", 101)
  48. def test_rejects_empty_batch():
  49. with pytest.raises(ActionRejected):
  50. ActionBatch.builder("hood.0", allow_unverified=True).build()
  51. def test_generic_builder_has_no_product_helpers():
  52. assert not hasattr(ActionBatchBuilder, "set_fan")
  53. assert not hasattr(ActionBatchBuilder, "set_ptc")
  54. assert not hasattr(ActionBatchBuilder, "set_door_close_percent")
  55. b = ActionBatchBuilder("hood.0", allow_unverified=True)
  56. batch = b.set("custom.actuator", 12.5).valid_for(timedelta(seconds=15)).build()
  57. assert dict(batch.continuous_targets)["custom.actuator"] == 12.5
  58. def test_offline_safe_targets_are_profile_helper_not_client_policy():
  59. b = prod2u.builder("hood.0", allow_unverified=True)
  60. for fan_id in prod2u.FAN_IDS:
  61. b.set(fan_id, 10)
  62. for ptc_id in prod2u.PTC_IDS:
  63. b.set(ptc_id, 0)
  64. for door_id in prod2u.DOOR_IDS:
  65. b.set(door_id, 0)
  66. batch = b.build()
  67. d = dict(batch.continuous_targets)
  68. assert d["fan.chamber.1"] == 10
  69. assert d["ptc.chamber.1"] == 0
  70. assert d["door.x.close_percent"] == 0
  71. assert d["door.y.close_percent"] == 0
  72. assert d["door.z.close_percent"] == 0
  73. def test_with_lease_returns_new_batch():
  74. batch = prod2u.builder("hood.0", allow_unverified=True).set_fan(1, 10).valid_for(timedelta(seconds=15)).build()
  75. bound = batch.with_lease("L", 9)
  76. assert bound.lease_id == "L"
  77. assert batch.lease_id == ""
  78. def test_exception_types():
  79. e = ApiVersionMismatch("hth.api.v0", "hth.api.v1")
  80. assert e.found == "hth.api.v0"
  81. gap = DataGap(dropped_count=4, resume_after_event_id=12)
  82. assert gap.dropped_count == 4
  83. epoch = DeviceEpochChanged(1, 2, "hood.0")
  84. assert epoch.new_epoch == 2