annotate tests/__init__.py @ 500:d3cd7d8d6b25 default tip

web: Breaking changes in flask-login API.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 07 Jun 2020 00:56:00 -0700
parents 36c3e9b1d1e3
children
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 os
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
464
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
3 import urllib.request, urllib.parse, urllib.error # noqa
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import shutil
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import unittest
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 class WikkedTest(unittest.TestCase):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 def setUp(self):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
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
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 os.path.dirname(os.path.dirname(__file__)),
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 'test_data')
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 for key in kwargs:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 return wiki
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 return wiki
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
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
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
66
464
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
67 def format_link(title, url, missing=False, mod=None, endpoint=None):
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
68 res = '<a class="wiki-link'
54
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
69 if missing:
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
70 res += ' missing'
464
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
71 res += '"'
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
72
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
73 if endpoint:
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
74 url = '%s:%s' % (endpoint, url)
497
36c3e9b1d1e3 cm: Upgrade all dependencies and fix deprecation issues.
Ludovic Chabant <ludovic@chabant.com>
parents: 464
diff changeset
75 url = urllib.parse.quote(url, safe='/:')
464
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
76 res += ' data-wiki-url="%s"' % url
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
77
54
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
78 if mod:
464
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
79 res += ' data-wiki-mod="%s"' % mod
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
80
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
81 if endpoint:
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
82 res += ' href="/read/%s"' % url
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
83 res += ' data-wiki-endpoint="%s"' % endpoint
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
84 else:
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
85 res += ' href="/read' + url + '"'
1dc6a0a74da3 wiki: Improve consistency of absolute/relative links.
Ludovic Chabant <ludovic@chabant.com>
parents: 357
diff changeset
86
54
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
87 res += '>' + title + '</a>'
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
88 return res
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
89
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 83
diff changeset
90
54
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
91 def format_include(url, args=None, mod=None):
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
92 res = '<div class=\"wiki-include\" data-wiki-url=\"' + url + '\"'
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
93 if mod:
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
94 res += ' data-wiki-mod=\"' + mod + '\"'
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
95 res += '>'
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
96 if args:
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
97 res += args
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
98 res += "</div>\n"
9dfbc2a40b71 Formatter changes:
Ludovic Chabant <ludovic@chabant.com>
parents: 51
diff changeset
99 return res