Source

storage-ng / backends / _tests / test_fileserver.py

# 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
import tempfile

import pytest

from config import MTIME
from ..fileserver import Backend
from . 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.