Source

storage-ng / middleware / _tests / test_serializer.py

Full commit
# Copyright: 2011 MoinMoin:RonnyPfannschmidt
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - serializer / deserializer tests
"""


from __future__ import absolute_import, division

from storage.memory import BytesStorage, FileStorage

from middleware.indexing import IndexingMiddleware, AccessDenied
from middleware.serializer import serialize, deserialize
from backend.storages import MutableBackend


from StringIO import StringIO

contents = [
    (u'Foo', {'name': u'Foo'}, ''),
    (u'Foo', {'name': u'Foo'}, '2nd'),
    (u'Subdir', {'name': u'Subdir'}, ''),
    (u'Subdir/Foo', {'name': u'Subdir/Foo'}, ''),
    (u'Subdir/Bar', {'name': u'Subdir/Bar'}, ''),
]



scenarios = [
    ('Simple', ['']),
    ('Nested', ['', 'Subdir']),
]


def pytest_generate_tests(metafunc):
    metafunc.addcall(id='Simple->Simple', param=('Simple', 'Simple'))

def pytest_funcarg__source(request):
    # scenario
    return make_middleware(request)

def pytest_funcarg__target(request):
    # scenario
    return make_middleware(request)

def make_middleware(request):
    tmpdir = request.getfuncargvalue('tmpdir')
    # scenario

    meta_store = BytesStorage()
    data_store = FileStorage()
    backend = MutableBackend(meta_store, data_store)
    backend.create()
    backend.open()
    request.addfinalizer(backend.destroy)
    request.addfinalizer(backend.close)
    
    mw = IndexingMiddleware(index_dir=str(tmpdir/'foo'),
                            backend=backend)
    mw.create()
    mw.open()
    request.addfinalizer(mw.destroy)
    request.addfinalizer(mw.close)
    return mw


def test_serialize_deserialize(source, target):
    i = 0
    for name, meta, data in contents:
        item = source['name']
        item.create_revision(dict(meta, mtime=i), StringIO(data))
        i += 1

    io = StringIO()
    serialize(source.backend, io)
    io.seek(0)
    deserialize(io, target.backend)
    target.rebuild()

    print sorted(source.backend)
    print sorted(target.backend)
    assert sorted(source.backend)  == sorted(target.backend)