Mercurial > wikked
comparison tests/mock.py @ 51:2733871775cd
More unit tests.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 31 Jan 2013 12:28:10 -0800 |
parents | fb6ae96756c1 |
children | 9afe4a1dbd1e |
comparison
equal
deleted
inserted
replaced
50:350f7f084028 | 51:2733871775cd |
---|---|
17 self._passthrough: ['txt', 'html'] | 17 self._passthrough: ['txt', 'html'] |
18 } | 18 } |
19 | 19 |
20 self.config_text = "" | 20 self.config_text = "" |
21 self.special_filenames = [] | 21 self.special_filenames = [] |
22 self.use_db = False | |
22 | 23 |
23 self.logger_factory = lambda: logging.getLogger('wikked.tests') | 24 self.logger_factory = lambda: logging.getLogger('wikked.tests') |
24 self.page_factory = lambda wiki, url: MockPage(wiki, url) | 25 self.page_factory = lambda wiki, url: MockPage(wiki, url) |
25 self.config_factory = lambda: StringIO.StringIO(self.config_text) | 26 self.config_factory = lambda: StringIO.StringIO(self.config_text) |
26 self.fs_factory = lambda cfg: MockFileSystem() | 27 self.fs_factory = lambda cfg: MockFileSystem() |
42 | 43 |
43 class MockDatabase(Database): | 44 class MockDatabase(Database): |
44 def __init__(self, content=None, logger=None): | 45 def __init__(self, content=None, logger=None): |
45 Database.__init__(self, logger) | 46 Database.__init__(self, logger) |
46 self.content = content | 47 self.content = content |
48 self.conn = None | |
47 self._open_count = 0 | 49 self._open_count = 0 |
48 | 50 |
49 def initDb(self): | 51 def initDb(self): |
50 pass | 52 pass |
51 | 53 |
52 def open(self): | 54 def open(self): |
53 self._open_count += 1 | 55 self._open_count += 1 |
56 self.conn = 'MOCK_CONNECTION' | |
54 | 57 |
55 def close(self): | 58 def close(self): |
56 self._open_count -= 1 | 59 self._open_count -= 1 |
57 if self._open_count < 0: | 60 if self._open_count < 0: |
58 raise Exception( | 61 raise Exception( |
59 "The database was closed more times than it was open.") | 62 "The database was closed more times than it was open.") |
63 elif self._open_count == 0: | |
64 self.conn = None | |
60 | 65 |
61 def reset(self, pages): | 66 def reset(self, pages): |
62 pass | 67 pass |
63 | 68 |
64 def update(self, pages): | 69 def update(self, pages): |
91 self.logger = logger | 96 self.logger = logger |
92 self.excluded = [] | 97 self.excluded = [] |
93 | 98 |
94 def getPageInfos(self, subdir=None): | 99 def getPageInfos(self, subdir=None): |
95 node = self._getNode(subdir) | 100 node = self._getNode(subdir) |
101 if node is None: | |
102 raise PageNotFoundError() | |
96 for n in self._getChildren(node): | 103 for n in self._getChildren(node): |
97 yield self._getPageInfo(n) | 104 yield self._getPageInfo(n) |
98 | 105 |
99 def getPageInfo(self, path): | 106 def getPageInfo(self, path): |
100 node = self._getNode(path) | 107 node = self._getNode(path) |
108 if node is None: | |
109 raise PageNotFoundError() | |
101 return self._getPageInfo(node) | 110 return self._getPageInfo(node) |
102 | 111 |
103 def getPage(self, url): | 112 def getPage(self, url): |
104 path = self._getPath(url, True) | 113 path = self._getPath(url, True) |
105 node = self._getNode(path) | 114 node = self._getNode(path) |
115 if node is None: | |
116 raise PageNotFoundError() | |
106 return self._getPageInfo(node, True) | 117 return self._getPageInfo(node, True) |
107 | 118 |
108 def setPage(self, path, content): | 119 def setPage(self, path, content): |
109 pass | 120 raise NotImplementedError() |
110 | 121 |
111 def pageExists(self, url): | 122 def pageExists(self, url): |
112 return False | 123 try: |
124 self._getPath(url, True) | |
125 return True | |
126 except PageNotFoundError: | |
127 return False | |
113 | 128 |
114 def getPhysicalNamespacePath(self, url): | 129 def getPhysicalNamespacePath(self, url): |
115 return None | 130 raise NotImplementedError() |
116 | 131 |
117 def _getPageInfo(self, node, with_content=False): | 132 def _getPageInfo(self, node, with_content=False): |
118 path_split = os.path.splitext(node['path']) | 133 path_split = os.path.splitext(node['path']) |
119 url = self.slugify(path_split[0]) | 134 url = self.slugify(path_split[0]) |
120 info = { | 135 info = { |
127 | 142 |
128 def _getNode(self, path): | 143 def _getNode(self, path): |
129 node = self.structure | 144 node = self.structure |
130 if path: | 145 if path: |
131 for n in path.split('/'): | 146 for n in path.split('/'): |
147 if n not in node: | |
148 return None | |
132 node = node[n] | 149 node = node[n] |
133 else: | 150 else: |
134 path = '' | 151 path = '' |
135 if isinstance(node, types.StringTypes): | 152 if isinstance(node, types.StringTypes): |
136 return {'type': 'file', 'path': path, 'content': node} | 153 return {'type': 'file', 'path': path, 'content': node} |