def _update_core_properties(self, debug=False): with self._lock: pressured_acts = [] partially_fulfilled_acts = [] for act in self._state_activations(): if self[":activity"].changed_signal() not in set(act.constraint.signals()): if act.is_pressured(): pressured_acts.append(act.id) if act.spiky(): partially_fulfilled_acts.append(act) PropertyWrapper( prop=self[":pressure"], ctx=self, allow_write=True, allow_read=True ).set(len(pressured_acts) > 0) PropertyWrapper( prop=self[":activity"], ctx=self, allow_write=True, allow_read=True ).set(len(partially_fulfilled_acts) > 0) if debug: partially_fulfilled_info = "; ".join( f"{act} -> {', '.join(repr(spike) for spike in act.spikes())}" for act in partially_fulfilled_acts) if partially_fulfilled_info: logger.info(partially_fulfilled_info)
def test_property_no_read(under_test_nothing: PropertyWrapper, default_property_base: Property): assert (not default_property_base._lock.locked()) with LogCapture(attributes=strip_prefix) as log_capture: under_test_nothing.get() log_capture.check( f"Unauthorized read access in property-wrapper for {under_test_nothing.prop.id()}!", )
def test_property_child(under_test_read_write: PropertyWrapper, default_property_base, context_mock): assert under_test_read_write.push( Property(name=CHILD_PROPERTY_NAME, default_value=DEFAULT_PROPERTY_VALUE)) assert list(under_test_read_write.enum())[0] == CHILD_PROPERTY_ID assert under_test_read_write.prop.children[CHILD_PROPERTY_NAME].read( ) == DEFAULT_PROPERTY_VALUE
def test_property_read_only(under_test_read_only: PropertyWrapper, default_property_base): # Make sure that writing to read-only wrapper is ineffective assert (not default_property_base._lock.locked()) with LogCapture(attributes=strip_prefix) as log_capture: under_test_read_only.set(NEW_PROPERTY_VALUE) log_capture.check( f"Unauthorized write access in property-wrapper {under_test_read_only.prop.id()}!", ) assert (under_test_read_only.get() == DEFAULT_PROPERTY_VALUE)
def test_property_write(under_test_read_write: PropertyWrapper, default_property_base, context_mock): # Make sure that writing to writable wrapper is effective assert (default_property_base._lock.locked()) under_test_read_write.set(NEW_PROPERTY_VALUE) assert (under_test_read_write.get() == NEW_PROPERTY_VALUE) context_mock.emit.assert_called_once_with( s(f"{under_test_read_write.prop.id()}:changed"), parents=None, wipe=True)
def test_property_illegal_push(context_mock): prop_no_push = Property(name=DEFAULT_PROPERTY_NAME, default_value=DEFAULT_PROPERTY_VALUE, allow_push=False) prop_no_push.set_parent_path(DEFAULT_MODULE_NAME) wrapper = PropertyWrapper(prop=prop_no_push, ctx=context_mock, allow_read=True, allow_write=True) with LogCapture(attributes=strip_prefix) as log_capture: assert not wrapper.push(child=Property(name=CHILD_PROPERTY_NAME)) log_capture.check( f'Unauthorized push in property {DEFAULT_MODULE_NAME}:{DEFAULT_PROPERTY_NAME}!', )
def _update_core_properties(self): with self._lock: pressured_acts = False partially_fulfilled_acts = 0 for act in self._state_activations(): if self[":activity"].changed_signal() not in set(act.constraint.signals()): pressured_acts |= act.is_pressured() partially_fulfilled_acts += act.spiky() PropertyWrapper( prop=self[":pressure"], ctx=self, allow_write=True, allow_read=True ).set(pressured_acts) PropertyWrapper( prop=self[":activity"], ctx=self, allow_write=True, allow_read=True ).set(partially_fulfilled_acts)
def test_property_get(under_test_read_only: PropertyWrapper, default_property_base: Property): assert (not default_property_base._lock.locked()) assert (under_test_read_only.get() == DEFAULT_PROPERTY_VALUE)
def under_test_read_write(default_property_base, context_mock): return PropertyWrapper(prop=default_property_base, ctx=context_mock, allow_read=True, allow_write=True)
def under_test_nothing(default_property_base, context_mock): return PropertyWrapper(prop=default_property_base, ctx=context_mock, allow_read=False, allow_write=False)
def test_flag_property(context_mock): prop_base = Property(name="flag_prop", is_flag_property=True) prop_base.set_parent_path(DEFAULT_MODULE_NAME) prop_wrapper = PropertyWrapper(prop=prop_base, ctx=context_mock, allow_read=True, allow_write=True) assert (prop_base._lock.locked()) prop_wrapper.set(True) assert (prop_wrapper.get() is True) context_mock.emit.assert_any_call( SignalRef(f"{prop_wrapper.prop.id()}:changed"), parents=None, wipe=True, payload=True, boring=False) context_mock.emit.assert_any_call( SignalRef(f"{prop_wrapper.prop.id()}:true"), parents=None, wipe=True, boring=False) context_mock.emit.reset_mock() prop_wrapper.set(False) assert (prop_wrapper.get() is False) context_mock.emit.assert_any_call( SignalRef(f"{prop_wrapper.prop.id()}:changed"), parents=None, wipe=True, payload=False, boring=False) context_mock.emit.assert_any_call( SignalRef(f"{prop_wrapper.prop.id()}:false"), parents=None, wipe=True, boring=False) context_mock.emit.reset_mock() prop_wrapper.set(None) assert (prop_wrapper.get() is None) context_mock.emit.assert_called_once_with( SignalRef(f"{prop_wrapper.prop.id()}:changed"), parents=None, wipe=True, payload=None, boring=False)