Ejemplo n.º 1
0
    def test_update_dict_in_dict_with_pointer(self):
        shared = share.SharedDataManager()

        class Foo(IdentifiedObject):
            data = shared.create_self('data', lambda: {})

        foo = Foo()
        foo.data = {'foo': {'bar': 'baz'}}

        assert foo.data == {'foo': {'bar': 'baz'}}

        foo.data['foo'] = {'bar': 'baz'}
        # In this case local data is used
        assert foo.data == {'foo': {'bar': 'baz'}}

        shared.commit()
        shared.purge_data()

        # In this case, "data" key was pending and has been commit
        assert foo.data == {'foo': {'bar': 'baz'}}

        # In this case "data" key was NOT pending and has not been commit
        foo.data['foo']['bar'] = 'biz'
        foo.data = dict(foo.data)

        shared.commit()
        shared.purge_data()

        assert foo.data == {'foo': {'bar': 'biz'}}
Ejemplo n.º 2
0
    def test_list_index(self):
        shared = share.SharedDataManager()

        class Foo(IdentifiedObject):
            position = shared.create_self(
                'position',
                (0, 0, 0),
                indexes=[shared.make_index(share.ListIndex, 'positions')],
            )

        with pytest.raises(UnknownSharedData):
            shared.get('positions')

        foo = Foo()
        foo.position = (0, 1, 2)

        assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
        assert shared.get('positions') == [(0, 1, 2)]

        foo2 = Foo()
        foo2.position = (3, 4, 5)

        assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
        assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
        assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]

        foo2.position = (6, 7, 8)
        assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
        assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]
Ejemplo n.º 3
0
    def test_auto_self_shared_data(self):
        shared = share.SharedDataManager()

        class MySubject(IdentifiedObject):
            age = shared.create_self('age', 0)

            @property
            def id(self):
                return id(self)

        subject = MySubject()
        assert subject.age == 0
Ejemplo n.º 4
0
    def test_commit(self):
        shared = share.SharedDataManager()

        class Foo(object):
            counter = shared.create('counter', 0)

        foo = Foo()
        foo.counter = 42

        assert shared.get('counter') == 42

        shared.commit()
        assert shared.get('counter') == 42
Ejemplo n.º 5
0
    def test_refresh_without_commit(self):
        shared = share.SharedDataManager()

        class Foo(object):
            counter = shared.create('counter', 0)

        foo = Foo()
        foo.counter = 42

        assert shared.get('counter') == 42

        shared.refresh()
        with pytest.raises(UnknownSharedData):
            shared.get('counter')
Ejemplo n.º 6
0
    def test_update_dict_with_pointer(self):
        shared = share.SharedDataManager()

        class Foo(IdentifiedObject):
            data = shared.create('data', lambda: {})

        foo = Foo()
        foo.data = {'foo': 'bar'}

        assert shared.get('data') == {'foo': 'bar'}

        foo.data['foo'] = 'buz'
        # In this case local data is used
        assert shared.get('data') == {'foo': 'buz'}
Ejemplo n.º 7
0
    def test_update_list_with_pointer(self):
        shared = share.SharedDataManager()

        class Foo(IdentifiedObject):
            data = shared.create('data', [])

        foo = Foo()
        foo.data = ['foo']

        assert foo.data == ['foo']
        assert shared.get('data') == ['foo']

        foo.data.append('bar')
        assert shared.get('data') == ['foo', 'bar']
Ejemplo n.º 8
0
    def test_simple_share_with_class(self):
        shared = share.SharedDataManager()

        class Foo(object):
            counter = shared.create('counter', 0)

        foo = Foo()
        foo.counter = 42

        assert shared.get('counter') == 42

        foo.counter = 48

        assert shared.get('counter') == 48
Ejemplo n.º 9
0
    def test_position_index(self):
        class ListIndex(share.SharedDataIndex):
            def add(self, subject, value):
                try:
                    values = self.shared_data_manager.get(self.key)
                except UnknownSharedData:
                    values = []

                values.append(value)
                self.shared_data_manager.set(self.key, values)

            def remove(self, subject, value):
                values = self.shared_data_manager.get(self.key)
                values.remove(value)
                self.shared_data_manager.set(self.key, values)

        shared = share.SharedDataManager()

        class Foo(IdentifiedObject):
            position = shared.create_self(
                'position',
                (0, 0, 0),
                indexes=[shared.make_index(ListIndex, 'positions')],
            )

        with pytest.raises(UnknownSharedData):
            shared.get('positions')

        foo = Foo()
        foo.position = (0, 1, 2)

        assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
        assert shared.get('positions') == [(0, 1, 2)]

        foo2 = Foo()
        foo2.position = (3, 4, 5)

        assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
        assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
        assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]

        foo2.position = (6, 7, 8)
        assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
        assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]
Ejemplo n.º 10
0
    def test_auto_self_shared_data_with_callable(self):
        shared = share.SharedDataManager()

        class MySubject(IdentifiedObject):
            colors = shared.create_self('colors', lambda: [])

            @property
            def id(self):
                return id(self)

        subjecta = MySubject()
        subjectb = MySubject()

        assert subjecta.colors == []
        assert subjectb.colors == []

        subjecta.colors = ['toto']
        subjectb.colors = ['tata']

        assert subjecta.colors == ['toto']
        assert subjectb.colors == ['tata']