annotate tests/mock.py @ 82:9afe4a1dbd1e

Refactoring of core wiki classes: - Use proper classes instead of dictionaries more often. - Simplified `Page`'s public API. - Page meta property values are now always stored in an array, even if there's only one occurence for the given key. - Updated unit-tests.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 19 Mar 2013 19:54:11 -0700
parents 2733871775cd
children fd6eccb24882
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import re
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import types
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import codecs
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import logging
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 import StringIO
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 from wikked.page import Page
82
9afe4a1dbd1e Refactoring of core wiki classes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
8 from wikked.fs import PageInfo, PageNotFoundError
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 from wikked.db import Database
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 from wikked.indexer import WikiIndex
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 from wikked.scm import SourceControl
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 class MockWikiParameters(object):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 def __init__(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 self.formatters = {
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 self._passthrough: ['txt', 'html']
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 }
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 self.config_text = ""
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 self.special_filenames = []
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
22 self.use_db = False
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 self.logger_factory = lambda: logging.getLogger('wikked.tests')
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 self.page_factory = lambda wiki, url: MockPage(wiki, url)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 self.config_factory = lambda: StringIO.StringIO(self.config_text)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 self.fs_factory = lambda cfg: MockFileSystem()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 self.index_factory = lambda cfg: MockWikiIndex()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 self.db_factory = lambda cfg: MockDatabase()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 self.scm_factory = lambda cfg: MockSourceControl()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 def getSpecialFilenames(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 return self.special_filenames
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 def _passthrough(self, text):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 return text
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 class MockPage(Page):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 def __init__(self, wiki, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 Page.__init__(self, wiki, url)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 class MockDatabase(Database):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 def __init__(self, content=None, logger=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 Database.__init__(self, logger)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 self.content = content
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
48 self.conn = None
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 self._open_count = 0
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 def initDb(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 def open(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 self._open_count += 1
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
56 self.conn = 'MOCK_CONNECTION'
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 def close(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 self._open_count -= 1
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 if self._open_count < 0:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 raise Exception(
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 "The database was closed more times than it was open.")
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
63 elif self._open_count == 0:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
64 self.conn = None
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 def reset(self, pages):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 def update(self, pages):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 def getPageUrls(self, subdir=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 def getPages(self, subdir=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 def getPage(self, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 return None
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 def pageExists(self, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 return False
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 def getLinksTo(self, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 class MockFileSystem():
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 def __init__(self, structure=None, slugify=Page.title_to_url, logger=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 if not structure:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 structure = []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 if not slugify:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 slugify = lambda x: x
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 self.structure = structure
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 self.slugify = slugify
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 self.logger = logger
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 self.excluded = []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 def getPageInfos(self, subdir=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 node = self._getNode(subdir)
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
101 if node is None:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
102 raise PageNotFoundError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 for n in self._getChildren(node):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 yield self._getPageInfo(n)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 def getPageInfo(self, path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 node = self._getNode(path)
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
108 if node is None:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
109 raise PageNotFoundError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 return self._getPageInfo(node)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 def getPage(self, url):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 path = self._getPath(url, True)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 node = self._getNode(path)
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
115 if node is None:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
116 raise PageNotFoundError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 return self._getPageInfo(node, True)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 def setPage(self, path, content):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
120 raise NotImplementedError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 def pageExists(self, url):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
123 try:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
124 self._getPath(url, True)
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
125 return True
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
126 except PageNotFoundError:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
127 return False
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 def getPhysicalNamespacePath(self, url):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
130 raise NotImplementedError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 def _getPageInfo(self, node, with_content=False):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 path_split = os.path.splitext(node['path'])
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 url = self.slugify(path_split[0])
82
9afe4a1dbd1e Refactoring of core wiki classes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
135 info = PageInfo(url, node['path'])
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 if with_content:
82
9afe4a1dbd1e Refactoring of core wiki classes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
137 info.content = node['content']
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 return info
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 def _getNode(self, path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 node = self.structure
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 if path:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143 for n in path.split('/'):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
144 if n not in node:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
145 return None
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146 node = node[n]
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 path = ''
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149 if isinstance(node, types.StringTypes):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150 return {'type': 'file', 'path': path, 'content': node}
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151 return {'type': 'dir', 'path': path, 'content': node}
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
153 def _getChildren(self, node):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154 if node['type'] != 'dir':
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155 raise Exception("'%s' is not a directory." % node['path'])
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156 for name in node['content']:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 child_path = os.path.join(node['path'], name)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158 child = node['content'][name]
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159 if isinstance(child, types.StringTypes):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160 yield {
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
161 'type': 'file',
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
162 'path': child_path,
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163 'content': child
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
164 }
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
165 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
166 for c in self._getChildren({
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
167 'type': 'dir',
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 'path': child_path,
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 'content': child
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170 }):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 yield c
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 def _getPath(self, url, is_file):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174 path = ''
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 current = self.structure
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 parts = unicode(url).lower().split('/')
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 for i, part in enumerate(parts):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178 for name in current:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179 name_slug = self.slugify(name)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 if is_file and i == len(parts) - 1:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181 if re.match(r"%s\.[a-z]+" % re.escape(part), name_slug):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 current = current[name]
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183 path = os.path.join(path, name)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
184 break
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
185 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186 if name_slug == part:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 current = current[name]
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 path = os.path.join(path, name)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 break
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 # Failed to find a part of the URL.
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 raise PageNotFoundError("No such page: " + url)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 return path
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 @staticmethod
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 def save_structure(path, structure):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 if not os.path.isdir(path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 os.makedirs(path)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 for node in structure:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 node_path = os.path.join(path, node)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201 if isinstance(structure[node], types.StringTypes):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202 with codecs.open(node_path, 'w', encoding='utf-8') as f:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203 f.write(structure[node])
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
204 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 MockFileSystem.save_structure(node_path, structure[node])
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 class MockWikiIndex(WikiIndex):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 def __init__(self, logger=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210 WikiIndex.__init__(self, logger)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 def initIndex(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215 def reset(self, pages):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218 def update(self, pages):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 def search(self, query):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 # url, title, content_highlights
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 return None
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
224
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 class MockSourceControl(SourceControl):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 def __init__(self, logger=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 SourceControl.__init__(self, logger)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
230 def initRepo(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231 pass
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233 def getSpecialFilenames(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
235
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
236 def getHistory(self, path=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
237 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
238
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
239 def getState(self, path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
240 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
241
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
242 def getRevision(self, path, rev):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
243 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
244
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
245 def diff(self, path, rev1, rev2):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
246 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
247
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
248 def commit(self, paths, op_meta):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
249 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
250
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
251 def revert(self, paths=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
252 raise NotImplementedError()