def test_inline_submodel() -> None: model = state.build_model(RootState()) mock = MagicMock() model.observe_property(lambda x: x.sub, mock) mock.reset_mock() model.submodel(lambda x: x.sub).update_properties(v1=4, v2=2) mock.assert_called_with(SubState(v1=4, v2=2))
def test_simple(self) -> None: root = state.build_model(State()) root.update_properties( userlist=UserList([User("willsmith", "*****@*****.**")])) submodel = root.submodel(lambda x: x.userlist) test_type: state.protocols.Model[UserList] = submodel assert test_type is not None submock = MagicMock() submodel.observe_property(lambda x: x, submock) submock.assert_called_with( UserList([User("willsmith", "*****@*****.**")]))
def test_fancy(benchmark): @dataclass class State: value: int = 42 model = state.build_model(State()) emitted = [] def on_update(e): assert e == 42 emitted.append(True) model.observe_property(lambda x: x.value, on_update) @benchmark def doit(): model.state.value = 0 def update(state): state.value = 42 model.update_state(update) assert emitted
def Model2() -> state.protocols.Model[NotADataClass]: # noqa return state.build_model(NotADataClass())
def test_build_model(self) -> None: model = state.build_model(State()) m2: state.protocols.Model[State] = model self.assertIsNotNone(m2) self.assertEqual(model.state.value, 0)
def Model(s: State = State()) -> state.protocols.Model[State]: # noqa return state.build_model(s)
def RootModel() -> state.protocols.Model[State]: # noqa return state.build_model(State())
def test_readme(self) -> None: app = state.build_model(AppState()) # Add some initial values app.update_properties( regional_managers=[Person("Michael", "Scott")], assistant_to_the_regional_managers=[Person("Dwight", "Schrute")], employees=[Person("Jim", "Halpert"), Person("Pam", "Beesly")]) # Ensure that the values actually were persisted to the application # state self.assertEqual(app.state.regional_managers, [Person("Michael", "Scott")]) self.assertEqual(app.state.assistant_to_the_regional_managers, [Person("Dwight", "Schrute")]) self.assertEqual(app.state.employees, [Person("Jim", "Halpert"), Person("Pam", "Beesly")]) x: AppState regional_manager = MagicMock() # Observe changes in the first regional manager, a token is returned to # allow you to disconnect later if needed. token = app.observe_property(lambda x: x.regional_managers[0], regional_manager) # Whenever we observe, the callback is always initially called with # the current value. regional_manager.assert_called_with(Person("Michael", "Scott")) pams_last_name = MagicMock() app.observe_property(lambda x: x.employees[1].last_name, pams_last_name) pams_last_name.assert_called_with("Beesly") # create a submodel to track Pam Beesly pam = app.submodel(lambda x: x.employees[1]) pams_last_name.reset_mock() pam.update_properties(last_name="Halpert") self.assertEqual(app.state.employees[1].last_name, "Halpert") # Note that the callback was called with exactly the attribute that was # observed to: x.employees[1].last_name pams_last_name.assert_called_with("Halpert") regional_manager.reset_mock() app.update_properties(regional_managers=[Person("Dwight", "Schrute")], assistant_to_the_regional_managers=[]) # In this case, the entire first object is returned as opposed to Pam's # last name regional_manager.assert_called_with(Person("Dwight", "Schrute")) regional_manager.reset_mock() token.disconnect() task = asyncio.get_event_loop().create_task(self.__myfunc(app)) asyncio.get_event_loop().call_soon(lambda: app.update_properties( regional_managers=[Person("Jim", "Halpert")])) asyncio.get_event_loop().run_until_complete(task) self.assertEqual(task.result(), [Person("Jim", "Halpert")]) # we disconnected the token, so no more notifications for the regional # manager regional_manager.assert_not_called()