Mercurial > wikked
annotate tests/__init__.py @ 360:a78a4b20b720 0.6.0
Update requirements for `setuptools`.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 20 Sep 2015 19:19:31 -0700 |
parents | 666a9d0981bb |
children | 1dc6a0a74da3 |
rev | line source |
---|---|
49 | 1 import os |
2 import os.path | |
336
03e3e793fa22
Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents:
225
diff
changeset
|
3 import urllib.request, urllib.parse, urllib.error |
49 | 4 import shutil |
5 import unittest | |
6 from wikked.wiki import Wiki | |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
7 from wikked.db.sql import SQLDatabase |
336
03e3e793fa22
Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents:
225
diff
changeset
|
8 from .mock import MockWikiParameters, MockFileSystem |
49 | 9 |
10 | |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
11 class MockWikiParametersWithStructure(MockWikiParameters): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
12 def __init__(self, structure, root=None): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
13 super(MockWikiParametersWithStructure, self).__init__(root) |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
14 self.structure = structure |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
15 |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
16 def fs_factory(self): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
17 return MockFileSystem(self.root, self.config, self.structure) |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
18 |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
19 |
49 | 20 class WikkedTest(unittest.TestCase): |
21 def setUp(self): | |
51 | 22 # Directory you can use for temporary files. |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
23 self.test_data_dir = os.path.join( |
49 | 24 os.path.dirname(os.path.dirname(__file__)), |
25 'test_data') | |
26 | |
27 def tearDown(self): | |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
28 if hasattr(self, 'wiki') and self.wiki is not None: |
336
03e3e793fa22
Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents:
225
diff
changeset
|
29 self.wiki.db.close(None) |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
30 |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
31 if os.path.isdir(self.test_data_dir): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
32 shutil.rmtree(self.test_data_dir) |
49 | 33 |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
34 def _getParameters(self, root=None): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
35 return MockWikiParameters(root) |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
36 |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
37 def _getWiki(self, parameters=None, **kwargs): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
38 parameters = parameters or self._getParameters() |
49 | 39 for key in kwargs: |
40 setattr(parameters, key, kwargs[key]) | |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
41 self.wiki = Wiki(parameters) |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
42 self._onWikiCreated(self.wiki) |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
43 return self.wiki |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
44 |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
45 def _getStartedWiki(self, **kwargs): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
46 wiki = self._getWiki(**kwargs) |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
47 wiki.start() |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
48 self._onWikiStarted(wiki) |
49 | 49 return wiki |
50 | |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
51 def _getWikiFromStructure(self, structure, root='/'): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
52 params = self._getParameters(root) |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
53 params.fs_factory = lambda: MockFileSystem( |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
54 params.root, params.config, structure) |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
55 params.db_factory = lambda: SQLDatabase(params.config) |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
56 params.config_text = "[wiki]\ndatabase_url = sqlite://\n" |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
57 wiki = self._getStartedWiki(parameters=params) |
49 | 58 return wiki |
59 | |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
60 def _onWikiCreated(self, wiki): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
61 pass |
54 | 62 |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
63 def _onWikiStarted(self, wiki): |
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
64 wiki.reset() |
83
65f83a9b42f1
Added support for numbered template parameters.
Ludovic Chabant <ludovic@chabant.com>
parents:
54
diff
changeset
|
65 |
54 | 66 |
67 def format_link(title, url, missing=False, mod=None): | |
68 res = '<a class=\"wiki-link' | |
69 if missing: | |
70 res += ' missing' | |
336
03e3e793fa22
Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents:
225
diff
changeset
|
71 url = urllib.parse.quote(url) |
54 | 72 res += '\" data-wiki-url=\"' + url + '\"' |
73 if mod: | |
74 res += ' data-wiki-mod=\"' + mod + '\"' | |
357
666a9d0981bb
Quick fixes for the unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
336
diff
changeset
|
75 res += ' href="/read' + url + '"' |
54 | 76 res += '>' + title + '</a>' |
77 return res | |
78 | |
225
ebb12ff21cb2
Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents:
83
diff
changeset
|
79 |
54 | 80 def format_include(url, args=None, mod=None): |
81 res = '<div class=\"wiki-include\" data-wiki-url=\"' + url + '\"' | |
82 if mod: | |
83 res += ' data-wiki-mod=\"' + mod + '\"' | |
84 res += '>' | |
85 if args: | |
86 res += args | |
87 res += "</div>\n" | |
88 return res |