rev: e8207340ff5226a3bb61a2552b98a4aa9a680c4f Wikked/tests/__init__.py -rw-r--r-- 2.8 KiB View raw Log this file
e8207340ff52Ludovic Chabant tests: Fix URL encoding used in tests. 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import os.path
import urllib.request, urllib.parse, urllib.error  # noqa
import shutil
import unittest
from wikked.wiki import Wiki
from wikked.db.sql import SQLDatabase
from .mock import MockWikiParameters, MockFileSystem


class MockWikiParametersWithStructure(MockWikiParameters):
    def __init__(self, structure, root=None):
        super(MockWikiParametersWithStructure, self).__init__(root)
        self.structure = structure

    def fs_factory(self):
        return MockFileSystem(self.root, self.config, self.structure)


class WikkedTest(unittest.TestCase):
    def setUp(self):
        # Directory you can use for temporary files.
        self.test_data_dir = os.path.join(
            os.path.dirname(os.path.dirname(__file__)),
            'test_data')

    def tearDown(self):
        if hasattr(self, 'wiki') and self.wiki is not None:
            self.wiki.db.close(None)

        if os.path.isdir(self.test_data_dir):
            shutil.rmtree(self.test_data_dir)

    def _getParameters(self, root=None):
        return MockWikiParameters(root)

    def _getWiki(self, parameters=None, **kwargs):
        parameters = parameters or self._getParameters()
        for key in kwargs:
            setattr(parameters, key, kwargs[key])
        self.wiki = Wiki(parameters)
        self._onWikiCreated(self.wiki)
        return self.wiki

    def _getStartedWiki(self, **kwargs):
        wiki = self._getWiki(**kwargs)
        wiki.start()
        self._onWikiStarted(wiki)
        return wiki

    def _getWikiFromStructure(self, structure, root='/'):
        params = self._getParameters(root)
        params.fs_factory = lambda: MockFileSystem(
                params.root, params.config, structure)
        params.db_factory = lambda: SQLDatabase(params.config)
        params.config_text = "[wiki]\ndatabase_url = sqlite://\n"
        wiki = self._getStartedWiki(parameters=params)
        return wiki

    def _onWikiCreated(self, wiki):
        pass

    def _onWikiStarted(self, wiki):
        wiki.reset()


def format_link(title, url, missing=False, mod=None, endpoint=None):
    res = '<a class="wiki-link'
    if missing:
        res += ' missing'
    res += '"'

    if endpoint:
        url = '%s:%s' % (endpoint, url)
    url = urllib.parse.quote(url, safe='/:')
    res += ' data-wiki-url="%s"' % url

    if mod:
        res += ' data-wiki-mod="%s"' % mod

    if endpoint:
        res += ' href="/read/%s"' % url
        res += ' data-wiki-endpoint="%s"' % endpoint
    else:
        res += ' href="/read' + url + '"'

    res += '>' + title + '</a>'
    return res


def format_include(url, args=None, mod=None):
    res = '<div class=\"wiki-include\" data-wiki-url=\"' + url + '\"'
    if mod:
        res += ' data-wiki-mod=\"' + mod + '\"'
    res += '>'
    if args:
        res += args
    res += "</div>\n"
    return res