Exemple #1
0
    def test_list(self):
        class ListModel(Model):
            data = List([1, 2, 3])

        model = ListModel()
        obs = Observer(model)
        obs.notify = Mock()

        model.data.append(4)
        assert obs.notify.called

        obs.notify.reset_mock()
        del model.data[0]
        assert obs.notify.called

        obs.notify.reset_mock()
        assert model.data[0] == 2  # no modification
        assert not obs.notify.called

        obs.notify.reset_mock()
        model.data[0] = 1
        assert model.data[0] == 1
        assert obs.notify.called

        obs.notify.reset_mock()
        model.data.pop()  # remove last item
        assert model.data[-1] == 3
        assert obs.notify.called
Exemple #2
0
    def test_notify(self):
        model = TestModel()
        obs = Observer(model)
        obs.notify = Mock()

        model.attr1 = 1
        obs.notify.assert_called_with('attr1', 1, 0)  # name, new, old

        model.attr1 = 1
        obs.notify.assert_called_with('attr1', 1, 1)
Exemple #3
0
    def test_wildcards(self):
        model = WildcardModel()
        obs = Observer(model)
        obs.notify = Mock()

        model.attr1 = 1
        obs.notify.assert_called_with('attr1', 1, 0)

        obs.notify.reset_mock()
        model.attr2 = 2
        self.assertFalse(obs.notify.called)

        model.test_attr = 1
        obs.notify.assert_called_with('test_attr', 1, 0)

        model.data1 = 1
        obs.notify.assert_called_with('data1', 1, 0)
Exemple #4
0
    def test_dict(self):
        class DictModel(Model):
            data = Dict({1: 'blub', 'test': True})

        model = DictModel()
        obs = Observer(model)
        obs.notify = Mock()

        model.data[0] = 'new'
        assert model.data[0] == 'new'
        assert obs.notify.called

        obs.notify.reset_mock()
        del model.data[0]
        assert obs.notify.called

        obs.notify.reset_mock()
        assert 0 not in model.data  # no modification
        assert not obs.notify.called

        obs.notify.reset_mock()
        model.data.popitem()
        assert obs.notify.called
Exemple #5
0
    def test_wildcards(self):
        obs = Observer(Mock())
        mock = Mock()
        obs.register_callback('*', mock)

        args = ['attr1', 0, 1]
        obs.notify(*args)
        mock.assert_called_with(*args)

        args = ['test_attr', 0, 1]
        obs.notify(*args)
        mock.assert_called_with(*args)

        mock = Mock()
        obs.register_callback('attr?', mock)

        args = ['attr1', 0, 1]
        obs.notify(*args)
        mock.assert_called_with(*args)

        mock.reset_mock()
        args = ['test_attr', 0, 1]
        obs.notify(*args)
        self.assertFalse(mock.called)