Source

storage-ng / backend / _tests / test_fileserver.py

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

"""
MoinMoin - fileserver backend tests
"""


from __future__ import absolute_import, division

import os, tempfile

import pytest

from backend.fileserver import Backend
from backend._tests import BackendTestBase


class TestFileServerBackend(BackendTestBase):
    def setup_method(self, method):
        self.path = path = tempfile.mkdtemp()
        self.be = Backend(path)
        self.be.open()

    def _prepare(self, items):
        expected_result = set()
        for name, meta, data in items:
            fn = os.path.join(self.path, name)
            dn = os.path.dirname(fn)
            try:
                os.makedirs(dn)
            except:
                pass
            with open(fn, 'wb') as f:
                f.write(data)
            meta = tuple(sorted(meta.items()))
            expected_result.add((meta, data))
        return expected_result
    
    def test_files(self):
        # note: as we can only store the data into the file system, meta can
        # only have items that are generated by the fileserver backend:
        items = [#name,  meta,   data
                 ('foo.png', dict(size=11, contenttype='image/png'), 'png content'),
                 ('bar.txt', dict(size=12, contenttype='text/plain'), 'text content'),
                ]
        expected_result = self._prepare(items)
        result = set()
        for i in self.be:
            meta, data = self.be.retrieve(i)
            # we don't want to check mtime
            del meta['mtime']
            meta = tuple(sorted(meta.items()))
            data = data.read()
            result.add((meta, data))
        assert result == expected_result

    def test_dir(self):
        # note: as we can only store the data into the file system, meta can
        # only have items that are generated by the fileserver backend:
        items = [#name,  meta,   data
                 ('dir/foo.png', dict(size=11, contenttype='image/png'), 'png content'),
                 ('dir/bar.txt', dict(size=12, contenttype='text/plain'), 'text content'),
                ]
        expected_result = self._prepare(items)
        dir_meta = tuple(sorted(dict(size=0, contenttype='text/x.moin.wiki;charset=utf-8').items()))
        dir_data = """\
= Directory contents =
 * [[../]]
 * [[/bar.txt|bar.txt]]
 * [[/foo.png|foo.png]]
""".replace('\n', '\r\n')
        expected_result.add((dir_meta, dir_data))
        result = set()
        for i in self.be:
            meta, data = self.be.retrieve(i)
            # we don't want to check mtime
            del meta['mtime']
            meta = tuple(sorted(meta.items()))
            data = data.read()
            result.add((meta, data))
        assert result == expected_result