Skip to content
Snippets Groups Projects
Select Git revision
  • 0c1c10868a6977a7593193c7ce613151d606002f
  • master default protected
  • L2SS-1914-fix_job_dispatch
  • TMSS-3170
  • TMSS-3167
  • TMSS-3161
  • TMSS-3158-Front-End-Only-Allow-Changing-Again
  • TMSS-3133
  • TMSS-3319-Fix-Templates
  • test-fix-deploy
  • TMSS-3134
  • TMSS-2872
  • defer-state
  • add-custom-monitoring-points
  • TMSS-3101-Front-End-Only
  • TMSS-984-choices
  • SDC-1400-Front-End-Only
  • TMSS-3079-PII
  • TMSS-2936
  • check-for-max-244-subbands
  • TMSS-2927---Front-End-Only-PXII
  • Before-Remove-TMSS
  • LOFAR-Release-4_4_318 protected
  • LOFAR-Release-4_4_317 protected
  • LOFAR-Release-4_4_316 protected
  • LOFAR-Release-4_4_315 protected
  • LOFAR-Release-4_4_314 protected
  • LOFAR-Release-4_4_313 protected
  • LOFAR-Release-4_4_312 protected
  • LOFAR-Release-4_4_311 protected
  • LOFAR-Release-4_4_310 protected
  • LOFAR-Release-4_4_309 protected
  • LOFAR-Release-4_4_308 protected
  • LOFAR-Release-4_4_307 protected
  • LOFAR-Release-4_4_306 protected
  • LOFAR-Release-4_4_304 protected
  • LOFAR-Release-4_4_303 protected
  • LOFAR-Release-4_4_302 protected
  • LOFAR-Release-4_4_301 protected
  • LOFAR-Release-4_4_300 protected
  • LOFAR-Release-4_4_299 protected
41 results

__init__.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    test_control_model.py 2.23 KiB
    """Tests for ska.base.control_model."""
    import pytest
    
    from collections import Counter
    
    from ska.base.control_model import DeviceStateModel
    from ska.base.faults import StateModelError
    
    
    class OnOffModel(DeviceStateModel):
        __transitions = {
            ("ON", "off"): (
                "OFF",
                lambda self: self.count_transition("on->off"),
            ),
            ("OFF", "on"): (
                "ON",
                lambda self: self.count_transition("off->on"),
            ),
        }
    
        def __init__(self):
            super().__init__(self.__transitions, "ON")
            self.counter = Counter()
    
        def count_transition(self, name):
            self.counter[name] += 1
    
    
    def test_initial_state():
        on_off = OnOffModel()
        assert on_off.state == "ON"
    
    
    def test_try_valid_action_returns_true_and_does_not_change_state():
        on_off = OnOffModel()
        assert on_off.try_action("off")
        assert on_off.state == "ON"
    
    
    def test_try_invalid_action_raises_and_does_not_change_state():
        on_off = OnOffModel()
        with pytest.raises(StateModelError):
            on_off.try_action("on")
        assert on_off.state == "ON"
    
    
    def test_valid_state_transitions_succeed():
        on_off = OnOffModel()
    
        on_off.perform_action("off")
        assert on_off.state == "OFF"
        on_off.perform_action("on")
        assert on_off.state == "ON"
    
    
    def test_invalid_state_transitions_fail():
        on_off = OnOffModel()
    
        with pytest.raises(StateModelError):
            on_off.perform_action("on")
        on_off.perform_action("off")
        with pytest.raises(StateModelError):
            on_off.perform_action("off")
        with pytest.raises(StateModelError):
            on_off.perform_action("invalid")
    
    
    def test_side_effect_is_called():
        on_off = OnOffModel()
    
        on_off.perform_action("off")
        on_off.perform_action("on")
        on_off.perform_action("off")
    
        assert on_off.counter["on->off"] == 2
        assert on_off.counter["off->on"] == 1
    
    
    def test_update_transitions_only_applies_to_instances():
        on_off = OnOffModel()
    
        on_off_updated = OnOffModel()
        on_off_updated.update_transitions({("ON", "break"): ("BROKEN", None)})
    
        assert on_off.is_action_allowed("off")
        assert not on_off.is_action_allowed("break")
        assert on_off_updated.is_action_allowed("off")
        assert on_off_updated.is_action_allowed("break")