storage-ng / middleware / _tests /

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

MoinMoin - router middleware tests

from __future__ import absolute_import, division

from StringIO import StringIO

import pytest

from config import NAME, REVID

from middleware.router import Backend as RouterBackend
from backend.storages import MutableBackend as StorageBackend, Backend as ROBackend

from storage.memory import BytesStorage as MemoryBytesStorage
from storage.memory import FileStorage as MemoryFileStorage

def make_ro_backend():
    store = StorageBackend(MemoryBytesStorage(), MemoryFileStorage())
    store.create(){NAME:'test'}, StringIO('')){NAME:'test2'}, StringIO(''))
    return ROBackend(store.meta_store, store.data_store)

def pytest_funcarg__router(request):
    root_be = StorageBackend(MemoryBytesStorage(), MemoryFileStorage())
    sub_be = StorageBackend(MemoryBytesStorage(), MemoryFileStorage())
    ro_be = make_ro_backend()
    router = RouterBackend([('sub', sub_be),('ro',ro_be), ('', root_be)])

    def finalize():

    return router

def revid_split(revid):
    # router revids are <backend_mountpoint>/<backend_revid>, split that:
    return revid.rsplit(u'/', 1)

def test_store_get_del(router):
    root_name = u'foo'
    root_revid =, StringIO(''))
    sub_name = u'sub/bar'
    sub_revid =, StringIO(''))

    assert revid_split(root_revid)[0] == ''
    assert revid_split(sub_revid)[0] == 'sub'

    # when going via the router backend, we get back fully qualified names:
    root_meta, _ = router.retrieve(root_revid)
    sub_meta, _ = router.retrieve(sub_revid)
    assert root_name == root_meta[NAME]
    assert sub_name == sub_meta[NAME]

    # when looking into the storage backend, we see relative names (without mountpoint):
    root_meta, _ = router.mapping[-1][1].retrieve(revid_split(root_revid)[1])
    sub_meta, _ = router.mapping[0][1].retrieve(revid_split(sub_revid)[1])
    assert root_name == root_meta[NAME]
    assert sub_name == 'sub' + '/' + sub_meta[NAME]
    # delete revs:

def test_store_readonly_fails(router):
    with pytest.raises(TypeError):'ro/testing'), StringIO(''))

def test_del_readonly_fails(router):
    ro_id = next(iter(router)) # we have only readonly items
    print ro_id
    with pytest.raises(TypeError):

def test_destroy_create_dont_touch_ro(router):
    existing = set(router)
    root_revid ='foo'), StringIO(''))
    sub_revid ='sub/bar'), StringIO(''))


    assert set(router) == existing

def test_iter(router):
    existing = set(router)
    root_revid ='foo'), StringIO(''))
    sub_revid ='sub/bar'), StringIO(''))
    assert set(router) == (set([root_revid, sub_revid])|existing)